webgl_buffergeometry_attributes_integer.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGL 2 - buffergeometry - integer attributes</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. <meta property="og:title" content="three.js WebGL 2 - buffergeometry - integer attributes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_attributes_integer.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_attributes_integer.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGL 2 - buffergeometry - integer attributes</div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. let camera, scene, renderer, mesh;
  27. init();
  28. function init() {
  29. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  30. camera.position.z = 2500;
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x050505 );
  33. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  34. // geometry
  35. const triangles = 10000;
  36. const geometry = new THREE.BufferGeometry();
  37. const positions = [];
  38. const uvs = [];
  39. const textureIndices = [];
  40. const n = 800, n2 = n / 2; // triangles spread in the cube
  41. const d = 50, d2 = d / 2; // individual triangle size
  42. for ( let i = 0; i < triangles; i ++ ) {
  43. // positions
  44. const x = Math.random() * n - n2;
  45. const y = Math.random() * n - n2;
  46. const z = Math.random() * n - n2;
  47. const ax = x + Math.random() * d - d2;
  48. const ay = y + Math.random() * d - d2;
  49. const az = z + Math.random() * d - d2;
  50. const bx = x + Math.random() * d - d2;
  51. const by = y + Math.random() * d - d2;
  52. const bz = z + Math.random() * d - d2;
  53. const cx = x + Math.random() * d - d2;
  54. const cy = y + Math.random() * d - d2;
  55. const cz = z + Math.random() * d - d2;
  56. positions.push( ax, ay, az );
  57. positions.push( bx, by, bz );
  58. positions.push( cx, cy, cz );
  59. // uvs
  60. uvs.push( 0, 0 );
  61. uvs.push( 0.5, 1 );
  62. uvs.push( 1, 0 );
  63. // texture indices
  64. const t = i % 3;
  65. textureIndices.push( t, t, t );
  66. }
  67. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  68. geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  69. geometry.setAttribute( 'textureIndex', new THREE.Int16BufferAttribute( textureIndices, 1 ) );
  70. geometry.attributes.textureIndex.gpuType = THREE.IntType;
  71. geometry.computeBoundingSphere();
  72. // material
  73. const loader = new THREE.TextureLoader();
  74. const map1 = loader.load( 'textures/crate.gif' );
  75. const map2 = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  76. const map3 = loader.load( 'textures/terrain/grasslight-big.jpg' );
  77. const material = new THREE.ShaderMaterial( {
  78. uniforms: {
  79. uTextures: {
  80. value: [ map1, map2, map3 ]
  81. }
  82. },
  83. vertexShader: /* glsl */`
  84. in int textureIndex;
  85. flat out int vIndex; // "flat" indicates that the value will not be interpolated (required for integer attributes)
  86. out vec2 vUv;
  87. void main() {
  88. vIndex = textureIndex;
  89. vUv = uv;
  90. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  91. }
  92. `,
  93. fragmentShader: /* glsl */`
  94. flat in int vIndex;
  95. in vec2 vUv;
  96. uniform sampler2D uTextures[ 3 ];
  97. out vec4 outColor;
  98. void main() {
  99. if ( vIndex == 0 ) outColor = texture( uTextures[ 0 ], vUv );
  100. else if ( vIndex == 1 ) outColor = texture( uTextures[ 1 ], vUv );
  101. else if ( vIndex == 2 ) outColor = texture( uTextures[ 2 ], vUv );
  102. }
  103. `,
  104. side: THREE.DoubleSide,
  105. glslVersion: THREE.GLSL3
  106. } );
  107. // mesh
  108. mesh = new THREE.Mesh( geometry, material );
  109. scene.add( mesh );
  110. // renderer
  111. renderer = new THREE.WebGLRenderer( { antialias: true } );
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. renderer.setAnimationLoop( animate );
  115. document.body.appendChild( renderer.domElement );
  116. }
  117. function animate() {
  118. const time = Date.now() * 0.001;
  119. mesh.rotation.x = time * 0.25;
  120. mesh.rotation.y = time * 0.5;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>
粤ICP备19079148号