webgl_postprocessing_masking.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  14. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  15. import { TexturePass } from './jsm/postprocessing/TexturePass.js';
  16. import { ClearPass } from './jsm/postprocessing/ClearPass.js';
  17. import { MaskPass, ClearMaskPass } from './jsm/postprocessing/MaskPass.js';
  18. import { CopyShader } from './jsm/shaders/CopyShader.js';
  19. var camera, composer, renderer;
  20. var box, torus;
  21. init();
  22. animate();
  23. function init() {
  24. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  25. camera.position.z = 10;
  26. var scene1 = new THREE.Scene();
  27. var scene2 = new THREE.Scene();
  28. box = new THREE.Mesh( new THREE.BoxBufferGeometry( 4, 4, 4 ) );
  29. scene1.add( box );
  30. torus = new THREE.Mesh( new THREE.TorusBufferGeometry( 3, 1, 16, 32 ) );
  31. scene2.add( torus );
  32. renderer = new THREE.WebGLRenderer();
  33. renderer.setClearColor( 0xe0e0e0 );
  34. renderer.setPixelRatio( window.devicePixelRatio );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. renderer.autoClear = false;
  37. document.body.appendChild( renderer.domElement );
  38. //
  39. var clearPass = new ClearPass();
  40. var clearMaskPass = new ClearMaskPass();
  41. var maskPass1 = new MaskPass( scene1, camera );
  42. var maskPass2 = new MaskPass( scene2, camera );
  43. var texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  44. texture1.minFilter = THREE.LinearFilter;
  45. var texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  46. var texturePass1 = new TexturePass( texture1 );
  47. var texturePass2 = new TexturePass( texture2 );
  48. var outputPass = new ShaderPass( CopyShader );
  49. var parameters = {
  50. minFilter: THREE.LinearFilter,
  51. magFilter: THREE.LinearFilter,
  52. format: THREE.RGBFormat,
  53. stencilBuffer: true
  54. };
  55. var renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
  56. composer = new EffectComposer( renderer, renderTarget );
  57. composer.addPass( clearPass );
  58. composer.addPass( maskPass1 );
  59. composer.addPass( texturePass1 );
  60. composer.addPass( clearMaskPass );
  61. composer.addPass( maskPass2 );
  62. composer.addPass( texturePass2 );
  63. composer.addPass( clearMaskPass );
  64. composer.addPass( outputPass );
  65. window.addEventListener( 'resize', onWindowResize, false );
  66. }
  67. function onWindowResize() {
  68. var width = window.innerWidth;
  69. var height = window.innerHeight;
  70. camera.aspect = width / height;
  71. camera.updateProjectionMatrix();
  72. renderer.setSize( width, height );
  73. composer.setSize( width, height );
  74. }
  75. function animate() {
  76. requestAnimationFrame( animate );
  77. var time = performance.now() * 0.001;
  78. box.position.x = Math.cos( time / 1.5 ) * 2;
  79. box.position.y = Math.sin( time ) * 2;
  80. box.rotation.x = time;
  81. box.rotation.y = time / 2;
  82. torus.position.x = Math.cos( time ) * 2;
  83. torus.position.y = Math.sin( time / 1.5 ) * 2;
  84. torus.rotation.x = time;
  85. torus.rotation.y = time / 2;
  86. renderer.clear();
  87. composer.render( time );
  88. }
  89. </script>
  90. </body>
  91. </html>