custom-buffergeometry-cube-typedarrays.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = 1;
  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. function makeInstance(geometry, color, x) {
  125. const material = new THREE.MeshPhongMaterial({color, map: texture});
  126. const cube = new THREE.Mesh(geometry, material);
  127. scene.add(cube);
  128. cube.position.x = x;
  129. return cube;
  130. }
  131. const cubes = [
  132. makeInstance(geometry, 0x88FF88, 0),
  133. makeInstance(geometry, 0x8888FF, -4),
  134. makeInstance(geometry, 0xFF8888, 4),
  135. ];
  136. function resizeRendererToDisplaySize(renderer) {
  137. const canvas = renderer.domElement;
  138. const width = canvas.clientWidth;
  139. const height = canvas.clientHeight;
  140. const needResize = canvas.width !== width || canvas.height !== height;
  141. if (needResize) {
  142. renderer.setSize(width, height, false);
  143. }
  144. return needResize;
  145. }
  146. function render(time) {
  147. time *= 0.001;
  148. if (resizeRendererToDisplaySize(renderer)) {
  149. const canvas = renderer.domElement;
  150. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  151. camera.updateProjectionMatrix();
  152. }
  153. cubes.forEach((cube, ndx) => {
  154. const speed = 1 + ndx * .1;
  155. const rot = time * speed;
  156. cube.rotation.x = rot;
  157. cube.rotation.y = rot;
  158. });
  159. renderer.render(scene, camera);
  160. requestAnimationFrame(render);
  161. }
  162. requestAnimationFrame(render);
  163. }
  164. main();
  165. </script>
  166. </html>