webgl_postprocessing_masking.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - 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. <!-- Import maps polyfill -->
  12. <!-- Remove this when import maps will be widely supported -->
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  25. import { TexturePass } from 'three/addons/postprocessing/TexturePass.js';
  26. import { ClearPass } from 'three/addons/postprocessing/ClearPass.js';
  27. import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js';
  28. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  29. let camera, composer, renderer;
  30. let box, torus;
  31. init();
  32. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  35. camera.position.z = 10;
  36. const scene1 = new THREE.Scene();
  37. const scene2 = new THREE.Scene();
  38. box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
  39. scene1.add( box );
  40. torus = new THREE.Mesh( new THREE.TorusGeometry( 3, 1, 16, 32 ) );
  41. scene2.add( torus );
  42. renderer = new THREE.WebGLRenderer();
  43. renderer.setClearColor( 0xe0e0e0 );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.autoClear = false;
  47. document.body.appendChild( renderer.domElement );
  48. //
  49. const clearPass = new ClearPass();
  50. const clearMaskPass = new ClearMaskPass();
  51. const maskPass1 = new MaskPass( scene1, camera );
  52. const maskPass2 = new MaskPass( scene2, camera );
  53. const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  54. texture1.colorSpace = THREE.SRGBColorSpace;
  55. texture1.minFilter = THREE.LinearFilter;
  56. const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  57. texture2.colorSpace = THREE.SRGBColorSpace;
  58. const texturePass1 = new TexturePass( texture1 );
  59. const texturePass2 = new TexturePass( texture2 );
  60. const outputPass = new OutputPass();
  61. const parameters = {
  62. stencilBuffer: true
  63. };
  64. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
  65. composer = new EffectComposer( renderer, renderTarget );
  66. composer.addPass( clearPass );
  67. composer.addPass( maskPass1 );
  68. composer.addPass( texturePass1 );
  69. composer.addPass( clearMaskPass );
  70. composer.addPass( maskPass2 );
  71. composer.addPass( texturePass2 );
  72. composer.addPass( clearMaskPass );
  73. composer.addPass( outputPass );
  74. window.addEventListener( 'resize', onWindowResize );
  75. }
  76. function onWindowResize() {
  77. const width = window.innerWidth;
  78. const height = window.innerHeight;
  79. camera.aspect = width / height;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( width, height );
  82. composer.setSize( width, height );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. const time = performance.now() * 0.001 + 6000;
  87. box.position.x = Math.cos( time / 1.5 ) * 2;
  88. box.position.y = Math.sin( time ) * 2;
  89. box.rotation.x = time;
  90. box.rotation.y = time / 2;
  91. torus.position.x = Math.cos( time ) * 2;
  92. torus.position.y = Math.sin( time / 1.5 ) * 2;
  93. torus.rotation.x = time;
  94. torus.rotation.y = time / 2;
  95. renderer.clear();
  96. composer.render( time );
  97. }
  98. </script>
  99. </body>
  100. </html>