webgpu_materials_arrays.html 4.7 KB

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