webgl_loader_nrrd.html 5.6 KB

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