webgl_buffergeometry_indexed.html 4.1 KB

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