NormalMapShader.js 1.0 KB

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