webgl2_materials_texture3d.html 5.9 KB

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