2
0

webgl_loader_nrrd.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. let 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. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x000000, 1 );
  56. scene.add( hemiLight );
  57. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  58. dirLight.position.set( 200, 200, 200 );
  59. scene.add( dirLight );
  60. const loader = new NRRDLoader();
  61. loader.load( "models/nrrd/I.nrrd", function ( volume ) {
  62. //box helper to see the extend of the volume
  63. const geometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  64. const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  65. const cube = new THREE.Mesh( geometry, material );
  66. cube.visible = false;
  67. const box = new THREE.BoxHelper( cube );
  68. scene.add( box );
  69. box.applyMatrix4( volume.matrix );
  70. scene.add( cube );
  71. //z plane
  72. const sliceZ = volume.extractSlice( 'z', Math.floor( volume.RASDimensions[ 2 ] / 4 ) );
  73. scene.add( sliceZ.mesh );
  74. //y plane
  75. const sliceY = volume.extractSlice( 'y', Math.floor( volume.RASDimensions[ 1 ] / 2 ) );
  76. scene.add( sliceY.mesh );
  77. //x plane
  78. const sliceX = volume.extractSlice( 'x', Math.floor( volume.RASDimensions[ 0 ] / 2 ) );
  79. scene.add( sliceX.mesh );
  80. gui.add( sliceX, "index", 0, volume.RASDimensions[ 0 ], 1 ).name( "indexX" ).onChange( function () {
  81. sliceX.repaint.call( sliceX );
  82. } );
  83. gui.add( sliceY, "index", 0, volume.RASDimensions[ 1 ], 1 ).name( "indexY" ).onChange( function () {
  84. sliceY.repaint.call( sliceY );
  85. } );
  86. gui.add( sliceZ, "index", 0, volume.RASDimensions[ 2 ], 1 ).name( "indexZ" ).onChange( function () {
  87. sliceZ.repaint.call( sliceZ );
  88. } );
  89. gui.add( volume, "lowerThreshold", volume.min, volume.max, 1 ).name( "Lower Threshold" ).onChange( function () {
  90. volume.repaintAllSlices();
  91. } );
  92. gui.add( volume, "upperThreshold", volume.min, volume.max, 1 ).name( "Upper Threshold" ).onChange( function () {
  93. volume.repaintAllSlices();
  94. } );
  95. gui.add( volume, "windowLow", volume.min, volume.max, 1 ).name( "Window Low" ).onChange( function () {
  96. volume.repaintAllSlices();
  97. } );
  98. gui.add( volume, "windowHigh", volume.min, volume.max, 1 ).name( "Window High" ).onChange( function () {
  99. volume.repaintAllSlices();
  100. } );
  101. } );
  102. const vtkmaterial = new THREE.MeshLambertMaterial( { wireframe: false, morphTargets: false, side: THREE.DoubleSide, color: 0xff0000 } );
  103. const vtkloader = new VTKLoader();
  104. vtkloader.load( "models/vtk/liver.vtk", function ( geometry ) {
  105. geometry.computeVertexNormals();
  106. const mesh = new THREE.Mesh( geometry, vtkmaterial );
  107. scene.add( mesh );
  108. const visibilityControl = {
  109. visible: true
  110. };
  111. gui.add( visibilityControl, "visible" ).name( "Model Visible" ).onChange( function () {
  112. mesh.visible = visibilityControl.visible;
  113. renderer.render( scene, camera );
  114. } );
  115. } );
  116. // renderer
  117. renderer = new THREE.WebGLRenderer();
  118. renderer.setPixelRatio( window.devicePixelRatio );
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. container = document.createElement( 'div' );
  121. document.body.appendChild( container );
  122. container.appendChild( renderer.domElement );
  123. controls = new TrackballControls( camera, renderer.domElement );
  124. controls.minDistance = 100;
  125. controls.maxDistance = 500;
  126. controls.rotateSpeed = 5.0;
  127. controls.zoomSpeed = 5;
  128. controls.panSpeed = 2;
  129. stats = new Stats();
  130. container.appendChild( stats.dom );
  131. const gui = new GUI();
  132. setupInset();
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. controls.handleResize();
  140. }
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. controls.update();
  144. //copy position of the camera into inset
  145. camera2.position.copy( camera.position );
  146. camera2.position.sub( controls.target );
  147. camera2.position.setLength( 300 );
  148. camera2.lookAt( scene2.position );
  149. renderer.render( scene, camera );
  150. renderer2.render( scene2, camera2 );
  151. stats.update();
  152. }
  153. function setupInset() {
  154. const insetWidth = 150, insetHeight = 150;
  155. container2 = document.getElementById( 'inset' );
  156. container2.width = insetWidth;
  157. container2.height = insetHeight;
  158. // renderer
  159. renderer2 = new THREE.WebGLRenderer( { alpha: true } );
  160. renderer2.setClearColor( 0x000000, 0 );
  161. renderer2.setSize( insetWidth, insetHeight );
  162. container2.appendChild( renderer2.domElement );
  163. // scene
  164. scene2 = new THREE.Scene();
  165. // camera
  166. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  167. camera2.up = camera.up; // important!
  168. // axes
  169. axes2 = new THREE.AxesHelper( 100 );
  170. scene2.add( axes2 );
  171. }
  172. </script>
  173. </body>
  174. </html>