PolylineArrowMaterial.glsl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifdef GL_OES_standard_derivatives
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. uniform vec4 color;
  5. float getPointOnLine(vec2 p0, vec2 p1, float x)
  6. {
  7. float slope = (p0.y - p1.y) / (p0.x - p1.x);
  8. return slope * (x - p0.x) + p0.y;
  9. }
  10. czm_material czm_getMaterial(czm_materialInput materialInput)
  11. {
  12. czm_material material = czm_getDefaultMaterial(materialInput);
  13. vec2 st = materialInput.st;
  14. #ifdef GL_OES_standard_derivatives
  15. float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
  16. #else
  17. float base = 0.975; // 2.5% of the line will be the arrow head
  18. #endif
  19. vec2 center = vec2(1.0, 0.5);
  20. float ptOnUpperLine = getPointOnLine(vec2(base, 1.0), center, st.s);
  21. float ptOnLowerLine = getPointOnLine(vec2(base, 0.0), center, st.s);
  22. float halfWidth = 0.15;
  23. float s = step(0.5 - halfWidth, st.t);
  24. s *= 1.0 - step(0.5 + halfWidth, st.t);
  25. s *= 1.0 - step(base, st.s);
  26. float t = step(base, materialInput.st.s);
  27. t *= 1.0 - step(ptOnUpperLine, st.t);
  28. t *= step(ptOnLowerLine, st.t);
  29. // Find the distance from the closest separator (region between two colors)
  30. float dist;
  31. if (st.s < base)
  32. {
  33. float d1 = abs(st.t - (0.5 - halfWidth));
  34. float d2 = abs(st.t - (0.5 + halfWidth));
  35. dist = min(d1, d2);
  36. }
  37. else
  38. {
  39. float d1 = czm_infinity;
  40. if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth)
  41. {
  42. d1 = abs(st.s - base);
  43. }
  44. float d2 = abs(st.t - ptOnUpperLine);
  45. float d3 = abs(st.t - ptOnLowerLine);
  46. dist = min(min(d1, d2), d3);
  47. }
  48. vec4 outsideColor = vec4(0.0);
  49. vec4 currentColor = mix(outsideColor, color, clamp(s + t, 0.0, 1.0));
  50. vec4 outColor = czm_antialias(outsideColor, color, currentColor, dist);
  51. outColor = czm_gammaCorrect(outColor);
  52. material.diffuse = outColor.rgb;
  53. material.alpha = outColor.a;
  54. return material;
  55. }