webgl_loader_nrrd.html 4.5 KB

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