direct-volume.frag.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Copyright (c) 2017-2019 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. varying vec3 unitCoord;
  10. varying vec3 origPos;
  11. varying float instance;
  12. uniform mat4 uInvView;
  13. uniform float uIsoValue;
  14. uniform vec3 uGridDim;
  15. uniform sampler2D tTransferTex;
  16. uniform int uObjectId;
  17. uniform int uInstanceCount;
  18. uniform int uGroupCount;
  19. uniform vec3 uHighlightColor;
  20. uniform vec3 uSelectColor;
  21. uniform vec2 uMarkerTexDim;
  22. uniform sampler2D tMarker;
  23. uniform float uAlpha;
  24. uniform float uPickingAlphaThreshold;
  25. #if defined(dGridTexType_2d)
  26. precision highp sampler2D;
  27. uniform sampler2D tGridTex;
  28. uniform vec3 uGridTexDim;
  29. #elif defined(dGridTexType_3d)
  30. precision highp sampler3D;
  31. uniform sampler3D tGridTex;
  32. #endif
  33. #if defined(dColorType_uniform)
  34. uniform vec3 uColor;
  35. #elif defined(dColorType_varying)
  36. uniform vec2 uColorTexDim;
  37. uniform sampler2D tColor;
  38. #endif
  39. #include common
  40. #include light_frag_params
  41. #include read_from_texture
  42. #include texture3d_from_2d_nearest
  43. #include texture3d_from_2d_linear
  44. #if defined(dGridTexType_2d)
  45. vec4 textureVal(vec3 pos) {
  46. return texture3dFrom2dLinear(tGridTex, pos, uGridDim, uGridTexDim.xy);
  47. }
  48. vec4 textureGroup(vec3 pos) {
  49. vec3 nearestPos = floor(pos * uGridDim + 0.5) / uGridDim + 0.5 / uGridDim;
  50. return texture3dFrom2dNearest(tGridTex, nearestPos, uGridDim, uGridTexDim.xy);
  51. }
  52. #elif defined(dGridTexType_3d)
  53. vec4 textureVal(vec3 pos) {
  54. return texture(tGridTex, pos);
  55. }
  56. vec4 textureGroup(vec3 pos) {
  57. return texelFetch(tGridTex, ivec3(pos * uGridDim), 0);
  58. }
  59. #endif
  60. vec4 transferFunction(float value) {
  61. return texture2D(tTransferTex, vec2(value, 0.0));
  62. }
  63. const float gradOffset = 0.5;
  64. vec4 raymarch(vec3 startLoc, vec3 step, vec3 viewDir) {
  65. vec3 scaleVol = vec3(1.0) / uGridDim;
  66. vec3 pos = startLoc;
  67. float prevValue = -1.0;
  68. float value = 0.0;
  69. vec4 src = vec4(0.0);
  70. vec4 dst = vec4(0.0);
  71. #if defined(dRenderMode_isosurface)
  72. vec3 isoPos;
  73. float tmp;
  74. vec3 color = vec3(0.45, 0.55, 0.8);
  75. vec3 gradient = vec3(1.0);
  76. vec3 dx = vec3(gradOffset * scaleVol.x, 0.0, 0.0);
  77. vec3 dy = vec3(0.0, gradOffset * scaleVol.y, 0.0);
  78. vec3 dz = vec3(0.0, 0.0, gradOffset * scaleVol.z);
  79. #endif
  80. for(int i = 0; i < dMaxSteps; ++i){
  81. value = textureVal(pos).a; // current voxel value
  82. if(pos.x > 1.01 || pos.y > 1.01 || pos.z > 1.01 || pos.x < -0.01 || pos.y < -0.01 || pos.z < -0.01)
  83. break;
  84. #if defined(dRenderMode_volume)
  85. src = transferFunction(value);
  86. src.rgb *= src.a;
  87. dst = (1.0 - dst.a) * src + dst; // standard blending
  88. #endif
  89. #if defined(dRenderMode_isosurface)
  90. if(prevValue > 0.0 && ( // there was a prev Value
  91. (prevValue < uIsoValue && value > uIsoValue) || // entering isosurface
  92. (prevValue > uIsoValue && value < uIsoValue) // leaving isosurface
  93. )) {
  94. tmp = ((prevValue - uIsoValue) / ((prevValue - uIsoValue) - (value - uIsoValue)));
  95. isoPos = mix(pos - step, pos, tmp);
  96. #if defined(dRenderVariant_pick)
  97. if (uAlpha < uPickingAlphaThreshold)
  98. discard; // ignore so the element below can be picked
  99. #endif
  100. #if defined(dRenderVariant_pickObject)
  101. return vec4(encodeFloatRGB(float(uObjectId)), 1.0);
  102. #elif defined(dRenderVariant_pickInstance)
  103. return vec4(encodeFloatRGB(instance), 1.0);
  104. #elif defined(dRenderVariant_pickGroup)
  105. float group = floor(decodeFloatRGB(textureGroup(isoPos).rgb) + 0.5);
  106. return vec4(encodeFloatRGB(group), 1.0);
  107. #elif defined(dRenderVariant_depth)
  108. return packDepthToRGBA(gl_FragCoord.z); // TODO calculate isosurface depth
  109. #elif defined(dRenderVariant_color)
  110. // compute gradient by central differences
  111. gradient.x = textureVal(isoPos - dx).a - textureVal(isoPos + dx).a;
  112. gradient.y = textureVal(isoPos - dy).a - textureVal(isoPos + dy).a;
  113. gradient.z = textureVal(isoPos - dz).a - textureVal(isoPos + dz).a;
  114. gradient = normalize(gradient);
  115. float d = float(dot(gradient, viewDir) > 0.0);
  116. gradient = (2.0 * d - 1.0) * gradient;
  117. float group = floor(decodeFloatRGB(textureGroup(isoPos).rgb) + 0.5);
  118. #if defined(dColorType_instance)
  119. color = readFromTexture(tColor, instance, uColorTexDim).rgb;
  120. #elif defined(dColorType_group)
  121. color = readFromTexture(tColor, group, uColorTexDim).rgb;
  122. #elif defined(dColorType_groupInstance)
  123. color = readFromTexture(tColor, instance * float(uGroupCount) + group, uColorTexDim).rgb;
  124. #endif
  125. vec3 normal = normalize(gradient);
  126. vec3 vViewPosition = normalize(viewDir);
  127. vec4 material = vec4(color, uAlpha);
  128. #include apply_light_color
  129. float vMarker = readFromTexture(tMarker, instance * float(uGroupCount) + group, uMarkerTexDim).a;
  130. #include apply_marker_color
  131. src.rgb = gl_FragColor.rgb;
  132. src.a = gl_FragColor.a;
  133. // draw interior darker
  134. if( (prevValue - uIsoValue) > 0.0 ) {
  135. src.rgb *= 0.5;
  136. }
  137. src.rgb *= src.a;
  138. dst = (1.0 - dst.a) * src + dst; // standard blending
  139. if(dst.a >= 1.0) {
  140. break;
  141. }
  142. #endif
  143. }
  144. prevValue = value;
  145. #endif
  146. pos += step;
  147. }
  148. return dst;
  149. }
  150. void main () {
  151. vec3 cameraPos = uInvView[3].xyz / uInvView[3].w;
  152. vec3 rayDir = normalize(origPos - cameraPos);
  153. vec3 startLoc = unitCoord;
  154. vec3 step = rayDir * (1.0 / uGridDim) * 0.1;
  155. gl_FragColor = raymarch(startLoc, step, normalize(cameraPos));
  156. if (length(gl_FragColor.rgb) < 0.00001) discard;
  157. #if defined(dRenderMode_volume)
  158. gl_FragColor.a *= uAlpha;
  159. #endif
  160. }
  161. `;