const appearance = new Cesium.PolylineMaterialAppearance({
material: new Cesium.Material({
fabric: {
uniforms: { color: material.color },
source:
`float getPointOnLine(vec2 p0, vec2 p1, float x)
{
float slope = (p0.y - p1.y) / (p0.x - p1.x);
return slope * (x - p0.x) + p0.y;
}
czm_material czm_getMaterial(czm_materialInput materialInput) {
czm_material material = czm_getDefaultMaterial(materialInput);
vec2 st = materialInput.st;
// if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives)){
// float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
// }else{
// // If no derivatives available (IE 10?), 2.5% of the line will be the arrow head
// float base = 0.975;
// }
float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
vec2 center = vec2(1.0, 0.5);
float ptOnUpperLine = getPointOnLine(vec2(base, 1.0), center, st.s);
float ptOnLowerLine = getPointOnLine(vec2(base, 0.0), center, st.s);
float base1 = abs(fwidth(st.s)) * 10.0 * czm_pixelRatio;
vec2 center1 = vec2(0, 0.5);
float ptOnUpperLine1 = getPointOnLine(vec2(base1, 1.0), center1, st.s);
float ptOnLowerLine1 = getPointOnLine(vec2(base1, 0.0), center1, st.s);
// 留下s值为1的区域作为非箭头区域,
float halfWidth = 0.15;
float s = step(0.5 - halfWidth, st.t);
s *= 1.0 - step(0.5 + halfWidth, st.t);
if(st.s > base){
s *= 1.0 - step(base, st.s);
}else{
s *= step(base1, st.s);
}
// 留下t值为1的区域作为箭头区域,其余区域t值均为0
float t = step(base, materialInput.st.s);
if(st.s > base)
{
t *= 1.0 - step(ptOnUpperLine, st.t);
t *= step(ptOnLowerLine, st.t);
}
if(st.s < base1)
{
t = 1.0 - step(base1, materialInput.st.s);
t *= 1.0 - step(ptOnUpperLine1, st.t);
t *= step(ptOnLowerLine1, st.t);
}
// 抗锯齿
// Find the distance from the closest separator (region between two colors)
float dist;
if (st.s> base1 && st.s < base)
{
float d1 = abs(st.t - (0.5 - halfWidth));
float d2 = abs(st.t - (0.5 + halfWidth));
dist = min(d1, d2);
}
else
{
if(st.s < base1)
{
dist = 0.0;
float d1 = czm_infinity;
if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth)
{
d1 = abs(st.s - base1);
}
float d2 = abs(st.t - ptOnUpperLine1);
float d3 = abs(st.t - ptOnLowerLine1);
dist = min(min(d1, d2), d3);
}else
{
float d1 = czm_infinity;
if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth)
{
d1 = abs(st.s - base);
}
float d2 = abs(st.t - ptOnUpperLine);
float d3 = abs(st.t - ptOnLowerLine);
dist = min(min(d1, d2), d3);
}
}
vec4 outsideColor = vec4(0.0);
vec4 currentColor = mix(outsideColor, color, clamp(s + t, 0.0, 1.0));
vec4 outColor = czm_antialias(outsideColor, color, currentColor, dist);
outColor = czm_gammaCorrect(outColor);
material.diffuse = outColor.rgb;
material.alpha = outColor.a;
return material;
}`,
},
}),
})
Cesium 实现双箭头材质(fabric改造箭头线材质的着色器)
于 2024-09-30 10:43:53 首次发布