webgl_buffergeometry_indexed.html 3.7 KB

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