custom-buffergeometry-cube-typedarrays.html 5.7 KB

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