2
0

webgl_loader_nrrd.html 5.4 KB

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