PolylineArrowMaterial.glsl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives))
  15. float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
  16. #else
  17. // If no derivatives available (IE 10?), 2.5% of the line will be the arrow head
  18. float base = 0.975;
  19. #endif
  20. vec2 center = vec2(1.0, 0.5);
  21. float ptOnUpperLine = getPointOnLine(vec2(base, 1.0), center, st.s);
  22. float ptOnLowerLine = getPointOnLine(vec2(base, 0.0), center, st.s);
  23. float halfWidth = 0.15;
  24. float s = step(0.5 - halfWidth, st.t);
  25. s *= 1.0 - step(0.5 + halfWidth, st.t);
  26. s *= 1.0 - step(base, st.s);
  27. float t = step(base, materialInput.st.s);
  28. t *= 1.0 - step(ptOnUpperLine, st.t);
  29. t *= step(ptOnLowerLine, st.t);
  30. // Find the distance from the closest separator (region between two colors)
  31. float dist;
  32. if (st.s < base)
  33. {
  34. float d1 = abs(st.t - (0.5 - halfWidth));
  35. float d2 = abs(st.t - (0.5 + halfWidth));
  36. dist = min(d1, d2);
  37. }
  38. else
  39. {
  40. float d1 = czm_infinity;
  41. if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth)
  42. {
  43. d1 = abs(st.s - base);
  44. }
  45. float d2 = abs(st.t - ptOnUpperLine);
  46. float d3 = abs(st.t - ptOnLowerLine);
  47. dist = min(min(d1, d2), d3);
  48. }
  49. vec4 outsideColor = vec4(0.0);
  50. vec4 currentColor = mix(outsideColor, color, clamp(s + t, 0.0, 1.0));
  51. vec4 outColor = czm_antialias(outsideColor, color, currentColor, dist);
  52. outColor = czm_gammaCorrect(outColor);
  53. material.diffuse = outColor.rgb;
  54. material.alpha = outColor.a;
  55. return material;
  56. }