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

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