threejs-custom-buffergeometry-cube-indexed.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 - Indexed</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/r110/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 positions = [];
  76. const normals = [];
  77. const uvs = [];
  78. for (const vertex of vertices) {
  79. positions.push(...vertex.pos);
  80. normals.push(...vertex.norm);
  81. uvs.push(...vertex.uv);
  82. }
  83. const geometry = new THREE.BufferGeometry();
  84. const positionNumComponents = 3;
  85. const normalNumComponents = 3;
  86. const uvNumComponents = 2;
  87. geometry.addAttribute(
  88. 'position',
  89. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  90. geometry.addAttribute(
  91. 'normal',
  92. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  93. geometry.addAttribute(
  94. 'uv',
  95. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  96. geometry.setIndex([
  97. 0, 1, 2, 2, 1, 3,
  98. 4, 5, 6, 6, 5, 7,
  99. 8, 9, 10, 10, 9, 11,
  100. 12, 13, 14, 14, 13, 15,
  101. 16, 17, 18, 18, 17, 19,
  102. 20, 21, 22, 22, 21, 23,
  103. ]);
  104. const loader = new THREE.TextureLoader();
  105. const texture = loader.load('resources/images/star.png');
  106. function makeInstance(geometry, color, x) {
  107. const material = new THREE.MeshPhongMaterial({color, map: texture});
  108. const cube = new THREE.Mesh(geometry, material);
  109. scene.add(cube);
  110. cube.position.x = x;
  111. return cube;
  112. }
  113. const cubes = [
  114. makeInstance(geometry, 0x88FF88, 0),
  115. makeInstance(geometry, 0x8888FF, -4),
  116. makeInstance(geometry, 0xFF8888, 4),
  117. ];
  118. function resizeRendererToDisplaySize(renderer) {
  119. const canvas = renderer.domElement;
  120. const width = canvas.clientWidth;
  121. const height = canvas.clientHeight;
  122. const needResize = canvas.width !== width || canvas.height !== height;
  123. if (needResize) {
  124. renderer.setSize(width, height, false);
  125. }
  126. return needResize;
  127. }
  128. function render(time) {
  129. time *= 0.001;
  130. if (resizeRendererToDisplaySize(renderer)) {
  131. const canvas = renderer.domElement;
  132. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  133. camera.updateProjectionMatrix();
  134. }
  135. cubes.forEach((cube, ndx) => {
  136. const speed = 1 + ndx * .1;
  137. const rot = time * speed;
  138. cube.rotation.x = rot;
  139. cube.rotation.y = rot;
  140. });
  141. renderer.render(scene, camera);
  142. requestAnimationFrame(render);
  143. }
  144. requestAnimationFrame(render);
  145. }
  146. main();
  147. </script>
  148. </html>