NormalMapShader.js 1.3 KB

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