VolumeShader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. console.warn( "THREE.VolumeShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  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. THREE.VolumeRenderShader1 = {
  8. uniforms: {
  9. "u_size": { value: new THREE.Vector3( 1, 1, 1 ) },
  10. "u_renderstyle": { value: 0 },
  11. "u_renderthreshold": { value: 0.5 },
  12. "u_clim": { value: new THREE.Vector2( 1, 1 ) },
  13. "u_data": { value: null },
  14. "u_cmdata": { value: null }
  15. },
  16. vertexShader: [
  17. " varying vec4 v_nearpos;",
  18. " varying vec4 v_farpos;",
  19. " varying vec3 v_position;",
  20. " void main() {",
  21. // Prepare transforms to map to "camera view". See also:
  22. // https://threejs.org/docs/#api/renderers/webgl/WebGLProgram
  23. " mat4 viewtransformf = modelViewMatrix;",
  24. " mat4 viewtransformi = inverse(modelViewMatrix);",
  25. // Project local vertex coordinate to camera position. Then do a step
  26. // backward (in cam coords) to the near clipping plane, and project back. Do
  27. // the same for the far clipping plane. This gives us all the information we
  28. // need to calculate the ray and truncate it to the viewing cone.
  29. " vec4 position4 = vec4(position, 1.0);",
  30. " vec4 pos_in_cam = viewtransformf * position4;",
  31. // Intersection of ray and near clipping plane (z = -1 in clip coords)
  32. " pos_in_cam.z = -pos_in_cam.w;",
  33. " v_nearpos = viewtransformi * pos_in_cam;",
  34. // Intersection of ray and far clipping plane (z = +1 in clip coords)
  35. " pos_in_cam.z = pos_in_cam.w;",
  36. " v_farpos = viewtransformi * pos_in_cam;",
  37. // Set varyings and output pos
  38. " v_position = position;",
  39. " gl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;",
  40. " }",
  41. ].join( "\n" ),
  42. fragmentShader: [
  43. " precision highp float;",
  44. " precision mediump sampler3D;",
  45. " uniform vec3 u_size;",
  46. " uniform int u_renderstyle;",
  47. " uniform float u_renderthreshold;",
  48. " uniform vec2 u_clim;",
  49. " uniform sampler3D u_data;",
  50. " uniform sampler2D u_cmdata;",
  51. " varying vec3 v_position;",
  52. " varying vec4 v_nearpos;",
  53. " varying vec4 v_farpos;",
  54. // The maximum distance through our rendering volume is sqrt(3).
  55. " const int MAX_STEPS = 887; // 887 for 512^3, 1774 for 1024^3",
  56. " const int REFINEMENT_STEPS = 4;",
  57. " const float relative_step_size = 1.0;",
  58. " const vec4 ambient_color = vec4(0.2, 0.4, 0.2, 1.0);",
  59. " const vec4 diffuse_color = vec4(0.8, 0.2, 0.2, 1.0);",
  60. " const vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);",
  61. " const float shininess = 40.0;",
  62. " void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);",
  63. " void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);",
  64. " float sample1(vec3 texcoords);",
  65. " vec4 apply_colormap(float val);",
  66. " vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray);",
  67. " void main() {",
  68. // Normalize clipping plane info
  69. " vec3 farpos = v_farpos.xyz / v_farpos.w;",
  70. " vec3 nearpos = v_nearpos.xyz / v_nearpos.w;",
  71. // Calculate unit vector pointing in the view direction through this fragment.
  72. " vec3 view_ray = normalize(nearpos.xyz - farpos.xyz);",
  73. // Compute the (negative) distance to the front surface or near clipping plane.
  74. // v_position is the back face of the cuboid, so the initial distance calculated in the dot
  75. // product below is the distance from near clip plane to the back of the cuboid
  76. " float distance = dot(nearpos - v_position, view_ray);",
  77. " distance = max(distance, min((-0.5 - v_position.x) / view_ray.x,",
  78. " (u_size.x - 0.5 - v_position.x) / view_ray.x));",
  79. " distance = max(distance, min((-0.5 - v_position.y) / view_ray.y,",
  80. " (u_size.y - 0.5 - v_position.y) / view_ray.y));",
  81. " distance = max(distance, min((-0.5 - v_position.z) / view_ray.z,",
  82. " (u_size.z - 0.5 - v_position.z) / view_ray.z));",
  83. // Now we have the starting position on the front surface
  84. " vec3 front = v_position + view_ray * distance;",
  85. // Decide how many steps to take
  86. " int nsteps = int(-distance / relative_step_size + 0.5);",
  87. " if ( nsteps < 1 )",
  88. " discard;",
  89. // Get starting location and step vector in texture coordinates
  90. " vec3 step = ((v_position - front) / u_size) / float(nsteps);",
  91. " vec3 start_loc = front / u_size;",
  92. // For testing: show the number of steps. This helps to establish
  93. // whether the rays are correctly oriented
  94. //'gl_FragColor = vec4(0.0, float(nsteps) / 1.0 / u_size.x, 1.0, 1.0);',
  95. //'return;',
  96. " if (u_renderstyle == 0)",
  97. " cast_mip(start_loc, step, nsteps, view_ray);",
  98. " else if (u_renderstyle == 1)",
  99. " cast_iso(start_loc, step, nsteps, view_ray);",
  100. " if (gl_FragColor.a < 0.05)",
  101. " discard;",
  102. " }",
  103. " float sample1(vec3 texcoords) {",
  104. " /* Sample float value from a 3D texture. Assumes intensity data. */",
  105. " return texture(u_data, texcoords.xyz).r;",
  106. " }",
  107. " vec4 apply_colormap(float val) {",
  108. " val = (val - u_clim[0]) / (u_clim[1] - u_clim[0]);",
  109. " return texture2D(u_cmdata, vec2(val, 0.5));",
  110. " }",
  111. " void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {",
  112. " float max_val = -1e6;",
  113. " int max_i = 100;",
  114. " vec3 loc = start_loc;",
  115. // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  116. // non-constant expression. So we use a hard-coded max, and an additional condition
  117. // inside the loop.
  118. " for (int iter=0; iter<MAX_STEPS; iter++) {",
  119. " if (iter >= nsteps)",
  120. " break;",
  121. // Sample from the 3D texture
  122. " float val = sample1(loc);",
  123. // Apply MIP operation
  124. " if (val > max_val) {",
  125. " max_val = val;",
  126. " max_i = iter;",
  127. " }",
  128. // Advance location deeper into the volume
  129. " loc += step;",
  130. " }",
  131. // Refine location, gives crispier images
  132. " vec3 iloc = start_loc + step * (float(max_i) - 0.5);",
  133. " vec3 istep = step / float(REFINEMENT_STEPS);",
  134. " for (int i=0; i<REFINEMENT_STEPS; i++) {",
  135. " max_val = max(max_val, sample1(iloc));",
  136. " iloc += istep;",
  137. " }",
  138. // Resolve final color
  139. " gl_FragColor = apply_colormap(max_val);",
  140. " }",
  141. " void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {",
  142. " gl_FragColor = vec4(0.0); // init transparent",
  143. " vec4 color3 = vec4(0.0); // final color",
  144. " vec3 dstep = 1.5 / u_size; // step to sample derivative",
  145. " vec3 loc = start_loc;",
  146. " float low_threshold = u_renderthreshold - 0.02 * (u_clim[1] - u_clim[0]);",
  147. // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  148. // non-constant expression. So we use a hard-coded max, and an additional condition
  149. // inside the loop.
  150. " for (int iter=0; iter<MAX_STEPS; iter++) {",
  151. " if (iter >= nsteps)",
  152. " break;",
  153. // Sample from the 3D texture
  154. " float val = sample1(loc);",
  155. " if (val > low_threshold) {",
  156. // Take the last interval in smaller steps
  157. " vec3 iloc = loc - 0.5 * step;",
  158. " vec3 istep = step / float(REFINEMENT_STEPS);",
  159. " for (int i=0; i<REFINEMENT_STEPS; i++) {",
  160. " val = sample1(iloc);",
  161. " if (val > u_renderthreshold) {",
  162. " gl_FragColor = add_lighting(val, iloc, dstep, view_ray);",
  163. " return;",
  164. " }",
  165. " iloc += istep;",
  166. " }",
  167. " }",
  168. // Advance location deeper into the volume
  169. " loc += step;",
  170. " }",
  171. " }",
  172. " vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray)",
  173. " {",
  174. // Calculate color by incorporating lighting
  175. // View direction
  176. " vec3 V = normalize(view_ray);",
  177. // calculate normal vector from gradient
  178. " vec3 N;",
  179. " float val1, val2;",
  180. " val1 = sample1(loc + vec3(-step[0], 0.0, 0.0));",
  181. " val2 = sample1(loc + vec3(+step[0], 0.0, 0.0));",
  182. " N[0] = val1 - val2;",
  183. " val = max(max(val1, val2), val);",
  184. " val1 = sample1(loc + vec3(0.0, -step[1], 0.0));",
  185. " val2 = sample1(loc + vec3(0.0, +step[1], 0.0));",
  186. " N[1] = val1 - val2;",
  187. " val = max(max(val1, val2), val);",
  188. " val1 = sample1(loc + vec3(0.0, 0.0, -step[2]));",
  189. " val2 = sample1(loc + vec3(0.0, 0.0, +step[2]));",
  190. " N[2] = val1 - val2;",
  191. " val = max(max(val1, val2), val);",
  192. " float gm = length(N); // gradient magnitude",
  193. " N = normalize(N);",
  194. // Flip normal so it points towards viewer
  195. " float Nselect = float(dot(N, V) > 0.0);",
  196. " N = (2.0 * Nselect - 1.0) * N; // == Nselect * N - (1.0-Nselect)*N;",
  197. // Init colors
  198. " vec4 ambient_color = vec4(0.0, 0.0, 0.0, 0.0);",
  199. " vec4 diffuse_color = vec4(0.0, 0.0, 0.0, 0.0);",
  200. " vec4 specular_color = vec4(0.0, 0.0, 0.0, 0.0);",
  201. // note: could allow multiple lights
  202. " for (int i=0; i<1; i++)",
  203. " {",
  204. // Get light direction (make sure to prevent zero devision)
  205. " vec3 L = normalize(view_ray); //lightDirs[i];",
  206. " float lightEnabled = float( length(L) > 0.0 );",
  207. " L = normalize(L + (1.0 - lightEnabled));",
  208. // Calculate lighting properties
  209. " float lambertTerm = clamp(dot(N, L), 0.0, 1.0);",
  210. " vec3 H = normalize(L+V); // Halfway vector",
  211. " float specularTerm = pow(max(dot(H, N), 0.0), shininess);",
  212. // Calculate mask
  213. " float mask1 = lightEnabled;",
  214. // Calculate colors
  215. " ambient_color += mask1 * ambient_color; // * gl_LightSource[i].ambient;",
  216. " diffuse_color += mask1 * lambertTerm;",
  217. " specular_color += mask1 * specularTerm * specular_color;",
  218. " }",
  219. // Calculate final color by componing different components
  220. " vec4 final_color;",
  221. " vec4 color = apply_colormap(val);",
  222. " final_color = color * (ambient_color + diffuse_color) + specular_color;",
  223. " final_color.a = color.a;",
  224. " return final_color;",
  225. " }",
  226. ].join( "\n" )
  227. };