|
@@ -0,0 +1,172 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js - WebGPU - Instance Mesh</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">
|
|
|
+ <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
|
|
|
+ <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+
|
|
|
+ <div id="info">
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Instance Mesh
|
|
|
+ </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-nodes/": "./jsm/nodes/"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+
|
|
|
+ <script type="module">
|
|
|
+
|
|
|
+ import * as THREE from 'three';
|
|
|
+
|
|
|
+ import Stats from './jsm/libs/stats.module.js';
|
|
|
+ import { GUI } from './jsm/libs/lil-gui.module.min.js';
|
|
|
+
|
|
|
+ import { normalWorld } from 'three-nodes/Nodes.js';
|
|
|
+
|
|
|
+ import WebGPU from './jsm/capabilities/WebGPU.js';
|
|
|
+ import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
|
|
|
+
|
|
|
+ let camera, scene, renderer, stats;
|
|
|
+
|
|
|
+ let mesh;
|
|
|
+ const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
|
|
|
+ const count = Math.pow( amount, 3 );
|
|
|
+ const dummy = new THREE.Object3D();
|
|
|
+
|
|
|
+ init().then( animate ).catch( error );
|
|
|
+
|
|
|
+ async function init() {
|
|
|
+
|
|
|
+ if ( WebGPU.isAvailable() === false ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WebGPU.getErrorMessage() );
|
|
|
+
|
|
|
+ throw new Error( 'No WebGPU support' );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
|
|
|
+ camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
|
|
|
+ camera.lookAt( 0, 0, 0 );
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ const material = new THREE.MeshBasicMaterial();
|
|
|
+ material.colorNode = normalWorld;
|
|
|
+
|
|
|
+ const loader = new THREE.BufferGeometryLoader();
|
|
|
+ loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
|
|
|
+
|
|
|
+ geometry.computeVertexNormals();
|
|
|
+ geometry.scale( 0.5, 0.5, 0.5 );
|
|
|
+
|
|
|
+ mesh = new THREE.InstancedMesh( geometry, material, count );
|
|
|
+ scene.add( mesh );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ const gui = new GUI();
|
|
|
+ gui.add( mesh, 'count', 0, count );
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ renderer = new WebGPURenderer();
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ stats = new Stats();
|
|
|
+ document.body.appendChild( stats.dom );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize );
|
|
|
+
|
|
|
+ return renderer.init();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ requestAnimationFrame( animate );
|
|
|
+
|
|
|
+ render();
|
|
|
+
|
|
|
+ stats.update();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function render() {
|
|
|
+
|
|
|
+ if ( mesh ) {
|
|
|
+
|
|
|
+ const time = Date.now() * 0.001;
|
|
|
+
|
|
|
+ mesh.rotation.x = Math.sin( time / 4 );
|
|
|
+ mesh.rotation.y = Math.sin( time / 2 );
|
|
|
+
|
|
|
+ let i = 0;
|
|
|
+ const offset = ( amount - 1 ) / 2;
|
|
|
+
|
|
|
+ for ( let x = 0; x < amount; x ++ ) {
|
|
|
+
|
|
|
+ for ( let y = 0; y < amount; y ++ ) {
|
|
|
+
|
|
|
+ for ( let z = 0; z < amount; z ++ ) {
|
|
|
+
|
|
|
+ dummy.position.set( offset - x, offset - y, offset - z );
|
|
|
+ dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
|
|
|
+ dummy.rotation.z = dummy.rotation.y * 2;
|
|
|
+
|
|
|
+ dummy.updateMatrix();
|
|
|
+
|
|
|
+ mesh.setMatrixAt( i ++, dummy.matrix );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ renderer.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function error( error ) {
|
|
|
+
|
|
|
+ console.error( error );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+
|
|
|
+ </body>
|
|
|
+</html>
|