UNPKG

gisthreemap

Version:

基于webGL的三维api

791 lines (735 loc) 27.5 kB
import LineFlowMaterialProperty from './LineFlowMaterialProperty' import CircleDiffuseMaterialProperty from './CircleDiffuseMaterialProperty' import RadarScanMaterialProperty from './RadarScanMaterialProperty' import EllipsoidTrailMaterialProperty from './ellipsoidTrailMaterialProperty' import EllipsoidElectricMaterialProperty from './EllipsoidElectricMaterialProperty' import WallDiffuseMaterialProperty from './WallDiffuseMaterialProperty' /** * 材质效果 * entity的材质使用MaterialProperty,而promise使用的是material。 */ class CustomMaterial { constructor() { this.initpritelineMaterial() this.LineFlowMaterial() this.initCircleDiffuseMaterialProperty() this.initHexagonSpreadMaterialProperty() this.initScanlineMaterialProperty() this.initRadarScanMaterialProperty() this.initellipsoidTrailMaterialProperty() this.initEllipsoidElectricMaterialProperty() this.initTrailLineMaterial() this.initWallDiffuse() } initpritelineMaterial() { Object.defineProperties(Spriteline1MaterialProperty.prototype, { isConstant: { get: function () { return false }, }, definitionChanged: { get: function () { return this._definitionChanged }, }, color: Cesium.createPropertyDescriptor('color'), duration: Cesium.createPropertyDescriptor('duration') }) Spriteline1MaterialProperty.prototype.getType = function (time) { return 'Spriteline1' } Spriteline1MaterialProperty.prototype.getValue = function ( time, result ) { if (!Cesium.defined(result)) { result = {} } result.image = this.image result.time = ((performance.now() - this._time) % this.duration) / this.duration return result } Spriteline1MaterialProperty.prototype.equals = function (e) { return ( this === e || (e instanceof Spriteline1MaterialProperty && this.duration === e.duration) ) } Cesium.Spriteline1MaterialProperty = Spriteline1MaterialProperty Cesium.Material.Spriteline1Type = 'Spriteline1' Cesium.Material.Spriteline1Source = ` czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t)); material.alpha = colorImage.a; material.diffuse = colorImage.rgb * 1.5 ; return material; } ` // st :二维纹理坐标 // czm_material:保存可用于照明的材质信息 Cesium.Material._materialCache.addMaterial(Cesium.Material.Spriteline1Type, { fabric: { type: Cesium.Material.Spriteline1Type, uniforms: { color: new Cesium.Color(1, 0, 0, 0.5), image: '', transparent: true, time: 20, }, source: Cesium.Material.Spriteline1Source, }, translucent: function (material) { return true } }) } // 飞线效果 LineFlowMaterial() { Object.defineProperties(LineFlowMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed'), percent: Cesium.createPropertyDescriptor('percent'), gradient: Cesium.createPropertyDescriptor('gradient'), }) Cesium.LineFlowMaterialProperty = LineFlowMaterialProperty Cesium.Material.LineFlowMaterialProperty = 'LineFlowMaterialProperty' Cesium.Material.LineFlowMaterialType = 'LineFlowMaterialType' Cesium.Material.LineFlowMaterialSource = ` uniform vec4 color; uniform float speed; uniform float percent; uniform float gradient; czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; float t =fract(czm_frameNumber * speed / 1000.0); t *= (1.0 + percent); float alpha = smoothstep(t- percent, t, st.s) * step(-t, -st.s); alpha += gradient; material.diffuse = color.rgb; material.alpha = alpha; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.LineFlowMaterialType, { fabric: { type: Cesium.Material.LineFlowMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0, percent: 0.1, gradient: 0.01 }, source: Cesium.Material.LineFlowMaterialSource }, translucent: function (material) { return true } }) } //扩散圆效果 initCircleDiffuseMaterialProperty() { Object.defineProperties(CircleDiffuseMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed') }) Cesium.CircleDiffuseMaterialProperty = CircleDiffuseMaterialProperty; Cesium.Material.CircleDiffuseMaterialProperty = 'CircleDiffuseMaterialProperty'; Cesium.Material.CircleDiffuseMaterialType = 'CircleDiffuseMaterialType'; Cesium.Material.CircleDiffuseMaterialSource = ` uniform vec4 color; uniform float speed; vec3 circlePing(float r, float innerTail, float frontierBorder, float timeResetSeconds, float radarPingSpeed, float fadeDistance){ float t = fract(czm_frameNumber * speed / 1000.0); float time = mod(t, timeResetSeconds) * radarPingSpeed; float circle; circle += smoothstep(time - innerTail, time, r) * smoothstep(time + frontierBorder,time, r); circle *= smoothstep(fadeDistance, 0.0, r); return vec3(circle); } czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st * 2.0 - 1.0 ; vec2 center = vec2(0.); float time = fract(czm_frameNumber * speed / 1000.0); vec3 flagColor; float r = length(st - center) / 4.; flagColor += circlePing(r, 0.25, 0.025, 4.0, 0.3, 1.0) * color.rgb; material.alpha = length(flagColor); material.diffuse = flagColor.rgb; return material; }` Cesium.Material._materialCache.addMaterial(Cesium.Material.CircleDiffuseMaterialType, { fabric: { type: Cesium.Material.CircleDiffuseMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0 }, source: Cesium.Material.CircleDiffuseMaterialSource }, translucent: function (material) { return true; } }) } // 六角形材质 initHexagonSpreadMaterialProperty() { Object.defineProperties(HexagonSpreadMaterialProperty.prototype, { isConstant: { get: function () { return false }, }, definitionChanged: { get: function () { return this._definitionChanged }, }, color: Cesium.createPropertyDescriptor('color'), }) HexagonSpreadMaterialProperty.prototype.getType = function (_time) { return Cesium.Material.HexagonSpreadMaterialType } HexagonSpreadMaterialProperty.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 = Cesium.Material.HexagonSpreadMaterialImage return result } HexagonSpreadMaterialProperty.prototype.equals = function (other) { const reData = ( this === other || (other instanceof HexagonSpreadMaterialProperty && Cesium.Property.equals(this._color, other._color)) ) return reData } Cesium.HexagonSpreadMaterialProperty = HexagonSpreadMaterialProperty Cesium.Material.HexagonSpreadMaterialType = 'HexagonSpreadMaterial' Cesium.Material.HexagonSpreadMaterialImage = './../../data/pictures/hexagon.png' Cesium.Material.HexagonSpreadSource = ` czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; vec4 colorImage = texture2D(image, vec2(st)); material.alpha = colorImage.a * color.a * 0.5; material.diffuse = 1.8 * color.rgb ; return material; } ` Cesium.Material._materialCache.addMaterial( Cesium.Material.HexagonSpreadMaterialType, { fabric: { type: Cesium.Material.HexagonSpreadMaterialType, uniforms: { color: new Cesium.Color(1, 0, 0, 0.5), image: Cesium.Material.HexagonSpreadMaterialImage, }, source: Cesium.Material.HexagonSpreadSource, }, translucent: function (material) { return true }, } ) } // 发光线圈 initScanlineMaterialProperty() { Object.defineProperties(ScanlineMaterialProperty.prototype, { isConstant: { get: function () { return false }, }, definitionChanged: { get: function () { return this._definitionChanged }, }, color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed'), }) ScanlineMaterialProperty.prototype.getType = function (_time) { return Cesium.Material.ScanlineType } ScanlineMaterialProperty.prototype.getValue = function ( time, result ) { if (!Cesium.defined(result)) { result = {} } result.color = Cesium.Property.getValueOrClonedDefault( this._color, time, Cesium.Color.WHITE, result.color ) result.speed = this.speed return result } ScanlineMaterialProperty.prototype.equals = function (other) { const reData = this === other || (other instanceof ScanlineMaterialProperty && Cesium.Property.equals(this.color, other.color) && Cesium.Property.equals(this.speed, other.speed)) return reData } Cesium.ScanlineMaterialProperty = ScanlineMaterialProperty Cesium.Material.ScanlineType = 'Scanline' Cesium.Material.ScanlineSource = ` uniform vec4 color; uniform float speed; float circle(vec2 uv, float r, float blur) { float d = length(uv) * 1.0; /* 2.0 */ float c = smoothstep(r+blur, r, d); return c; } czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st - 0.5; material.diffuse = 2.8 * color.rgb; material.emission = vec3(0); float t = fract(czm_frameNumber * (11000.0 - speed) / 500000.0); float s = 0.3; float radius1 = smoothstep(.0, s, t) * 0.5; float alpha1 = circle(st, radius1, 0.01) * circle(st, radius1, -0.01); float alpha2 = circle(st, radius1, 0.01 - radius1) * circle(st, radius1, 0.01); float radius2 = 0.5 + smoothstep(s, 1.0, t) * 0.5; float alpha3 = circle(st, radius1, radius2 + 0.01 - radius1) * circle(st, radius1, -0.01); material.alpha = smoothstep(1.0, s, t) * (alpha1 + alpha2*0.1 + alpha3*0.1); material.alpha *= color.a ; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.ScanlineType, { fabric: { type: Cesium.Material.ScanlineType, uniforms: { color: new Cesium.Color(1, 0, 0, 0.5), time: 0, speed: 10, }, source: Cesium.Material.ScanlineSource, }, translucent: function (t) { return true }, }) } // 雷达平扫 initRadarScanMaterialProperty() { Object.defineProperties(RadarScanMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed') }) Cesium.RadarScanMaterialProperty = RadarScanMaterialProperty; Cesium.Material.RadarScanMaterialProperty = 'RadarScanMaterialProperty'; Cesium.Material.RadarScanMaterialType = 'RadarScanMaterialType'; Cesium.Material.RadarScanMaterialSource = ` uniform vec4 color; uniform float speed; #define PI 3.14159265359 czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; vec2 scrPt = st * 2.0 - 1.0; float time = czm_frameNumber * speed / 1000.0 ; vec3 col = vec3(0.0); mat2 rot; float theta = -time * 1.0 * PI - 2.2; float cosTheta, sinTheta; cosTheta = cos(theta); sinTheta = sin(theta); rot[0][0] = cosTheta; rot[0][1] = -sinTheta; rot[1][0] = sinTheta; rot[1][1] = cosTheta; vec2 scrPtRot = rot * scrPt; float angle = 1.0 - (atan(scrPtRot.y, scrPtRot.x) / 6.2831 + 0.5); float falloff = length(scrPtRot); material.alpha = pow(length(col + vec3(.5)),5.0); material.diffuse = (0.5 + pow(angle, 2.0) * falloff ) * color.rgb ; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.RadarScanMaterialType, { fabric: { type: Cesium.Material.RadarScanMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0 }, source: Cesium.Material.RadarScanMaterialSource }, translucent: function (material) { return true; } }) } // 轨迹球体 initellipsoidTrailMaterialProperty() { Object.defineProperties(EllipsoidTrailMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed') }) Cesium.EllipsoidTrailMaterialProperty = EllipsoidTrailMaterialProperty; Cesium.Material.EllipsoidTrailMaterialProperty = 'EllipsoidTrailMaterialProperty'; Cesium.Material.EllipsoidTrailMaterialType = 'EllipsoidTrailMaterialType'; Cesium.Material.EllipsoidTrailMaterialSource = ` uniform vec4 color; uniform float speed; czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; float time = fract(czm_frameNumber * speed / 1000.0); float alpha = abs(smoothstep(0.5,1.,fract( -st.t - time))); alpha += .1; material.alpha = alpha; material.diffuse = color.rgb; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.EllipsoidTrailMaterialType, { fabric: { type: Cesium.Material.EllipsoidTrailMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0 }, source: Cesium.Material.EllipsoidTrailMaterialSource }, translucent: function (material) { return true } }) } // 电弧球体 initEllipsoidElectricMaterialProperty() { Object.defineProperties(EllipsoidElectricMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), speed: Cesium.createPropertyDescriptor('speed') }) Cesium.EllipsoidElectricMaterialProperty = EllipsoidElectricMaterialProperty; Cesium.Material.EllipsoidElectricMaterialProperty = 'EllipsoidElectricMaterialProperty'; Cesium.Material.EllipsoidElectricMaterialType = 'EllipsoidElectricMaterialType'; Cesium.Material.EllipsoidElectricMaterialSource = ` uniform vec4 color; uniform float speed; #define pi 3.1415926535 #define PI2RAD 0.01745329252 #define TWO_PI (2. * PI) float rands(float p){ return fract(sin(p) * 10000.0); } float noise(vec2 p){ float time = fract( czm_frameNumber * speed / 1000.0); float t = time / 20000.0; if(t > 1.0) t -= floor(t); return rands(p.x * 14. + p.y * sin(t) * 0.5); } vec2 sw(vec2 p){ return vec2(floor(p.x), floor(p.y)); } vec2 se(vec2 p){ return vec2(ceil(p.x), floor(p.y)); } vec2 nw(vec2 p){ return vec2(floor(p.x), ceil(p.y)); } vec2 ne(vec2 p){ return vec2(ceil(p.x), ceil(p.y)); } float smoothNoise(vec2 p){ vec2 inter = smoothstep(0.0, 1.0, fract(p)); float s = mix(noise(sw(p)), noise(se(p)), inter.x); float n = mix(noise(nw(p)), noise(ne(p)), inter.x); return mix(s, n, inter.y); } float fbm(vec2 p){ float z = 2.0; float rz = 0.0; vec2 bp = p; for(float i = 1.0; i < 6.0; i++){ rz += abs((smoothNoise(p) - 0.5)* 2.0) / z; z *= 2.0; p *= 2.0; } return rz; } czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; vec2 st2 = materialInput.st; float time = fract( czm_frameNumber * speed / 1000.0); if (st.t < 0.5) { discard; } st *= 4.; float rz = fbm(st); st /= exp(mod( time * 2.0, pi)); rz *= pow(15., 0.9); vec4 temp = vec4(0); temp = mix( color / rz, vec4(color.rgb, 0.1), 0.2); if (st2.s < 0.05) { temp = mix(vec4(color.rgb, 0.1), temp, st2.s / 0.05); } if (st2.s > 0.95){ temp = mix(temp, vec4(color.rgb, 0.1), (st2.s - 0.95) / 0.05); } material.diffuse = temp.rgb; material.alpha = temp.a * 2.0; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.EllipsoidElectricMaterialType, { fabric: { type: Cesium.Material.EllipsoidElectricMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), speed: 10.0 }, source: Cesium.Material.EllipsoidElectricMaterialSource }, translucent: function (material) { return true; } }) } // 流动线 initTrailLineMaterial() { Object.defineProperties( PolylineTrailLinkMaterialPropertyRoad1.prototype, { isConstant: { get: function () { return false; } }, definitionChanged: { get: function () { return this._definitionChanged; } }, color: Cesium.createPropertyDescriptor("color") } ); PolylineTrailLinkMaterialPropertyRoad1.prototype.getType = function (time) { return "PolylineTrailLink"; }; PolylineTrailLinkMaterialPropertyRoad1.prototype.getValue = function (time, result) { if (!Cesium.defined(result)) { result = {}; } result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color); result.time = ((new Date().getTime() - this._time) % this.duration) / this.duration; return result; }; PolylineTrailLinkMaterialPropertyRoad1.prototype.equals = function (other) { return (this === other || (other instanceof PolylineTrailLinkMaterialPropertyRoad1 && Cesium.Property.equals(this._color, other._color))); }; Cesium.PolylineTrailLinkMaterialPropertyRoad1 = PolylineTrailLinkMaterialPropertyRoad1; Cesium.Material.PolylineTrailLinkType = "PolylineTrailLink"; Cesium.Material.PolylineTrailLinkImage = this.image; //道路样式的png Cesium.Material.PolylineTrailLinkSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\ {\n\ czm_material material = czm_getDefaultMaterial(materialInput);\n\ vec2 st = materialInput.st;\n\ vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\ material.alpha = colorImage.a * color.a;\n\ material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\ return material;\n\ }"; Cesium.Material._materialCache.addMaterial( Cesium.Material.PolylineTrailLinkType, { fabric: { type: Cesium.Material.PolylineTrailLinkType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 0.5), image: Cesium.Material.PolylineTrailLinkImage, time: 0, constantSpeed: 300, depthFailMaterial: true }, source: Cesium.Material.PolylineTrailLinkSource }, translucent: function (material) { return true; } } ); } // 发光墙体 initWallDiffuse() { Object.defineProperties(WallDiffuseMaterialProperty.prototype, { color: Cesium.createPropertyDescriptor('color'), }) Cesium.WallDiffuseMaterialProperty = WallDiffuseMaterialProperty; Cesium.Material.WallDiffuseMaterialProperty = 'WallDiffuseMaterialProperty'; Cesium.Material.WallDiffuseMaterialType = 'WallDiffuseMaterialType'; Cesium.Material.WallDiffuseMaterialSource = ` uniform vec4 color; czm_material czm_getMaterial(czm_materialInput materialInput){ czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; material.diffuse = color.rgb * 2.0; material.alpha = color.a * (1.0 - fract(st.t)) * 0.8; return material; } ` Cesium.Material._materialCache.addMaterial(Cesium.Material.WallDiffuseMaterialType, { fabric: { type: Cesium.Material.WallDiffuseMaterialType, uniforms: { color: new Cesium.Color(1.0, 0.0, 0.0, 1.0), }, source: Cesium.Material.WallDiffuseMaterialSource }, translucent: function (material) { return true; } }) } // 动态墙 initPolylineTrailLink() { Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, { isConstant: { get: function () { return false; } }, definitionChanged: { get: function () { return this._definitionChanged; } }, color: Cesium.createPropertyDescriptor('color') }); PolylineTrailLinkMaterialProperty.prototype.getType = function (time) { return 'PolylineTrailLink'; }; PolylineTrailLinkMaterialProperty.prototype.getValue = function (time, result) { if (!Cesium.defined(result)) { result = {}; } result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color); if (this.trailImage) { result.image = this.trailImage; } else { result.image = Cesium.Material.PolylineTrailLinkImage } if (this.duration) { result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration; } viewer.scene.requestRender(); return result; }; PolylineTrailLinkMaterialProperty.prototype.equals = function (other) { return this === other || (other instanceof PolylineTrailLinkMaterialProperty && Cesium.Property.equals(this._color, other._color)) }; Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty; Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink'; Cesium.Material.PolylineTrailLinkImage = "./static/image/colors.png"; Cesium.Material.PolylineTrailLinkSource = `czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st; vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t)); vec4 fragColor; fragColor.rgb = color.rgb / 1.0; fragColor = czm_gammaCorrect(fragColor); material.alpha = colorImage.a * color.a; material.diffuse = color.rgb; material.emission = fragColor.rgb; return material; }`; Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, { fabric: { type: Cesium.Material.PolylineTrailLinkType, uniforms: { color: new Cesium.Color(1.0, 1.0, 1.0, 1), image: Cesium.Material.PolylineTrailLinkImage, time: 0 }, source: Cesium.Material.PolylineTrailLinkSource }, translucent: function (material) { return true; } }); } } // 穿梭发光线 function Spriteline1MaterialProperty(duration, image) { this._definitionChanged = new Cesium.Event() this.duration = duration this.image = image this._time = performance.now() } /** * 六边形扩散材质 * @date:2022-01-12 */ function HexagonSpreadMaterialProperty(color) { this._definitionChanged = new Cesium.Event() this._color = undefined this._colorSubscription = undefined this.color = color this._time = new Date().getTime() } /**发光线圈 */ function ScanlineMaterialProperty(color, speed) { this._definitionChanged = new Cesium.Event() this.color = color || Cesium.Color.YELLOW this.speed = speed || 10 } /**流动线 */ function PolylineTrailLinkMaterialPropertyRoad1(image, color, duration) { this._definitionChanged = new Cesium.Event(); this._color = undefined; this._colorSubscription = undefined; this.color = color; this.image = image this.duration = duration; this._time = new Date().getTime(); } /**动态扩散墙 */ function PolylineTrailLinkMaterialProperty(options) { this._definitionChanged = new Cesium.Event(); this._color = undefined; this._colorSubscription = undefined; this.color = options.color; this.duration = options.duration; this.trailImage = options.trailImage; this._time = (new Date()).getTime(); } export default CustomMaterial