webgl_texture3d.html 5.7 KB

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