PolylineArrowMaterial.glsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifdef GL_OES_standard_derivatives
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. uniform vec4 color;
  5. varying float v_width;
  6. float getPointOnLine(vec2 p0, vec2 p1, float x)
  7. {
  8. float slope = (p0.y - p1.y) / (p0.x - p1.x);
  9. return slope * (x - p0.x) + p0.y;
  10. }
  11. czm_material czm_getMaterial(czm_materialInput materialInput)
  12. {
  13. czm_material material = czm_getDefaultMaterial(materialInput);
  14. vec2 st = materialInput.st;
  15. #ifdef GL_OES_standard_derivatives
  16. float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
  17. #else
  18. float base = 0.975; // 2.5% of the line will be the arrow head
  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. }