webgl_buffergeometry_indexed.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - indexed</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - indexed</div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. let camera, scene, renderer, stats;
  25. let mesh;
  26. init();
  27. function init() {
  28. //
  29. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  30. camera.position.z = 64;
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x050505 );
  33. //
  34. const light = new THREE.HemisphereLight();
  35. light.intensity = 3;
  36. scene.add( light );
  37. //
  38. const geometry = new THREE.BufferGeometry();
  39. const indices = [];
  40. const vertices = [];
  41. const normals = [];
  42. const colors = [];
  43. const size = 20;
  44. const segments = 10;
  45. const halfSize = size / 2;
  46. const segmentSize = size / segments;
  47. const _color = new THREE.Color();
  48. // generate vertices, normals and color data for a simple grid geometry
  49. for ( let i = 0; i <= segments; i ++ ) {
  50. const y = ( i * segmentSize ) - halfSize;
  51. for ( let j = 0; j <= segments; j ++ ) {
  52. const x = ( j * segmentSize ) - halfSize;
  53. vertices.push( x, - y, 0 );
  54. normals.push( 0, 0, 1 );
  55. const r = ( x / size ) + 0.5;
  56. const g = ( y / size ) + 0.5;
  57. _color.setRGB( r, g, 1, THREE.SRGBColorSpace );
  58. colors.push( _color.r, _color.g, _color.b );
  59. }
  60. }
  61. // generate indices (data for element array buffer)
  62. for ( let i = 0; i < segments; i ++ ) {
  63. for ( let j = 0; j < segments; j ++ ) {
  64. const a = i * ( segments + 1 ) + ( j + 1 );
  65. const b = i * ( segments + 1 ) + j;
  66. const c = ( i + 1 ) * ( segments + 1 ) + j;
  67. const d = ( i + 1 ) * ( segments + 1 ) + ( j + 1 );
  68. // generate two faces (triangles) per iteration
  69. indices.push( a, b, d ); // face one
  70. indices.push( b, c, d ); // face two
  71. }
  72. }
  73. //
  74. geometry.setIndex( indices );
  75. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  76. geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  77. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  78. const material = new THREE.MeshPhongMaterial( {
  79. side: THREE.DoubleSide,
  80. vertexColors: true
  81. } );
  82. mesh = new THREE.Mesh( geometry, material );
  83. scene.add( mesh );
  84. //
  85. renderer = new THREE.WebGLRenderer( { antialias: true } );
  86. renderer.setPixelRatio( window.devicePixelRatio );
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. renderer.setAnimationLoop( animate );
  89. document.body.appendChild( renderer.domElement );
  90. //
  91. stats = new Stats();
  92. document.body.appendChild( stats.dom );
  93. //
  94. const gui = new GUI();
  95. gui.add( material, 'wireframe' );
  96. //
  97. window.addEventListener( 'resize', onWindowResize );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. //
  105. function animate() {
  106. const time = Date.now() * 0.001;
  107. mesh.rotation.x = time * 0.25;
  108. mesh.rotation.y = time * 0.5;
  109. renderer.render( scene, camera );
  110. stats.update();
  111. }
  112. </script>
  113. </body>
  114. </html>