NormalMapShader.js 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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: /* glsl */`
  22. varying vec2 vUv;
  23. void main() {
  24. vUv = uv;
  25. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  26. }`,
  27. fragmentShader: /* glsl */`
  28. uniform float height;
  29. uniform vec2 resolution;
  30. uniform sampler2D heightMap;
  31. varying vec2 vUv;
  32. void main() {
  33. float val = texture2D( heightMap, vUv ).x;
  34. float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;
  35. float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;
  36. gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );
  37. }`
  38. };
  39. THREE.NormalMapShader = NormalMapShader;
  40. } )();