custom-buffergeometry-cube-typedarrays.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Custom BufferGeometry - TypedArrays</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. function main() {
  33. const canvas = document.querySelector( '#c' );
  34. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  35. const fov = 75;
  36. const aspect = 2; // the canvas default
  37. const near = 0.1;
  38. const far = 100;
  39. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  40. camera.position.z = 5;
  41. const scene = new THREE.Scene();
  42. {
  43. const color = 0xFFFFFF;
  44. const intensity = 3;
  45. const light = new THREE.DirectionalLight( color, intensity );
  46. light.position.set( - 1, 2, 4 );
  47. scene.add( light );
  48. }
  49. // NOT A GOOD EXAMPLE OF HOW TO MAKE A CUBE!
  50. // Only trying to make it clear most vertices are unique
  51. const vertices = [
  52. // front
  53. { pos: [ - 1, - 1, 1 ], norm: [ 0, 0, 1 ], uv: [ 0, 0 ], }, // 0
  54. { pos: [ 1, - 1, 1 ], norm: [ 0, 0, 1 ], uv: [ 1, 0 ], }, // 1
  55. { pos: [ - 1, 1, 1 ], norm: [ 0, 0, 1 ], uv: [ 0, 1 ], }, // 2
  56. { pos: [ 1, 1, 1 ], norm: [ 0, 0, 1 ], uv: [ 1, 1 ], }, // 3
  57. // right
  58. { pos: [ 1, - 1, 1 ], norm: [ 1, 0, 0 ], uv: [ 0, 0 ], }, // 4
  59. { pos: [ 1, - 1, - 1 ], norm: [ 1, 0, 0 ], uv: [ 1, 0 ], }, // 5
  60. { pos: [ 1, 1, 1 ], norm: [ 1, 0, 0 ], uv: [ 0, 1 ], }, // 6
  61. { pos: [ 1, 1, - 1 ], norm: [ 1, 0, 0 ], uv: [ 1, 1 ], }, // 7
  62. // back
  63. { pos: [ 1, - 1, - 1 ], norm: [ 0, 0, - 1 ], uv: [ 0, 0 ], }, // 8
  64. { pos: [ - 1, - 1, - 1 ], norm: [ 0, 0, - 1 ], uv: [ 1, 0 ], }, // 9
  65. { pos: [ 1, 1, - 1 ], norm: [ 0, 0, - 1 ], uv: [ 0, 1 ], }, // 10
  66. { pos: [ - 1, 1, - 1 ], norm: [ 0, 0, - 1 ], uv: [ 1, 1 ], }, // 11
  67. // left
  68. { pos: [ - 1, - 1, - 1 ], norm: [ - 1, 0, 0 ], uv: [ 0, 0 ], }, // 12
  69. { pos: [ - 1, - 1, 1 ], norm: [ - 1, 0, 0 ], uv: [ 1, 0 ], }, // 13
  70. { pos: [ - 1, 1, - 1 ], norm: [ - 1, 0, 0 ], uv: [ 0, 1 ], }, // 14
  71. { pos: [ - 1, 1, 1 ], norm: [ - 1, 0, 0 ], uv: [ 1, 1 ], }, // 15
  72. // top
  73. { pos: [ 1, 1, - 1 ], norm: [ 0, 1, 0 ], uv: [ 0, 0 ], }, // 16
  74. { pos: [ - 1, 1, - 1 ], norm: [ 0, 1, 0 ], uv: [ 1, 0 ], }, // 17
  75. { pos: [ 1, 1, 1 ], norm: [ 0, 1, 0 ], uv: [ 0, 1 ], }, // 18
  76. { pos: [ - 1, 1, 1 ], norm: [ 0, 1, 0 ], uv: [ 1, 1 ], }, // 19
  77. // bottom
  78. { pos: [ 1, - 1, 1 ], norm: [ 0, - 1, 0 ], uv: [ 0, 0 ], }, // 20
  79. { pos: [ - 1, - 1, 1 ], norm: [ 0, - 1, 0 ], uv: [ 1, 0 ], }, // 21
  80. { pos: [ 1, - 1, - 1 ], norm: [ 0, - 1, 0 ], uv: [ 0, 1 ], }, // 22
  81. { pos: [ - 1, - 1, - 1 ], norm: [ 0, - 1, 0 ], uv: [ 1, 1 ], }, // 23
  82. ];
  83. const numVertices = vertices.length;
  84. const positionNumComponents = 3;
  85. const normalNumComponents = 3;
  86. const uvNumComponents = 2;
  87. const positions = new Float32Array( numVertices * positionNumComponents );
  88. const normals = new Float32Array( numVertices * normalNumComponents );
  89. const uvs = new Float32Array( numVertices * uvNumComponents );
  90. let posNdx = 0;
  91. let nrmNdx = 0;
  92. let uvNdx = 0;
  93. for ( const vertex of vertices ) {
  94. positions.set( vertex.pos, posNdx );
  95. normals.set( vertex.norm, nrmNdx );
  96. uvs.set( vertex.uv, uvNdx );
  97. posNdx += positionNumComponents;
  98. nrmNdx += normalNumComponents;
  99. uvNdx += uvNumComponents;
  100. }
  101. const geometry = new THREE.BufferGeometry();
  102. geometry.setAttribute(
  103. 'position',
  104. new THREE.BufferAttribute( positions, positionNumComponents ) );
  105. geometry.setAttribute(
  106. 'normal',
  107. new THREE.BufferAttribute( normals, normalNumComponents ) );
  108. geometry.setAttribute(
  109. 'uv',
  110. new THREE.BufferAttribute( uvs, uvNumComponents ) );
  111. geometry.setIndex( [
  112. 0, 1, 2, 2, 1, 3, // front
  113. 4, 5, 6, 6, 5, 7, // right
  114. 8, 9, 10, 10, 9, 11, // back
  115. 12, 13, 14, 14, 13, 15, // left
  116. 16, 17, 18, 18, 17, 19, // top
  117. 20, 21, 22, 22, 21, 23, // bottom
  118. ] );
  119. const loader = new THREE.TextureLoader();
  120. const texture = loader.load( 'resources/images/star.png' );
  121. texture.colorSpace = THREE.SRGBColorSpace;
  122. function makeInstance( geometry, color, x ) {
  123. const material = new THREE.MeshPhongMaterial( { color, map: texture } );
  124. const cube = new THREE.Mesh( geometry, material );
  125. scene.add( cube );
  126. cube.position.x = x;
  127. return cube;
  128. }
  129. const cubes = [
  130. makeInstance( geometry, 0x88FF88, 0 ),
  131. makeInstance( geometry, 0x8888FF, - 4 ),
  132. makeInstance( geometry, 0xFF8888, 4 ),
  133. ];
  134. function resizeRendererToDisplaySize( renderer ) {
  135. const canvas = renderer.domElement;
  136. const width = canvas.clientWidth;
  137. const height = canvas.clientHeight;
  138. const needResize = canvas.width !== width || canvas.height !== height;
  139. if ( needResize ) {
  140. renderer.setSize( width, height, false );
  141. }
  142. return needResize;
  143. }
  144. function render( time ) {
  145. time *= 0.001;
  146. if ( resizeRendererToDisplaySize( renderer ) ) {
  147. const canvas = renderer.domElement;
  148. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  149. camera.updateProjectionMatrix();
  150. }
  151. cubes.forEach( ( cube, ndx ) => {
  152. const speed = 1 + ndx * .1;
  153. const rot = time * speed;
  154. cube.rotation.x = rot;
  155. cube.rotation.y = rot;
  156. } );
  157. renderer.render( scene, camera );
  158. requestAnimationFrame( render );
  159. }
  160. requestAnimationFrame( render );
  161. }
  162. main();
  163. </script>
  164. </html>