webgl_buffergeometry_indexed.html 4.2 KB

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