threejs-custom-buffergeometry-cube-typedarrays.html 5.4 KB

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