123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - 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="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { pass, texture } from 'three/tsl';
- let camera, postProcessing, renderer;
- let box, torus;
- init();
- function init() {
- // scene
- const baseScene = new THREE.Scene();
- baseScene.background = new THREE.Color( 0xe0e0e0 );
- const maskScene1 = new THREE.Scene();
- box = new THREE.Mesh( new THREE.BoxGeometry( 4, 4, 4 ) );
- maskScene1.add( box );
- const maskScene2 = new THREE.Scene();
- torus = new THREE.Mesh( new THREE.TorusGeometry( 3, 1, 16, 32 ) );
- maskScene2.add( torus );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.z = 10;
- // textures
- const texture1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
- texture1.colorSpace = THREE.SRGBColorSpace;
- texture1.minFilter = THREE.LinearFilter;
- texture1.flipY = false;
- const texture2 = new THREE.TextureLoader().load( 'textures/2294472375_24a3b8ef46_o.jpg' );
- texture2.colorSpace = THREE.SRGBColorSpace;
- texture2.flipY = false;
- // renderer
- renderer = new THREE.WebGPURenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize );
- // post processing
- const base = pass( baseScene, camera );
- const sceneMask1 = pass( maskScene1, camera ).a;
- const sceneMask2 = pass( maskScene2, camera ).a;
- let compose = base;
- compose = sceneMask1.mix( compose, texture( texture1 ) );
- compose = sceneMask2.mix( compose, texture( texture2 ) );
- postProcessing = new THREE.PostProcessing( renderer );
- postProcessing.outputNode = compose;
- }
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- }
- function 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;
- postProcessing.render();
- }
- </script>
- </body>
- </html>
|