Cesium 实现双箭头材质(fabric改造箭头线材质的着色器)

在这里插入图片描述

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箭头线实现原理分析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值