Skip to content

Commit 72d85d9

Browse files
committed
Separate shader hooks into dedicated functions to avoid name collisions/shadowing
1 parent a568475 commit 72d85d9

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

examples/debug-color/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@
6464
}
6565
return quatVec(quaternion, normal);
6666
}
67+
68+
vec3 objectNormal;
6769
`,
6870
objectModifier: /*glsl*/`
6971
// Determine normal in object space
70-
vec3 objectNormal = gsplatNormal(scales, quaternion);
72+
objectNormal = gsplatNormal(scales, quaternion);
7173
`,
7274
splatColor: /*glsl*/`
7375
// Transform normal into world space

src/Splat.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,12 @@ export class Splat extends THREE.Mesh<SplatGeometry, THREE.ShaderMaterial> {
429429

430430
// Prepare compile hook
431431
this.material.onBeforeCompile = (program, renderer) => {
432+
if (!program.defines) {
433+
program.defines = {};
434+
}
435+
432436
if (this.shaderHooks?.vertex) {
437+
program.defines.HOOK_UNIFORMS = !!this.shaderHooks.vertex.uniforms;
433438
if (this.shaderHooks.vertex.uniforms) {
434439
// Generate uniform code block
435440
const uniforms = Object.entries(this.shaderHooks.vertex.uniforms)
@@ -439,24 +444,30 @@ export class Splat extends THREE.Mesh<SplatGeometry, THREE.ShaderMaterial> {
439444
)
440445
.join("\n");
441446
program.vertexShader = program.vertexShader.replace(
442-
"#define HOOK_UNIFORMS",
447+
"{{HOOK_UNIFORMS}}",
443448
uniforms,
444449
);
445450
}
451+
program.defines.HOOK_GLOBAL = !!this.shaderHooks.vertex.global;
446452
program.vertexShader = program.vertexShader.replace(
447-
"#define HOOK_GLOBAL",
453+
"{{HOOK_GLOBAL}}",
448454
this.shaderHooks.vertex.global ?? "",
449455
);
456+
program.defines.HOOK_OBJECT_MODIFIER =
457+
!!this.shaderHooks.vertex.objectModifier;
450458
program.vertexShader = program.vertexShader.replace(
451-
"#define HOOK_OBJECT_MODIFIER",
459+
"{{HOOK_OBJECT_MODIFIER}}",
452460
this.shaderHooks.vertex.objectModifier ?? "",
453461
);
462+
program.defines.HOOK_WORLD_MODIFIER =
463+
!!this.shaderHooks.vertex.worldModifier;
454464
program.vertexShader = program.vertexShader.replace(
455-
"#define HOOK_WORLD_MODIFIER",
465+
"{{HOOK_WORLD_MODIFIER}}",
456466
this.shaderHooks.vertex.worldModifier ?? "",
457467
);
468+
program.defines.HOOK_SPLAT_COLOR = !!this.shaderHooks.vertex.splatColor;
458469
program.vertexShader = program.vertexShader.replace(
459-
"#define HOOK_SPLAT_COLOR",
470+
"{{HOOK_SPLAT_COLOR}}",
460471
this.shaderHooks.vertex.splatColor ?? "",
461472
);
462473
}

src/shaders/splatVertex.glsl

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,34 @@ uniform float preBlurAmount;
5050
uniform float clipXY;
5151
uniform float focalAdjustment;
5252

53-
#define HOOK_GLOBAL
53+
// Shader hooks
54+
#ifdef HOOK_GLOBAL
55+
{{HOOK_GLOBAL}}
56+
#endif
57+
58+
#ifdef HOOK_UNIFORMS
59+
{{HOOK_UNIFORMS}}
60+
#endif
61+
62+
#ifdef HOOK_OBJECT_MODIFIER
63+
void _shader_hook_object_modifier(inout vec3 center, inout vec3 scales, inout vec4 quaternion, inout vec4 rgba) {
64+
{{HOOK_OBJECT_MODIFIER}}
65+
}
66+
#endif
67+
68+
#ifdef HOOK_WORLD_MODIFIER
69+
void _shader_hook_world_modifier(inout vec3 center, inout vec3 scales, inout vec4 quaternion, inout vec4 rgba) {
70+
{{HOOK_WORLD_MODIFIER}}
71+
}
72+
#endif
73+
74+
#ifdef HOOK_SPLAT_COLOR
75+
vec4 _shader_hook_splat_color(in vec3 center, in vec3 scales, in vec4 quaternion, inout vec4 rgba, in vec3 viewCenter) {
76+
{{HOOK_SPLAT_COLOR}}
77+
return rgba;
78+
}
79+
#endif
5480

55-
#define HOOK_UNIFORMS
5681

5782
void main() {
5883
// Default to outside the frustum so it's discarded if we return early
@@ -68,7 +93,9 @@ void main() {
6893
uint sIndex = splatIndex & 0x3FFFFFu;
6994
uint objectIndex = splatIndex >> 26u;
7095
decodeSplat(sIndex, center, scales, quaternion, rgba);
71-
#define HOOK_OBJECT_MODIFIER
96+
#ifdef HOOK_OBJECT_MODIFIER
97+
_shader_hook_object_modifier(center, scales, quaternion, rgba);
98+
#endif
7299

73100
#ifdef USE_BATCHING
74101
mat4 splatModelMatrix = getBatchingMatrix(objectIndex);
@@ -86,7 +113,10 @@ void main() {
86113
center = (splatModelMatrix * vec4(center, 1.0)).xyz;
87114
scales *= modelScale;
88115
rgba.a *= opacity;
89-
#define HOOK_WORLD_MODIFIER
116+
117+
#ifdef HOOK_WORLD_MODIFIER
118+
_shader_hook_world_modifier(center, scales, quaternion, rgba);
119+
#endif
90120

91121
if (rgba.a < minAlpha) {
92122
return;
@@ -224,7 +254,9 @@ void main() {
224254
rgba.rgb += evaluateSH(viewDir, sh1, sh2, sh3);
225255
#endif
226256

227-
#define HOOK_SPLAT_COLOR
257+
#ifdef HOOK_SPLAT_COLOR
258+
rgba = _shader_hook_splat_color(center, scales, quaternion, rgba, viewCenter);
259+
#endif
228260

229261
vRgba = rgba;
230262
vSplatUv = position.xy * maxStdDev;

0 commit comments

Comments
 (0)