NormalMapShader.js 1.1 KB

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