Refactor uniform creation in water material shaders and tests

This commit is contained in:
2026-04-07 07:50:20 +02:00
parent 372a5551aa
commit 691b0ca645
3 changed files with 41 additions and 36 deletions

View File

@@ -933,24 +933,22 @@ export function createWaterMaterial(options) {
} }
`; `;
const uniforms = UniformsUtils.merge([ const uniforms = UniformsUtils.clone(UniformsLib.fog);
UniformsLib.fog, Object.assign(uniforms, {
{ time: animationUniform,
time: animationUniform, waterColor: { value: [cr, cg, cb] },
waterColor: { value: [cr, cg, cb] }, surfaceOpacity: { value: clampedOpacity },
surfaceOpacity: { value: clampedOpacity }, waveStrength: { value: waveStrength },
waveStrength: { value: waveStrength }, isTopFace: { value: topFaceFlag },
isTopFace: { value: topFaceFlag }, surfaceDisplacementEnabled: surfaceDisplacementEnabledUniform,
surfaceDisplacementEnabled: surfaceDisplacementEnabledUniform, halfSize: { value: halfSize },
halfSize: { value: halfSize }, contactPatches: contactPatchesUniform,
contactPatches: contactPatchesUniform, contactPatchAxes: contactPatchAxesUniform,
contactPatchAxes: contactPatchAxesUniform, contactPatchShapes: contactPatchShapesUniform,
contactPatchShapes: contactPatchShapesUniform, reflectionTexture: reflectionTextureUniform,
reflectionTexture: reflectionTextureUniform, reflectionMatrix: reflectionMatrixUniform,
reflectionMatrix: reflectionMatrixUniform, reflectionEnabled: reflectionEnabledUniform
reflectionEnabled: reflectionEnabledUniform });
}
]);
const material = new ShaderMaterial({ const material = new ShaderMaterial({
vertexShader, vertexShader,

View File

@@ -1188,24 +1188,22 @@ export function createWaterMaterial(options: WaterMaterialOptions): WaterMateria
} }
`; `;
const uniforms = UniformsUtils.merge([ const uniforms = UniformsUtils.clone(UniformsLib.fog);
UniformsLib.fog, Object.assign(uniforms, {
{ time: animationUniform,
time: animationUniform, waterColor: { value: [cr, cg, cb] },
waterColor: { value: [cr, cg, cb] }, surfaceOpacity: { value: clampedOpacity },
surfaceOpacity: { value: clampedOpacity }, waveStrength: { value: waveStrength },
waveStrength: { value: waveStrength }, isTopFace: { value: topFaceFlag },
isTopFace: { value: topFaceFlag }, surfaceDisplacementEnabled: surfaceDisplacementEnabledUniform,
surfaceDisplacementEnabled: surfaceDisplacementEnabledUniform, halfSize: { value: halfSize },
halfSize: { value: halfSize }, contactPatches: contactPatchesUniform,
contactPatches: contactPatchesUniform, contactPatchAxes: contactPatchAxesUniform,
contactPatchAxes: contactPatchAxesUniform, contactPatchShapes: contactPatchShapesUniform,
contactPatchShapes: contactPatchShapesUniform, reflectionTexture: reflectionTextureUniform,
reflectionTexture: reflectionTextureUniform, reflectionMatrix: reflectionMatrixUniform,
reflectionMatrix: reflectionMatrixUniform, reflectionEnabled: reflectionEnabledUniform
reflectionEnabled: reflectionEnabledUniform });
}
]);
const material = new ShaderMaterial({ const material = new ShaderMaterial({
vertexShader, vertexShader,

View File

@@ -616,5 +616,14 @@ describe("water material helpers", () => {
expect(result.reflectionTextureUniform).not.toBeNull(); expect(result.reflectionTextureUniform).not.toBeNull();
expect(result.reflectionMatrixUniform).not.toBeNull(); expect(result.reflectionMatrixUniform).not.toBeNull();
expect(result.reflectionEnabledUniform?.value).toBe(0); expect(result.reflectionEnabledUniform?.value).toBe(0);
expect(result.animationUniform).toBe(material.uniforms["time"]);
expect(result.reflectionEnabledUniform).toBe(material.uniforms["reflectionEnabled"]);
if (result.animationUniform !== null && result.reflectionEnabledUniform !== null) {
result.animationUniform.value = 2.5;
result.reflectionEnabledUniform.value = 0.36;
expect(material.uniforms["time"]?.value).toBe(2.5);
expect(material.uniforms["reflectionEnabled"]?.value).toBe(0.36);
}
}); });
}); });