webgpu_materials_arrays.html 5.0 KB

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