webgl_loader_nrrd.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - NRRD 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #inset {
  10. width: 150px;
  11. height: 150px;
  12. background-color: transparent; /* or transparent; will show through only if renderer alpha: true */
  13. border: none; /* or none; */
  14. margin: 0;
  15. padding: 0px;
  16. position: absolute;
  17. left: 20px;
  18. bottom: 20px;
  19. z-index: 100;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  26. NRRD format loader test
  27. </div>
  28. <div id="inset"></div>
  29. <script type="module">
  30. import * as THREE from '../build/three.module.js';
  31. import Stats from './jsm/libs/stats.module.js';
  32. import { GUI } from './jsm/libs/dat.gui.module.js';
  33. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  34. import { NRRDLoader } from './jsm/loaders/NRRDLoader.js';
  35. import { VTKLoader } from './jsm/loaders/VTKLoader.js';
  36. var container,
  37. stats,
  38. camera,
  39. controls,
  40. scene,
  41. renderer,
  42. container2,
  43. renderer2,
  44. camera2,
  45. axes2,
  46. scene2;
  47. init();
  48. animate();
  49. function init() {
  50. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
  51. camera.position.z = 300;
  52. scene = new THREE.Scene();
  53. scene.add( camera );
  54. // light
  55. var dirLight = new THREE.DirectionalLight( 0xffffff );
  56. dirLight.position.set( 200, 200, 1000 ).normalize();
  57. camera.add( dirLight );
  58. camera.add( dirLight.target );
  59. var loader = new NRRDLoader();
  60. loader.load( "models/nrrd/I.nrrd", function ( volume ) {
  61. var geometry,
  62. material,
  63. sliceZ,
  64. sliceY,
  65. sliceX;
  66. //box helper to see the extend of the volume
  67. var geometry = new THREE.BoxBufferGeometry( volume.xLength, volume.yLength, volume.zLength );
  68. var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  69. var cube = new THREE.Mesh( geometry, material );
  70. cube.visible = false;
  71. var box = new THREE.BoxHelper( cube );
  72. scene.add( box );
  73. box.applyMatrix4( volume.matrix );
  74. scene.add( cube );
  75. //z plane
  76. sliceZ = volume.extractSlice( 'z', Math.floor( volume.RASDimensions[ 2 ] / 4 ) );
  77. scene.add( sliceZ.mesh );
  78. //y plane
  79. sliceY = volume.extractSlice( 'y', Math.floor( volume.RASDimensions[ 1 ] / 2 ) );
  80. scene.add( sliceY.mesh );
  81. //x plane
  82. sliceX = volume.extractSlice( 'x', Math.floor( volume.RASDimensions[ 0 ] / 2 ) );
  83. scene.add( sliceX.mesh );
  84. gui.add( sliceX, "index", 0, volume.RASDimensions[ 0 ], 1 ).name( "indexX" ).onChange( function () {
  85. sliceX.repaint.call( sliceX );
  86. } );
  87. gui.add( sliceY, "index", 0, volume.RASDimensions[ 1 ], 1 ).name( "indexY" ).onChange( function () {
  88. sliceY.repaint.call( sliceY );
  89. } );
  90. gui.add( sliceZ, "index", 0, volume.RASDimensions[ 2 ], 1 ).name( "indexZ" ).onChange( function () {
  91. sliceZ.repaint.call( sliceZ );
  92. } );
  93. gui.add( volume, "lowerThreshold", volume.min, volume.max, 1 ).name( "Lower Threshold" ).onChange( function () {
  94. volume.repaintAllSlices();
  95. } );
  96. gui.add( volume, "upperThreshold", volume.min, volume.max, 1 ).name( "Upper Threshold" ).onChange( function () {
  97. volume.repaintAllSlices();
  98. } );
  99. gui.add( volume, "windowLow", volume.min, volume.max, 1 ).name( "Window Low" ).onChange( function () {
  100. volume.repaintAllSlices();
  101. } );
  102. gui.add( volume, "windowHigh", volume.min, volume.max, 1 ).name( "Window High" ).onChange( function () {
  103. volume.repaintAllSlices();
  104. } );
  105. } );
  106. var vtkmaterial = new THREE.MeshLambertMaterial( { wireframe: false, morphTargets: false, side: THREE.DoubleSide, color: 0xff0000 } );
  107. var vtkloader = new VTKLoader();
  108. vtkloader.load( "models/vtk/liver.vtk", function ( geometry ) {
  109. geometry.computeVertexNormals();
  110. var mesh = new THREE.Mesh( geometry, vtkmaterial );
  111. scene.add( mesh );
  112. var visibilityControl = {
  113. visible: true
  114. };
  115. gui.add( visibilityControl, "visible" ).name( "Model Visible" ).onChange( function () {
  116. mesh.visible = visibilityControl.visible;
  117. renderer.render( scene, camera );
  118. } );
  119. } );
  120. // renderer
  121. renderer = new THREE.WebGLRenderer( { alpha: true } );
  122. renderer.setPixelRatio( window.devicePixelRatio );
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. container = document.createElement( 'div' );
  125. document.body.appendChild( container );
  126. container.appendChild( renderer.domElement );
  127. controls = new TrackballControls( camera, renderer.domElement );
  128. controls.rotateSpeed = 5.0;
  129. controls.zoomSpeed = 5;
  130. controls.panSpeed = 2;
  131. controls.staticMoving = true;
  132. stats = new Stats();
  133. container.appendChild( stats.dom );
  134. var gui = new GUI();
  135. setupInset();
  136. window.addEventListener( 'resize', onWindowResize, false );
  137. }
  138. function onWindowResize() {
  139. camera.aspect = window.innerWidth / window.innerHeight;
  140. camera.updateProjectionMatrix();
  141. renderer.setSize( window.innerWidth, window.innerHeight );
  142. controls.handleResize();
  143. }
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. controls.update();
  147. //copy position of the camera into inset
  148. camera2.position.copy( camera.position );
  149. camera2.position.sub( controls.target );
  150. camera2.position.setLength( 300 );
  151. camera2.lookAt( scene2.position );
  152. renderer.render( scene, camera );
  153. renderer2.render( scene2, camera2 );
  154. stats.update();
  155. }
  156. function setupInset() {
  157. var insetWidth = 150,
  158. insetHeight = 150;
  159. container2 = document.getElementById( 'inset' );
  160. container2.width = insetWidth;
  161. container2.height = insetHeight;
  162. // renderer
  163. renderer2 = new THREE.WebGLRenderer( { alpha: true } );
  164. renderer2.setClearColor( 0x000000, 0 );
  165. renderer2.setSize( insetWidth, insetHeight );
  166. container2.appendChild( renderer2.domElement );
  167. // scene
  168. scene2 = new THREE.Scene();
  169. // camera
  170. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  171. camera2.up = camera.up; // important!
  172. // axes
  173. axes2 = new THREE.AxesHelper( 100 );
  174. scene2.add( axes2 );
  175. }
  176. </script>
  177. </body>
  178. </html>