NormalMapShader.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. console.warn( "THREE.NormalMapShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * Normal map shader
  4. * - compute normals from heightmap
  5. */
  6. THREE.NormalMapShader = {
  7. uniforms: {
  8. "heightMap": { value: null },
  9. "resolution": { value: new THREE.Vector2( 512, 512 ) },
  10. "scale": { value: new THREE.Vector2( 1, 1 ) },
  11. "height": { value: 0.05 }
  12. },
  13. vertexShader: [
  14. "varying vec2 vUv;",
  15. "void main() {",
  16. " vUv = uv;",
  17. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  18. "}"
  19. ].join( "\n" ),
  20. fragmentShader: [
  21. "uniform float height;",
  22. "uniform vec2 resolution;",
  23. "uniform sampler2D heightMap;",
  24. "varying vec2 vUv;",
  25. "void main() {",
  26. " float val = texture2D( heightMap, vUv ).x;",
  27. " float valU = texture2D( heightMap, vUv + vec2( 1.0 / resolution.x, 0.0 ) ).x;",
  28. " float valV = texture2D( heightMap, vUv + vec2( 0.0, 1.0 / resolution.y ) ).x;",
  29. " gl_FragColor = vec4( ( 0.5 * normalize( vec3( val - valU, val - valV, height ) ) + 0.5 ), 1.0 );",
  30. "}"
  31. ].join( "\n" )
  32. };