webgl2_materials_texture3d.html 6.0 KB

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