| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <html lang="en">
- <head>
- <title>three.js webgpu - texture gather</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <meta property="og:title" content="three.js webgpu - texture gather">
- <meta property="og:type" content="website">
- <meta property="og:url" content="https://threejs.org/examples/webgpu_texturegather.html">
- <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_texturegather.jpg">
- <link type="text/css" rel="stylesheet" href="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Texture Gather</span>
- </div>
- <small>
- This example demonstrates texture gather
- <br /> Left canvas is using WebGPU Backend, right canvas is WebGL Backend.
- <br /> The top half gathers color values and the bottom half gathers depth comparison.
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { If, vec4, uv, ivec2, texture, Fn } from 'three/tsl';
- // WebGPU Backend
- init();
- // WebGL Backend
- init( true );
- async function init( forceWebGL = false ) {
- const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
- const camera = new THREE.OrthographicCamera( - aspect, aspect );
- camera.position.z = 2;
- const scene = new THREE.Scene();
- // texture
- const material = new THREE.MeshBasicNodeMaterial( { color: 0xffffff } );
- const colorNode = texture();
- const depthNode = texture();
- material.colorNode = Fn( () => {
- const color = vec4( 1 ).toVar();
- const vuv = uv().toVar();
- If( vuv.y.greaterThan( 0.5 ), () => {
- color.assign(
- colorNode.sample( vuv.mul( 10 ) ).offset( ivec2( 0, 7 ) ).gather( 0 )
- );
- } ).Else( () => {
- color.assign(
- depthNode.sample( vuv ).offset( ivec2( 0, 7 ) ).gather( 0 ).compare( 1 )
- );
- } );
- return color;
- } )();
- //
- const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
- scene.add( plane );
- const renderer = new THREE.WebGPURenderer( { antialias: false, forceWebGL: forceWebGL } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth / 2, window.innerHeight );
- renderer.setAnimationLoop( animate );
- await renderer.init();
- document.body.appendChild( renderer.domElement );
- renderer.domElement.style.position = 'absolute';
- renderer.domElement.style.top = '0';
- renderer.domElement.style.left = '0';
- renderer.domElement.style.width = '50%';
- renderer.domElement.style.height = '100%';
- if ( forceWebGL ) {
- renderer.domElement.style.left = '50%';
- scene.background = new THREE.Color( 0x212121 );
- } else {
- scene.background = new THREE.Color( 0x313131 );
- }
- //
- const depthTexture = new THREE.DepthTexture();
- depthTexture.compareFunction = THREE.LessEqualCompare;
- const rt = new THREE.RenderTarget( 100, 100, {
- depthTexture,
- generateMipmaps: true,
- minFilter: THREE.LinearMipmapLinearFilter
- } );
- rt.texture.wrapS = THREE.RepeatWrapping;
- rt.texture.wrapT = THREE.RepeatWrapping;
- const cameraZ = 2.5;
- const rtScene = new THREE.Scene();
- rtScene.background = new THREE.Color( 0x808080 );
- const rtCamera = new THREE.PerspectiveCamera( 50, 1, cameraZ - 0.5 * Math.sqrt( 3 ), cameraZ + 0.5 * Math.sqrt( 3 ) );
- rtCamera.position.z = cameraZ;
- const dirLight = new THREE.DirectionalLight();
- dirLight.position.set( 1, 1, 0 );
- rtScene.add( dirLight );
- rtScene.add( new THREE.AmbientLight( 0xffffff, 0.1 ) );
- const box = new THREE.Mesh(
- new THREE.BoxGeometry(),
- new THREE.MeshStandardNodeMaterial( { color: 0xff0000 } )
- );
- box.rotation.set( Math.PI / 4, Math.PI / 4, 0 );
- rtScene.add( box );
- renderer.setRenderTarget( rt );
- renderer.render( rtScene, rtCamera );
- renderer.setRenderTarget( null );
- colorNode.value = rt.texture;
- depthNode.value = rt.depthTexture;
- //
- function animate() {
- renderer.render( scene, camera );
- }
- window.addEventListener( 'resize', onWindowResize );
- function onWindowResize() {
- renderer.setSize( window.innerWidth / 2, window.innerHeight );
- const aspect = ( window.innerWidth / 2 ) / window.innerHeight;
- const frustumHeight = camera.top - camera.bottom;
- camera.left = - frustumHeight * aspect / 2;
- camera.right = frustumHeight * aspect / 2;
- camera.updateProjectionMatrix();
- renderer.render( scene, camera );
- }
- }
- </script>
- </body>
- </html>
|