webgl_materials_texture3d_volume1.html 8.7 KB

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