webgl_loader_nrrd.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  12. NRRD format loader test
  13. </div>
  14. <div id="inset"></div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  28. import { NRRDLoader } from 'three/addons/loaders/NRRDLoader.js';
  29. import { VTKLoader } from 'three/addons/loaders/VTKLoader.js';
  30. let stats,
  31. camera,
  32. controls,
  33. scene,
  34. renderer;
  35. init();
  36. function init() {
  37. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
  38. camera.position.z = 300;
  39. scene = new THREE.Scene();
  40. scene.add( camera );
  41. // light
  42. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x000000, 3 );
  43. scene.add( hemiLight );
  44. const dirLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
  45. dirLight.position.set( 200, 200, 200 );
  46. scene.add( dirLight );
  47. const loader = new NRRDLoader();
  48. loader.load( 'models/nrrd/I.nrrd', function ( volume ) {
  49. //box helper to see the extend of the volume
  50. const geometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  51. const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  52. const cube = new THREE.Mesh( geometry, material );
  53. cube.visible = false;
  54. const box = new THREE.BoxHelper( cube );
  55. scene.add( box );
  56. box.applyMatrix4( volume.matrix );
  57. scene.add( cube );
  58. //z plane
  59. const sliceZ = volume.extractSlice( 'z', Math.floor( volume.RASDimensions[ 2 ] / 4 ) );
  60. scene.add( sliceZ.mesh );
  61. //y plane
  62. const sliceY = volume.extractSlice( 'y', Math.floor( volume.RASDimensions[ 1 ] / 2 ) );
  63. scene.add( sliceY.mesh );
  64. //x plane
  65. const sliceX = volume.extractSlice( 'x', Math.floor( volume.RASDimensions[ 0 ] / 2 ) );
  66. scene.add( sliceX.mesh );
  67. gui.add( sliceX, 'index', 0, volume.RASDimensions[ 0 ], 1 ).name( 'indexX' ).onChange( function () {
  68. sliceX.repaint.call( sliceX );
  69. } );
  70. gui.add( sliceY, 'index', 0, volume.RASDimensions[ 1 ], 1 ).name( 'indexY' ).onChange( function () {
  71. sliceY.repaint.call( sliceY );
  72. } );
  73. gui.add( sliceZ, 'index', 0, volume.RASDimensions[ 2 ], 1 ).name( 'indexZ' ).onChange( function () {
  74. sliceZ.repaint.call( sliceZ );
  75. } );
  76. gui.add( volume, 'lowerThreshold', volume.min, volume.max, 1 ).name( 'Lower Threshold' ).onChange( function () {
  77. volume.repaintAllSlices();
  78. } );
  79. gui.add( volume, 'upperThreshold', volume.min, volume.max, 1 ).name( 'Upper Threshold' ).onChange( function () {
  80. volume.repaintAllSlices();
  81. } );
  82. gui.add( volume, 'windowLow', volume.min, volume.max, 1 ).name( 'Window Low' ).onChange( function () {
  83. volume.repaintAllSlices();
  84. } );
  85. gui.add( volume, 'windowHigh', volume.min, volume.max, 1 ).name( 'Window High' ).onChange( function () {
  86. volume.repaintAllSlices();
  87. } );
  88. } );
  89. const vtkmaterial = new THREE.MeshLambertMaterial( { wireframe: false, side: THREE.DoubleSide, color: 0xff0000 } );
  90. const vtkloader = new VTKLoader();
  91. vtkloader.load( 'models/vtk/liver.vtk', function ( geometry ) {
  92. geometry.computeVertexNormals();
  93. const mesh = new THREE.Mesh( geometry, vtkmaterial );
  94. scene.add( mesh );
  95. const visibilityControl = {
  96. visible: true
  97. };
  98. gui.add( visibilityControl, 'visible' ).name( 'Model Visible' ).onChange( function () {
  99. mesh.visible = visibilityControl.visible;
  100. renderer.render( scene, camera );
  101. } );
  102. } );
  103. // renderer
  104. renderer = new THREE.WebGLRenderer( { antialias: true } );
  105. renderer.setPixelRatio( window.devicePixelRatio );
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. renderer.setAnimationLoop( animate );
  108. document.body.appendChild( renderer.domElement );
  109. controls = new TrackballControls( camera, renderer.domElement );
  110. controls.minDistance = 100;
  111. controls.maxDistance = 500;
  112. controls.rotateSpeed = 5.0;
  113. controls.zoomSpeed = 5;
  114. controls.panSpeed = 2;
  115. stats = new Stats();
  116. document.body.appendChild( stats.dom );
  117. const gui = new GUI();
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. controls.handleResize();
  125. }
  126. function animate() {
  127. controls.update();
  128. renderer.render( scene, camera );
  129. stats.update();
  130. }
  131. </script>
  132. </body>
  133. </html>