Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(material as any).onBeforeCompile = (shader: THREE.Shader) => {
shader.fragmentShader = shader.fragmentShader.replace(
"#include ",
`#include
uniform sampler2D displacementMap;
uniform float displacementScale;
uniform float displacementBias;`
);
shader.fragmentShader = shader.fragmentShader.replace(
"#include ",
`#ifdef USE_MAP
float minElevation = ${EarthConstants.MIN_ELEVATION.toFixed(1)};
float maxElevation = ${EarthConstants.MAX_ELEVATION.toFixed(1)};
float elevationRange = maxElevation - minElevation;
float disp = texture2D( displacementMap, vUv ).x * displacementScale + displacementBias;
vec4 texelColor = texture2D( map, vec2((disp - minElevation) / elevationRange, 0.0) );
texelColor = mapTexelToLinear( texelColor );
diffuseColor *= texelColor;
#endif`
);
// We remove the displacement map from manipulating the vertices, it is
// however still required for the pixel shader, so it can't be directly
// removed.
shader.vertexShader = shader.vertexShader.replace(
"#include ",
""
);
};