webgl_materials_texture3d_volume1.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - vtk loader</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. window.volume = volume;
  96. if (false) {
  97. // Box helper to see the extend of the volume
  98. var geometry = new THREE.BoxBufferGeometry( volume.xLength, volume.yLength, volume.zLength );
  99. geometry = geometry.translate(+volume.xLength / 2, -volume.yLength / 2, -volume.zLength / 2);
  100. var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  101. var cube = new THREE.Mesh( geometry, material );
  102. cube.visible = false;
  103. var box = new THREE.BoxHelper( cube );
  104. scene.add( box );
  105. box.applyMatrix(volume.matrix);
  106. scene.add( cube );
  107. }
  108. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  109. // THREEJS will select R32F (33326) based on the RedFormat and FloatType.
  110. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  111. // TODO: look the dtype up in the volume metadata
  112. var texture = new THREE.Texture3D(volume.data, volume.xLength, volume.yLength, volume.zLength, THREE.RedFormat, THREE.FloatType);
  113. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  114. texture.unpackAlignment = 1;
  115. texture.needsUpdate = true;
  116. // Colormap texture
  117. var cmtexture = new THREE.TextureLoader().load( 'textures/cm_viridis.png' ); // e.g. cm_gray or cm_viridis
  118. // Material (shaders) to render the volume using raycasting
  119. var volmaterial = new THREE.ShaderMaterial( THREE.ShaderLib['volumerender1'] );
  120. volmaterial.side = THREE.BackSide; // The volume shader uses the backface as its "reference point"
  121. // Apply volume material uniform info
  122. volmaterial.uniforms.u_data = { type: 't', value: texture };
  123. volmaterial.uniforms.u_cmdata = { type: 't', value: cmtexture };
  124. volmaterial.uniforms.u_size = { type: 'v3', value: [volume.xLength, volume.yLength, volume.zLength] };
  125. volmaterial.uniforms.u_renderstyle = {type: 'int', value: 0}; // 0: MIP, 1: ISO
  126. volmaterial.uniforms.u_renderthreshold = {type: 'f', value: 0.3}; // For ISO renderstyle
  127. volmaterial.uniforms.u_clim = {type: 'v2', value: [0, 1]};
  128. volmaterial.needsUpdate = true;
  129. // Geometry for the volume
  130. var volgeometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  131. volgeometry = volgeometry.translate(volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5);
  132. var volcube = new THREE.Mesh(volgeometry, volmaterial);
  133. scene.add( volcube );
  134. // TODO: the texture coordinates currently map directly to world coordinates. That's why we translate
  135. // the geometry above. But the extractSlice() below assume that the volume is centered at the origin.
  136. // We need to add a material attribute with texture coordinates to fix this.
  137. return;
  138. //z plane
  139. var indexZ = 0;
  140. var sliceZ = volume.extractSlice('z',Math.floor(volume.RASDimensions[2]/4));
  141. scene.add( sliceZ.mesh );
  142. //y plane
  143. var indexY = 0;
  144. var sliceY = volume.extractSlice('y',Math.floor(volume.RASDimensions[1]/2));
  145. scene.add( sliceY.mesh );
  146. //x plane
  147. var indexX = 0;
  148. var sliceX = volume.extractSlice('x',Math.floor(volume.RASDimensions[0]/2));
  149. scene.add( sliceX.mesh );
  150. gui.add( sliceX, "index", 0, volume.RASDimensions[0], 1 ).name( "indexX" ).onChange( function () {sliceX.repaint.call(sliceX);} );
  151. gui.add( sliceY, "index", 0, volume.RASDimensions[1], 1 ).name( "indexY" ).onChange( function () {sliceY.repaint.call(sliceY);} );
  152. gui.add( sliceZ, "index", 0, volume.RASDimensions[2], 1 ).name( "indexZ" ).onChange( function () {sliceZ.repaint.call(sliceZ);} );
  153. gui.add( volume, "lowerThreshold", volume.min, volume.max, 1).name( "Lower Threshold").onChange( function () {
  154. volume.repaintAllSlices();
  155. });
  156. gui.add( volume, "upperThreshold", volume.min, volume.max, 1).name( "Upper Threshold").onChange( function () {
  157. volume.repaintAllSlices();
  158. });
  159. gui.add( volume, "windowLow", volume.min, volume.max, 1).name( "Window Low").onChange( function () {
  160. volume.repaintAllSlices();
  161. });
  162. gui.add( volume, "windowHigh", volume.min, volume.max, 1).name( "Window High").onChange( function () {
  163. volume.repaintAllSlices();
  164. });
  165. } );
  166. // renderer
  167. var canvas = document.createElement( 'canvas' );
  168. var context = canvas.getContext( 'webgl2' );
  169. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  170. renderer.setPixelRatio( window.devicePixelRatio );
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. container = document.createElement( 'div' );
  173. document.body.appendChild( container );
  174. container.appendChild( renderer.domElement );
  175. controls = new THREE.TrackballControls( camera, renderer.domElement );
  176. controls.rotateSpeed = 5.0;
  177. controls.zoomSpeed = 5;
  178. controls.panSpeed = 2;
  179. controls.noZoom = false;
  180. controls.noPan = false;
  181. controls.staticMoving = true;
  182. controls.dynamicDampingFactor = 0.3;
  183. stats = new Stats();
  184. container.appendChild( stats.dom );
  185. var gui = new dat.GUI();
  186. setupInset();
  187. window.addEventListener( 'resize', onWindowResize, false );
  188. }
  189. function onWindowResize() {
  190. camera.aspect = window.innerWidth / window.innerHeight;
  191. camera.updateProjectionMatrix();
  192. renderer.setSize( window.innerWidth, window.innerHeight );
  193. controls.handleResize();
  194. }
  195. function animate() {
  196. requestAnimationFrame( animate );
  197. controls.update();
  198. //copy position of the camera into inset
  199. camera2.position.copy( camera.position );
  200. camera2.position.sub( controls.target );
  201. camera2.position.setLength( 300 );
  202. camera2.lookAt( scene2.position );
  203. renderer.render( scene, camera );
  204. renderer2.render( scene2, camera2);
  205. stats.update();
  206. }
  207. function rotateAroundWorldAxis(object, axis, radians) {
  208. var rotWorldMatrix = new THREE.Matrix4();
  209. rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
  210. object.applyMatrix(rotWorldMatrix);
  211. }
  212. function setupInset () {
  213. var insetWidth = 150,
  214. insetHeight = 150;
  215. container2 = document.getElementById('inset');
  216. container2.width = insetWidth;
  217. container2.height = insetHeight;
  218. // renderer
  219. renderer2 = new THREE.WebGLRenderer({alpha : true});
  220. renderer2.setClearColor( 0x000000, 0 );
  221. renderer2.setSize( insetWidth, insetHeight );
  222. container2.appendChild( renderer2.domElement );
  223. // scene
  224. scene2 = new THREE.Scene();
  225. // camera
  226. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  227. camera2.up = camera.up; // important!
  228. // axes
  229. axes2 = new THREE.AxesHelper( 100 );
  230. scene2.add( axes2 );
  231. }
  232. </script>
  233. </body>
  234. </html>