NormalMapShader.js 1.0 KB

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