NormalMapShader.js 988 B

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