webgl2_materials_texture3d.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. <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> - Float volume render test (mip / isosurface)
  12. </div>
  13. <div id="inset"></div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { GUI } from './jsm/libs/dat.gui.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { NRRDLoader } from './jsm/loaders/NRRDLoader.js';
  19. import { VolumeRenderShader1 } from './jsm/shaders/VolumeShader.js';
  20. import { WEBGL } from './jsm/WebGL.js';
  21. if ( WEBGL.isWebGL2Available() === false ) {
  22. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  23. }
  24. let renderer,
  25. scene,
  26. camera,
  27. controls,
  28. material,
  29. volconfig,
  30. cmtextures;
  31. init();
  32. function init() {
  33. scene = new THREE.Scene();
  34. // Create renderer
  35. renderer = new THREE.WebGLRenderer();
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. document.body.appendChild( renderer.domElement );
  39. // Create camera (The volume renderer does not work very well with perspective yet)
  40. const h = 512; // frustum height
  41. const aspect = window.innerWidth / window.innerHeight;
  42. camera = new THREE.OrthographicCamera( - h * aspect / 2, h * aspect / 2, h / 2, - h / 2, 1, 1000 );
  43. camera.position.set( 0, 0, 128 );
  44. camera.up.set( 0, 0, 1 ); // In our data, z is up
  45. // Create controls
  46. controls = new OrbitControls( camera, renderer.domElement );
  47. controls.addEventListener( 'change', render );
  48. controls.target.set( 64, 64, 128 );
  49. controls.minZoom = 0.5;
  50. controls.maxZoom = 4;
  51. controls.update();
  52. // scene.add( new AxesHelper( 128 ) );
  53. // Lighting is baked into the shader a.t.m.
  54. // let dirLight = new DirectionalLight( 0xffffff );
  55. // The gui for interaction
  56. volconfig = { clim1: 0, clim2: 1, renderstyle: 'iso', isothreshold: 0.15, colormap: 'viridis' };
  57. const gui = new GUI();
  58. gui.add( volconfig, 'clim1', 0, 1, 0.01 ).onChange( updateUniforms );
  59. gui.add( volconfig, 'clim2', 0, 1, 0.01 ).onChange( updateUniforms );
  60. gui.add( volconfig, 'colormap', { gray: 'gray', viridis: 'viridis' } ).onChange( updateUniforms );
  61. gui.add( volconfig, 'renderstyle', { mip: 'mip', iso: 'iso' } ).onChange( updateUniforms );
  62. gui.add( volconfig, 'isothreshold', 0, 1, 0.01 ).onChange( updateUniforms );
  63. // Load the data ...
  64. new NRRDLoader().load( "models/nrrd/stent.nrrd", function ( volume ) {
  65. // Texture to hold the volume. We have scalars, so we put our data in the red channel.
  66. // THREEJS will select R32F (33326) based on the THREE.RedFormat and THREE.FloatType.
  67. // Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
  68. // TODO: look the dtype up in the volume metadata
  69. const texture = new THREE.DataTexture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
  70. texture.format = THREE.RedFormat;
  71. texture.type = THREE.FloatType;
  72. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  73. texture.unpackAlignment = 1;
  74. // Colormap textures
  75. cmtextures = {
  76. viridis: new THREE.TextureLoader().load( 'textures/cm_viridis.png', render ),
  77. gray: new THREE.TextureLoader().load( 'textures/cm_gray.png', render )
  78. };
  79. // Material
  80. const shader = VolumeRenderShader1;
  81. const uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  82. uniforms[ "u_data" ].value = texture;
  83. uniforms[ "u_size" ].value.set( volume.xLength, volume.yLength, volume.zLength );
  84. uniforms[ "u_clim" ].value.set( volconfig.clim1, volconfig.clim2 );
  85. uniforms[ "u_renderstyle" ].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  86. uniforms[ "u_renderthreshold" ].value = volconfig.isothreshold; // For ISO renderstyle
  87. uniforms[ "u_cmdata" ].value = cmtextures[ volconfig.colormap ];
  88. material = new THREE.ShaderMaterial( {
  89. uniforms: uniforms,
  90. vertexShader: shader.vertexShader,
  91. fragmentShader: shader.fragmentShader,
  92. side: THREE.BackSide // The volume shader uses the backface as its "reference point"
  93. } );
  94. // THREE.Mesh
  95. const geometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
  96. geometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
  97. const mesh = new THREE.Mesh( geometry, material );
  98. scene.add( mesh );
  99. render();
  100. } );
  101. window.addEventListener( 'resize', onWindowResize );
  102. }
  103. function updateUniforms() {
  104. material.uniforms[ "u_clim" ].value.set( volconfig.clim1, volconfig.clim2 );
  105. material.uniforms[ "u_renderstyle" ].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
  106. material.uniforms[ "u_renderthreshold" ].value = volconfig.isothreshold; // For ISO renderstyle
  107. material.uniforms[ "u_cmdata" ].value = cmtextures[ volconfig.colormap ];
  108. render();
  109. }
  110. function onWindowResize() {
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. const aspect = window.innerWidth / window.innerHeight;
  113. const frustumHeight = camera.top - camera.bottom;
  114. camera.left = - frustumHeight * aspect / 2;
  115. camera.right = frustumHeight * aspect / 2;
  116. camera.updateProjectionMatrix();
  117. render();
  118. }
  119. function render() {
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>