2
0

webgl_buffergeometry_indexed.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. let camera, scene, renderer, stats;
  17. let mesh;
  18. init();
  19. animate();
  20. function init() {
  21. //
  22. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  23. camera.position.z = 64;
  24. scene = new THREE.Scene();
  25. scene.background = new THREE.Color( 0x050505 );
  26. //
  27. const light = new THREE.HemisphereLight();
  28. scene.add( light );
  29. //
  30. const geometry = new THREE.BufferGeometry();
  31. const indices = [];
  32. const vertices = [];
  33. const normals = [];
  34. const colors = [];
  35. const size = 20;
  36. const segments = 10;
  37. const halfSize = size / 2;
  38. const segmentSize = size / segments;
  39. // generate vertices, normals and color data for a simple grid geometry
  40. for ( let i = 0; i <= segments; i ++ ) {
  41. const y = ( i * segmentSize ) - halfSize;
  42. for ( let j = 0; j <= segments; j ++ ) {
  43. const x = ( j * segmentSize ) - halfSize;
  44. vertices.push( x, - y, 0 );
  45. normals.push( 0, 0, 1 );
  46. const r = ( x / size ) + 0.5;
  47. const g = ( y / size ) + 0.5;
  48. colors.push( r, g, 1 );
  49. }
  50. }
  51. // generate indices (data for element array buffer)
  52. for ( let i = 0; i < segments; i ++ ) {
  53. for ( let j = 0; j < segments; j ++ ) {
  54. const a = i * ( segments + 1 ) + ( j + 1 );
  55. const b = i * ( segments + 1 ) + j;
  56. const c = ( i + 1 ) * ( segments + 1 ) + j;
  57. const d = ( i + 1 ) * ( segments + 1 ) + ( j + 1 );
  58. // generate two faces (triangles) per iteration
  59. indices.push( a, b, d ); // face one
  60. indices.push( b, c, d ); // face two
  61. }
  62. }
  63. //
  64. geometry.setIndex( indices );
  65. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  66. geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  67. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  68. const material = new THREE.MeshPhongMaterial( {
  69. side: THREE.DoubleSide,
  70. vertexColors: true
  71. } );
  72. mesh = new THREE.Mesh( geometry, material );
  73. scene.add( mesh );
  74. //
  75. renderer = new THREE.WebGLRenderer( { antialias: true } );
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. document.body.appendChild( renderer.domElement );
  79. //
  80. stats = new Stats();
  81. document.body.appendChild( stats.dom );
  82. //
  83. const gui = new GUI();
  84. gui.add( material, 'wireframe' );
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. //
  94. function animate() {
  95. requestAnimationFrame( animate );
  96. render();
  97. stats.update();
  98. }
  99. function render() {
  100. const time = Date.now() * 0.001;
  101. mesh.rotation.x = time * 0.25;
  102. mesh.rotation.y = time * 0.5;
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>