1
0

webgpu_materials_arrays.html 4.7 KB

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