PolylineFlowDashImageMaterial 求解答


const MaterialType = "PolylineFlowDashImageMaterial";


function PolylineFlowDashImageMaterialProperty(option) {


  if (option.url && typeof option.url === "string") {
    this.url = option.url;
  } else {
    throw new Error("url is not a string");
  }

  this._color = undefined;
  this._definitionChanged = new Cesium.Event();
  this._time = (new Date()).getTime();

  if (option.color && option.color instanceof Cesium.Color) {
    this.color = option.color;
  } else {
    this.color = Cesium.Color.RED.withAlpha(0.5)
  }

  if (option.duration && typeof option.duration === "number" && option.duration > 0) {
    this.duration = option.duration;
  } else {
    this.duration = 10;
  }

  if (option.repeatPixel && typeof option.repeatPixel === "number" && option.repeatPixel > 0) {
    this.repeatPixel = option.repeatPixel;
  } else {
    this.repeatPixel = 200;
  }

}



Object.defineProperties(PolylineFlowDashImageMaterialProperty.prototype, {
  isConstant: {
    get: function () {
      return false;
    }
  },
  definitionChanged: {
    get: function () {
      return this._definitionChanged;
    }
  },
  color: Cesium.createPropertyDescriptor('color')
});

PolylineFlowDashImageMaterialProperty.prototype.getType = () => {
  return MaterialType;
}

PolylineFlowDashImageMaterialProperty.prototype.getValue = function (time, result) {
  if (!Cesium.defined(result)) {
    result = {};
  }
  result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
  result.image = this.url;
  result.repeatPixel = this.repeatPixel;
  result.time = (((new Date()).getTime() - this._time) % (this.duration * 1000)) / (this.duration * 1000);
  return result;
}

PolylineFlowDashImageMaterialProperty.prototype.equals = (other) => {
  return this === other
}

Cesium.Material._materialCache.addMaterial(MaterialType, {
  fabric: {
    type: MaterialType,
    uniforms: {
      color: Cesium.Color.RED.withAlpha(0.5),
      image: Cesium.Material.DefaultImageId,
      time: 0.0,
      repeatPixel: 300
    },
    source:
      `
    in float v_polylineAngle;
    mat2 rotate(float rad) {
        float c = cos(rad);
        float s = sin(rad);
        return mat2(
            c, s,
            -s, c
        );
    }

    czm_material czm_getMaterial(czm_materialInput materialInput)
    {
          czm_material material = czm_getDefaultMaterial(materialInput);
          vec2 st = materialInput.st;

          vec2 pos = rotate(v_polylineAngle) * gl_FragCoord.xy;

          float pos_x = pos.x;

          float dashPosition = fract(pos_x / (repeatPixel * czm_pixelRatio));

          vec4 colorImage = texture(
            image,
            vec2(fract(dashPosition - time), fract(st.t))
          );

          material.alpha = colorImage.a;
          material.diffuse = color.rgb;

          return material;
    }
    `
  },
  translucent: () => {
    return true;
  }
});

const positions = [
  [118.8958776368, 31.9551742856, 37],
  [118.8952529569, 31.9556593492, 37],
  [118.895163408, 31.9555758481, 37],
  [118.8951946168, 31.95555155, 37]
]

viewer.entities.add({
  polyline: {
    positions: Cesium.Cartesian3.fromDegreesArrayHeights(
      function () {
        let arr = []
        for (let i = 0; i < positions.length; i++) {
          arr.push(positions[i][0], positions[i][1], positions[i][2])
        }
        return arr
      }()
    ),
    width: 30,
    material: new PolylineFlowDashImageMaterialProperty({
      url: "/img/arrow2.png",
      color: Cesium.Color.YELLOW,
      repeatPixel: 200,
      duration: 10
    }),
  },
});

viewer.zoomTo(viewer.entities)

PolylineDashImageMaterial.gif

camera视角的倾斜的时候,贴图方向会改变。有大佬知道怎么改片元着色器吗?

以下是贴图


arrow2.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容