three.js dev template - module
HTML
<script type="importmap">
{
"imports": {
"three": "https://raw.githack.com/mrdoob/three.js/dev/build/three.webgpu.js",
"three/webgpu": "https://raw.githack.com/mrdoob/three.js/dev/build/three.webgpu.js",
"three/tsl": "https://raw.githack.com/mrdoob/three.js/dev/build/three.tsl.js",
"three/addons/": "https://raw.githack.com/mrdoob/three.js/dev/examples/jsm/"
}
}
</script>
CSS
body {
margin: 0px;
}
JavaScript
import * as THREE from 'three/webgpu';
import { vec2, vec3, texture, mix, color, storage, instanceIndex, Fn } from 'three/tsl';
import { Inspector } from 'three/addons/inspector/Inspector.js';
let camera, scene, renderer;
let box;
let updateHeightfields = null;
init();
async function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
camera.position.z = 2;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
//
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
await renderer.init();
const textureLoader = new THREE.TextureLoader();
const textureDisplace = await textureLoader.loadAsync( 'https://threejs.org/examples/textures/transition/transition1.png' );
textureDisplace.wrapS = THREE.RepeatWrapping;
textureDisplace.wrapT = THREE.RepeatWrapping;
// storage buffer repoint
const gridSize = 16;
const gridCount = gridSize * gridSize;
const heightfieldA = new THREE.StorageBufferAttribute( gridCount, 3 );
const heightfieldB = new THREE.StorageBufferAttribute( gridCount, 3 );
const heightfieldStorage = storage( heightfieldA, 'vec3', gridCount );
const computeHeightfield = Fn( () => {
const gridUV = vec2( instanceIndex.mod( gridSize ), instanceIndex.div( gridSize ) ).add( 0.5 ).div( gridSize );
const height = texture( textureDisplace, gridUV ).level( 0 ).x;
heightfieldStorage.element( instanceIndex ).assign( vec3( gridUV.sub( 0.5 ).mul( 0.5 ), height.mul( 0.25 ) ) );
} )().compute( gridCount );
updateHeightfields = () => {
heightfieldStorage.value =...