threejs-custom-buffergeometry-cube.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 - Cube</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/r119/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, 0], },
  46. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  47. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  48. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  49. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  50. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  51. // right
  52. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  53. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  54. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  55. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  56. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  57. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  58. // back
  59. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  60. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  61. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  62. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  63. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  64. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  65. // left
  66. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  67. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  68. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  69. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  70. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  71. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  72. // top
  73. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], },
  74. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  75. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  76. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  77. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  78. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], },
  79. // bottom
  80. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], },
  81. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  82. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  83. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  84. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  85. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], },
  86. ];
  87. const positions = [];
  88. const normals = [];
  89. const uvs = [];
  90. for (const vertex of vertices) {
  91. positions.push(...vertex.pos);
  92. normals.push(...vertex.norm);
  93. uvs.push(...vertex.uv);
  94. }
  95. const geometry = new THREE.BufferGeometry();
  96. const positionNumComponents = 3;
  97. const normalNumComponents = 3;
  98. const uvNumComponents = 2;
  99. geometry.setAttribute(
  100. 'position',
  101. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  102. geometry.setAttribute(
  103. 'normal',
  104. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  105. geometry.setAttribute(
  106. 'uv',
  107. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  108. const loader = new THREE.TextureLoader();
  109. const texture = loader.load('resources/images/star.png');
  110. function makeInstance(geometry, color, x) {
  111. const material = new THREE.MeshPhongMaterial({color, map: texture});
  112. const cube = new THREE.Mesh(geometry, material);
  113. scene.add(cube);
  114. cube.position.x = x;
  115. return cube;
  116. }
  117. const cubes = [
  118. makeInstance(geometry, 0x88FF88, 0),
  119. makeInstance(geometry, 0x8888FF, -4),
  120. makeInstance(geometry, 0xFF8888, 4),
  121. ];
  122. function resizeRendererToDisplaySize(renderer) {
  123. const canvas = renderer.domElement;
  124. const width = canvas.clientWidth;
  125. const height = canvas.clientHeight;
  126. const needResize = canvas.width !== width || canvas.height !== height;
  127. if (needResize) {
  128. renderer.setSize(width, height, false);
  129. }
  130. return needResize;
  131. }
  132. function render(time) {
  133. time *= 0.001;
  134. if (resizeRendererToDisplaySize(renderer)) {
  135. const canvas = renderer.domElement;
  136. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  137. camera.updateProjectionMatrix();
  138. }
  139. cubes.forEach((cube, ndx) => {
  140. const speed = 1 + ndx * .1;
  141. const rot = time * speed;
  142. cube.rotation.x = rot;
  143. cube.rotation.y = rot;
  144. });
  145. renderer.render(scene, camera);
  146. requestAnimationFrame(render);
  147. }
  148. requestAnimationFrame(render);
  149. }
  150. main();
  151. </script>
  152. </html>