webgpu_postprocessing_masking.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <script type="importmap">
  12. {
  13. "imports": {
  14. "three": "../build/three.webgpu.js",
  15. "three/tsl": "../build/three.webgpu.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import { pass, texture } from 'three/tsl';
  23. let camera, postProcessing, renderer;
  24. let box, torus;
  25. init();
  26. function init() {
  27. // scene
  28. const baseScene = new THREE.Scene();
  29. baseScene.background = new THREE.Color( 0xe0e0e0 );
  30. const maskScene1 = new THREE.Scene();
  31. box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
  32. maskScene1.add( box );
  33. const maskScene2 = new THREE.Scene();
  34. torus = new THREE.Mesh( new THREE.TorusGeometry( 3, 1, 16, 32 ) );
  35. maskScene2.add( torus );
  36. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  37. camera.position.z = 10;
  38. // textures
  39. const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  40. texture1.colorSpace = THREE.SRGBColorSpace;
  41. texture1.minFilter = THREE.LinearFilter;
  42. texture1.flipY = false;
  43. const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  44. texture2.colorSpace = THREE.SRGBColorSpace;
  45. texture2.flipY = false;
  46. // renderer
  47. renderer = new THREE.WebGPURenderer();
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. renderer.setAnimationLoop( animate );
  51. document.body.appendChild( renderer.domElement );
  52. window.addEventListener( 'resize', onWindowResize );
  53. // post processing
  54. const base = pass( baseScene, camera );
  55. const sceneMask1 = pass( maskScene1, camera ).a;
  56. const sceneMask2 = pass( maskScene2, camera ).a;
  57. let compose = base;
  58. compose = sceneMask1.mix( compose, texture( texture1 ) );
  59. compose = sceneMask2.mix( compose, texture( texture2 ) );
  60. postProcessing = new THREE.PostProcessing( renderer );
  61. postProcessing.outputNode = compose;
  62. }
  63. function onWindowResize() {
  64. const width = window.innerWidth;
  65. const height = window.innerHeight;
  66. camera.aspect = width / height;
  67. camera.updateProjectionMatrix();
  68. renderer.setSize( width, height );
  69. }
  70. function animate() {
  71. const time = performance.now() * 0.001 + 6000;
  72. box.position.x = Math.cos( time / 1.5 ) * 2;
  73. box.position.y = Math.sin( time ) * 2;
  74. box.rotation.x = time;
  75. box.rotation.y = time / 2;
  76. torus.position.x = Math.cos( time ) * 2;
  77. torus.position.y = Math.sin( time / 1.5 ) * 2;
  78. torus.rotation.x = time;
  79. torus.rotation.y = time / 2;
  80. postProcessing.render();
  81. }
  82. </script>
  83. </body>
  84. </html>