custom-buffergeometry-cube-indexed.html 5.2 KB

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