1
0

NormalMapShader.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {
  2. Vector2
  3. } from 'three';
  4. /** @module NormalMapShader */
  5. /**
  6. * Normal map shader, compute normals from heightmap.
  7. * @constant
  8. * @type {Object}
  9. */
  10. const NormalMapShader = {
  11. name: 'NormalMapShader',
  12. uniforms: {
  13. 'heightMap': { value: null },
  14. 'resolution': { value: new Vector2( 512, 512 ) },
  15. 'scale': { value: new Vector2( 1, 1 ) },
  16. 'height': { value: 0.05 }
  17. },
  18. vertexShader: /* glsl */`
  19. varying vec2 vUv;
  20. void main() {
  21. vUv = uv;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  23. }`,
  24. fragmentShader: /* glsl */`
  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. };
  36. export { NormalMapShader };
粤ICP备19079148号