webgl_materials_texture3d_volume1.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/TrackballControls.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, THREE.RedFormat, THREE.FloatType);
  100. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  101. texture.unpackAlignment = 1;
  102. texture.needsUpdate = true;
  103. // Colormap textures
  104. this.cmtextures = { viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png' ),
  105. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png' )
  106. }
  107. // Material (shaders) to render the volume using raycasting
  108. var volmaterial = new THREE.ShaderMaterial( THREE.VolumeRenderShader1 );
  109. volmaterial.side = THREE.BackSide; // The volume shader uses the backface as its "reference point"
  110. // Apply standard volume material uniform info
  111. volmaterial.uniforms.u_data = { type: 't', value: texture };
  112. volmaterial.uniforms.u_size = { type: 'v3', value: [volume.xLength, volume.yLength, volume.zLength] };
  113. // Geometry for the volume
  114. var volgeometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  115. volgeometry = volgeometry.translate(volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5);
  116. var volcube = new THREE.Mesh(volgeometry, volmaterial);
  117. scene.add( volcube );
  118. // Support modifying volume rendering settings at runtime
  119. this.volmaterial = volmaterial;
  120. this.volumeConfig = {clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis'};
  121. this.updateUniforms();
  122. gui.add(volumeConfig, 'clim1', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  123. gui.add(volumeConfig, 'clim2', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  124. gui.add(volumeConfig, 'colormap', {gray: 'gray', viridis: 'viridis'}).onChange(this.updateUniforms.bind(this));
  125. gui.add(volumeConfig, 'renderstyle', {mip: 'mip', iso: 'iso'}).onChange(this.updateUniforms.bind(this));
  126. gui.add(volumeConfig, 'isothreshold', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  127. // TODO: the texture coordinates currently map directly to world coordinates. That's why we translate
  128. // the geometry above. But the extractSlice() below assume that the volume is centered at the origin.
  129. // We'd need to add a material attribute with texture coordinates to fix this.
  130. return;
  131. //z plane
  132. var indexZ = 0;
  133. var sliceZ = volume.extractSlice('z',Math.floor(volume.RASDimensions[2]/4));
  134. scene.add( sliceZ.mesh );
  135. //y plane
  136. var indexY = 0;
  137. var sliceY = volume.extractSlice('y',Math.floor(volume.RASDimensions[1]/2));
  138. scene.add( sliceY.mesh );
  139. //x plane
  140. var indexX = 0;
  141. var sliceX = volume.extractSlice('x',Math.floor(volume.RASDimensions[0]/2));
  142. scene.add( sliceX.mesh );
  143. gui.add( sliceX, "index", 0, volume.RASDimensions[0], 1 ).name( "indexX" ).onChange( function () {sliceX.repaint.call(sliceX);} );
  144. gui.add( sliceY, "index", 0, volume.RASDimensions[1], 1 ).name( "indexY" ).onChange( function () {sliceY.repaint.call(sliceY);} );
  145. gui.add( sliceZ, "index", 0, volume.RASDimensions[2], 1 ).name( "indexZ" ).onChange( function () {sliceZ.repaint.call(sliceZ);} );
  146. gui.add( volume, "lowerThreshold", volume.min, volume.max, 1).name( "Lower Threshold").onChange( function () {
  147. volume.repaintAllSlices();
  148. });
  149. gui.add( volume, "upperThreshold", volume.min, volume.max, 1).name( "Upper Threshold").onChange( function () {
  150. volume.repaintAllSlices();
  151. });
  152. gui.add( volume, "windowLow", volume.min, volume.max, 1).name( "Window Low").onChange( function () {
  153. volume.repaintAllSlices();
  154. });
  155. gui.add( volume, "windowHigh", volume.min, volume.max, 1).name( "Window High").onChange( function () {
  156. volume.repaintAllSlices();
  157. });
  158. } );
  159. // renderer
  160. var canvas = document.createElement( 'canvas' );
  161. var context = canvas.getContext( 'webgl2' );
  162. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  163. renderer.setPixelRatio( window.devicePixelRatio );
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. container = document.createElement( 'div' );
  166. document.body.appendChild( container );
  167. container.appendChild( renderer.domElement );
  168. controls = new THREE.TrackballControls( camera, renderer.domElement );
  169. controls.rotateSpeed = 5.0;
  170. controls.zoomSpeed = 5;
  171. controls.panSpeed = 2;
  172. controls.noZoom = false;
  173. controls.noPan = false;
  174. controls.staticMoving = true;
  175. controls.dynamicDampingFactor = 0.3;
  176. stats = new Stats();
  177. container.appendChild( stats.dom );
  178. var gui = new dat.GUI();
  179. setupInset();
  180. window.addEventListener( 'resize', onWindowResize, false );
  181. }
  182. function updateUniforms() {
  183. config = this.volumeConfig;
  184. this.volmaterial.uniforms.u_clim = {type: 'v2', value: [config.clim1, config.clim2]};
  185. this.volmaterial.uniforms.u_renderstyle = {type: 'int', value: config.renderstyle == 'mip' ? 0 : 1}; // 0: MIP, 1: ISO
  186. this.volmaterial.uniforms.u_renderthreshold = {type: 'f', value: config.isothreshold}; // For ISO renderstyle
  187. this.volmaterial.uniforms.u_cmdata = { type: 't', value: this.cmtextures[config.colormap] };
  188. this.volmaterial.needsUpdate = true;
  189. }
  190. function onWindowResize() {
  191. this.camera.right = window.innerWidth / 5.0;
  192. this.camera.left = -this.camera.right;
  193. this.camera.top = window.innerHeight / 5.0;
  194. this.camera.bottom = -this.camera.top;
  195. camera.updateProjectionMatrix();
  196. renderer.setSize( window.innerWidth, window.innerHeight );
  197. controls.handleResize();
  198. }
  199. function animate() {
  200. requestAnimationFrame( animate );
  201. controls.update();
  202. //copy position of the camera into inset
  203. camera2.position.copy( camera.position );
  204. camera2.position.sub( controls.target );
  205. camera2.position.setLength( 300 );
  206. camera2.lookAt( scene2.position );
  207. renderer.render( scene, camera );
  208. renderer2.render( scene2, camera2);
  209. stats.update();
  210. }
  211. function rotateAroundWorldAxis(object, axis, radians) {
  212. var rotWorldMatrix = new THREE.Matrix4();
  213. rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
  214. object.applyMatrix(rotWorldMatrix);
  215. }
  216. function setupInset () {
  217. var insetWidth = 150,
  218. insetHeight = 150;
  219. container2 = document.getElementById('inset');
  220. container2.width = insetWidth;
  221. container2.height = insetHeight;
  222. // renderer
  223. renderer2 = new THREE.WebGLRenderer( {alpha : true} );
  224. renderer2.setClearColor( 0x000000, 0 );
  225. renderer2.setSize( insetWidth, insetHeight );
  226. container2.appendChild( renderer2.domElement );
  227. // scene
  228. scene2 = new THREE.Scene();
  229. // camera
  230. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  231. camera2.up = camera.up; // important!
  232. // axes
  233. axes2 = new THREE.AxesHelper( 100 );
  234. scene2.add( axes2 );
  235. }
  236. </script>
  237. </body>
  238. </html>