webgpu_postprocessing_masking.html 3.5 KB

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