webgl_depth_texture.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - Depth Texture</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #error {
  10. margin: auto;
  11. margin-top: 40px;
  12. display: block;
  13. max-width: 400px;
  14. padding: 20px;
  15. background: #CE0808;
  16. }
  17. </style>
  18. <script id="post-vert" type="x-shader/x-vertex">
  19. varying vec2 vUv;
  20. void main() {
  21. vUv = uv;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  23. }
  24. </script>
  25. <script id="post-frag" type="x-shader/x-fragment">
  26. #include <packing>
  27. varying vec2 vUv;
  28. uniform sampler2D tDiffuse;
  29. uniform sampler2D tDepth;
  30. uniform float cameraNear;
  31. uniform float cameraFar;
  32. float readDepth( sampler2D depthSampler, vec2 coord ) {
  33. float fragCoordZ = texture2D( depthSampler, coord ).x;
  34. float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
  35. return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
  36. }
  37. void main() {
  38. //vec3 diffuse = texture2D( tDiffuse, vUv ).rgb;
  39. float depth = readDepth( tDepth, vUv );
  40. gl_FragColor.rgb = 1.0 - vec3( depth );
  41. gl_FragColor.a = 1.0;
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. <canvas></canvas>
  47. <div id="info">
  48. <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> webgl - depth texture<br/>
  49. Stores render target depth in a texture attachment.<br/>
  50. Created by <a href="http://twitter.com/mattdesl" target="_blank" rel="noopener">@mattdesl</a>.
  51. <div id="error" style="display: none;">
  52. Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
  53. This demo will not work.
  54. </div>
  55. </div>
  56. <script type="module">
  57. import * as THREE from '../build/three.module.js';
  58. import Stats from './jsm/libs/stats.module.js';
  59. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  60. var camera, scene, renderer, controls, stats;
  61. var target;
  62. var postScene, postCamera;
  63. var supportsExtension = true;
  64. init();
  65. animate();
  66. function init() {
  67. renderer = new THREE.WebGLRenderer( { canvas: document.querySelector( 'canvas' ) } );
  68. if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
  69. supportsExtension = false;
  70. document.querySelector( '#error' ).style.display = 'block';
  71. return;
  72. }
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. //
  76. stats = new Stats();
  77. document.body.appendChild( stats.dom );
  78. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  79. camera.position.z = 4;
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.enableDamping = true;
  82. controls.dampingFactor = 0.05;
  83. // Create a multi render target with Float buffers
  84. target = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
  85. target.texture.format = THREE.RGBFormat;
  86. target.texture.minFilter = THREE.NearestFilter;
  87. target.texture.magFilter = THREE.NearestFilter;
  88. target.texture.generateMipmaps = false;
  89. target.stencilBuffer = false;
  90. target.depthBuffer = true;
  91. target.depthTexture = new THREE.DepthTexture();
  92. target.depthTexture.type = THREE.UnsignedShortType;
  93. // Our scene
  94. scene = new THREE.Scene();
  95. setupScene();
  96. // Setup post-processing step
  97. setupPost();
  98. onWindowResize();
  99. window.addEventListener( 'resize', onWindowResize, false );
  100. }
  101. function setupPost() {
  102. // Setup post processing stage
  103. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  104. var postMaterial = new THREE.ShaderMaterial( {
  105. vertexShader: document.querySelector( '#post-vert' ).textContent.trim(),
  106. fragmentShader: document.querySelector( '#post-frag' ).textContent.trim(),
  107. uniforms: {
  108. cameraNear: { value: camera.near },
  109. cameraFar: { value: camera.far },
  110. tDiffuse: { value: target.texture },
  111. tDepth: { value: target.depthTexture }
  112. }
  113. } );
  114. var postPlane = new THREE.PlaneBufferGeometry( 2, 2 );
  115. var postQuad = new THREE.Mesh( postPlane, postMaterial );
  116. postScene = new THREE.Scene();
  117. postScene.add( postQuad );
  118. }
  119. function setupScene() {
  120. //var diffuse = new TextureLoader().load( 'textures/brick_diffuse.jpg' );
  121. //diffuse.wrapS = diffuse.wrapT = RepeatWrapping;
  122. // Setup some geometries
  123. var geometry = new THREE.TorusKnotBufferGeometry( 1, 0.3, 128, 64 );
  124. var material = new THREE.MeshBasicMaterial( { color: 'blue' } );
  125. var count = 50;
  126. var scale = 5;
  127. for ( var i = 0; i < count; i ++ ) {
  128. var r = Math.random() * 2.0 * Math.PI;
  129. var z = ( Math.random() * 2.0 ) - 1.0;
  130. var zScale = Math.sqrt( 1.0 - z * z ) * scale;
  131. var mesh = new THREE.Mesh( geometry, material );
  132. mesh.position.set(
  133. Math.cos( r ) * zScale,
  134. Math.sin( r ) * zScale,
  135. z * scale
  136. );
  137. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  138. scene.add( mesh );
  139. }
  140. }
  141. function onWindowResize() {
  142. var aspect = window.innerWidth / window.innerHeight;
  143. camera.aspect = aspect;
  144. camera.updateProjectionMatrix();
  145. var dpr = renderer.getPixelRatio();
  146. target.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
  147. renderer.setSize( window.innerWidth, window.innerHeight );
  148. }
  149. function animate() {
  150. if ( ! supportsExtension ) return;
  151. requestAnimationFrame( animate );
  152. // render scene into target
  153. renderer.setRenderTarget( target );
  154. renderer.render( scene, camera );
  155. // render post FX
  156. renderer.setRenderTarget( null );
  157. renderer.render( postScene, postCamera );
  158. controls.update(); // required because damping is enabled
  159. stats.update();
  160. }
  161. </script>
  162. </body>
  163. </html>