|
@@ -0,0 +1,133 @@
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js - WebGPU - Compute Texture</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="info">
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Texture
|
|
|
+ <br>Texture generated using GPU Compute.
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
|
|
|
+
|
|
|
+ <script type="importmap">
|
|
|
+ {
|
|
|
+ "imports": {
|
|
|
+ "three": "../build/three.module.js",
|
|
|
+ "three/addons/": "./jsm/",
|
|
|
+ "three/nodes": "./jsm/nodes/Nodes.js"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+
|
|
|
+ <script type="module">
|
|
|
+
|
|
|
+ import * as THREE from 'three';
|
|
|
+ import { ShaderNode, texture, textureStore, wgslFn, instanceIndex, MeshBasicNodeMaterial } from 'three/nodes';
|
|
|
+
|
|
|
+ import WebGPU from 'three/addons/capabilities/WebGPU.js';
|
|
|
+ import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
|
|
|
+
|
|
|
+ let camera, scene, renderer;
|
|
|
+
|
|
|
+ init();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ if ( WebGPU.isAvailable() === false ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WebGPU.getErrorMessage() );
|
|
|
+
|
|
|
+ throw new Error( 'No WebGPU support' );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ camera = new THREE.OrthographicCamera( - 1.0, 1.0, 1.0, - 1.0, 0, 1 );
|
|
|
+ camera.position.z = 1;
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ // texture
|
|
|
+
|
|
|
+ const width = 512, height = 512;
|
|
|
+
|
|
|
+ const storageTexture = new THREE.Texture();
|
|
|
+ storageTexture.image = { width, height };
|
|
|
+ storageTexture.magFilter = THREE.LinearFilter;
|
|
|
+ storageTexture.minFilter = THREE.NearestFilter;
|
|
|
+
|
|
|
+ // create function
|
|
|
+
|
|
|
+ const computeShaderNode = new ShaderNode( ( stack ) => {
|
|
|
+
|
|
|
+ // the first function will be the main one
|
|
|
+
|
|
|
+ const computeWGSL = wgslFn( `
|
|
|
+ fn computeWGSL( storageTex: texture_storage_2d<rgba8unorm, write>, index:u32 ) -> void {
|
|
|
+
|
|
|
+ let posX = index % ${ width };
|
|
|
+ let posY = index / ${ width };
|
|
|
+ let indexUV = vec2<u32>( posX, posY );
|
|
|
+ let uv = getUV( posX, posY );
|
|
|
+
|
|
|
+ textureStore( storageTex, indexUV, vec4f( uv, 0, 1 ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ fn getUV( posX:u32, posY:u32 ) -> vec2<f32> {
|
|
|
+
|
|
|
+ let uv = vec2<f32>( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
|
|
|
+
|
|
|
+ return uv;
|
|
|
+
|
|
|
+ }
|
|
|
+ ` );
|
|
|
+
|
|
|
+ stack.add( computeWGSL( { storageTex: textureStore( storageTexture ), index: instanceIndex } ) );
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ // compute
|
|
|
+
|
|
|
+ const computeNode = computeShaderNode.compute( width * height );
|
|
|
+
|
|
|
+ const material = new MeshBasicNodeMaterial( { color: 0x00ff00 } );
|
|
|
+ material.colorNode = texture( storageTexture );
|
|
|
+
|
|
|
+ const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
|
|
|
+ scene.add( plane );
|
|
|
+
|
|
|
+ renderer = new WebGPURenderer();
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ renderer.setAnimationLoop( animate );
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ // compute texture
|
|
|
+ renderer.compute( computeNode );
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ renderer.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|