custom-buffergeometry-cube-typedarrays.html 5.4 KB

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