webgl_postprocessing_masking.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  26. import { TexturePass } from 'three/addons/postprocessing/TexturePass.js';
  27. import { ClearPass } from 'three/addons/postprocessing/ClearPass.js';
  28. import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js';
  29. import { CopyShader } from 'three/addons/shaders/CopyShader.js';
  30. let camera, composer, renderer;
  31. let box, torus;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  36. camera.position.z = 10;
  37. const scene1 = new THREE.Scene();
  38. const scene2 = new THREE.Scene();
  39. box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
  40. scene1.add( box );
  41. torus = new THREE.Mesh( new THREE.TorusGeometry( 3, 1, 16, 32 ) );
  42. scene2.add( torus );
  43. renderer = new THREE.WebGLRenderer();
  44. renderer.setClearColor( 0xe0e0e0 );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.autoClear = false;
  48. document.body.appendChild( renderer.domElement );
  49. //
  50. const clearPass = new ClearPass();
  51. const clearMaskPass = new ClearMaskPass();
  52. const maskPass1 = new MaskPass( scene1, camera );
  53. const maskPass2 = new MaskPass( scene2, camera );
  54. const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  55. texture1.minFilter = THREE.LinearFilter;
  56. const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  57. const texturePass1 = new TexturePass( texture1 );
  58. const texturePass2 = new TexturePass( texture2 );
  59. const outputPass = new ShaderPass( CopyShader );
  60. const parameters = {
  61. stencilBuffer: true
  62. };
  63. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
  64. composer = new EffectComposer( renderer, renderTarget );
  65. composer.addPass( clearPass );
  66. composer.addPass( maskPass1 );
  67. composer.addPass( texturePass1 );
  68. composer.addPass( clearMaskPass );
  69. composer.addPass( maskPass2 );
  70. composer.addPass( texturePass2 );
  71. composer.addPass( clearMaskPass );
  72. composer.addPass( outputPass );
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. const width = window.innerWidth;
  77. const height = window.innerHeight;
  78. camera.aspect = width / height;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( width, height );
  81. composer.setSize( width, height );
  82. }
  83. function animate() {
  84. requestAnimationFrame( 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. renderer.clear();
  95. composer.render( time );
  96. }
  97. </script>
  98. </body>
  99. </html>