webgl2_volume_perlin.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl2 - volume</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
  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 { ImprovedNoise } from './jsm/math/ImprovedNoise.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { WEBGL } from './jsm/WebGL.js';
  19. if ( WEBGL.isWebGL2Available() === false ) {
  20. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  21. }
  22. let renderer, scene, camera;
  23. let mesh;
  24. init();
  25. animate();
  26. function init() {
  27. renderer = new THREE.WebGLRenderer();
  28. renderer.setPixelRatio( window.devicePixelRatio );
  29. renderer.setSize( window.innerWidth, window.innerHeight );
  30. document.body.appendChild( renderer.domElement );
  31. scene = new THREE.Scene();
  32. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  33. camera.position.set( 0, 0, 2 );
  34. new OrbitControls( camera, renderer.domElement );
  35. // Texture
  36. const size = 128;
  37. const data = new Uint8Array( size * size * size );
  38. let i = 0;
  39. const scale = 0.051;
  40. const perlin = new ImprovedNoise();
  41. for ( let z = 0; z < size; z ++ ) {
  42. for ( let y = 0; y < size; y ++ ) {
  43. for ( let x = 0; x < size; x ++ ) {
  44. data[ i ] = 128 + 128 * perlin.noise( x * scale, y * scale, z * scale );
  45. i ++;
  46. }
  47. }
  48. }
  49. const texture = new THREE.DataTexture3D( data, size, size, size );
  50. texture.format = THREE.RedFormat;
  51. texture.minFilter = THREE.LinearFilter;
  52. texture.magFilter = THREE.LinearFilter;
  53. texture.unpackAlignment = 1;
  54. // Material
  55. const vertexShader = `#version 300 es
  56. in vec3 position;
  57. uniform mat4 modelMatrix;
  58. uniform mat4 modelViewMatrix;
  59. uniform mat4 projectionMatrix;
  60. uniform vec3 cameraPos;
  61. out vec3 vOrigin;
  62. out vec3 vDirection;
  63. void main() {
  64. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  65. vOrigin = vec3( inverse( modelMatrix ) * vec4( cameraPos, 1.0 ) ).xyz;
  66. vDirection = position - vOrigin;
  67. gl_Position = projectionMatrix * mvPosition;
  68. }
  69. `;
  70. const fragmentShader = `#version 300 es
  71. precision highp float;
  72. precision highp sampler3D;
  73. uniform mat4 modelViewMatrix;
  74. uniform mat4 projectionMatrix;
  75. in vec3 vOrigin;
  76. in vec3 vDirection;
  77. out vec4 color;
  78. uniform sampler3D map;
  79. uniform float threshold;
  80. uniform float steps;
  81. vec2 hitBox( vec3 orig, vec3 dir ) {
  82. const vec3 box_min = vec3( - 0.5 );
  83. const vec3 box_max = vec3( 0.5 );
  84. vec3 inv_dir = 1.0 / dir;
  85. vec3 tmin_tmp = ( box_min - orig ) * inv_dir;
  86. vec3 tmax_tmp = ( box_max - orig ) * inv_dir;
  87. vec3 tmin = min( tmin_tmp, tmax_tmp );
  88. vec3 tmax = max( tmin_tmp, tmax_tmp );
  89. float t0 = max( tmin.x, max( tmin.y, tmin.z ) );
  90. float t1 = min( tmax.x, min( tmax.y, tmax.z ) );
  91. return vec2( t0, t1 );
  92. }
  93. float sample1( vec3 p ) {
  94. return texture( map, p ).r;
  95. }
  96. #define epsilon .0001
  97. vec3 normal( vec3 coord ) {
  98. if ( coord.x < epsilon ) return vec3( 1.0, 0.0, 0.0 );
  99. if ( coord.y < epsilon ) return vec3( 0.0, 1.0, 0.0 );
  100. if ( coord.z < epsilon ) return vec3( 0.0, 0.0, 1.0 );
  101. if ( coord.x > 1.0 - epsilon ) return vec3( - 1.0, 0.0, 0.0 );
  102. if ( coord.y > 1.0 - epsilon ) return vec3( 0.0, - 1.0, 0.0 );
  103. if ( coord.z > 1.0 - epsilon ) return vec3( 0.0, 0.0, - 1.0 );
  104. float step = 0.01;
  105. float x = sample1( coord + vec3( - step, 0.0, 0.0 ) ) - sample1( coord + vec3( step, 0.0, 0.0 ) );
  106. float y = sample1( coord + vec3( 0.0, - step, 0.0 ) ) - sample1( coord + vec3( 0.0, step, 0.0 ) );
  107. float z = sample1( coord + vec3( 0.0, 0.0, - step ) ) - sample1( coord + vec3( 0.0, 0.0, step ) );
  108. return normalize( vec3( x, y, z ) );
  109. }
  110. void main(){
  111. vec3 rayDir = normalize( vDirection );
  112. vec2 bounds = hitBox( vOrigin, rayDir );
  113. if ( bounds.x > bounds.y ) discard;
  114. bounds.x = max( bounds.x, 0.0 );
  115. vec3 p = vOrigin + bounds.x * rayDir;
  116. vec3 inc = 1.0 / abs( rayDir );
  117. float delta = min( inc.x, min( inc.y, inc.z ) );
  118. delta /= steps;
  119. for ( float t = bounds.x; t < bounds.y; t += delta ) {
  120. float d = sample1( p + 0.5 );
  121. if ( d > threshold ) {
  122. color.rgb = normal( p + 0.5 ) * 0.5 + ( p * 1.5 + 0.25 );
  123. color.a = 1.;
  124. break;
  125. }
  126. p += rayDir * delta;
  127. }
  128. if ( color.a == 0.0 ) discard;
  129. }
  130. `;
  131. const geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  132. const material = new THREE.RawShaderMaterial( {
  133. uniforms: {
  134. map: { value: texture },
  135. cameraPos: { value: new THREE.Vector3() },
  136. threshold: { value: 0.6 },
  137. steps: { value: 200 }
  138. },
  139. vertexShader,
  140. fragmentShader,
  141. side: THREE.BackSide,
  142. } );
  143. mesh = new THREE.Mesh( geometry, material );
  144. scene.add( mesh );
  145. //
  146. const parameters = { threshold: 0.6, steps: 200 };
  147. function update() {
  148. material.uniforms.threshold.value = parameters.threshold;
  149. material.uniforms.steps.value = parameters.steps;
  150. }
  151. const gui = new GUI();
  152. gui.add( parameters, 'threshold', 0, 1, 0.01 ).onChange( update );
  153. gui.add( parameters, 'steps', 0, 300, 1 ).onChange( update );
  154. window.addEventListener( 'resize', onWindowResize, false );
  155. }
  156. function onWindowResize() {
  157. camera.aspect = window.innerWidth / window.innerHeight;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  160. }
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. mesh.material.uniforms.cameraPos.value.copy( camera.position );
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>