123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - renderbundle</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">
- <style>
- #info {
- background-color: rgba(0,0,0,0.75);
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - renderbundle
- <br />
- (WebGL uses 10 times fewer meshes to prevent performance issues.)
- </div>
- <div id="backend" style="position: absolute; top: 200px; left: 0; color: #fff; background-color: rgba(0,0,0,0.75); padding: 5px;">
- Draw Calls: 0
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/",
- "stats-gl": "https://cdn.jsdelivr.net/npm/stats-gl@2.2.7/dist/main.js"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'stats-gl';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- let camera, scene, renderer;
- let controls, stats;
- let gui;
- let geometries, group;
- let renderTimeAverages = [];
- //
- const position = new THREE.Vector3();
- const rotation = new THREE.Euler();
- const quaternion = new THREE.Quaternion();
- const scale = new THREE.Vector3();
- //
- const MAX_GEOMETRY_COUNT = 4000;
- const api = {
- webgpu: true,
- renderBundle: true,
- count: MAX_GEOMETRY_COUNT,
- opacity: 1,
- dynamic: false
- };
- init( ! api.webgpu );
- //
- function randomizeMatrix( matrix ) {
- position.x = Math.random() * 80 - 40;
- position.y = Math.random() * 80 - 40;
- position.z = Math.random() * 80 - 40;
- rotation.x = Math.random() * 2 * Math.PI;
- rotation.y = Math.random() * 2 * Math.PI;
- rotation.z = Math.random() * 2 * Math.PI;
- quaternion.setFromEuler( rotation );
- const factorScale = api.webgpu ? 1 : 2.0;
- scale.x = scale.y = scale.z = 0.35 * factorScale + ( Math.random() * 0.5 * factorScale );
- return matrix.compose( position, quaternion, scale );
- }
- function randomizeRotationSpeed( rotation ) {
- rotation.x = Math.random() * .05;
- rotation.y = Math.random() * .05;
- rotation.z = Math.random() * .05;
- return rotation;
- }
- function initGeometries() {
- geometries = [
- new THREE.ConeGeometry( 1.0, 2.0, 3, 1 ),
- new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
- new THREE.PlaneGeometry( 2.0, 2, 1, 1 ),
- new THREE.CapsuleGeometry( ),
- new THREE.CircleGeometry( 1.0, 3 ),
- new THREE.CylinderGeometry( 1.0, 1.0, 2.0, 3, 1 ),
- new THREE.DodecahedronGeometry( 1.0, 0 ),
- new THREE.IcosahedronGeometry( 1.0, 0 ),
- new THREE.OctahedronGeometry( 1.0, 0 ),
- new THREE.PolyhedronGeometry( [ 0, 0, 0 ], [ 0, 0, 0 ], 1, 0 ),
- new THREE.RingGeometry( 1.0, 1.5, 3 ),
- new THREE.SphereGeometry( 1.0, 3, 2 ),
- new THREE.TetrahedronGeometry( 1.0, 0 ),
- new THREE.TorusGeometry( 1.0, 0.5, 3, 3 ),
- new THREE.TorusKnotGeometry( 1.0, 0.5, 20, 3, 1, 1 ),
- ];
- }
- function cleanup() {
- if ( group ) {
- group.parent.remove( group );
- if ( group.dispose ) {
- group.dispose();
- }
- }
- }
- function initMesh( count ) {
- cleanup();
- initRegularMesh( count );
- }
- function initRegularMesh( count ) {
- group = api.renderBundle ? new THREE.BundleGroup() : new THREE.Group();
- for ( let i = 0; i < count; i ++ ) {
- const material = new THREE.MeshToonNodeMaterial( {
- color: new THREE.Color( Math.random() * 0xffffff ),
- side: THREE.DoubleSide,
- } );
- const child = new THREE.Mesh( geometries[ i % geometries.length ], material );
- randomizeMatrix( child.matrix );
- child.matrix.decompose( child.position, child.quaternion, child.scale );
- child.userData.rotationSpeed = randomizeRotationSpeed( new THREE.Euler() );
- child.frustumCulled = false;
- group.add( child );
- }
- scene.add( group );
- }
- async function init( forceWebGL = false ) {
- const count = api.count / ( api.webgpu ? 1 : 10 );
- renderTimeAverages = [];
- if ( renderer ) {
- renderer.dispose();
- controls.dispose();
- document.body.removeChild( stats.dom );
- document.body.removeChild( renderer.domElement );
- }
- // camera
- const aspect = window.innerWidth / window.innerHeight;
- camera = new THREE.PerspectiveCamera( 70, aspect, 1, 100 );
- camera.position.z = 50;
- // renderer
- renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- // scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xc1c1c1 );
- const light = new THREE.DirectionalLight( 0xffffff, 3.4 );
- scene.add( light );
- document.body.appendChild( renderer.domElement );
- await renderer.init();
- initGeometries();
- initMesh( count );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.autoRotate = true;
- controls.autoRotateSpeed = 1.0;
- // stats
- stats = new Stats( {
- precision: 3,
- horizontal: false
- } );
- // stats.init( renderer );
- document.body.appendChild( stats.dom );
- stats.dom.style.position = 'absolute';
- // gui
- gui = new GUI();
- gui.add( api, 'renderBundle' ).onChange( () => {
- init( ! api.webgpu );
- } );
- gui.add( api, 'webgpu' ).onChange( () => {
- init( ! api.webgpu );
- } );
- gui.add( api, 'dynamic' ).onChange( () => {
- group.static = ! group.static;
- } );
- // listeners
- window.addEventListener( 'resize', onWindowResize );
- function onWindowResize() {
- const width = window.innerWidth;
- const height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- group.needsUpdate = true;
-
- }
- async function animate() {
- animateMeshes();
- controls.update();
- const renderTimeAverage = performance.now();
- renderer.render( scene, camera );
- // push only the last 60 render times
- renderTimeAverages.push( performance.now() - renderTimeAverage );
- if ( renderTimeAverages.length > 60 ) renderTimeAverages.shift();
-
- const average = renderTimeAverages.reduce( ( a, b ) => a + b, 0 ) / renderTimeAverages.length;
- stats.update();
- document.getElementById( 'backend' ).innerText = `Average Render Time ${api.renderBundle ? '(Bundle)' : ''}: ` + average.toFixed( 2 ) + 'ms';
- }
- function animateMeshes() {
- const count = api.count / ( api.webgpu ? 1 : 10 );
- const loopNum = api.dynamic ? count : 0;
- for ( let i = 0; i < loopNum; i ++ ) {
- const child = group.children[ i ];
- const rotationSpeed = child.userData.rotationSpeed;
- child.rotation.set(
- child.rotation.x + rotationSpeed.x,
- child.rotation.y + rotationSpeed.y,
- child.rotation.z + rotationSpeed.z
- );
- }
- }
- }
- </script>
- </body>
- </html>
|