webgl_materials_texture3d_volume1.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - volume rendering example</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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 5;
  22. display:block;
  23. }
  24. .dg {
  25. z-index: 10 !important;
  26. }
  27. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  28. #inset {
  29. width: 150px;
  30. height: 150px;
  31. background-color: transparent; /* or transparent; will show through only if renderer alpha: true */
  32. border: none; /* or none; */
  33. margin: 0;
  34. padding: 0px;
  35. position: absolute;
  36. left: 20px;
  37. bottom: 20px;
  38. z-index: 100;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div id="info">
  44. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  45. Float volume render test (mip / isosurface)
  46. </div>
  47. <div id="inset"></div>
  48. <script src="../build/three.js"></script>
  49. <script src="js/controls/OrthographicTrackballControls.js"></script>
  50. <script src="js/Volume.js"></script>
  51. <script src="js/VolumeSlice.js"></script>
  52. <script src="js/loaders/NRRDLoader.js"></script>
  53. <script src="js/shaders/VolumeShader.js"></script>
  54. <script src="js/Detector.js"></script>
  55. <script src="js/libs/stats.min.js"></script>
  56. <script src="js/libs/gunzip.min.js"></script>
  57. <script src="js/libs/dat.gui.min.js"></script>
  58. <script>
  59. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  60. var container,
  61. stats,
  62. camera,
  63. controls,
  64. scene,
  65. renderer,
  66. gui,
  67. container2,
  68. renderer2,
  69. camera2,
  70. axes2,
  71. scene2;
  72. init();
  73. animate();
  74. function init() {
  75. // The volume renderer does not work very well with perspective yet
  76. //camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 1e10 );
  77. camera = new THREE.OrthographicCamera()
  78. camera.position.z = 1000;
  79. this.camera.right = window.innerWidth / 5.0;
  80. this.camera.left = -this.camera.right;
  81. this.camera.top = window.innerHeight / 5.0;
  82. this.camera.bottom = -this.camera.top;
  83. this.camera.near = 0.1;
  84. this.camera.far = 5000;
  85. this.camera.updateProjectionMatrix();
  86. scene = new THREE.Scene();
  87. scene.add( camera );
  88. // light
  89. var dirLight = new THREE.DirectionalLight( 0xffffff );
  90. dirLight.position.set( 200, 200, 1000 ).normalize();
  91. camera.add( dirLight );
  92. camera.add( dirLight.target );
  93. var loader = new THREE.NRRDLoader();
  94. loader.load( "models/nrrd/stent.nrrd", function ( volume ) {
  95. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  96. // THREEJS will select R32F (33326) based on the RedFormat and FloatType.
  97. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  98. // TODO: look the dtype up in the volume metadata
  99. var texture = new THREE.Texture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
  100. texture.format = THREE.RedFormat;
  101. texture.type = THREE.FloatType;
  102. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  103. texture.unpackAlignment = 1;
  104. texture.needsUpdate = true;
  105. // Colormap textures
  106. this.cmtextures = { viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png' ),
  107. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png' )
  108. }
  109. // Material (shaders) to render the volume using raycasting
  110. var volmaterial = new THREE.ShaderMaterial( THREE.VolumeRenderShader1 );
  111. volmaterial.side = THREE.BackSide; // The volume shader uses the backface as its "reference point"
  112. // Apply standard volume material uniform info
  113. volmaterial.uniforms.u_data = { type: 't', value: texture };
  114. volmaterial.uniforms.u_size = { type: 'v3', value: [volume.xLength, volume.yLength, volume.zLength] };
  115. // Geometry for the volume
  116. var volgeometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  117. volgeometry = volgeometry.translate(volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5);
  118. var volcube = new THREE.Mesh(volgeometry, volmaterial);
  119. scene.add( volcube );
  120. // Support modifying volume rendering settings at runtime
  121. this.volmaterial = volmaterial;
  122. this.volumeConfig = {clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis'};
  123. this.updateUniforms();
  124. gui.add(volumeConfig, 'clim1', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  125. gui.add(volumeConfig, 'clim2', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  126. gui.add(volumeConfig, 'colormap', {gray: 'gray', viridis: 'viridis'}).onChange(this.updateUniforms.bind(this));
  127. gui.add(volumeConfig, 'renderstyle', {mip: 'mip', iso: 'iso'}).onChange(this.updateUniforms.bind(this));
  128. gui.add(volumeConfig, 'isothreshold', 0, 1, 0.01).onChange(this.updateUniforms.bind(this));
  129. // TODO: the texture coordinates currently map directly to world coordinates. That's why we translate
  130. // the geometry above. But the extractSlice() below assume that the volume is centered at the origin.
  131. // We'd need to add a material attribute with texture coordinates to fix this.
  132. return;
  133. //z plane
  134. var indexZ = 0;
  135. var sliceZ = volume.extractSlice('z',Math.floor(volume.RASDimensions[2]/4));
  136. scene.add( sliceZ.mesh );
  137. //y plane
  138. var indexY = 0;
  139. var sliceY = volume.extractSlice('y',Math.floor(volume.RASDimensions[1]/2));
  140. scene.add( sliceY.mesh );
  141. //x plane
  142. var indexX = 0;
  143. var sliceX = volume.extractSlice('x',Math.floor(volume.RASDimensions[0]/2));
  144. scene.add( sliceX.mesh );
  145. gui.add( sliceX, "index", 0, volume.RASDimensions[0], 1 ).name( "indexX" ).onChange( function () {sliceX.repaint.call(sliceX);} );
  146. gui.add( sliceY, "index", 0, volume.RASDimensions[1], 1 ).name( "indexY" ).onChange( function () {sliceY.repaint.call(sliceY);} );
  147. gui.add( sliceZ, "index", 0, volume.RASDimensions[2], 1 ).name( "indexZ" ).onChange( function () {sliceZ.repaint.call(sliceZ);} );
  148. gui.add( volume, "lowerThreshold", volume.min, volume.max, 1).name( "Lower Threshold").onChange( function () {
  149. volume.repaintAllSlices();
  150. });
  151. gui.add( volume, "upperThreshold", volume.min, volume.max, 1).name( "Upper Threshold").onChange( function () {
  152. volume.repaintAllSlices();
  153. });
  154. gui.add( volume, "windowLow", volume.min, volume.max, 1).name( "Window Low").onChange( function () {
  155. volume.repaintAllSlices();
  156. });
  157. gui.add( volume, "windowHigh", volume.min, volume.max, 1).name( "Window High").onChange( function () {
  158. volume.repaintAllSlices();
  159. });
  160. } );
  161. // renderer
  162. var canvas = document.createElement( 'canvas' );
  163. var context = canvas.getContext( 'webgl2' );
  164. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  165. renderer.setPixelRatio( window.devicePixelRatio );
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. container = document.createElement( 'div' );
  168. document.body.appendChild( container );
  169. container.appendChild( renderer.domElement );
  170. controls = new THREE.OrthographicTrackballControls( camera, renderer.domElement );
  171. controls.target = new THREE.Vector3(64, 64, 128);
  172. controls.rotateSpeed = 5.0;
  173. controls.zoomSpeed = 5;
  174. controls.panSpeed = 2;
  175. controls.noZoom = false;
  176. controls.noPan = false;
  177. controls.staticMoving = true;
  178. controls.dynamicDampingFactor = 0.3;
  179. stats = new Stats();
  180. container.appendChild( stats.dom );
  181. var gui = new dat.GUI();
  182. setupInset();
  183. window.addEventListener( 'resize', onWindowResize, false );
  184. }
  185. function updateUniforms() {
  186. config = this.volumeConfig;
  187. this.volmaterial.uniforms.u_clim = {type: 'v2', value: [config.clim1, config.clim2]};
  188. this.volmaterial.uniforms.u_renderstyle = {type: 'int', value: config.renderstyle == 'mip' ? 0 : 1}; // 0: MIP, 1: ISO
  189. this.volmaterial.uniforms.u_renderthreshold = {type: 'f', value: config.isothreshold}; // For ISO renderstyle
  190. this.volmaterial.uniforms.u_cmdata = { type: 't', value: this.cmtextures[config.colormap] };
  191. this.volmaterial.needsUpdate = true;
  192. }
  193. function onWindowResize() {
  194. this.camera.right = window.innerWidth / 5.0;
  195. this.camera.left = -this.camera.right;
  196. this.camera.top = window.innerHeight / 5.0;
  197. this.camera.bottom = -this.camera.top;
  198. camera.updateProjectionMatrix();
  199. renderer.setSize( window.innerWidth, window.innerHeight );
  200. controls.handleResize();
  201. }
  202. function animate() {
  203. requestAnimationFrame( animate );
  204. controls.update();
  205. //copy position of the camera into inset
  206. camera2.position.copy( camera.position );
  207. camera2.position.sub( controls.target );
  208. camera2.position.setLength( 300 );
  209. camera2.lookAt( scene2.position );
  210. renderer.render( scene, camera );
  211. renderer2.render( scene2, camera2);
  212. stats.update();
  213. }
  214. function rotateAroundWorldAxis(object, axis, radians) {
  215. var rotWorldMatrix = new THREE.Matrix4();
  216. rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
  217. object.applyMatrix(rotWorldMatrix);
  218. }
  219. function setupInset () {
  220. var insetWidth = 150,
  221. insetHeight = 150;
  222. container2 = document.getElementById('inset');
  223. container2.width = insetWidth;
  224. container2.height = insetHeight;
  225. // renderer
  226. renderer2 = new THREE.WebGLRenderer( {alpha : true} );
  227. renderer2.setClearColor( 0x000000, 0 );
  228. renderer2.setSize( insetWidth, insetHeight );
  229. container2.appendChild( renderer2.domElement );
  230. // scene
  231. scene2 = new THREE.Scene();
  232. // camera
  233. camera2 = new THREE.PerspectiveCamera( 50, insetWidth / insetHeight, 1, 1000 );
  234. camera2.up = camera.up; // important!
  235. // axes
  236. axes2 = new THREE.AxesHelper( 100 );
  237. scene2.add( axes2 );
  238. }
  239. </script>
  240. </body>
  241. </html>
粤ICP备19079148号