2
0

NormalMapShader.js 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {
  2. Vector2
  3. } from 'three';
  4. /**
  5. * Normal map shader
  6. * - compute normals from heightmap
  7. */
  8. const NormalMapShader = {
  9. name: 'NormalMapShader',
  10. uniforms: {
  11. 'heightMap': { value: null },
  12. 'resolution': { value: new Vector2( 512, 512 ) },
  13. 'scale': { value: new Vector2( 1, 1 ) },
  14. 'height': { value: 0.05 }
  15. },
  16. vertexShader: /* glsl */`
  17. varying vec2 vUv;
  18. void main() {
  19. vUv = uv;
  20. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  21. }`,
  22. fragmentShader: /* glsl */`
  23. uniform float height;
  24. uniform vec2 resolution;
  25. uniform sampler2D heightMap;
  26. varying vec2 vUv;
  27. void main() {
  28. float val = texture2D( heightMap, vUv ).x;
  29. float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;
  30. float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;
  31. gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );
  32. }`
  33. };
  34. export { NormalMapShader };