2
0

NormalMapShader.js 971 B

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