Skip to content

Commit 392f871

Browse files
Simplify lighting structs in shaders
1 parent 9b8afee commit 392f871

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
@@ -20,6 +20,7 @@ Internals:
2020
- Move some shaders to shader.js (#54)
2121
- Replace BoxGeometry by lower-level BufferGeometry (#56)
2222
- Update minify to version 8
23+
- Simplify lighting structs in shaders (#76)
2324

2425
Documentation:
2526
- Move the documentation to this repository and make it more interative

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
@@ -183,71 +183,35 @@ export default function ({ color, coords, opacity = 1 }, extent) {
183183
vec3 direction;
184184
};
185185
186-
float getDistanceAttenuation(const in float lightDistance, const in float cutoffDistance, const in float decayExponent) {
187-
if (cutoffDistance > 0.0 && decayExponent > 0.0) {
188-
return pow(saturate(-lightDistance / cutoffDistance + 1.0), decayExponent);
189-
}
190-
return 1.0;
191-
}
192-
193-
float getSpotAttenuation(const in float coneCosine, const in float penumbraCosine, const in float angleCosine) {
194-
return smoothstep(coneCosine, penumbraCosine, angleCosine);
195-
}
196-
197186
#if NUM_DIR_LIGHTS > 0
198-
struct DirectionalLight {
199-
vec3 direction;
200-
vec3 color;
201-
};
202-
203-
uniform DirectionalLight directionalLights[NUM_DIR_LIGHTS];
204-
205-
void getDirectionalLightInfo(const in DirectionalLight directionalLight, out IncidentLight light) {
206-
light.color = directionalLight.color;
207-
light.direction = directionalLight.direction;
208-
}
187+
uniform IncidentLight directionalLights[NUM_DIR_LIGHTS];
209188
#endif
210189
#if NUM_POINT_LIGHTS > 0
211190
struct PointLight {
212-
vec3 position;
213191
vec3 color;
214-
float distance;
215-
float decay;
192+
vec3 position;
216193
};
217194
218195
uniform PointLight pointLights[NUM_POINT_LIGHTS];
219196
220197
void getPointLightInfo(const in PointLight pointLight, out IncidentLight light) {
221-
vec3 lVector = pointLight.position - geometry.position;
222-
light.direction = normalize(lVector);
223-
float lightDistance = length(lVector);
224-
light.color = pointLight.color * getDistanceAttenuation(lightDistance, pointLight.distance, pointLight.decay);
198+
light.direction = normalize(pointLight.position + vViewPosition);
199+
light.color = pointLight.color;
225200
}
226201
#endif
227202
#if NUM_SPOT_LIGHTS > 0
228203
struct SpotLight {
229-
vec3 position;
230-
vec3 direction;
231204
vec3 color;
232-
float distance;
233-
float decay;
234205
float coneCos;
235-
float penumbraCos;
206+
vec3 direction;
207+
vec3 position;
236208
};
237209
238210
uniform SpotLight spotLights[NUM_SPOT_LIGHTS];
239211
240212
void getSpotLightInfo(const in SpotLight spotLight, out IncidentLight light) {
241-
vec3 lVector = spotLight.position - geometry.position;
242-
light.direction = normalize(lVector);
243-
float angleCos = dot(light.direction, spotLight.direction);
244-
float spotAttenuation = getSpotAttenuation(spotLight.coneCos, spotLight.penumbraCos, angleCos);
245-
if (spotAttenuation > 0.0) {
246-
float lightDistance = length(lVector);
247-
light.color = spotLight.color * spotAttenuation * getDistanceAttenuation(lightDistance, spotLight.distance, spotLight.decay);
248-
} else {
249-
light.color = vec3(0.0);
250-
}
213+
light.direction = normalize(spotLight.position + vViewPosition);
214+
light.color = spotLight.color * max(dot(light.direction, spotLight.direction), 0.0);
251215
}
252216
#endif
253217
@@ -264,27 +228,20 @@ export default function ({ color, coords, opacity = 1 }, extent) {
264228
265229
IncidentLight directLight;
266230
231+
#if NUM_DIR_LIGHTS > 0
232+
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
233+
reflectedLight += RE_Direct(directionalLights[i], normal);
234+
}
235+
#endif
267236
#if NUM_POINT_LIGHTS > 0
268-
PointLight pointLight;
269237
for (int i = 0; i < NUM_POINT_LIGHTS; i++) {
270-
pointLight = pointLights[i];
271-
getPointLightInfo(pointLight, directLight);
238+
getPointLightInfo(pointLights[i], directLight);
272239
reflectedLight += RE_Direct(directLight, normal);
273240
}
274241
#endif
275242
#if NUM_SPOT_LIGHTS > 0
276-
SpotLight spotLight;
277243
for (int i = 0; i < NUM_SPOT_LIGHTS; i++) {
278-
spotLight = spotLights[i];
279-
getSpotLightInfo(spotLight, directLight);
280-
reflectedLight += RE_Direct(directLight, normal);
281-
}
282-
#endif
283-
#if NUM_DIR_LIGHTS > 0
284-
DirectionalLight directionalLight;
285-
for (int i = 0; i < NUM_DIR_LIGHTS; i++) {
286-
directionalLight = directionalLights[i];
287-
getDirectionalLightInfo(directionalLight, directLight);
244+
getSpotLightInfo(spotLight, spotLights[i]);
288245
reflectedLight += RE_Direct(directLight, normal);
289246
}
290247
#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)