direct-volume.frag.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /**
  2. * Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Michael Krone <michael.krone@uni-tuebingen.de>
  6. */
  7. export default `
  8. precision highp float;
  9. precision highp int;
  10. #include common
  11. #include light_frag_params
  12. #if dClipObjectCount != 0
  13. uniform int uClipObjectType[dClipObjectCount];
  14. uniform bool uClipObjectInvert[dClipObjectCount];
  15. uniform vec3 uClipObjectPosition[dClipObjectCount];
  16. uniform vec4 uClipObjectRotation[dClipObjectCount];
  17. uniform vec3 uClipObjectScale[dClipObjectCount];
  18. #endif
  19. #include common_clip
  20. #include read_from_texture
  21. #include texture3d_from_1d_trilinear
  22. #include texture3d_from_2d_nearest
  23. #include texture3d_from_2d_linear
  24. uniform mat4 uProjection, uTransform, uModelView, uModel, uView;
  25. uniform vec3 uCameraDir;
  26. uniform sampler2D tDepth;
  27. uniform vec2 uDrawingBufferSize;
  28. varying vec3 vOrigPos;
  29. varying float vInstance;
  30. varying vec4 vBoundingSphere;
  31. varying mat4 vTransform;
  32. uniform mat4 uInvView;
  33. uniform vec2 uIsoValue;
  34. uniform vec3 uGridDim;
  35. uniform vec3 uBboxSize;
  36. uniform sampler2D tTransferTex;
  37. uniform float uTransferScale;
  38. uniform float uStepScale;
  39. uniform float uJumpLength;
  40. uniform int uObjectId;
  41. uniform int uVertexCount;
  42. uniform int uInstanceCount;
  43. uniform int uGroupCount;
  44. uniform vec3 uHighlightColor;
  45. uniform vec3 uSelectColor;
  46. uniform vec2 uMarkerTexDim;
  47. uniform sampler2D tMarker;
  48. uniform float uFogNear;
  49. uniform float uFogFar;
  50. uniform vec3 uFogColor;
  51. uniform float uAlpha;
  52. uniform float uPickingAlphaThreshold;
  53. uniform bool uTransparentBackground;
  54. uniform float uInteriorDarkening;
  55. uniform bool uInteriorColorFlag;
  56. uniform vec3 uInteriorColor;
  57. bool interior;
  58. uniform float uNear;
  59. uniform float uFar;
  60. uniform float uIsOrtho;
  61. uniform vec3 uCellDim;
  62. uniform vec3 uCameraPosition;
  63. uniform mat4 uCartnToUnit;
  64. #if __VERSION__ != 100
  65. // for webgl1 this is given as a 'define'
  66. uniform int uMaxSteps;
  67. #endif
  68. #if defined(dGridTexType_2d)
  69. precision highp sampler2D;
  70. uniform sampler2D tGridTex;
  71. uniform vec3 uGridTexDim;
  72. #elif defined(dGridTexType_3d)
  73. precision highp sampler3D;
  74. uniform sampler3D tGridTex;
  75. #endif
  76. #if defined(dRenderVariant_color)
  77. #if defined(dColorType_uniform)
  78. uniform vec3 uColor;
  79. #elif defined(dColorType_texture)
  80. uniform vec2 uColorTexDim;
  81. uniform sampler2D tColor;
  82. #endif
  83. #ifdef dOverpaint
  84. varying vec4 vOverpaint;
  85. uniform vec2 uOverpaintTexDim;
  86. uniform sampler2D tOverpaint;
  87. #endif
  88. #endif
  89. #if defined(dGridTexType_2d)
  90. vec4 textureVal(vec3 pos) {
  91. return texture3dFrom2dLinear(tGridTex, pos + (vec3(0.5, 0.5, 0.0) / uGridDim), uGridDim, uGridTexDim.xy);
  92. }
  93. vec4 textureGroup(vec3 pos) {
  94. return texture3dFrom2dNearest(tGridTex, pos + (vec3(0.5, 0.5, 0.0) / uGridDim), uGridDim, uGridTexDim.xy);
  95. }
  96. #elif defined(dGridTexType_3d)
  97. vec4 textureVal(vec3 pos) {
  98. return texture(tGridTex, pos + (vec3(0.5) / uGridDim));
  99. }
  100. vec4 textureGroup(vec3 pos) {
  101. return texelFetch(tGridTex, ivec3(pos * uGridDim), 0);
  102. }
  103. #endif
  104. #include wboit_params
  105. vec4 transferFunction(float value) {
  106. return texture2D(tTransferTex, vec2(value, 0.0));
  107. }
  108. float getDepth(const in vec2 coords) {
  109. #ifdef depthTextureSupport
  110. if (!uRenderWboit) {
  111. // in case of opaque volumes (and depth texture support)
  112. return texture2D(tDepth, coords).r;
  113. } else {
  114. return unpackRGBAToDepth(texture2D(tDepth, coords));
  115. }
  116. #else
  117. return unpackRGBAToDepth(texture2D(tDepth, coords));
  118. #endif
  119. }
  120. const float gradOffset = 0.5;
  121. vec3 v3m4(vec3 p, mat4 m) {
  122. return (m * vec4(p, 1.0)).xyz;
  123. }
  124. float preFogAlphaBlended = 0.0;
  125. vec4 raymarch(vec3 startLoc, vec3 step, vec3 rayDir) {
  126. #if defined(dRenderVariant_color) && !defined(dIgnoreLight)
  127. mat3 normalMatrix = transpose3(inverse3(mat3(uModelView * vTransform)));
  128. #endif
  129. mat4 cartnToUnit = uCartnToUnit * inverse4(vTransform);
  130. #if defined(dClipVariant_pixel) && dClipObjectCount != 0
  131. mat4 modelTransform = uModel * vTransform * uTransform;
  132. #endif
  133. mat4 modelViewTransform = uModelView * vTransform * uTransform;
  134. vec3 scaleVol = vec3(1.0) / uGridDim;
  135. vec3 pos = startLoc;
  136. vec4 cell;
  137. float prevValue = -1.0;
  138. float value = 0.0;
  139. vec4 src = vec4(0.0);
  140. vec4 dst = vec4(0.0);
  141. bool hit = false;
  142. float fragmentDepth;
  143. vec3 posMin = vec3(0.0);
  144. vec3 posMax = vec3(1.0) - vec3(1.0) / uGridDim;
  145. vec3 unitPos;
  146. vec3 isoPos;
  147. vec3 nextPos;
  148. float nextValue;
  149. vec3 color = vec3(0.45, 0.55, 0.8);
  150. vec3 gradient = vec3(1.0);
  151. vec3 dx = vec3(gradOffset * scaleVol.x, 0.0, 0.0);
  152. vec3 dy = vec3(0.0, gradOffset * scaleVol.y, 0.0);
  153. vec3 dz = vec3(0.0, 0.0, gradOffset * scaleVol.z);
  154. float maxDist = min(vBoundingSphere.w * 2.0, uFar - uNear);
  155. float maxDistSq = maxDist * maxDist;
  156. for (int i = 0; i < uMaxSteps; ++i) {
  157. // break when beyond bounding-sphere or far-plane
  158. vec3 distVec = startLoc - pos;
  159. if (dot(distVec, distVec) > maxDistSq) break;
  160. unitPos = v3m4(pos, cartnToUnit);
  161. // continue when outside of grid
  162. if (unitPos.x > posMax.x || unitPos.y > posMax.y || unitPos.z > posMax.z ||
  163. unitPos.x < posMin.x || unitPos.y < posMin.y || unitPos.z < posMin.z
  164. ) {
  165. if (hit) break;
  166. prevValue = value;
  167. pos += step;
  168. continue;
  169. }
  170. cell = textureVal(unitPos);
  171. value = cell.a; // current voxel value
  172. if (uJumpLength > 0.0 && value < 0.01) {
  173. nextPos = pos + rayDir * uJumpLength;
  174. nextValue = textureVal(v3m4(nextPos, cartnToUnit)).a;
  175. if (nextValue < 0.01) {
  176. prevValue = nextValue;
  177. pos = nextPos;
  178. continue;
  179. }
  180. }
  181. #if defined(dRenderMode_isosurface)
  182. if (prevValue > 0.0 && ( // there was a prev Value
  183. (prevValue < uIsoValue.x && value > uIsoValue.x) || // entering isosurface
  184. (prevValue > uIsoValue.x && value < uIsoValue.x) // leaving isosurface
  185. )) {
  186. isoPos = v3m4(mix(pos - step, pos, ((prevValue - uIsoValue.x) / ((prevValue - uIsoValue.x) - (value - uIsoValue.x)))), cartnToUnit);
  187. vec4 mvPosition = modelViewTransform * vec4(isoPos * uGridDim, 1.0);
  188. #if defined(dClipVariant_pixel) && dClipObjectCount != 0
  189. vec3 vModelPosition = v3m4(isoPos * uGridDim, modelTransform);
  190. if (clipTest(vec4(vModelPosition, 0.0), 0)) {
  191. prevValue = value;
  192. pos += step;
  193. continue;
  194. }
  195. #endif
  196. float depth = calcDepth(mvPosition.xyz);
  197. if (depth > getDepth(gl_FragCoord.xy / uDrawingBufferSize))
  198. break;
  199. #ifdef enabledFragDepth
  200. if (!hit) {
  201. gl_FragDepthEXT = depth;
  202. }
  203. #endif
  204. #if defined(dRenderVariant_pickObject)
  205. return vec4(encodeFloatRGB(float(uObjectId)), 1.0);
  206. #elif defined(dRenderVariant_pickInstance)
  207. return vec4(encodeFloatRGB(vInstance), 1.0);
  208. #elif defined(dRenderVariant_pickGroup)
  209. #ifdef dPackedGroup
  210. return vec4(textureGroup(floor(isoPos * uGridDim + 0.5) / uGridDim).rgb, 1.0);
  211. #else
  212. vec3 g = floor(isoPos * uGridDim + 0.5);
  213. return vec4(encodeFloatRGB(g.z + g.y * uGridDim.z + g.x * uGridDim.z * uGridDim.y), 1.0);
  214. #endif
  215. #elif defined(dRenderVariant_depth)
  216. #ifdef enabledFragDepth
  217. return packDepthToRGBA(gl_FragDepthEXT);
  218. #else
  219. return packDepthToRGBA(gl_FragCoord.z);
  220. #endif
  221. #elif defined(dRenderVariant_color)
  222. #ifdef dPackedGroup
  223. float group = decodeFloatRGB(textureGroup(floor(isoPos * uGridDim + 0.5) / uGridDim).rgb);
  224. #else
  225. vec3 g = floor(isoPos * uGridDim + 0.5);
  226. float group = g.z + g.y * uGridDim.z + g.x * uGridDim.z * uGridDim.y;
  227. #endif
  228. #if defined(dColorType_uniform)
  229. color = uColor;
  230. #elif defined(dColorType_instance)
  231. color = readFromTexture(tColor, vInstance, uColorTexDim).rgb;
  232. #elif defined(dColorType_group)
  233. color = readFromTexture(tColor, group, uColorTexDim).rgb;
  234. #elif defined(dColorType_groupInstance)
  235. color = readFromTexture(tColor, vInstance * float(uGroupCount) + group, uColorTexDim).rgb;
  236. #elif defined(dColorType_vertex)
  237. color = texture3dFrom1dTrilinear(tColor, isoPos, uGridDim, uColorTexDim, 0.0).rgb;
  238. #elif defined(dColorType_vertexInstance)
  239. color = texture3dFrom1dTrilinear(tColor, isoPos, uGridDim, uColorTexDim, vInstance * float(uVertexCount)).rgb;
  240. #endif
  241. // handle flipping and negative isosurfaces
  242. #ifdef dFlipSided
  243. bool flipped = value < uIsoValue.y; // flipped
  244. #else
  245. bool flipped = value > uIsoValue.y;
  246. #endif
  247. interior = value < uIsoValue.x && flipped;
  248. #ifndef dDoubleSided
  249. if (interior) {
  250. prevValue = value;
  251. pos += step;
  252. continue;
  253. }
  254. #endif
  255. vec3 vViewPosition = mvPosition.xyz;
  256. vec4 material = vec4(color, uAlpha);
  257. #ifdef dIgnoreLight
  258. gl_FragColor = material;
  259. #else
  260. #if defined(dFlatShaded)
  261. // nearest grid point
  262. isoPos = floor(isoPos * uGridDim + 0.5) / uGridDim;
  263. #endif
  264. #ifdef dPackedGroup
  265. // compute gradient by central differences
  266. gradient.x = textureVal(isoPos - dx).a - textureVal(isoPos + dx).a;
  267. gradient.y = textureVal(isoPos - dy).a - textureVal(isoPos + dy).a;
  268. gradient.z = textureVal(isoPos - dz).a - textureVal(isoPos + dz).a;
  269. #else
  270. gradient = textureVal(isoPos).xyz * 2.0 - 1.0;
  271. #endif
  272. vec3 normal = -normalize(normalMatrix * normalize(gradient));
  273. normal = normal * (float(flipped) * 2.0 - 1.0);
  274. normal = normal * -(float(interior) * 2.0 - 1.0);
  275. #include apply_light_color
  276. #endif
  277. float vMarker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  278. #include apply_interior_color
  279. #include apply_marker_color
  280. preFogAlphaBlended = (1.0 - preFogAlphaBlended) * gl_FragColor.a + preFogAlphaBlended;
  281. fragmentDepth = depth;
  282. #include apply_fog
  283. src = gl_FragColor;
  284. if (!uTransparentBackground) {
  285. // done in 'apply_fog' otherwise
  286. src.rgb *= src.a;
  287. }
  288. dst = (1.0 - dst.a) * src + dst; // standard blending
  289. #endif
  290. #ifdef dSingleLayer
  291. break;
  292. #endif
  293. hit = true;
  294. }
  295. prevValue = value;
  296. #elif defined(dRenderMode_volume)
  297. vec4 mvPosition = modelViewTransform * vec4(unitPos * uGridDim, 1.0);
  298. if (calcDepth(mvPosition.xyz) > getDepth(gl_FragCoord.xy / uDrawingBufferSize))
  299. break;
  300. #if defined(dClipVariant_pixel) && dClipObjectCount != 0
  301. vec3 vModelPosition = v3m4(unitPos * uGridDim, modelTransform);
  302. if (clipTest(vec4(vModelPosition, 0.0), 0)) {
  303. prevValue = value;
  304. pos += step;
  305. continue;
  306. }
  307. #endif
  308. #if defined(dRenderVariant_color)
  309. vec3 vViewPosition = mvPosition.xyz;
  310. vec4 material = transferFunction(value);
  311. #ifdef dIgnoreLight
  312. gl_FragColor.rgb = material.rgb;
  313. #else
  314. if (material.a >= 0.01) {
  315. #ifdef dPackedGroup
  316. // compute gradient by central differences
  317. gradient.x = textureVal(unitPos - dx).a - textureVal(unitPos + dx).a;
  318. gradient.y = textureVal(unitPos - dy).a - textureVal(unitPos + dy).a;
  319. gradient.z = textureVal(unitPos - dz).a - textureVal(unitPos + dz).a;
  320. #else
  321. gradient = cell.xyz * 2.0 - 1.0;
  322. #endif
  323. vec3 normal = -normalize(normalMatrix * normalize(gradient));
  324. #include apply_light_color
  325. } else {
  326. gl_FragColor.rgb = material.rgb;
  327. }
  328. #endif
  329. gl_FragColor.a = material.a * uAlpha * uTransferScale;
  330. #ifdef dPackedGroup
  331. float group = decodeFloatRGB(textureGroup(floor(unitPos * uGridDim + 0.5) / uGridDim).rgb);
  332. #else
  333. vec3 g = floor(unitPos * uGridDim + 0.5);
  334. float group = g.z + g.y * uGridDim.z + g.x * uGridDim.z * uGridDim.y;
  335. #endif
  336. float vMarker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  337. #include apply_marker_color
  338. preFogAlphaBlended = (1.0 - preFogAlphaBlended) * gl_FragColor.a + preFogAlphaBlended;
  339. fragmentDepth = calcDepth(mvPosition.xyz);
  340. #include apply_fog
  341. src = gl_FragColor;
  342. if (!uTransparentBackground) {
  343. // done in 'apply_fog' otherwise
  344. src.rgb *= src.a;
  345. }
  346. dst = (1.0 - dst.a) * src + dst; // standard blending
  347. #endif
  348. #endif
  349. // break if the color is opaque enough
  350. if (dst.a > 0.95)
  351. break;
  352. pos += step;
  353. }
  354. #if defined(dRenderMode_isosurface) && defined(enabledFragDepth)
  355. // ensure depth is written everywhere
  356. if (!hit)
  357. gl_FragDepthEXT = 1.0;
  358. #endif
  359. return dst;
  360. }
  361. // TODO: support float texture for higher precision values???
  362. // TODO: support clipping exclusion texture support
  363. void main() {
  364. if (gl_FrontFacing)
  365. discard;
  366. #if defined(dRenderVariant_pick) || defined(dRenderVariant_depth)
  367. #if defined(dRenderMode_volume)
  368. // always ignore pick & depth for volume
  369. discard;
  370. #elif defined(dRenderMode_isosurface)
  371. if (uAlpha < uPickingAlphaThreshold)
  372. discard; // ignore so the element below can be picked
  373. #endif
  374. #endif
  375. vec3 rayDir = mix(normalize(vOrigPos - uCameraPosition), uCameraDir, uIsOrtho);
  376. vec3 step = rayDir * uStepScale;
  377. float boundingSphereNear = distance(vBoundingSphere.xyz, uCameraPosition) - vBoundingSphere.w;
  378. float d = max(uNear, boundingSphereNear);
  379. gl_FragColor = raymarch(uCameraPosition + (d * rayDir), step, rayDir);
  380. #if defined(dRenderVariant_pick) || defined(dRenderVariant_depth)
  381. // discard when nothing was hit
  382. if (gl_FragColor == vec4(0.0))
  383. discard;
  384. #endif
  385. #if defined(dRenderVariant_color)
  386. #if defined(dRenderMode_isosurface) && defined(enabledFragDepth)
  387. float fragmentDepth = gl_FragDepthEXT;
  388. #else
  389. float fragmentDepth = calcDepth((uView * vec4(uCameraPosition + (d * rayDir), 1.0)).xyz);
  390. #endif
  391. float preFogAlpha = clamp(preFogAlphaBlended, 0.0, 1.0);
  392. interior = false;
  393. #include wboit_write
  394. #endif
  395. }
  396. `;