1
0

webgpu_depth_texture.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/webgpu": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.tsl.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { texture } from 'three/tsl';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. let camera, scene, controls, renderer;
  27. let quad, renderTarget;
  28. const dpr = window.devicePixelRatio;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 20 );
  32. camera.position.z = 4;
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x222222 );
  35. scene.overrideMaterial = new THREE.MeshBasicNodeMaterial();
  36. //
  37. const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 128, 64 );
  38. const count = 50;
  39. const scale = 5;
  40. for ( let i = 0; i < count; i ++ ) {
  41. const r = Math.random() * 2.0 * Math.PI;
  42. const z = ( Math.random() * 2.0 ) - 1.0;
  43. const zScale = Math.sqrt( 1.0 - z * z ) * scale;
  44. const mesh = new THREE.Mesh( geometry );
  45. mesh.position.set(
  46. Math.cos( r ) * zScale,
  47. Math.sin( r ) * zScale,
  48. z * scale
  49. );
  50. mesh.rotation.set( Math.random(), Math.random(), Math.random() );
  51. scene.add( mesh );
  52. }
  53. //
  54. renderer = new THREE.WebGPURenderer( { antialias: true } );
  55. renderer.setPixelRatio( dpr );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setAnimationLoop( animate );
  58. document.body.appendChild( renderer.domElement );
  59. const depthTexture = new THREE.DepthTexture();
  60. depthTexture.type = THREE.FloatType;
  61. renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr );
  62. renderTarget.depthTexture = depthTexture;
  63. window.addEventListener( 'resize', onWindowResize );
  64. // FX
  65. const materialFX = new THREE.MeshBasicNodeMaterial();
  66. materialFX.colorNode = texture( depthTexture );
  67. quad = new THREE.QuadMesh( materialFX );
  68. //
  69. controls = new OrbitControls( camera, renderer.domElement );
  70. controls.enableDamping = true;
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
  77. }
  78. function animate() {
  79. renderer.setRenderTarget( renderTarget );
  80. renderer.render( scene, camera );
  81. renderer.setRenderTarget( null );
  82. quad.render( renderer );
  83. }
  84. </script>
  85. </body>
  86. </html>
粤ICP备19079148号