123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing - masking</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { TexturePass } from './jsm/postprocessing/TexturePass.js';
- import { ClearPass } from './jsm/postprocessing/ClearPass.js';
- import { MaskPass, ClearMaskPass } from './jsm/postprocessing/MaskPass.js';
- import { CopyShader } from './jsm/shaders/CopyShader.js';
- var camera, composer, renderer;
- var box, torus;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.z = 10;
- var scene1 = new THREE.Scene();
- var scene2 = new THREE.Scene();
- box = new THREE.Mesh( new THREE.BoxBufferGeometry( 4, 4, 4 ) );
- scene1.add( box );
- torus = new THREE.Mesh( new THREE.TorusBufferGeometry( 3, 1, 16, 32 ) );
- scene2.add( torus );
- renderer = new THREE.WebGLRenderer();
- renderer.setClearColor( 0xe0e0e0 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClear = false;
- document.body.appendChild( renderer.domElement );
- //
- var clearPass = new ClearPass();
- var clearMaskPass = new ClearMaskPass();
- var maskPass1 = new MaskPass( scene1, camera );
- var maskPass2 = new MaskPass( scene2, camera );
- var texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
- texture1.minFilter = THREE.LinearFilter;
- var texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
- var texturePass1 = new TexturePass( texture1 );
- var texturePass2 = new TexturePass( texture2 );
- var outputPass = new ShaderPass( CopyShader );
- var parameters = {
- minFilter: THREE.LinearFilter,
- magFilter: THREE.LinearFilter,
- format: THREE.RGBFormat,
- stencilBuffer: true
- };
- var renderTarget = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, parameters );
- composer = new EffectComposer( renderer, renderTarget );
- composer.addPass( clearPass );
- composer.addPass( maskPass1 );
- composer.addPass( texturePass1 );
- composer.addPass( clearMaskPass );
- composer.addPass( maskPass2 );
- composer.addPass( texturePass2 );
- composer.addPass( clearMaskPass );
- composer.addPass( outputPass );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- composer.setSize( width, height );
- }
- function animate() {
- requestAnimationFrame( animate );
- var time = performance.now() * 0.001;
- box.position.x = Math.cos( time / 1.5 ) * 2;
- box.position.y = Math.sin( time ) * 2;
- box.rotation.x = time;
- box.rotation.y = time / 2;
- torus.position.x = Math.cos( time ) * 2;
- torus.position.y = Math.sin( time / 1.5 ) * 2;
- torus.rotation.x = time;
- torus.rotation.y = time / 2;
- renderer.clear();
- composer.render( time );
- }
- </script>
- </body>
- </html>
|