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