webgpu_materials_arrays.html 5.3 KB

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