Skip to content

Commit eb108dc

Browse files
Simplify lighting structs in shaders
1 parent 6bda92a commit eb108dc

File tree

7 files changed

+88
-325
lines changed

7 files changed

+88
-325
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Documentation:
1717

1818
Internals:
1919
- Update minify to version 8
20+
- Simplify lighting structs in shaders (#76)
2021

2122
Bug fixes:
2223
- Correctly calculate the bounding box for primitives with scaled coordinates and radius bigger than the bounding box (#60)

src/primitives/cuboid.js

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -155,69 +155,39 @@ export default function ({ color, coords, edgeForm = {}, opacity = 1 }, extent)
155155
vec3 direction;
156156
};
157157
158-
float getDistanceAttenuation(const in float lightDistance, const in float cutoffDistance, const in float decayExponent) {
159-
if (cutoffDistance > 0.0 && decayExponent > 0.0) {
160-
return pow(saturate(-lightDistance / cutoffDistance + 1.0), decayExponent);
161-
}
162-
return 1.0;
163-
}
164-
165158
#if NUM_DIR_LIGHTS > 0
166-
struct DirectionalLight {
167-
vec3 direction;
168-
vec3 color;
169-
};
170-
171-
uniform DirectionalLight directionalLights[NUM_DIR_LIGHTS];
172-
173-
void getDirectionalLightInfo(const in DirectionalLight directionalLight, out IncidentLight light) {
174-
light.color = directionalLight.color;
175-
light.direction = directionalLight.direction;
176-
}
159+
uniform IncidentLight directionalLights[NUM_DIR_LIGHTS];
177160
#endif
178161
#if NUM_POINT_LIGHTS > 0
179162
struct PointLight {
180-
vec3 position;
181163
vec3 color;
182-
float distance;
183-
float decay;
164+
vec3 position;
184165
};
185166
186167
uniform PointLight pointLights[NUM_POINT_LIGHTS];
187168
188169
void getPointLightInfo(const in PointLight pointLight, out IncidentLight light) {
189-
vec3 lVector = pointLight.position + vViewPosition;
190-
191-
light.direction = normalize(lVector);
192-
light.color = pointLight.color + getDistanceAttenuation(length(lVector), pointLight.distance, pointLight.decay);
170+
light.direction = normalize(pointLight.position + vViewPosition);
171+
light.color = pointLight.color + 1.0;
193172
}
194173
#endif
195174
#if NUM_SPOT_LIGHTS > 0
196175
struct SpotLight {
197-
vec3 position;
198-
vec3 direction;
199176
vec3 color;
200-
float distance;
201-
float decay;
202177
float coneCos;
203-
float penumbraCos;
178+
vec3 direction;
179+
vec3 position;
204180
};
205181
206-
float getSpotAttenuation(const in float coneCosine, const in float penumbraCosine, const in float angleCosine) {
207-
return smoothstep(coneCosine, penumbraCosine, angleCosine);
208-
}
209-
210182
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
211183
212184
void getSpotLightInfo(const in SpotLight spotLight, out IncidentLight light) {
213-
vec3 lVector = spotLight.position + vViewPosition;
214-
light.direction = normalize(lVector);
185+
light.direction = normalize(spotLight.position + vViewPosition);
215186
216187
float angleCos = dot(light.direction, spotLight.direction);
217-
float spotAttenuation = getSpotAttenuation(spotLight.coneCos, spotLight.penumbraCos, angleCos);
218188
219-
if (spotAttenuation > 0.0) {
220-
light.color = spotLight.color * spotAttenuation + getDistanceAttenuation(length(lVector), spotLight.distance, spotLight.decay);
189+
if (angleCos > 0.0) {
190+
light.color = spotLight.color * angleCos + 1.0;
221191
} else {
222192
light.color = vec3(0.0);
223193
}
@@ -247,27 +217,21 @@ export default function ({ color, coords, edgeForm = {}, opacity = 1 }, extent)
247217
vec3 reflectedLight = vec3(0.0);
248218
249219
IncidentLight directLight;
220+
221+
#if NUM_DIR_LIGHTS > 0
222+
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
223+
reflectedLight += RE_Direct(directionalLights[i], normal, diffuseColor);
224+
}
225+
#endif
250226
#if NUM_POINT_LIGHTS > 0
251-
PointLight pointLight;
252227
for (int i = 0; i < NUM_POINT_LIGHTS; i++) {
253-
pointLight = pointLights[i];
254-
getPointLightInfo(pointLight, directLight);
228+
getPointLightInfo(pointLights[i], directLight);
255229
reflectedLight += RE_Direct(directLight, normal, diffuseColor);
256230
}
257231
#endif
258232
#if NUM_SPOT_LIGHTS > 0
259-
SpotLight spotLight;
260233
for (int i = 0; i < NUM_SPOT_LIGHTS; i++) {
261-
spotLight = spotLights[i];
262-
getSpotLightInfo(spotLight, directLight);
263-
reflectedLight += RE_Direct(directLight, normal, diffuseColor);
264-
}
265-
#endif
266-
#if NUM_DIR_LIGHTS > 0
267-
DirectionalLight directionalLight;
268-
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
269-
directionalLight = directionalLights[i];
270-
getDirectionalLightInfo(directionalLight, directLight);
234+
getSpotLightInfo(spotLights[i], directLight);
271235
reflectedLight += RE_Direct(directLight, normal, diffuseColor);
272236
}
273237
#endif

src/primitives/polygon.js

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -207,71 +207,35 @@ export default function ({ color, coords, edgeForm = {}, opacity = 1, vertexNorm
207207
vec3 direction;
208208
};
209209
210-
float getDistanceAttenuation(const in float lightDistance, const in float cutoffDistance, const in float decayExponent) {
211-
if (cutoffDistance > 0.0 && decayExponent > 0.0) {
212-
return pow(saturate(-lightDistance / cutoffDistance + 1.0), decayExponent);
213-
}
214-
return 1.0;
215-
}
216-
217-
float getSpotAttenuation(const in float coneCosine, const in float penumbraCosine, const in float angleCosine) {
218-
return smoothstep(coneCosine, penumbraCosine, angleCosine);
219-
}
220-
221210
#if NUM_DIR_LIGHTS > 0
222-
struct DirectionalLight {
223-
vec3 direction;
224-
vec3 color;
225-
};
226-
227-
uniform DirectionalLight directionalLights[NUM_DIR_LIGHTS];
228-
229-
void getDirectionalLightInfo(const in DirectionalLight directionalLight, out IncidentLight light) {
230-
light.color = directionalLight.color;
231-
light.direction = directionalLight.direction;
232-
}
211+
uniform IncidentLight directionalLights[NUM_DIR_LIGHTS];
233212
#endif
234213
#if NUM_POINT_LIGHTS > 0
235214
struct PointLight {
236-
vec3 position;
237215
vec3 color;
238-
float distance;
239-
float decay;
216+
vec3 position;
240217
};
241218
242219
uniform PointLight pointLights[NUM_POINT_LIGHTS];
243220
244221
void getPointLightInfo(const in PointLight pointLight, out IncidentLight light) {
245-
vec3 lVector = pointLight.position + vViewPosition;
246-
light.direction = normalize(lVector);
247-
float lightDistance = length(lVector);
248-
light.color = pointLight.color * getDistanceAttenuation(lightDistance, pointLight.distance, pointLight.decay);
222+
light.direction = normalize(pointLight.position + vViewPosition);
223+
light.color = pointLight.color;
249224
}
250225
#endif
251226
#if NUM_SPOT_LIGHTS > 0
252227
struct SpotLight {
253-
vec3 position;
254-
vec3 direction;
255228
vec3 color;
256-
float distance;
257-
float decay;
258229
float coneCos;
259-
float penumbraCos;
230+
vec3 direction;
231+
vec3 position;
260232
};
261233
262234
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
263235
264236
void getSpotLightInfo(const in SpotLight spotLight, out IncidentLight light) {
265-
vec3 lVector = spotLight.position + vViewPosition;
266-
light.direction = normalize(lVector);
267-
float angleCos = dot(light.direction, spotLight.direction);
268-
float spotAttenuation = getSpotAttenuation(spotLight.coneCos, spotLight.penumbraCos, angleCos);
269-
if (spotAttenuation > 0.0) {
270-
float lightDistance = length(lVector);
271-
light.color = spotLight.color * spotAttenuation * getDistanceAttenuation(lightDistance, spotLight.distance, spotLight.decay);
272-
} else {
273-
light.color = vec3(0.0);
274-
}
237+
light.direction = normalize(spotLight.position + vViewPosition);
238+
light.color = spotLight.color * max(dot(light.direction, spotLight.direction), 0.0);
275239
}
276240
#endif
277241
@@ -289,27 +253,20 @@ export default function ({ color, coords, edgeForm = {}, opacity = 1, vertexNorm
289253
290254
IncidentLight directLight;
291255
256+
#if NUM_DIR_LIGHTS > 0
257+
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
258+
reflectedLight += RE_Direct(directionalLights[i], normal);
259+
}
260+
#endif
292261
#if NUM_POINT_LIGHTS > 0
293-
PointLight pointLight;
294262
for (int i = 0; i < NUM_POINT_LIGHTS; i++) {
295-
pointLight = pointLights[i];
296-
getPointLightInfo(pointLight, directLight);
263+
getPointLightInfo(pointLights[i], directLight);
297264
reflectedLight += RE_Direct(directLight, normal);
298265
}
299266
#endif
300267
#if NUM_SPOT_LIGHTS > 0
301-
SpotLight spotLight;
302268
for (int i = 0; i < NUM_SPOT_LIGHTS; i++) {
303-
spotLight = spotLights[i];
304-
getSpotLightInfo(spotLight, directLight);
305-
reflectedLight += RE_Direct(directLight, normal);
306-
}
307-
#endif
308-
#if NUM_DIR_LIGHTS > 0
309-
DirectionalLight directionalLight;
310-
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
311-
directionalLight = directionalLights[i];
312-
getDirectionalLightInfo(directionalLight, directLight);
269+
getSpotLightInfo(spotLight, spotLights[i]);
313270
reflectedLight += RE_Direct(directLight, normal);
314271
}
315272
#endif

src/primitives/sphere.js

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,74 +60,35 @@ export default function ({ color, coords, opacity = 1, radius = 1 }, extent) {
6060
vec3 normal;
6161
};
6262
63-
float getDistanceAttenuation(const in float lightDistance, const in float cutoffDistance, const in float decayExponent) {
64-
if (cutoffDistance > 0.0 && decayExponent > 0.0) {
65-
return pow(saturate(- lightDistance / cutoffDistance + 1.0), decayExponent);
66-
}
67-
return 1.0;
68-
}
69-
7063
#if NUM_DIR_LIGHTS > 0
71-
struct DirectionalLight {
72-
vec3 direction;
73-
vec3 color;
74-
};
75-
76-
uniform DirectionalLight directionalLights[NUM_DIR_LIGHTS];
77-
78-
void getDirectionalLightInfo(const in DirectionalLight directionalLight, out IncidentLight light) {
79-
light.color = directionalLight.color;
80-
light.direction = directionalLight.direction;
81-
}
64+
uniform IncidentLight directionalLights[NUM_DIR_LIGHTS];
8265
#endif
8366
#if NUM_POINT_LIGHTS > 0
8467
struct PointLight {
85-
vec3 position;
8668
vec3 color;
87-
float distance;
88-
float decay;
69+
vec3 position;
8970
};
9071
9172
uniform PointLight pointLights[NUM_POINT_LIGHTS];
9273
9374
void getPointLightInfo(const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight light) {
94-
vec3 lVector = pointLight.position - geometry.position;
95-
96-
light.direction = normalize(lVector);
97-
light.color = pointLight.color * getDistanceAttenuation(length(lVector), pointLight.distance, pointLight.decay);
75+
light.direction = normalize(pointLight.position - geometry.position);
76+
light.color = pointLight.color;
9877
}
9978
#endif
10079
#if NUM_SPOT_LIGHTS > 0
10180
struct SpotLight {
102-
vec3 position;
103-
vec3 direction;
10481
vec3 color;
105-
float distance;
106-
float decay;
10782
float coneCos;
108-
float penumbraCos;
83+
vec3 direction;
84+
vec3 position;
10985
};
11086
11187
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
11288
113-
float getSpotAttenuation(const in float coneCosine, const in float penumbraCosine, const in float angleCosine) {
114-
return smoothstep(coneCosine, penumbraCosine, angleCosine);
115-
}
116-
11789
void getSpotLightInfo(const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight light) {
118-
vec3 lVector = spotLight.position - geometry.position;
119-
120-
light.direction = normalize(lVector);
121-
122-
float angleCos = dot(light.direction, spotLight.direction);
123-
124-
float spotAttenuation = getSpotAttenuation(spotLight.coneCos, spotLight.penumbraCos, angleCos);
125-
126-
if (spotAttenuation > 0.0) {
127-
light.color = spotLight.color * spotAttenuation * getDistanceAttenuation(length(lVector), spotLight.distance, spotLight.decay);
128-
} else {
129-
light.color = vec3(0.0);
130-
}
90+
light.direction = normalize(spotLight.position - geometry.position);
91+
light.color = spotLight.color * max(dot(light.direction, spotLight.direction), 0.0);
13192
}
13293
#endif
13394
@@ -145,6 +106,11 @@ export default function ({ color, coords, opacity = 1, radius = 1 }, extent) {
145106
146107
IncidentLight directLight;
147108
109+
#if NUM_DIR_LIGHTS > 0
110+
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
111+
light += saturate(dot(geometry.normal, directionalLights[i].direction)) * directionalLights[i].color;
112+
}
113+
#endif
148114
#if NUM_POINT_LIGHTS > 0
149115
for (int i = 0; i < NUM_POINT_LIGHTS; i++) {
150116
getPointLightInfo(pointLights[i], geometry, directLight);
@@ -159,13 +125,6 @@ export default function ({ color, coords, opacity = 1, radius = 1 }, extent) {
159125
light += saturate(dot(geometry.normal, directLight.direction)) * directLight.color;
160126
}
161127
#endif
162-
#if NUM_DIR_LIGHTS > 0
163-
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
164-
getDirectionalLightInfo(directionalLights[i], directLight);
165-
166-
light += saturate(dot(geometry.normal, directLight.direction)) * directLight.color;
167-
}
168-
#endif
169128
170129
vColor = vec4(light * diffuse * RECIPROCAL_PI, opacity);
171130
}

0 commit comments

Comments
 (0)