webgl2_materials_texture3d.html 5.8 KB

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