webgl_loader_nrrd.html 5.1 KB

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