Files
sdk4.0/src/Obj/Materail/CustomImageMaterialSource.js

104 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-07-03 13:54:01 +08:00
import Tools from "../../Tools";
function CustomImageMaterialSource() {
let tools = new Tools()
let _that = this
if (typeof Cesium !== 'undefined') (function (Cesium) {
/**
* 自定义材质线Property 适用于entity和primitive材质
* @param {object} options
* @param source {string} glsl的shader代码
* @param options.image {string} 图片地址
*/
function CustomMaterialSource(options = {}, source) {
var Color = Cesium.Color,
defaultValue = Cesium.defaultValue,
defineProperties = Object.defineProperties,
Event = Cesium.Event,
createPropertyDescriptor = Cesium.createPropertyDescriptor,
Property = Cesium.Property,
Material = Cesium.Material,
MaterialType = options.MaterialType || 'wallType' + parseInt(Math.random() * 1000);
// 创建自定义动态材质对象
function PolylineCustomMaterialProperty(options = {}) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
// 定义内置属性
this._definitionChanged = new Event();
this._color = undefined;
this._colorSubscription = undefined;
this._repeat = undefined;
this._repeatSubscription = undefined;
this.image = options.image;
this.color = new Cesium.Color.fromCssColorString(options.color || "rgba(4,253,231,0.87)");
this.repeat = options.repeat;
}
// 定义材质对象默认属性
defineProperties(PolylineCustomMaterialProperty.prototype, {
isvarant: {
get: function () {
return false;
}
},
definitionChanged: {
get: function () {
return this._definitionChanged;
}
},
repeat: Cesium.createPropertyDescriptor('repeat'),
color: createPropertyDescriptor('color')
});
// 材质对象需要有type函数 value函数 equals函数
PolylineCustomMaterialProperty.prototype.getType = function (time) {
return MaterialType;
};
PolylineCustomMaterialProperty.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.image;
result.repeat = Cesium.Property.getValueOrDefault(this.repeat);
return result;
};
PolylineCustomMaterialProperty.prototype.equals = function (other) {
return this === other || //
(other instanceof PolylineCustomMaterialProperty &&
Property.equals(this._color, other._color)) &&
Property.equals(this.repeat, other._repeat)
};
// 将定义的材质对象添加到cesium的材质队列中
Material._materialCache.addMaterial(MaterialType, {
fabric: {
type: MaterialType,
uniforms: {
color: new Cesium.Color(1.0, 1.0, 1.0, 1),
image: options.image || tools.getSourceRootPath() + "/img/material/arrow.png",
repeat: new Cesium.Cartesian2(100.0, 100.0),
},
// 动态材质shader
source: 'czm_material czm_getMaterial(czm_materialInput materialInput)\n\
{\n\
czm_material material = czm_getDefaultMaterial(materialInput);\n\
vec2 st = repeat * materialInput.st;\n\
vec4 colorImage = texture(image, st);\n\
\n\
material.diffuse = colorImage.rgb * 0.0;\n\
material.emission = colorImage.rgb * 0.3;\n\
\
return material;\n\
}',
},
})
return new PolylineCustomMaterialProperty(options);
}
Cesium.CustomImageMaterialSource = CustomMaterialSource;
})(Cesium);
}
export { CustomImageMaterialSource }