webgl_materials_texture3d_volume.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/OrthographicTrackballControls.js"></script>
  50. <script src="js/Volume.js"></script>
  51. <script src="js/VolumeSlice.js"></script>
  52. <script src="js/loaders/NRRDLoader.js"></script>
  53. <script src="js/shaders/VolumeShader.js"></script>
  54. <script src="js/Detector.js"></script>
  55. <script src="js/libs/stats.min.js"></script>
  56. <script src="js/libs/gunzip.min.js"></script>
  57. <script src="js/libs/dat.gui.min.js"></script>
  58. <script>
  59. if ( ! Detector.webgl ) { Detector.addGetWebGLMessage(); }
  60. var container,
  61. stats,
  62. camera,
  63. controls,
  64. scene,
  65. renderer,
  66. gui,
  67. container2,
  68. renderer2,
  69. camera2,
  70. axes2,
  71. scene2;
  72. init();
  73. animate();
  74. function init() {
  75. // The volume renderer does not work very well with perspective yet
  76. //camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
  77. camera = new THREE.OrthographicCamera()
  78. camera.position.z = 1000;
  79. this.camera.right = window.innerWidth / 5.0;
  80. this.camera.left = -this.camera.right;
  81. this.camera.top = window.innerHeight / 5.0;
  82. this.camera.bottom = -this.camera.top;
  83. this.camera.near = 0.1;
  84. this.camera.far = 5000;
  85. this.camera.updateProjectionMatrix();
  86. scene = new THREE.Scene();
  87. scene.add( camera );
  88. // light
  89. var dirLight = new THREE.DirectionalLight( 0xffffff );
  90. dirLight.position.set( 200, 200, 1000 ).normalize();
  91. camera.add( dirLight );
  92. camera.add( dirLight.target );
  93. var loader = new THREE.NRRDLoader();
  94. loader.load( "models/nrrd/stent.nrrd", function ( volume ) {
  95. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  96. // THREEJS will select R32F (33326) based on the RedFormat and FloatType.
  97. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  98. // TODO: look the dtype up in the volume metadata
  99. var texture = new THREE.Texture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
  100. texture.format = THREE.RedFormat;
  101. texture.type = THREE.FloatType;
  102. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  103. texture.unpackAlignment = 1;
  104. texture.needsUpdate = true;
  105. // Colormap textures
  106. this.cmtextures = {
  107. viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png' ),
  108. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png' )
  109. };
  110. // Material (shaders) to render the volume using raycasting
  111. var volmaterial = new THREE.ShaderMaterial( THREE.VolumeRenderShader1 );
  112. volmaterial.side = THREE.BackSide; // The volume shader uses the backface as its "reference point"
  113. // Apply standard volume material uniform info
  114. volmaterial.uniforms.u_data.value = texture;
  115. volmaterial.uniforms.u_size.value = [ volume.xLength, volume.yLength, volume.zLength ];
  116. // Geometry for the volume
  117. var volgeometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  118. volgeometry = volgeometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
  119. var volcube = new THREE.Mesh( volgeometry, volmaterial );
  120. scene.add( volcube );
  121. // Support modifying volume rendering settings at runtime
  122. this.volmaterial = volmaterial;
  123. this.volumeConfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' };
  124. this.updateUniforms();
  125. gui.add( volumeConfig, 'clim1', 0, 1, 0.01 ).onChange( this.updateUniforms.bind( this ) );
  126. gui.add( volumeConfig, 'clim2', 0, 1, 0.01 ).onChange( this.updateUniforms.bind( this ) );
  127. gui.add( volumeConfig, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( this.updateUniforms.bind( this ) );
  128. gui.add( volumeConfig, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( this.updateUniforms.bind( this ) );
  129. gui.add( volumeConfig, 'isothreshold', 0, 1, 0.01 ).onChange( this.updateUniforms.bind( this ) );
  130. // TODO: the texture coordinates currently map directly to world coordinates. That's why we translate
  131. // the geometry above. We'd need to add a material attribute with texture coordinates to fix this.
  132. } );
  133. // renderer
  134. var canvas = document.createElement( 'canvas' );
  135. var context = canvas.getContext( 'webgl2' );
  136. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  137. renderer.setPixelRatio( window.devicePixelRatio );
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. container = document.createElement( 'div' );
  140. document.body.appendChild( container );
  141. container.appendChild( renderer.domElement );
  142. controls = new THREE.OrthographicTrackballControls( camera, renderer.domElement );
  143. controls.target.set( 64, 64, 128 );
  144. controls.rotateSpeed = 5.0;
  145. controls.zoomSpeed = 5;
  146. controls.panSpeed = 2;
  147. controls.noZoom = false;
  148. controls.noPan = false;
  149. controls.staticMoving = true;
  150. controls.dynamicDampingFactor = 0.3;
  151. stats = new Stats();
  152. container.appendChild( stats.dom );
  153. var gui = new dat.GUI();
  154. setupInset();
  155. window.addEventListener( 'resize', onWindowResize, false );
  156. }
  157. function updateUniforms() {
  158. config = this.volumeConfig;
  159. this.volmaterial.uniforms.u_clim.value = [ config.clim1, config.clim2 ];
  160. this.volmaterial.uniforms.u_renderstyle.value = config.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  161. this.volmaterial.uniforms.u_renderthreshold.value = config.isothreshold; // For ISO renderstyle
  162. this.volmaterial.uniforms.u_cmdata.value = this.cmtextures[config.colormap];
  163. this.volmaterial.needsUpdate = true;
  164. }
  165. function onWindowResize() {
  166. this.camera.right = window.innerWidth / 5.0;
  167. this.camera.left = -this.camera.right;
  168. this.camera.top = window.innerHeight / 5.0;
  169. this.camera.bottom = -this.camera.top;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. controls.handleResize();
  173. }
  174. function animate() {
  175. requestAnimationFrame( animate );
  176. controls.update();
  177. //copy position of the camera into inset
  178. camera2.position.copy( camera.position );
  179. camera2.position.sub( controls.target );
  180. camera2.position.setLength( 300 );
  181. camera2.lookAt( scene2.position );
  182. renderer.render( scene, camera );
  183. renderer2.render( scene2, camera2);
  184. stats.update();
  185. }
  186. function setupInset () {
  187. var insetWidth = 150,
  188. insetHeight = 150;
  189. container2 = document.getElementById( 'inset' );
  190. container2.width = insetWidth;
  191. container2.height = insetHeight;
  192. // renderer
  193. renderer2 = new THREE.WebGLRenderer( { alpha : true } );
  194. renderer2.setClearColor( 0x000000, 0 );
  195. renderer2.setSize( insetWidth, insetHeight );
  196. container2.appendChild( renderer2.domElement );
  197. // scene
  198. scene2 = new THREE.Scene();
  199. // camera
  200. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  201. camera2.up = camera.up; // important!
  202. // axes
  203. axes2 = new THREE.AxesHelper( 100 );
  204. scene2.add( axes2 );
  205. }
  206. </script>
  207. </body>
  208. </html>