webgl_materials_texture3d_volume.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - volume rendering example</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 5;
  22. display:block;
  23. }
  24. .dg {
  25. z-index: 10 !important;
  26. }
  27. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  28. #inset {
  29. width: 150px;
  30. height: 150px;
  31. background-color: transparent; /* or transparent; will show through only if renderer alpha: true */
  32. border: none; /* or none; */
  33. margin: 0;
  34. padding: 0px;
  35. position: absolute;
  36. left: 20px;
  37. bottom: 20px;
  38. z-index: 100;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div id="info">
  44. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  45. Float volume render test (mip / isosurface)
  46. </div>
  47. <div id="inset"></div>
  48. <script src="../build/three.js"></script>
  49. <script src="js/controls/OrbitControls.js"></script>
  50. <script src="js/Volume.js"></script>
  51. <script src="js/loaders/NRRDLoader.js"></script>
  52. <script src="js/shaders/VolumeShader.js"></script>
  53. <script src="js/Detector.js"></script>
  54. <script src="js/libs/gunzip.min.js"></script>
  55. <script src="js/libs/dat.gui.min.js"></script>
  56. <script>
  57. if ( ! Detector.webgl ) { Detector.addGetWebGLMessage(); }
  58. var container,
  59. renderer,
  60. scene,
  61. camera,
  62. controls,
  63. volmaterial,
  64. volconfig,
  65. cmtextures,
  66. gui,
  67. drawrequested;
  68. init();
  69. function init() {
  70. scene = new THREE.Scene();
  71. // Create renderer
  72. var canvas = document.createElement( 'canvas' );
  73. var context = canvas.getContext( 'webgl2' );
  74. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  75. // Attach renderer to DOM
  76. container = document.createElement( 'div' );
  77. document.body.appendChild( container );
  78. container.appendChild( renderer.domElement );
  79. // Create camera (The volume renderer does not work very well with perspective yet)
  80. camera = new THREE.OrthographicCamera()
  81. camera.position.z = 1000;
  82. camera.near = 0.1;
  83. camera.far = 5000;
  84. camera.up = new THREE.Vector3( 0, 0, 1 ); // In our data, z is the up/down axis !!
  85. scene.add( camera );
  86. // Create controls
  87. controls = new THREE.OrbitControls( camera, renderer.domElement );
  88. controls.addEventListener( 'change', animate );
  89. controls.target.set( 64, 64, 128 );
  90. controls.enableDamping = false;
  91. controls.update();
  92. // Lighting is backed into the shader a.t.m.
  93. // var dirLight = new THREE.DirectionalLight( 0xffffff );
  94. // The gui for interaction
  95. volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' };
  96. var gui = new dat.GUI();
  97. gui.add( volconfig, 'clim1', 0, 1, 0.01 ).onChange( updateUniforms );
  98. gui.add( volconfig, 'clim2', 0, 1, 0.01 ).onChange( updateUniforms );
  99. gui.add( volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( updateUniforms );
  100. gui.add( volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( updateUniforms );
  101. gui.add( volconfig, 'isothreshold', 0, 1, 0.01 ).onChange( updateUniforms );
  102. // Keep track of window size (and initialize now)
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. onWindowResize();
  105. // Load the data ...
  106. var loader = new THREE.NRRDLoader();
  107. loader.load( "models/nrrd/stent.nrrd", function ( volume ) {
  108. window.volume = volume;
  109. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  110. // THREEJS will select R32F (33326) based on the RedFormat and FloatType.
  111. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  112. // TODO: look the dtype up in the volume metadata
  113. var texture = new THREE.Texture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
  114. texture.format = THREE.RedFormat;
  115. texture.type = THREE.FloatType;
  116. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  117. texture.unpackAlignment = 1;
  118. texture.needsUpdate = true;
  119. // Colormap textures
  120. cmtextures = {
  121. viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png' ),
  122. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png' )
  123. };
  124. // Material (shaders) to render the volume using raycasting
  125. volmaterial = new THREE.ShaderMaterial( THREE.VolumeRenderShader1 );
  126. volmaterial.side = THREE.BackSide; // The volume shader uses the backface as its "reference point"
  127. // Apply standard volume material uniform info
  128. volmaterial.uniforms.u_data.value = texture;
  129. volmaterial.uniforms.u_size.value = [ volume.xLength, volume.yLength, volume.zLength ];
  130. // Geometry for the volume
  131. var volgeometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  132. volgeometry = volgeometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
  133. var volcube = new THREE.Mesh( volgeometry, volmaterial );
  134. scene.add( volcube );
  135. // Apply uniforms now, this will call animate(), but apparently that's not enough sometimes
  136. this.updateUniforms();
  137. window.setTimeout(animate, 100);
  138. window.setTimeout(animate, 1000);
  139. } );
  140. }
  141. function updateUniforms() {
  142. volmaterial.uniforms.u_clim.value = [ volconfig.clim1, volconfig.clim2 ];
  143. volmaterial.uniforms.u_renderstyle.value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  144. volmaterial.uniforms.u_renderthreshold.value = volconfig.isothreshold; // For ISO renderstyle
  145. volmaterial.uniforms.u_cmdata.value = cmtextures[volconfig.colormap];
  146. volmaterial.needsUpdate = true;
  147. animate();
  148. }
  149. function onWindowResize() {
  150. camera.right = window.innerWidth / 5.0;
  151. camera.left = -camera.right;
  152. camera.top = window.innerHeight / 5.0;
  153. camera.bottom = -camera.top;
  154. camera.updateProjectionMatrix();
  155. renderer.setPixelRatio( window.devicePixelRatio );
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. if (controls.handleResize) { controls.handleResize(); }
  158. animate();
  159. }
  160. function animate() {
  161. // throttled animate function
  162. if ( !drawrequested ) {
  163. drawrequested = true;
  164. window.requestAnimationFrame( _animate );
  165. }
  166. }
  167. function _animate() {
  168. drawrequested = false;
  169. renderer.render( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>