webgl_loader_nrrd.html 5.5 KB

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