webgpu_depth_texture.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <html lang="en">
  2. <head>
  3. <title>three.js - WebGPU - Depth Texture</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Depth Texture
  11. </div>
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { texture, MeshBasicNodeMaterial } from 'three/nodes';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, controls, renderer;
  29. let cameraFX, sceneFX, renderTarget;
  30. const dpr = window.devicePixelRatio;
  31. init();
  32. function init() {
  33. if ( WebGPU.isAvailable() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU support' );
  36. }
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 20 );
  38. camera.position.z = 4;
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x222222 );
  41. scene.overrideMaterial = new MeshBasicNodeMaterial();
  42. //
  43. const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 128, 64 );
  44. const count = 50;
  45. const scale = 5;
  46. for ( let i = 0; i < count; i ++ ) {
  47. const r = Math.random() * 2.0 * Math.PI;
  48. const z = ( Math.random() * 2.0 ) - 1.0;
  49. const zScale = Math.sqrt( 1.0 - z * z ) * scale;
  50. const mesh = new THREE.Mesh( geometry );
  51. mesh.position.set(
  52. Math.cos( r ) * zScale,
  53. Math.sin( r ) * zScale,
  54. z * scale
  55. );
  56. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  57. scene.add( mesh );
  58. }
  59. //
  60. renderer = new WebGPURenderer( { antialias: true } );
  61. renderer.setPixelRatio( dpr );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. const depthTexture = new THREE.DepthTexture();
  66. depthTexture.type = THREE.FloatType;
  67. renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr );
  68. renderTarget.depthTexture = depthTexture;
  69. window.addEventListener( 'resize', onWindowResize );
  70. // FX
  71. cameraFX = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  72. sceneFX = new THREE.Scene();
  73. const geometryFX = new THREE.PlaneGeometry( 2, 2 );
  74. //
  75. const materialFX = new MeshBasicNodeMaterial();
  76. materialFX.colorNode = texture( depthTexture );
  77. const quad = new THREE.Mesh( geometryFX, materialFX );
  78. sceneFX.add( quad );
  79. //
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.enableDamping = true;
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
  88. }
  89. function animate() {
  90. renderer.setRenderTarget( renderTarget );
  91. renderer.render( scene, camera );
  92. renderer.setRenderTarget( null );
  93. renderer.render( sceneFX, cameraFX );
  94. }
  95. </script>
  96. </body>
  97. </html>