webgl2_materials_texture3d_volume.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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> - Float volume render test (mip / isosurface)
  45. </div>
  46. <div id="inset"></div>
  47. <script src="../build/three.js"></script>
  48. <script src="js/controls/OrbitControls.js"></script>
  49. <script src="js/Volume.js"></script>
  50. <script src="js/loaders/NRRDLoader.js"></script>
  51. <script src="js/shaders/VolumeShader.js"></script>
  52. <script src="js/WebGL.js"></script>
  53. <script src="js/libs/gunzip.min.js"></script>
  54. <script src="js/libs/dat.gui.min.js"></script>
  55. <script>
  56. if ( WEBGL.isWebGL2Available() === false ) {
  57. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  58. }
  59. var container,
  60. renderer,
  61. scene,
  62. camera,
  63. controls,
  64. material,
  65. volconfig,
  66. cmtextures;
  67. init();
  68. function init() {
  69. scene = new THREE.Scene();
  70. // Create renderer
  71. var canvas = document.createElement( 'canvas' );
  72. var context = canvas.getContext( 'webgl2' );
  73. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. document.body.appendChild( renderer.domElement );
  77. // Create camera (The volume renderer does not work very well with perspective yet)
  78. var h = 512; // frustum height
  79. var aspect = window.innerWidth / window.innerHeight;
  80. camera = new THREE.OrthographicCamera( - h * aspect / 2, h * aspect / 2, h / 2, - h / 2, 1, 1000 );
  81. camera.position.set( 0, 0, 128 );
  82. camera.up.set( 0, 0, 1 ); // In our data, z is up
  83. // Create controls
  84. controls = new THREE.OrbitControls( camera, renderer.domElement );
  85. controls.addEventListener( 'change', render );
  86. controls.target.set( 64, 64, 128 );
  87. controls.minZoom = 0.5;
  88. controls.maxZoom = 4;
  89. controls.update();
  90. // scene.add( new THREE.AxesHelper( 128 ) );
  91. // Lighting is baked into the shader a.t.m.
  92. // var dirLight = new THREE.DirectionalLight( 0xffffff );
  93. // The gui for interaction
  94. volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' };
  95. var gui = new dat.GUI();
  96. gui.add( volconfig, 'clim1', 0, 1, 0.01 ).onChange( updateUniforms );
  97. gui.add( volconfig, 'clim2', 0, 1, 0.01 ).onChange( updateUniforms );
  98. gui.add( volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( updateUniforms );
  99. gui.add( volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( updateUniforms );
  100. gui.add( volconfig, 'isothreshold', 0, 1, 0.01 ).onChange( updateUniforms );
  101. // Load the data ...
  102. new THREE.NRRDLoader().load( "models/nrrd/stent.nrrd", function ( volume ) {
  103. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  104. // THREEJS will select R32F (33326) based on the RedFormat and FloatType.
  105. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  106. // TODO: look the dtype up in the volume metadata
  107. var texture = new THREE.DataTexture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
  108. texture.format = THREE.RedFormat;
  109. texture.type = THREE.FloatType;
  110. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  111. texture.unpackAlignment = 1;
  112. texture.needsUpdate = true;
  113. // Colormap textures
  114. cmtextures = {
  115. viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png', render ),
  116. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png', render )
  117. };
  118. // Material
  119. var shader = THREE.VolumeRenderShader1;
  120. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  121. uniforms.u_data.value = texture;
  122. uniforms.u_size.value.set( volume.xLength, volume.yLength, volume.zLength );
  123. uniforms.u_clim.value.set( volconfig.clim1, volconfig.clim2 );
  124. uniforms.u_renderstyle.value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  125. uniforms.u_renderthreshold.value = volconfig.isothreshold; // For ISO renderstyle
  126. uniforms.u_cmdata.value = cmtextures[ volconfig.colormap ];
  127. material = new THREE.ShaderMaterial( {
  128. uniforms: uniforms,
  129. vertexShader: shader.vertexShader,
  130. fragmentShader: shader.fragmentShader,
  131. side: THREE.BackSide // The volume shader uses the backface as its "reference point"
  132. } );
  133. // Mesh
  134. var geometry = new THREE.BoxBufferGeometry( volume.xLength, volume.yLength, volume.zLength );
  135. geometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
  136. var mesh = new THREE.Mesh( geometry, material );
  137. scene.add( mesh );
  138. render();
  139. } );
  140. window.addEventListener( 'resize', onWindowResize, false );
  141. }
  142. function updateUniforms() {
  143. material.uniforms.u_clim.value.set( volconfig.clim1, volconfig.clim2 );
  144. material.uniforms.u_renderstyle.value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  145. material.uniforms.u_renderthreshold.value = volconfig.isothreshold; // For ISO renderstyle
  146. material.uniforms.u_cmdata.value = cmtextures[ volconfig.colormap ];
  147. render();
  148. }
  149. function onWindowResize() {
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. var aspect = window.innerWidth / window.innerHeight;
  152. var frustumHeight = camera.top - camera.bottom;
  153. camera.left = - frustumHeight * aspect / 2;
  154. camera.right = frustumHeight * aspect / 2;
  155. camera.updateProjectionMatrix();
  156. render();
  157. }
  158. function render() {
  159. renderer.render( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>