webgl_buffergeometry_attributes_integer.html 4.5 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. <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="importmap">
  34. {
  35. "imports": {
  36. "three": "../build/three.module.js",
  37. "three/addons/": "./jsm/"
  38. }
  39. }
  40. </script>
  41. <script type="module">
  42. import * as THREE from 'three';
  43. let camera, scene, renderer, mesh;
  44. init();
  45. function init() {
  46. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  47. camera.position.z = 2500;
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0x050505 );
  50. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  51. // geometry
  52. const triangles = 10000;
  53. const geometry = new THREE.BufferGeometry();
  54. const positions = [];
  55. const uvs = [];
  56. const textureIndices = [];
  57. const n = 800, n2 = n / 2; // triangles spread in the cube
  58. const d = 50, d2 = d / 2; // individual triangle size
  59. for ( let i = 0; i < triangles; i ++ ) {
  60. // positions
  61. const x = Math.random() * n - n2;
  62. const y = Math.random() * n - n2;
  63. const z = Math.random() * n - n2;
  64. const ax = x + Math.random() * d - d2;
  65. const ay = y + Math.random() * d - d2;
  66. const az = z + Math.random() * d - d2;
  67. const bx = x + Math.random() * d - d2;
  68. const by = y + Math.random() * d - d2;
  69. const bz = z + Math.random() * d - d2;
  70. const cx = x + Math.random() * d - d2;
  71. const cy = y + Math.random() * d - d2;
  72. const cz = z + Math.random() * d - d2;
  73. positions.push( ax, ay, az );
  74. positions.push( bx, by, bz );
  75. positions.push( cx, cy, cz );
  76. // uvs
  77. uvs.push( 0, 0 );
  78. uvs.push( 0.5, 1 );
  79. uvs.push( 1, 0 );
  80. // texture indices
  81. const t = i % 3;
  82. textureIndices.push( t, t, t );
  83. }
  84. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  85. geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  86. geometry.setAttribute( 'textureIndex', new THREE.Int16BufferAttribute( textureIndices, 1 ) );
  87. geometry.attributes.textureIndex.gpuType = THREE.IntType;
  88. geometry.computeBoundingSphere();
  89. // material
  90. const loader = new THREE.TextureLoader();
  91. const map1 = loader.load( 'textures/crate.gif' );
  92. const map2 = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  93. const map3 = loader.load( 'textures/terrain/grasslight-big.jpg' );
  94. const material = new THREE.ShaderMaterial( {
  95. uniforms: {
  96. uTextures: {
  97. value: [ map1, map2, map3 ]
  98. }
  99. },
  100. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  101. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  102. side: THREE.DoubleSide,
  103. glslVersion: THREE.GLSL3
  104. } );
  105. // mesh
  106. mesh = new THREE.Mesh( geometry, material );
  107. scene.add( mesh );
  108. // renderer
  109. renderer = new THREE.WebGLRenderer( { antialias: true } );
  110. renderer.setPixelRatio( window.devicePixelRatio );
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. renderer.setAnimationLoop( animate );
  113. document.body.appendChild( renderer.domElement );
  114. }
  115. function animate() {
  116. const time = Date.now() * 0.001;
  117. mesh.rotation.x = time * 0.25;
  118. mesh.rotation.y = time * 0.5;
  119. renderer.render( scene, camera );
  120. }
  121. </script>
  122. </body>
  123. </html>