You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.9 KiB
139 lines
3.9 KiB
2 years ago
|
<!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>
|
||
|
|
||
|
<!-- Import maps polyfill -->
|
||
|
<!-- Remove this when import maps will be widely supported -->
|
||
|
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
|
||
|
|
||
|
<script type="importmap">
|
||
|
{
|
||
|
"imports": {
|
||
|
"three": "../build/three.module.js",
|
||
|
"three/addons/": "./jsm/"
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<script type="module">
|
||
|
|
||
|
import * as THREE from 'three';
|
||
|
|
||
|
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
|
||
|
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
|
||
|
import { TexturePass } from 'three/addons/postprocessing/TexturePass.js';
|
||
|
import { ClearPass } from 'three/addons/postprocessing/ClearPass.js';
|
||
|
import { MaskPass, ClearMaskPass } from 'three/addons/postprocessing/MaskPass.js';
|
||
|
import { CopyShader } from 'three/addons/shaders/CopyShader.js';
|
||
|
|
||
|
let camera, composer, renderer;
|
||
|
let box, torus;
|
||
|
|
||
|
init();
|
||
|
animate();
|
||
|
|
||
|
function init() {
|
||
|
|
||
|
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
|
||
|
camera.position.z = 10;
|
||
|
|
||
|
const scene1 = new THREE.Scene();
|
||
|
const scene2 = new THREE.Scene();
|
||
|
|
||
|
box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
|
||
|
scene1.add( box );
|
||
|
|
||
|
torus = new THREE.Mesh( new THREE.TorusGeometry( 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 );
|
||
|
|
||
|
//
|
||
|
|
||
|
const clearPass = new ClearPass();
|
||
|
|
||
|
const clearMaskPass = new ClearMaskPass();
|
||
|
|
||
|
const maskPass1 = new MaskPass( scene1, camera );
|
||
|
const maskPass2 = new MaskPass( scene2, camera );
|
||
|
|
||
|
const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
|
||
|
texture1.minFilter = THREE.LinearFilter;
|
||
|
const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
|
||
|
|
||
|
const texturePass1 = new TexturePass( texture1 );
|
||
|
const texturePass2 = new TexturePass( texture2 );
|
||
|
|
||
|
const outputPass = new ShaderPass( CopyShader );
|
||
|
|
||
|
const parameters = {
|
||
|
stencilBuffer: true
|
||
|
};
|
||
|
|
||
|
const 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 );
|
||
|
|
||
|
}
|
||
|
|
||
|
function onWindowResize() {
|
||
|
|
||
|
const width = window.innerWidth;
|
||
|
const height = window.innerHeight;
|
||
|
|
||
|
camera.aspect = width / height;
|
||
|
camera.updateProjectionMatrix();
|
||
|
|
||
|
renderer.setSize( width, height );
|
||
|
composer.setSize( width, height );
|
||
|
|
||
|
}
|
||
|
|
||
|
function animate() {
|
||
|
|
||
|
requestAnimationFrame( animate );
|
||
|
|
||
|
const time = performance.now() * 0.001 + 6000;
|
||
|
|
||
|
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>
|