123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - materials arrays and geometry groups</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 Materials Arrays and Geometry Groups<br />
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- let renderer, scene, camera, controls;
- let planeMesh, boxMesh, boxMeshWireframe, planeMeshWireframe;
- let materials;
- const api = {
- webgpu: true
- };
- init( ! api.webgpu );
- function init( forceWebGL = false ) {
- if ( renderer ) {
- renderer.dispose();
- controls.dispose();
- document.body.removeChild( renderer.domElement );
- }
- // renderer
- renderer = new THREE.WebGLRenderer( {
- forceWebGL,
- antialias: true,
- } );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- // scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x000000 );
- // camera
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
- camera.position.set( 0, 0, 10 );
- // controls
- controls = new OrbitControls( camera, renderer.domElement );
- // materials
- materials = [
- new THREE.MeshBasicMaterial( { color: 0xff1493, side: THREE.DoubleSide } ),
- new THREE.MeshBasicMaterial( { color: 0x0000ff, side: THREE.DoubleSide } ),
- new THREE.MeshBasicMaterial( { color: 0x00ff00, side: THREE.DoubleSide } ),
- ];
- // plane geometry
- const planeGeometry = new THREE.PlaneGeometry( 1, 1, 4, 4 );
- planeGeometry.clearGroups();
- const numFacesPerRow = 4; // Number of faces in a row (since each face is made of 2 triangles)
- planeGeometry.addGroup( 0, 6 * numFacesPerRow, 0 );
- planeGeometry.addGroup( 6 * numFacesPerRow, 6 * numFacesPerRow, 1 );
- planeGeometry.addGroup( 12 * numFacesPerRow, 6 * numFacesPerRow, 2 );
- // box geometry
- const boxGeometry = new THREE.BoxGeometry( .75, .75, .75 );
- boxGeometry.clearGroups();
- boxGeometry.addGroup( 0, 6, 0 ); // front face
- boxGeometry.addGroup( 6, 6, 0 ); // back face
- boxGeometry.addGroup( 12, 6, 2 ); // top face
- boxGeometry.addGroup( 18, 6, 2 ); // bottom face
- boxGeometry.addGroup( 24, 6, 1 ); // left face
- boxGeometry.addGroup( 30, 6, 1 ); // right face
- scene.background = forceWebGL ? new THREE.Color( 0x000000 ) : new THREE.Color( 0x222222 );
- // meshes
- planeMesh = new THREE.Mesh( planeGeometry, materials );
- const materialsWireframe = [];
- for ( let index = 0; index < materials.length; index ++ ) {
- const material = new THREE.MeshBasicMaterial( { color: materials[ index ].color, side: THREE.DoubleSide, wireframe: true } );
- materialsWireframe.push( material );
- }
- planeMeshWireframe = new THREE.Mesh( planeGeometry, materialsWireframe );
- boxMeshWireframe = new THREE.Mesh( boxGeometry, materialsWireframe );
- boxMesh = new THREE.Mesh( boxGeometry, materials );
- planeMesh.position.set( - 1.5, - 1, 0 );
- boxMesh.position.set( 1.5, - 0.75, 0 );
- boxMesh.rotation.set( - Math.PI / 8, Math.PI / 4, Math.PI / 4 );
- planeMeshWireframe.position.set( - 1.5, 1, 0 );
- boxMeshWireframe.position.set( 1.5, 1.25, 0 );
- boxMeshWireframe.rotation.set( - Math.PI / 8, Math.PI / 4, Math.PI / 4 );
- scene.add( planeMesh, planeMeshWireframe );
- scene.add( boxMesh, boxMeshWireframe );
- }
- function animate() {
- boxMesh.rotation.y += 0.005;
- boxMesh.rotation.x += 0.005;
- boxMeshWireframe.rotation.y += 0.005;
- boxMeshWireframe.rotation.x += 0.005;
- renderer.render( scene, camera );
- }
- // gui
- const gui = new GUI();
- gui.add( api, 'webgpu' ).onChange( () => {
- init( ! api.webgpu );
- } );
- // 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 );
- }
- </script>
- </body>
- </html>
|