VolumeShader.js 9.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ( function () {
  2. /**
  3. * Shaders to render 3D volumes using raycasting.
  4. * The applied techniques are based on similar implementations in the Visvis and Vispy projects.
  5. * This is not the only approach, therefore it's marked 1.
  6. */
  7. var VolumeRenderShader1 = {
  8. uniforms: {
  9. 'u_size': {
  10. value: new THREE.Vector3( 1, 1, 1 )
  11. },
  12. 'u_renderstyle': {
  13. value: 0
  14. },
  15. 'u_renderthreshold': {
  16. value: 0.5
  17. },
  18. 'u_clim': {
  19. value: new THREE.Vector2( 1, 1 )
  20. },
  21. 'u_data': {
  22. value: null
  23. },
  24. 'u_cmdata': {
  25. value: null
  26. }
  27. },
  28. vertexShader: [ ' varying vec4 v_nearpos;', ' varying vec4 v_farpos;', ' varying vec3 v_position;', ' void main() {', // Prepare transforms to map to "camera view". See also:
  29. // https://threejs.org/docs/#api/renderers/webgl/WebGLProgram
  30. ' mat4 viewtransformf = modelViewMatrix;', ' mat4 viewtransformi = inverse(modelViewMatrix);', // Project local vertex coordinate to camera position. Then do a step
  31. // backward (in cam coords) to the near clipping plane, and project back. Do
  32. // the same for the far clipping plane. This gives us all the information we
  33. // need to calculate the ray and truncate it to the viewing cone.
  34. ' vec4 position4 = vec4(position, 1.0);', ' vec4 pos_in_cam = viewtransformf * position4;', // Intersection of ray and near clipping plane (z = -1 in clip coords)
  35. ' pos_in_cam.z = -pos_in_cam.w;', ' v_nearpos = viewtransformi * pos_in_cam;', // Intersection of ray and far clipping plane (z = +1 in clip coords)
  36. ' pos_in_cam.z = pos_in_cam.w;', ' v_farpos = viewtransformi * pos_in_cam;', // Set varyings and output pos
  37. ' v_position = position;', ' gl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;', ' }' ].join( '\n' ),
  38. fragmentShader: [ ' precision highp float;', ' precision mediump sampler3D;', ' uniform vec3 u_size;', ' uniform int u_renderstyle;', ' uniform float u_renderthreshold;', ' uniform vec2 u_clim;', ' uniform sampler3D u_data;', ' uniform sampler2D u_cmdata;', ' varying vec3 v_position;', ' varying vec4 v_nearpos;', ' varying vec4 v_farpos;', // The maximum distance through our rendering volume is sqrt(3).
  39. ' const int MAX_STEPS = 887; // 887 for 512^3, 1774 for 1024^3', ' const int REFINEMENT_STEPS = 4;', ' const float relative_step_size = 1.0;', ' const vec4 ambient_color = vec4(0.2, 0.4, 0.2, 1.0);', ' const vec4 diffuse_color = vec4(0.8, 0.2, 0.2, 1.0);', ' const vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);', ' const float shininess = 40.0;', ' void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);', ' void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);', ' float sample1(vec3 texcoords);', ' vec4 apply_colormap(float val);', ' vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray);', ' void main() {', // Normalize clipping plane info
  40. ' vec3 farpos = v_farpos.xyz / v_farpos.w;', ' vec3 nearpos = v_nearpos.xyz / v_nearpos.w;', // Calculate unit vector pointing in the view direction through this fragment.
  41. ' vec3 view_ray = normalize(nearpos.xyz - farpos.xyz);', // Compute the (negative) distance to the front surface or near clipping plane.
  42. // v_position is the back face of the cuboid, so the initial distance calculated in the dot
  43. // product below is the distance from near clip plane to the back of the cuboid
  44. ' float distance = dot(nearpos - v_position, view_ray);', ' distance = max(distance, min((-0.5 - v_position.x) / view_ray.x,', ' (u_size.x - 0.5 - v_position.x) / view_ray.x));', ' distance = max(distance, min((-0.5 - v_position.y) / view_ray.y,', ' (u_size.y - 0.5 - v_position.y) / view_ray.y));', ' distance = max(distance, min((-0.5 - v_position.z) / view_ray.z,', ' (u_size.z - 0.5 - v_position.z) / view_ray.z));', // Now we have the starting position on the front surface
  45. ' vec3 front = v_position + view_ray * distance;', // Decide how many steps to take
  46. ' int nsteps = int(-distance / relative_step_size + 0.5);', ' if ( nsteps < 1 )', ' discard;', // Get starting location and step vector in texture coordinates
  47. ' vec3 step = ((v_position - front) / u_size) / float(nsteps);', ' vec3 start_loc = front / u_size;', // For testing: show the number of steps. This helps to establish
  48. // whether the rays are correctly oriented
  49. //'gl_FragColor = vec4(0.0, float(nsteps) / 1.0 / u_size.x, 1.0, 1.0);',
  50. //'return;',
  51. ' if (u_renderstyle == 0)', ' cast_mip(start_loc, step, nsteps, view_ray);', ' else if (u_renderstyle == 1)', ' cast_iso(start_loc, step, nsteps, view_ray);', ' if (gl_FragColor.a < 0.05)', ' discard;', ' }', ' float sample1(vec3 texcoords) {', ' /* Sample float value from a 3D texture. Assumes intensity data. */', ' return texture(u_data, texcoords.xyz).r;', ' }', ' vec4 apply_colormap(float val) {', ' val = (val - u_clim[0]) / (u_clim[1] - u_clim[0]);', ' return texture2D(u_cmdata, vec2(val, 0.5));', ' }', ' void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {', ' float max_val = -1e6;', ' int max_i = 100;', ' vec3 loc = start_loc;', // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  52. // non-constant expression. So we use a hard-coded max, and an additional condition
  53. // inside the loop.
  54. ' for (int iter=0; iter<MAX_STEPS; iter++) {', ' if (iter >= nsteps)', ' break;', // Sample from the 3D texture
  55. ' float val = sample1(loc);', // Apply MIP operation
  56. ' if (val > max_val) {', ' max_val = val;', ' max_i = iter;', ' }', // Advance location deeper into the volume
  57. ' loc += step;', ' }', // Refine location, gives crispier images
  58. ' vec3 iloc = start_loc + step * (float(max_i) - 0.5);', ' vec3 istep = step / float(REFINEMENT_STEPS);', ' for (int i=0; i<REFINEMENT_STEPS; i++) {', ' max_val = max(max_val, sample1(iloc));', ' iloc += istep;', ' }', // Resolve final color
  59. ' gl_FragColor = apply_colormap(max_val);', ' }', ' void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {', ' gl_FragColor = vec4(0.0); // init transparent', ' vec4 color3 = vec4(0.0); // final color', ' vec3 dstep = 1.5 / u_size; // step to sample derivative', ' vec3 loc = start_loc;', ' float low_threshold = u_renderthreshold - 0.02 * (u_clim[1] - u_clim[0]);', // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  60. // non-constant expression. So we use a hard-coded max, and an additional condition
  61. // inside the loop.
  62. ' for (int iter=0; iter<MAX_STEPS; iter++) {', ' if (iter >= nsteps)', ' break;', // Sample from the 3D texture
  63. ' float val = sample1(loc);', ' if (val > low_threshold) {', // Take the last interval in smaller steps
  64. ' vec3 iloc = loc - 0.5 * step;', ' vec3 istep = step / float(REFINEMENT_STEPS);', ' for (int i=0; i<REFINEMENT_STEPS; i++) {', ' val = sample1(iloc);', ' if (val > u_renderthreshold) {', ' gl_FragColor = add_lighting(val, iloc, dstep, view_ray);', ' return;', ' }', ' iloc += istep;', ' }', ' }', // Advance location deeper into the volume
  65. ' loc += step;', ' }', ' }', ' vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray)', ' {', // Calculate color by incorporating lighting
  66. // View direction
  67. ' vec3 V = normalize(view_ray);', // calculate normal vector from gradient
  68. ' vec3 N;', ' float val1, val2;', ' val1 = sample1(loc + vec3(-step[0], 0.0, 0.0));', ' val2 = sample1(loc + vec3(+step[0], 0.0, 0.0));', ' N[0] = val1 - val2;', ' val = max(max(val1, val2), val);', ' val1 = sample1(loc + vec3(0.0, -step[1], 0.0));', ' val2 = sample1(loc + vec3(0.0, +step[1], 0.0));', ' N[1] = val1 - val2;', ' val = max(max(val1, val2), val);', ' val1 = sample1(loc + vec3(0.0, 0.0, -step[2]));', ' val2 = sample1(loc + vec3(0.0, 0.0, +step[2]));', ' N[2] = val1 - val2;', ' val = max(max(val1, val2), val);', ' float gm = length(N); // gradient magnitude', ' N = normalize(N);', // Flip normal so it points towards viewer
  69. ' float Nselect = float(dot(N, V) > 0.0);', ' N = (2.0 * Nselect - 1.0) * N; // == Nselect * N - (1.0-Nselect)*N;', // Init colors
  70. ' vec4 ambient_color = vec4(0.0, 0.0, 0.0, 0.0);', ' vec4 diffuse_color = vec4(0.0, 0.0, 0.0, 0.0);', ' vec4 specular_color = vec4(0.0, 0.0, 0.0, 0.0);', // note: could allow multiple lights
  71. ' for (int i=0; i<1; i++)', ' {', // Get light direction (make sure to prevent zero devision)
  72. ' vec3 L = normalize(view_ray); //lightDirs[i];', ' float lightEnabled = float( length(L) > 0.0 );', ' L = normalize(L + (1.0 - lightEnabled));', // Calculate lighting properties
  73. ' float lambertTerm = clamp(dot(N, L), 0.0, 1.0);', ' vec3 H = normalize(L+V); // Halfway vector', ' float specularTerm = pow(max(dot(H, N), 0.0), shininess);', // Calculate mask
  74. ' float mask1 = lightEnabled;', // Calculate colors
  75. ' ambient_color += mask1 * ambient_color; // * gl_LightSource[i].ambient;', ' diffuse_color += mask1 * lambertTerm;', ' specular_color += mask1 * specularTerm * specular_color;', ' }', // Calculate final color by componing different components
  76. ' vec4 final_color;', ' vec4 color = apply_colormap(val);', ' final_color = color * (ambient_color + diffuse_color) + specular_color;', ' final_color.a = color.a;', ' return final_color;', ' }' ].join( '\n' )
  77. };
  78. THREE.VolumeRenderShader1 = VolumeRenderShader1;
  79. } )();