1
0

webgl2_buffergeometry_attributes_integer.html 4.4 KB

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