1
0

webgl_buffergeometry_attributes_integer.html 4.3 KB

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