webgl_postprocessing_masking.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 { GammaCorrectionShader } from 'three/addons/shaders/GammaCorrectionShader.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.colorSpace = THREE.SRGBColorSpace;
  56. texture1.minFilter = THREE.LinearFilter;
  57. const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  58. texture2.colorSpace = THREE.SRGBColorSpace;
  59. const texturePass1 = new TexturePass( texture1 );
  60. const texturePass2 = new TexturePass( texture2 );
  61. const outputPass = new ShaderPass( GammaCorrectionShader );
  62. const parameters = {
  63. stencilBuffer: true
  64. };
  65. const renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
  66. composer = new EffectComposer( renderer, renderTarget );
  67. composer.addPass( clearPass );
  68. composer.addPass( maskPass1 );
  69. composer.addPass( texturePass1 );
  70. composer.addPass( clearMaskPass );
  71. composer.addPass( maskPass2 );
  72. composer.addPass( texturePass2 );
  73. composer.addPass( clearMaskPass );
  74. composer.addPass( outputPass );
  75. window.addEventListener( 'resize', onWindowResize );
  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. composer.setSize( width, height );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. const time = performance.now() * 0.001 + 6000;
  88. box.position.x = Math.cos( time / 1.5 ) * 2;
  89. box.position.y = Math.sin( time ) * 2;
  90. box.rotation.x = time;
  91. box.rotation.y = time / 2;
  92. torus.position.x = Math.cos( time ) * 2;
  93. torus.position.y = Math.sin( time / 1.5 ) * 2;
  94. torus.rotation.x = time;
  95. torus.rotation.y = time / 2;
  96. renderer.clear();
  97. composer.render( time );
  98. }
  99. </script>
  100. </body>
  101. </html>