webgl2_volume_instancing.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl2 - volume - instancing</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl2 - volume - instancing
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { VOXLoader, VOXDataTexture3D } from './jsm/loaders/VOXLoader.js';
  17. import { WEBGL } from './jsm/WebGL.js';
  18. if ( WEBGL.isWebGL2Available() === false ) {
  19. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  20. }
  21. let renderer, scene, camera;
  22. let controls;
  23. init();
  24. animate();
  25. function init() {
  26. renderer = new THREE.WebGLRenderer();
  27. renderer.setPixelRatio( window.devicePixelRatio );
  28. renderer.setSize( window.innerWidth, window.innerHeight );
  29. document.body.appendChild( renderer.domElement );
  30. scene = new THREE.Scene();
  31. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
  32. camera.position.set( 0, 0, 4 );
  33. controls = new OrbitControls( camera, renderer.domElement );
  34. controls.autoRotate = true;
  35. controls.autoRotateSpeed = - 1.0;
  36. controls.enableDamping = true;
  37. // Material
  38. const vertexShader = /* glsl */`
  39. in vec3 position;
  40. in mat4 instanceMatrix;
  41. uniform mat4 modelMatrix;
  42. uniform mat4 modelViewMatrix;
  43. uniform mat4 projectionMatrix;
  44. uniform vec3 cameraPos;
  45. out vec3 vOrigin;
  46. out vec3 vDirection;
  47. void main() {
  48. vec4 mvPosition = modelViewMatrix * instanceMatrix * vec4( position, 1.0 );
  49. vOrigin = vec3( inverse( instanceMatrix * modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz;
  50. vDirection = position - vOrigin;
  51. gl_Position = projectionMatrix * mvPosition;
  52. }
  53. `;
  54. const fragmentShader = /* glsl */`
  55. precision highp float;
  56. precision highp sampler3D;
  57. uniform mat4 modelViewMatrix;
  58. uniform mat4 projectionMatrix;
  59. in vec3 vOrigin;
  60. in vec3 vDirection;
  61. out vec4 color;
  62. uniform sampler3D map;
  63. uniform float threshold;
  64. uniform float steps;
  65. vec2 hitBox( vec3 orig, vec3 dir ) {
  66. const vec3 box_min = vec3( - 0.5 );
  67. const vec3 box_max = vec3( 0.5 );
  68. vec3 inv_dir = 1.0 / dir;
  69. vec3 tmin_tmp = ( box_min - orig ) * inv_dir;
  70. vec3 tmax_tmp = ( box_max - orig ) * inv_dir;
  71. vec3 tmin = min( tmin_tmp, tmax_tmp );
  72. vec3 tmax = max( tmin_tmp, tmax_tmp );
  73. float t0 = max( tmin.x, max( tmin.y, tmin.z ) );
  74. float t1 = min( tmax.x, min( tmax.y, tmax.z ) );
  75. return vec2( t0, t1 );
  76. }
  77. float sample1( vec3 p ) {
  78. return texture( map, p ).r;
  79. }
  80. #define epsilon .0001
  81. vec3 normal( vec3 coord ) {
  82. if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 );
  83. if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 );
  84. if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 );
  85. if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 );
  86. if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 );
  87. if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 );
  88. float step = 0.01;
  89. float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) );
  90. float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) );
  91. float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) );
  92. return normalize( vec3( x, y, z ) );
  93. }
  94. void main(){
  95. vec3 rayDir = normalize( vDirection );
  96. vec2 bounds = hitBox( vOrigin, rayDir );
  97. if ( bounds.x > bounds.y ) discard;
  98. bounds.x = max( bounds.x, 0.0 );
  99. vec3 p = vOrigin + bounds.x * rayDir;
  100. vec3 inc = 1.0 / abs( rayDir );
  101. float delta = min( inc.x, min( inc.y, inc.z ) );
  102. delta /= 50.0;
  103. for ( float t = bounds.x; t < bounds.y; t += delta ) {
  104. float d = sample1( p + 0.5 );
  105. if ( d > 0.5 ) {
  106. color.rgb = p * 2.0; // normal( p + 0.5 ); // * 0.5 + ( p * 1.5 + 0.25 );
  107. color.a = 1.;
  108. break;
  109. }
  110. p += rayDir * delta;
  111. }
  112. if ( color.a == 0.0 ) discard;
  113. }
  114. `;
  115. var loader = new VOXLoader();
  116. loader.load( 'models/vox/menger.vox', function ( chunks ) {
  117. for ( var i = 0; i < chunks.length; i ++ ) {
  118. const chunk = chunks[ i ];
  119. const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  120. const material = new THREE.RawShaderMaterial( {
  121. glslVersion: THREE.GLSL3,
  122. uniforms: {
  123. map: { value: new VOXDataTexture3D( chunk ) },
  124. cameraPos: { value: new THREE.Vector3() }
  125. },
  126. vertexShader,
  127. fragmentShader,
  128. side: THREE.BackSide
  129. } );
  130. const mesh = new THREE.InstancedMesh( geometry, material, 50000 );
  131. mesh.onBeforeRender = function () {
  132. this.material.uniforms.cameraPos.value.copy( camera.position );
  133. };
  134. const transform = new THREE.Object3D();
  135. for ( let i = 0; i < mesh.count; i ++ ) {
  136. transform.position.random().subScalar( 0.5 ).multiplyScalar( 150 );
  137. transform.rotation.x = Math.random() * Math.PI;
  138. transform.rotation.y = Math.random() * Math.PI;
  139. transform.rotation.z = Math.random() * Math.PI;
  140. transform.updateMatrix();
  141. mesh.setMatrixAt( i, transform.matrix );
  142. }
  143. scene.add( mesh );
  144. }
  145. } );
  146. window.addEventListener( 'resize', onWindowResize );
  147. }
  148. function onWindowResize() {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. controls.update();
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>