webgpu_materials_arrays.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials arrays and geometry groups</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU Materials Arrays and Geometry Groups<br />
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. let renderer, scene, camera, controls;
  26. let planeMesh, boxMesh, boxMeshWireframe, planeMeshWireframe;
  27. let materials;
  28. const api = {
  29. webgpu: true
  30. };
  31. init( ! api.webgpu );
  32. function init( forceWebGL = false ) {
  33. if ( renderer ) {
  34. renderer.dispose();
  35. controls.dispose();
  36. document.body.removeChild( renderer.domElement );
  37. }
  38. // renderer
  39. renderer = new THREE.WebGLRenderer( {
  40. forceWebGL,
  41. antialias: true,
  42. } );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setAnimationLoop( animate );
  46. document.body.appendChild( renderer.domElement );
  47. // scene
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0x000000 );
  50. // camera
  51. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  52. camera.position.set( 0, 0, 10 );
  53. // controls
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. // materials
  56. materials = [
  57. new THREE.MeshBasicMaterial( { color: 0xff1493, side: THREE.DoubleSide } ),
  58. new THREE.MeshBasicMaterial( { color: 0x0000ff, side: THREE.DoubleSide } ),
  59. new THREE.MeshBasicMaterial( { color: 0x00ff00, side: THREE.DoubleSide } ),
  60. ];
  61. // plane geometry
  62. const planeGeometry = new THREE.PlaneGeometry( 1, 1, 4, 4 );
  63. planeGeometry.clearGroups();
  64. const numFacesPerRow = 4; // Number of faces in a row (since each face is made of 2 triangles)
  65. planeGeometry.addGroup( 0, 6 * numFacesPerRow, 0 );
  66. planeGeometry.addGroup( 6 * numFacesPerRow, 6 * numFacesPerRow, 1 );
  67. planeGeometry.addGroup( 12 * numFacesPerRow, 6 * numFacesPerRow, 2 );
  68. // box geometry
  69. const boxGeometry = new THREE.BoxGeometry( .75, .75, .75 );
  70. boxGeometry.clearGroups();
  71. boxGeometry.addGroup( 0, 6, 0 ); // front face
  72. boxGeometry.addGroup( 6, 6, 0 ); // back face
  73. boxGeometry.addGroup( 12, 6, 2 ); // top face
  74. boxGeometry.addGroup( 18, 6, 2 ); // bottom face
  75. boxGeometry.addGroup( 24, 6, 1 ); // left face
  76. boxGeometry.addGroup( 30, 6, 1 ); // right face
  77. scene.background = forceWebGL ? new THREE.Color( 0x000000 ) : new THREE.Color( 0x222222 );
  78. // meshes
  79. planeMesh = new THREE.Mesh( planeGeometry, materials );
  80. const materialsWireframe = [];
  81. for ( let index = 0; index < materials.length; index ++ ) {
  82. const material = new THREE.MeshBasicMaterial( { color: materials[ index ].color, side: THREE.DoubleSide, wireframe: true } );
  83. materialsWireframe.push( material );
  84. }
  85. planeMeshWireframe = new THREE.Mesh( planeGeometry, materialsWireframe );
  86. boxMeshWireframe = new THREE.Mesh( boxGeometry, materialsWireframe );
  87. boxMesh = new THREE.Mesh( boxGeometry, materials );
  88. planeMesh.position.set( - 1.5, - 1, 0 );
  89. boxMesh.position.set( 1.5, - 0.75, 0 );
  90. boxMesh.rotation.set( - Math.PI / 8, Math.PI / 4, Math.PI / 4 );
  91. planeMeshWireframe.position.set( - 1.5, 1, 0 );
  92. boxMeshWireframe.position.set( 1.5, 1.25, 0 );
  93. boxMeshWireframe.rotation.set( - Math.PI / 8, Math.PI / 4, Math.PI / 4 );
  94. scene.add( planeMesh, planeMeshWireframe );
  95. scene.add( boxMesh, boxMeshWireframe );
  96. }
  97. function animate() {
  98. boxMesh.rotation.y += 0.005;
  99. boxMesh.rotation.x += 0.005;
  100. boxMeshWireframe.rotation.y += 0.005;
  101. boxMeshWireframe.rotation.x += 0.005;
  102. renderer.render( scene, camera );
  103. }
  104. // gui
  105. const gui = new GUI();
  106. gui.add( api, 'webgpu' ).onChange( () => {
  107. init( ! api.webgpu );
  108. } );
  109. // listeners
  110. window.addEventListener( 'resize', onWindowResize );
  111. function onWindowResize() {
  112. const width = window.innerWidth;
  113. const height = window.innerHeight;
  114. camera.aspect = width / height;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( width, height );
  117. }
  118. </script>
  119. </body>
  120. </html>