PolylineArrowMaterial.glsl 1.7 KB

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