VolumeShader.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @author Almar Klein / http://almarklein.org
  3. *
  4. * Shaders to render 3D volumes using raycasting.
  5. * The applied techniques are based on similar implementations in the Visvis and Vispy projects.
  6. * This is not the only approach, therefore it's marked 1.
  7. */
  8. THREE.VolumeRenderShader1 = {
  9. uniforms: {
  10. "u_size": { value: new THREE.Vector3( 1, 1, 1 ) },
  11. "u_renderstyle": { value: 0 },
  12. "u_renderthreshold": { value: 0.5 },
  13. "u_clim": { value: new THREE.Vector2( 1, 1 ) },
  14. "u_data": { value: null },
  15. "u_cmdata": { value: null }
  16. },
  17. vertexShader: [
  18. 'varying vec4 v_nearpos;',
  19. 'varying vec4 v_farpos;',
  20. 'varying vec3 v_position;',
  21. 'mat4 inversemat(mat4 m) {',
  22. // Taken from https://github.com/stackgl/glsl-inverse/blob/master/index.glsl
  23. // This function is licenced by the MIT license to Mikola Lysenko
  24. 'float',
  25. 'a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],',
  26. 'a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],',
  27. 'a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],',
  28. 'a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],',
  29. 'b00 = a00 * a11 - a01 * a10,',
  30. 'b01 = a00 * a12 - a02 * a10,',
  31. 'b02 = a00 * a13 - a03 * a10,',
  32. 'b03 = a01 * a12 - a02 * a11,',
  33. 'b04 = a01 * a13 - a03 * a11,',
  34. 'b05 = a02 * a13 - a03 * a12,',
  35. 'b06 = a20 * a31 - a21 * a30,',
  36. 'b07 = a20 * a32 - a22 * a30,',
  37. 'b08 = a20 * a33 - a23 * a30,',
  38. 'b09 = a21 * a32 - a22 * a31,',
  39. 'b10 = a21 * a33 - a23 * a31,',
  40. 'b11 = a22 * a33 - a23 * a32,',
  41. 'det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;',
  42. 'return mat4(',
  43. 'a11 * b11 - a12 * b10 + a13 * b09,',
  44. 'a02 * b10 - a01 * b11 - a03 * b09,',
  45. 'a31 * b05 - a32 * b04 + a33 * b03,',
  46. 'a22 * b04 - a21 * b05 - a23 * b03,',
  47. 'a12 * b08 - a10 * b11 - a13 * b07,',
  48. 'a00 * b11 - a02 * b08 + a03 * b07,',
  49. 'a32 * b02 - a30 * b05 - a33 * b01,',
  50. 'a20 * b05 - a22 * b02 + a23 * b01,',
  51. 'a10 * b10 - a11 * b08 + a13 * b06,',
  52. 'a01 * b08 - a00 * b10 - a03 * b06,',
  53. 'a30 * b04 - a31 * b02 + a33 * b00,',
  54. 'a21 * b02 - a20 * b04 - a23 * b00,',
  55. 'a11 * b07 - a10 * b09 - a12 * b06,',
  56. 'a00 * b09 - a01 * b07 + a02 * b06,',
  57. 'a31 * b01 - a30 * b03 - a32 * b00,',
  58. 'a20 * b03 - a21 * b01 + a22 * b00) / det;',
  59. '}',
  60. 'void main() {',
  61. // Prepare transforms to map to "camera view". See also:
  62. // https://threejs.org/docs/#api/renderers/webgl/WebGLProgram
  63. 'mat4 viewtransformf = viewMatrix;',
  64. 'mat4 viewtransformi = inversemat(viewMatrix);',
  65. // Project local vertex coordinate to camera position. Then do a step
  66. // backward (in cam coords) to the near clipping plane, and project back. Do
  67. // the same for the far clipping plane. This gives us all the information we
  68. // need to calculate the ray and truncate it to the viewing cone.
  69. 'vec4 position4 = vec4(position, 1.0);',
  70. 'vec4 pos_in_cam = viewtransformf * position4;',
  71. // Intersection of ray and near clipping plane (z = -1 in clip coords)
  72. 'pos_in_cam.z = -pos_in_cam.w;',
  73. 'v_nearpos = viewtransformi * pos_in_cam;',
  74. // Intersection of ray and far clipping plane (z = +1 in clip coords)
  75. 'pos_in_cam.z = pos_in_cam.w;',
  76. 'v_farpos = viewtransformi * pos_in_cam;',
  77. // Set varyings and output pos
  78. 'v_position = position;',
  79. 'gl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;',
  80. '}',
  81. ].join( '\n' ),
  82. fragmentShader: [
  83. 'precision highp float;',
  84. 'precision mediump sampler3D;',
  85. 'uniform vec3 u_size;',
  86. 'uniform int u_renderstyle;',
  87. 'uniform float u_renderthreshold;',
  88. 'uniform vec2 u_clim;',
  89. 'uniform sampler3D u_data;',
  90. 'uniform sampler2D u_cmdata;',
  91. 'varying vec3 v_position;',
  92. 'varying vec4 v_nearpos;',
  93. 'varying vec4 v_farpos;',
  94. // The maximum distance through our rendering volume is sqrt(3).
  95. 'const int MAX_STEPS = 887; // 887 for 512^3, 1774 for 1024^3',
  96. 'const int REFINEMENT_STEPS = 4;',
  97. 'const float relative_step_size = 1.0;',
  98. 'const vec4 ambient_color = vec4(0.2, 0.4, 0.2, 1.0);',
  99. 'const vec4 diffuse_color = vec4(0.8, 0.2, 0.2, 1.0);',
  100. 'const vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);',
  101. 'const float shininess = 40.0;',
  102. 'void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);',
  103. 'void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);',
  104. 'float sample1(vec3 texcoords);',
  105. 'vec4 apply_colormap(float val);',
  106. 'vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray);',
  107. 'void main() {',
  108. // Normalize clipping plane info
  109. 'vec3 farpos = v_farpos.xyz / v_farpos.w;',
  110. 'vec3 nearpos = v_nearpos.xyz / v_nearpos.w;',
  111. // Calculate unit vector pointing in the view direction through this fragment.
  112. 'vec3 view_ray = normalize(nearpos.xyz - farpos.xyz);',
  113. // Compute the (negative) distance to the front surface or near clipping plane.
  114. // v_position is the back face of the cuboid, so the initial distance calculated in the dot
  115. // product below is the distance from near clip plane to the back of the cuboid
  116. 'float distance = dot(nearpos - v_position, view_ray);',
  117. 'distance = max(distance, min((-0.5 - v_position.x) / view_ray.x,',
  118. '(u_size.x - 0.5 - v_position.x) / view_ray.x));',
  119. 'distance = max(distance, min((-0.5 - v_position.y) / view_ray.y,',
  120. '(u_size.y - 0.5 - v_position.y) / view_ray.y));',
  121. 'distance = max(distance, min((-0.5 - v_position.z) / view_ray.z,',
  122. '(u_size.z - 0.5 - v_position.z) / view_ray.z));',
  123. // Now we have the starting position on the front surface
  124. 'vec3 front = v_position + view_ray * distance;',
  125. // Decide how many steps to take
  126. 'int nsteps = int(-distance / relative_step_size + 0.5);',
  127. 'if ( nsteps < 1 )',
  128. 'discard;',
  129. // Get starting location and step vector in texture coordinates
  130. 'vec3 step = ((v_position - front) / u_size) / float(nsteps);',
  131. 'vec3 start_loc = front / u_size;',
  132. // For testing: show the number of steps. This helps to establish
  133. // whether the rays are correctly oriented
  134. //'gl_FragColor = vec4(0.0, float(nsteps) / 1.0 / u_size.x, 1.0, 1.0);',
  135. //'return;',
  136. 'if (u_renderstyle == 0)',
  137. 'cast_mip(start_loc, step, nsteps, view_ray);',
  138. 'else if (u_renderstyle == 1)',
  139. 'cast_iso(start_loc, step, nsteps, view_ray);',
  140. 'if (gl_FragColor.a < 0.05)',
  141. 'discard;',
  142. '}',
  143. 'float sample1(vec3 texcoords) {',
  144. '/* Sample float value from a 3D texture. Assumes intensity data. */',
  145. 'return texture(u_data, texcoords.xyz).r;',
  146. '}',
  147. 'vec4 apply_colormap(float val) {',
  148. 'val = (val - u_clim[0]) / (u_clim[1] - u_clim[0]);',
  149. 'return texture2D(u_cmdata, vec2(val, 0.5));',
  150. '}',
  151. 'void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {',
  152. 'float max_val = -1e6;',
  153. 'int max_i = 100;',
  154. 'vec3 loc = start_loc;',
  155. // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  156. // non-constant expression. So we use a hard-coded max, and an additional condition
  157. // inside the loop.
  158. 'for (int iter=0; iter<MAX_STEPS; iter++) {',
  159. 'if (iter >= nsteps)',
  160. 'break;',
  161. // Sample from the 3D texture
  162. 'float val = sample1(loc);',
  163. // Apply MIP operation
  164. 'if (val > max_val) {',
  165. 'max_val = val;',
  166. 'max_i = iter;',
  167. '}',
  168. // Advance location deeper into the volume
  169. 'loc += step;',
  170. '}',
  171. // Refine location, gives crispier images
  172. 'vec3 iloc = start_loc + step * (float(max_i) - 0.5);',
  173. 'vec3 istep = step / float(REFINEMENT_STEPS);',
  174. 'for (int i=0; i<REFINEMENT_STEPS; i++) {',
  175. 'max_val = max(max_val, sample1(iloc));',
  176. 'iloc += istep;',
  177. '}',
  178. // Resolve final color
  179. 'gl_FragColor = apply_colormap(max_val);',
  180. '}',
  181. 'void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {',
  182. 'gl_FragColor = vec4(0.0); // init transparent',
  183. 'vec4 color3 = vec4(0.0); // final color',
  184. 'vec3 dstep = 1.5 / u_size; // step to sample derivative',
  185. 'vec3 loc = start_loc;',
  186. 'float low_threshold = u_renderthreshold - 0.02 * (u_clim[1] - u_clim[0]);',
  187. // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
  188. // non-constant expression. So we use a hard-coded max, and an additional condition
  189. // inside the loop.
  190. 'for (int iter=0; iter<MAX_STEPS; iter++) {',
  191. 'if (iter >= nsteps)',
  192. 'break;',
  193. // Sample from the 3D texture
  194. 'float val = sample1(loc);',
  195. 'if (val > low_threshold) {',
  196. // Take the last interval in smaller steps
  197. 'vec3 iloc = loc - 0.5 * step;',
  198. 'vec3 istep = step / float(REFINEMENT_STEPS);',
  199. 'for (int i=0; i<REFINEMENT_STEPS; i++) {',
  200. 'val = sample1(iloc);',
  201. 'if (val > u_renderthreshold) {',
  202. 'gl_FragColor = add_lighting(val, iloc, dstep, view_ray);',
  203. 'return;',
  204. '}',
  205. 'iloc += istep;',
  206. '}',
  207. '}',
  208. // Advance location deeper into the volume
  209. 'loc += step;',
  210. '}',
  211. '}',
  212. 'vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray)',
  213. '{',
  214. // Calculate color by incorporating lighting
  215. // View direction
  216. 'vec3 V = normalize(view_ray);',
  217. // calculate normal vector from gradient
  218. 'vec3 N;',
  219. 'float val1, val2;',
  220. 'val1 = sample1(loc + vec3(-step[0], 0.0, 0.0));',
  221. 'val2 = sample1(loc + vec3(+step[0], 0.0, 0.0));',
  222. 'N[0] = val1 - val2;',
  223. 'val = max(max(val1, val2), val);',
  224. 'val1 = sample1(loc + vec3(0.0, -step[1], 0.0));',
  225. 'val2 = sample1(loc + vec3(0.0, +step[1], 0.0));',
  226. 'N[1] = val1 - val2;',
  227. 'val = max(max(val1, val2), val);',
  228. 'val1 = sample1(loc + vec3(0.0, 0.0, -step[2]));',
  229. 'val2 = sample1(loc + vec3(0.0, 0.0, +step[2]));',
  230. 'N[2] = val1 - val2;',
  231. 'val = max(max(val1, val2), val);',
  232. 'float gm = length(N); // gradient magnitude',
  233. 'N = normalize(N);',
  234. // Flip normal so it points towards viewer
  235. 'float Nselect = float(dot(N, V) > 0.0);',
  236. 'N = (2.0 * Nselect - 1.0) * N; // == Nselect * N - (1.0-Nselect)*N;',
  237. // Init colors
  238. 'vec4 ambient_color = vec4(0.0, 0.0, 0.0, 0.0);',
  239. 'vec4 diffuse_color = vec4(0.0, 0.0, 0.0, 0.0);',
  240. 'vec4 specular_color = vec4(0.0, 0.0, 0.0, 0.0);',
  241. // note: could allow multiple lights
  242. 'for (int i=0; i<1; i++)',
  243. '{',
  244. // Get light direction (make sure to prevent zero devision)
  245. 'vec3 L = normalize(view_ray); //lightDirs[i];',
  246. 'float lightEnabled = float( length(L) > 0.0 );',
  247. 'L = normalize(L + (1.0 - lightEnabled));',
  248. // Calculate lighting properties
  249. 'float lambertTerm = clamp(dot(N, L), 0.0, 1.0);',
  250. 'vec3 H = normalize(L+V); // Halfway vector',
  251. 'float specularTerm = pow(max(dot(H, N), 0.0), shininess);',
  252. // Calculate mask
  253. 'float mask1 = lightEnabled;',
  254. // Calculate colors
  255. 'ambient_color += mask1 * ambient_color; // * gl_LightSource[i].ambient;',
  256. 'diffuse_color += mask1 * lambertTerm;',
  257. 'specular_color += mask1 * specularTerm * specular_color;',
  258. '}',
  259. // Calculate final color by componing different components
  260. 'vec4 final_color;',
  261. 'vec4 color = apply_colormap(val);',
  262. 'final_color = color * (ambient_color + diffuse_color) + specular_color;',
  263. 'final_color.a = color.a;',
  264. 'return final_color;',
  265. '}',
  266. ].join( '\n' )
  267. };