Debian Codegraph @

src/scene/glsl/source/lighting_model_physical/add_light.glsl

raw
1 /*
2 Copyright 2020-2022 Michalis Kamburelis and glTF-Sample-Viewer authors.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14
15 Shader code used for adding light source contribution for PhysicalMaterial
16 (PBR lighting model).
17 This is used by both desktop OpenGL and OpenGLES.
18
19 In the future this may be used in both Gouraud and Phong shading,
20 so it may go either in vertex or fragment shader.
21 For now: since PhysicalMaterial now forces Phong shading, this is only used in Phong shading.
22 */
23
24 /* Light source position (or direction, if not LIGHT<Light>_TYPE_POSITIONAL)
25 in eye coordinates. */
26 uniform vec3 castle_LightSource<Light>Position;
27
28 /* SpotLight specific parameters */
29 #ifdef LIGHT<Light>_TYPE_SPOT
30 uniform vec3 castle_LightSource<Light>SpotDirection;
31 uniform float castle_LightSource<Light>SpotCosCutoff;
32 #ifdef LIGHT<Light>_HAS_BEAM_WIDTH
33 /* In radians. Note that this differs from gl_LightSource[<Light>].spotCutoff
34 that is in degrees. */
35 uniform float castle_LightSource<Light>SpotCutoff;
36 uniform float castle_LightSource<Light>BeamWidth;
37 #endif
38 #ifdef LIGHT<Light>_HAS_SPOT_EXPONENT
39 uniform float castle_LightSource<Light>SpotExponent;
40 #endif
41 #endif
42
43 uniform vec3 castle_LightSource<Light>Color;
44
45 #ifdef LIGHT<Light>_HAS_ATTENUATION
46 /* Attenuation: constant, linear, quadratic. */
47 uniform vec3 castle_LightSource<Light>Attenuation;
48 #endif
49
50 #ifdef LIGHT<Light>_HAS_RADIUS
51 uniform float castle_LightSource<Light>Radius;
52 #endif
53
54 /* EnvironmentLight parameters */
55 #ifdef LIGHT<Light>_TYPE_ENVIRONMENT
56 uniform samplerCube diffuseTexture;
57 uniform samplerCube specularTexture;
58 #endif
59
60 // In case of OpenGLES, all shader code is glued, so this is already declared
61 #ifndef GL_ES
62 AngularInfo getAngularInfo(const in vec3 pointToLight, const in vec3 normal, const in vec3 view);
63 vec3 specularReflection(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);
64 float visibilityOcclusion(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);
65 float microfacetDistribution(const in MaterialInfo materialInfo, const in AngularInfo angularInfo);
66 vec3 diffuse(const in MaterialInfo materialInfo);
67 #endif
68
69 /* Add light contribution.
70 Note: this never changes color.a.
71 */
72 void PLUG_add_light(inout vec4 color,
73 const in vec4 vertex_eye,
74 const in vec3 normal_eye,
75 const in MaterialInfo material_info,
76 const in vec3 view)
77 {
78 vec3 light_dir;
79
80 /* Calculate light_dir */
81 #ifdef LIGHT<Light>_TYPE_PUNCTUAL
82 #ifdef LIGHT<Light>_TYPE_POSITIONAL
83 light_dir = castle_LightSource<Light>Position - vec3(vertex_eye);
84 float distance_to_light = length(light_dir);
85 light_dir /= distance_to_light;
86 #else
87 light_dir = normalize(castle_LightSource<Light>Position);
88 #endif
89 #else
90 light_dir = normal_eye;
91 #endif
92
93 #ifdef LIGHT<Light>_TYPE_SPOT
94 /* Check SpotCosCutoff first, as we want to add nothing
95 (not even ambient term) when were outside of spot light cone. */
96
97 float spot_cos = dot(normalize(castle_LightSource<Light>SpotDirection), -light_dir);
98 if (spot_cos < castle_LightSource<Light>SpotCosCutoff)
99 return;
100 #endif
101
102 float scale = 1.0;
103 /* PLUG: light_scale (scale, normal_eye, light_dir) */
104
105 #ifdef LIGHT<Light>_TYPE_SPOT
106 #ifdef LIGHT<Light>_HAS_BEAM_WIDTH
107 /* calculate spot following VRML 2.0/X3D idea of beamWidth */
108 float cutOffAngle = castle_LightSource<Light>SpotCutoff;
109 if (castle_LightSource<Light>BeamWidth < cutOffAngle) {
110 scale *= clamp(
111 ( acos(spot_cos) - cutOffAngle) /
112 (castle_LightSource<Light>BeamWidth - cutOffAngle),
113 0.0, 1.0);
114 }
115 #endif
116
117 #ifdef LIGHT<Light>_HAS_SPOT_EXPONENT
118 /* calculate spot like fixed-function pipeline, using exponent */
119 scale *= pow(spot_cos, castle_LightSource<Light>SpotExponent);
120 #endif
121 #endif
122
123 #ifdef LIGHT<Light>_HAS_ATTENUATION
124 scale /= max(1.0,
125 castle_LightSource<Light>Attenuation.x +
126 castle_LightSource<Light>Attenuation.y * distance_to_light +
127 castle_LightSource<Light>Attenuation.z * distance_to_light * distance_to_light);
128 #endif
129
130 #ifdef LIGHT<Light>_HAS_RADIUS
131 if (distance_to_light >= castle_LightSource<Light>Radius)
132 scale = 0.0;
133 #endif
134
135 /* Main code that calculates per-light-source contribution to the final color.
136 Was called "getPointShade" in glTF-Sample-Viewer.
137
138 light_dir now must be the direction from current vertex to light source.
139 It is assumed to be already normalized.
140
141 view is direction from current vertex to camera, in eye-space,
142 already normalized.
143 IOW, "normalize(- vertex_eye)" because camera position is zero in eye-space.
144 */
145 AngularInfo angularInfo = getAngularInfo(light_dir, normal_eye, view);
146
147 if (angularInfo.NdotL > 0.0 || angularInfo.NdotV > 0.0)
148 {
149 // Calculate the shading terms for the microfacet specular shading model
150 vec3 F = specularReflection(material_info, angularInfo);
151 float Vis = visibilityOcclusion(material_info, angularInfo);
152 float D = microfacetDistribution(material_info, angularInfo);
153
154 // Calculation of analytical lighting contribution
155 vec3 diffuseContrib = (1.0 - F) * diffuse(material_info);
156 vec3 specContrib = F * Vis * D;
157
158 #ifdef LIGHT<Light>_TYPE_ENVIRONMENT
159 diffuseContrib *= textureCube(diffuseTexture, light_dir).rgb;
160 specContrib *= textureCube(specularTexture, light_dir).rgb;
161 #endif
162
163 // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
164 vec3 light_contribution = angularInfo.NdotL * (diffuseContrib + specContrib);
165
166 color.rgb += castle_LightSource<Light>Color *
167 light_contribution *
168 scale;
169 }
170 }