2
0

webgl_buffergeometry_indexed.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  26. let camera, scene, renderer, stats;
  27. let mesh;
  28. init();
  29. animate();
  30. function init() {
  31. //
  32. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  33. camera.position.z = 64;
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0x050505 );
  36. //
  37. const light = new THREE.HemisphereLight();
  38. scene.add( light );
  39. //
  40. const geometry = new THREE.BufferGeometry();
  41. const indices = [];
  42. const vertices = [];
  43. const normals = [];
  44. const colors = [];
  45. const size = 20;
  46. const segments = 10;
  47. const halfSize = size / 2;
  48. const segmentSize = size / segments;
  49. // generate vertices, normals and color data for a simple grid geometry
  50. for ( let i = 0; i <= segments; i ++ ) {
  51. const y = ( i * segmentSize ) - halfSize;
  52. for ( let j = 0; j <= segments; j ++ ) {
  53. const x = ( j * segmentSize ) - halfSize;
  54. vertices.push( x, - y, 0 );
  55. normals.push( 0, 0, 1 );
  56. const r = ( x / size ) + 0.5;
  57. const g = ( y / size ) + 0.5;
  58. colors.push( r, g, 1 );
  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. document.body.appendChild( renderer.domElement );
  89. //
  90. stats = new Stats();
  91. document.body.appendChild( stats.dom );
  92. //
  93. const gui = new GUI();
  94. gui.add( material, 'wireframe' );
  95. //
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. //
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. const time = Date.now() * 0.001;
  111. mesh.rotation.x = time * 0.25;
  112. mesh.rotation.y = time * 0.5;
  113. renderer.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>