custom-buffergeometry-cube-indexed.html 5.4 KB

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