UnpackDepthRGBAShader.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @module UnpackDepthRGBAShader
  3. * @three_import import { UnpackDepthRGBAShader } from 'three/addons/shaders/UnpackDepthRGBAShader.js';
  4. */
  5. /**
  6. * Unpack RGBA depth shader that shows RGBA encoded depth as monochrome color.
  7. *
  8. * @constant
  9. * @type {ShaderMaterial~Shader}
  10. */
  11. const UnpackDepthRGBAShader = {
  12. name: 'UnpackDepthRGBAShader',
  13. uniforms: {
  14. 'tDiffuse': { value: null },
  15. 'opacity': { value: 1.0 }
  16. },
  17. vertexShader: /* glsl */`
  18. varying vec2 vUv;
  19. void main() {
  20. vUv = uv;
  21. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  22. }`,
  23. fragmentShader: /* glsl */`
  24. uniform float opacity;
  25. uniform sampler2D tDiffuse;
  26. varying vec2 vUv;
  27. #include <packing>
  28. void main() {
  29. float depth = unpackRGBAToDepth( texture2D( tDiffuse, vUv ) );
  30. #ifdef USE_REVERSEDEPTHBUF
  31. if ( depth == 1.0 ) depth = 0.0; // wrong clear value?
  32. // [0, 1] -> [-1, 1]
  33. depth = depth * 2.0 - 1.0;
  34. // Reverse to forward depth (precision is already destroyed at this point)
  35. depth = 1.0 - depth;
  36. #endif
  37. gl_FragColor = vec4( vec3( 1.0 - depth ), opacity );
  38. }`
  39. };
  40. export { UnpackDepthRGBAShader };
粤ICP备19079148号