custom-buffergeometry-cube-typedarrays.html 5.7 KB

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