123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - compute geometry</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 webgpu</a> - compute geometry
- </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 { vec3, cos, sin, mat3, storage, Fn, instanceIndex, timerLocal } from 'three/tsl';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- let camera, scene, renderer;
- let computeUpdate;
- init();
- function init() {
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
- camera.position.set( 0, 0, 1 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x333333 );
- new GLTFLoader().load( 'models/gltf/LeePerrySmith/LeePerrySmith.glb', function ( gltf ) {
- const mesh = gltf.scene.children[ 0 ];
- mesh.scale.setScalar( .1 );
- mesh.material = new THREE.MeshNormalMaterial();
- scene.add( mesh );
- //
- const positionBaseAttribute = mesh.geometry.attributes.position;
- const normalBaseAttribute = mesh.geometry.attributes.normal;
- // replace geometry attributes for storage buffer attributes
- const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( positionBaseAttribute.count, 4 );
- const normalStorageBufferAttribute = new THREE.StorageBufferAttribute( normalBaseAttribute.count, 4 );
- mesh.geometry.setAttribute( 'position', positionStorageBufferAttribute );
- mesh.geometry.setAttribute( 'normal', normalStorageBufferAttribute );
- // compute shader
- const computeFn = Fn( () => {
- const positionAttribute = storage( positionBaseAttribute, 'vec3', positionBaseAttribute.count ).toReadOnly();
- const normalAttribute = storage( normalBaseAttribute, 'vec3', normalBaseAttribute.count ).toReadOnly();
- const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec4', positionStorageBufferAttribute.count );
- const normalStorageAttribute = storage( normalStorageBufferAttribute, 'vec4', normalStorageBufferAttribute.count );
- const time = timerLocal( 1 );
- const scale = 0.3;
- //
- const position = vec3( positionAttribute.element( instanceIndex ) );
- const normal = vec3( normalAttribute.element( instanceIndex ) );
- const theta = sin( time.add( position.y ) ).mul( scale );
- const c = cos( theta );
- const s = sin( theta );
- const m = mat3(
- c, 0, s,
- 0, 1, 0,
- s.negate(), 0, c
- );
- const transformed = position.mul( m );
- const transformedNormal = normal.mul( m );
- positionStorageAttribute.element( instanceIndex ).assign( transformed );
- normalStorageAttribute.element( instanceIndex ).assign( transformedNormal );
- } );
- computeUpdate = computeFn().compute( positionBaseAttribute.count );
- } );
- // renderer
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = .7;
- controls.maxDistance = 2;
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- async function animate() {
- if ( computeUpdate ) await renderer.computeAsync( computeUpdate );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|