webgpu_postprocessing_masking.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - masking</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. <meta property="og:title" content="three.js webgpu - masking">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_masking.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_masking.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info" class="invert">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Masking</span>
  18. </div>
  19. <small>Masking via Post Processing.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { pass, texture } from 'three/tsl';
  34. import { Inspector } from 'three/addons/inspector/Inspector.js';
  35. let camera, renderPipeline, renderer;
  36. let box, torus;
  37. init();
  38. function init() {
  39. // scene
  40. const baseScene = new THREE.Scene();
  41. baseScene.background = new THREE.Color( 0xe0e0e0 );
  42. const maskScene1 = new THREE.Scene();
  43. box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
  44. maskScene1.add( box );
  45. const maskScene2 = new THREE.Scene();
  46. torus = new THREE.Mesh( new THREE.TorusGeometry( 3, 1, 16, 32 ) );
  47. maskScene2.add( torus );
  48. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  49. camera.position.z = 10;
  50. // textures
  51. const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  52. texture1.colorSpace = THREE.SRGBColorSpace;
  53. texture1.minFilter = THREE.LinearFilter;
  54. texture1.generateMipmaps = false;
  55. texture1.flipY = false;
  56. const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  57. texture2.colorSpace = THREE.SRGBColorSpace;
  58. texture2.flipY = false;
  59. // renderer
  60. renderer = new THREE.WebGPURenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. renderer.inspector = new Inspector();
  65. document.body.appendChild( renderer.domElement );
  66. window.addEventListener( 'resize', onWindowResize );
  67. // post processing
  68. const base = pass( baseScene, camera );
  69. const sceneMask1 = pass( maskScene1, camera ).a;
  70. const sceneMask2 = pass( maskScene2, camera ).a;
  71. let compose = base;
  72. compose = sceneMask1.mix( compose, texture( texture1 ) );
  73. compose = sceneMask2.mix( compose, texture( texture2 ) );
  74. renderPipeline = new THREE.RenderPipeline( renderer );
  75. renderPipeline.outputNode = compose;
  76. }
  77. function onWindowResize() {
  78. const width = window.innerWidth;
  79. const height = window.innerHeight;
  80. camera.aspect = width / height;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( width, height );
  83. }
  84. function animate() {
  85. const time = performance.now() * 0.001 + 6000;
  86. box.position.x = Math.cos( time / 1.5 ) * 2;
  87. box.position.y = Math.sin( time ) * 2;
  88. box.rotation.x = time;
  89. box.rotation.y = time / 2;
  90. torus.position.x = Math.cos( time ) * 2;
  91. torus.position.y = Math.sin( time / 1.5 ) * 2;
  92. torus.rotation.x = time;
  93. torus.rotation.y = time / 2;
  94. renderPipeline.render();
  95. }
  96. </script>
  97. </body>
  98. </html>
粤ICP备19079148号