@@ -401,12 +401,17 @@ ${useTextureMaps ? ` if (material.uHasNormalMap == 1) {
401401 var mapN: vec3<f32>;
402402 if (material.uNormalMapMode == 1u) {
403403 // bump map: brightness is height, so the tangent-space normal comes from
404- // how fast that height changes between neighbouring texels.
405- let h = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).r;
406- let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(material.uNormalTexelSize.x, 0.0)).r;
407- let hv = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + vec2<f32>(0.0, material.uNormalTexelSize.y)).r;
404+ // how fast that height changes between neighbouring texels. sampling both
405+ // sides keeps the slope right at the edges of the map, where reaching past
406+ // one side would otherwise clamp and read back the same texel.
407+ let du = vec2<f32>(material.uNormalTexelSize.x, 0.0);
408+ let dv = vec2<f32>(0.0, material.uNormalTexelSize.y);
409+ let hl = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - du).r;
410+ let hr = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + du).r;
411+ let hd = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord - dv).r;
412+ let hu = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord + dv).r;
408413 // the surface leans away from the direction height increases in
409- mapN = normalize(vec3<f32>(h - hu, h - hv , 1.0));
414+ mapN = normalize(vec3<f32>((hl - hr) * 0.5, (hd - hu) * 0.5 , 1.0));
410415 } else {
411416 // normal map: rgb already holds the tangent-space normal
412417 mapN = textureSample(uNormalSampler, uNormalSampler_sampler, input.vTexCoord).rgb * 2.0 - 1.0;
0 commit comments