浏览代码

add @dc-modules/weather

tags/2.0.0
Caven Chen 4 年前
父节点
当前提交
fc860952ef
共有 5 个文件被更改,包括 424 次插入0 次删除
  1. 52
    0
      modules/weather/Weather.js
  2. 122
    0
      modules/weather/type/Cloud.js
  3. 98
    0
      modules/weather/type/Fog.js
  4. 76
    0
      modules/weather/type/Rain.js
  5. 76
    0
      modules/weather/type/Snow.js

+ 52
- 0
modules/weather/Weather.js 查看文件

@@ -0,0 +1,52 @@
/**
* @Author: Caven
* @Date: 2020-11-30 20:54:58
*/

import Fog from './type/Fog'
import Rain from './type/Rain'
import Snow from './type/Snow'
import Cloud from './type/Cloud'

class Weather {
constructor() {
this._comps = {
fog: new Fog(),
rain: new Rain(),
snow: new Snow(),
cloud: new Cloud()
}
}

get fog() {
return this._comps.fog
}

get rain() {
return this._comps.rain
}

get snow() {
return this._comps.snow
}

get cloud() {
return this._comps.cloud
}

/**
*
* @param viewer
*/
install(viewer) {
Object.keys(this._comps).forEach(key => {
this._comps[key].addTo(viewer)
})
Object.defineProperty(viewer, 'weather', {
value: this,
writable: false
})
}
}

export default Weather

+ 122
- 0
modules/weather/type/Cloud.js 查看文件

@@ -0,0 +1,122 @@
/**
* @Author: Caven
* @Date: 2020-11-30 20:19:19
*/

const { State, Util } = DC

const { Cesium } = DC.Namespace

const IMG = require('@dc-modules/images/cloud.jpg')

class Cloud {
constructor() {
this._id = Util.uuid()
this._viewer = undefined
this._delegate = undefined
this._rotateAmount = 0
this._enable = false
this.type = 'cloud'
this._heading = 0
this._state = State.INITIALIZED
}

set enable(enable) {
if (!this._viewer.scene.mode === Cesium.SceneMode.SCENE3D) {
return this
}
this._enable = this._delegate.show = enable
if (this._enable) {
this._viewer.scene.postUpdate.addEventListener(this._onRotate, this)
} else {
this._viewer.scene.postUpdate.removeEventListener(this._onRotate, this)
}
return this
}

get enable() {
return this._enable
}

set rotateAmount(rotateAmount) {
this._rotateAmount = rotateAmount
return this
}

get rotateAmount() {
return this._rotateAmount
}

/**
*
* @param scene
* @param time
* @private
*/
_onRotate(scene, time) {
if (this._rotateAmount === 0) {
return
}
this._heading += this._rotateAmount
if (this._heading >= 360 || this._heading <= -360) {
this._heading = 0
}
this._delegate.modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(
new Cesium.Cartesian3(),
new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(this._heading), 0, 0)
)
}

/**
*
* @private
*/
_createPrimitive() {
this._delegate = new Cesium.Primitive({
appearance: new Cesium.EllipsoidSurfaceAppearance({
material: new Cesium.Material({
fabric: {
type: 'Image',
uniforms: {
color: new Cesium.Color(1.0, 1.0, 1.0, 1.0),
image: IMG
},
components: {
alpha:
'texture2D(image, fract(repeat * materialInput.st)).r * color.a',
diffuse: 'vec3(1.0)'
},
translucent: true,
aboveGround: true
}
})
})
})
this._delegate.geometryInstances = new Cesium.GeometryInstance({
geometry: new Cesium.EllipsoidGeometry({
vertexFormat: Cesium.VertexFormat.POSITION_AND_ST,
radii: this._viewer.scene.globe.ellipsoid.radii
}),
id: this._id
})
this._delegate.show = this._enable
this._viewer.scene.primitives.add(this._delegate)
}

/**
*
* @param viewer
* @returns {Cloud}
*/
addTo(viewer) {
if (!viewer) {
return this
}
this._viewer = viewer
this._createPrimitive()
this._state = State.ADDED
return this
}
}

export default Cloud

+ 98
- 0
modules/weather/type/Fog.js 查看文件

@@ -0,0 +1,98 @@
/**
* @Author: Caven
* @Date: 2020-02-26 23:05:44
*/

const { State, Util } = DC

const { Cesium } = DC.Namespace

const FogShader = require('@dc-modules/material/shader/weather/FogShader.glsl')

class Fog {
constructor() {
this._id = Util.uuid()
this._viewer = undefined
this._delegate = undefined
this._enable = false
this._fogByDistance = { near: 10, nearValue: 0, far: 2000, farValue: 1.0 }
this._color = new Cesium.Color(0, 0, 0, 1)
this.type = 'fog'
this._state = State.INITIALIZED
}

set enable(enable) {
this._enable = enable
if (enable && this._viewer && !this._delegate) {
this._createPostProcessStage()
}
this._delegate && (this._delegate.enabled = enable)
return this
}

get enable() {
return this._enable
}

set fogByDistance(fogByDistance) {
this._fogByDistance = fogByDistance
this._delegate &&
(this._delegate.uniforms.fogByDistance = new Cesium.Cartesian4(
this._fogByDistance?.near || 10,
this._fogByDistance?.nearValue || 0.0,
this._fogByDistance?.far || 2000,
this._fogByDistance?.farValue || 1.0
))
return this
}

get fogByDistance() {
return this._fogByDistance
}

set color(color) {
this._color = color
this._delegate && (this._delegate.uniforms.fogColor = color)
}

get color() {
return this._color
}

/**
*
* @private
*/
_createPostProcessStage() {
this._delegate = new Cesium.PostProcessStage({
name: this._id,
fragmentShader: FogShader,
uniforms: {
fogByDistance: new Cesium.Cartesian4(
this._fogByDistance?.near || 10,
this._fogByDistance?.nearValue || 0.0,
this._fogByDistance?.far || 200,
this._fogByDistance?.farValue || 1.0
),
fogColor: this._color
}
})
this._viewer.scene.postProcessStages.add(this._delegate)
}

/**
*
* @param viewer
* @returns {Fog}
*/
addTo(viewer) {
if (!viewer) {
return this
}
this._viewer = viewer
this._state = State.ADDED
return this
}
}

export default Fog

+ 76
- 0
modules/weather/type/Rain.js 查看文件

@@ -0,0 +1,76 @@
/**
* @Author: Caven
* @Date: 2020-01-15 20:23:42
*/

const { State, Util } = DC

const { Cesium } = DC.Namespace

const RainShader = require('@dc-modules/material/shader/weather/RainShader.glsl')

class Rain {
constructor() {
this._id = Util.uuid()
this._viewer = undefined
this._delegate = undefined
this._enable = false
this._speed = 10.0
this.type = 'rain'
this._state = State.INITIALIZED
}

set enable(enable) {
this._enable = enable
if (enable && this._viewer && !this._delegate) {
this._createPostProcessStage()
}
this._delegate && (this._delegate.enabled = enable)
return this
}

get enable() {
return this._enable
}

set speed(speed) {
this._speed = speed
this._delegate && (this._delegate.uniforms.speed = speed)
return this
}

get speed() {
return this._speed
}

/**
*
* @private
*/
_createPostProcessStage() {
this._delegate = new Cesium.PostProcessStage({
name: this._id,
fragmentShader: RainShader,
uniforms: {
speed: this._speed
}
})
this._viewer.scene.postProcessStages.add(this._delegate)
}

/**
*
* @param viewer
* @returns {Rain}
*/
addTo(viewer) {
if (!viewer) {
return this
}
this._viewer = viewer
this._state = State.ADDED
return this
}
}

export default Rain

+ 76
- 0
modules/weather/type/Snow.js 查看文件

@@ -0,0 +1,76 @@
/**
* @Author: Caven
* @Date: 2020-01-15 20:23:46
*/

const { State, Util } = DC

const { Cesium } = DC.Namespace

const SnowShader = require('@dc-modules/material/shader/weather/SnowShader.glsl')

class Snow {
constructor() {
this._id = Util.uuid()
this._viewer = undefined
this._delegate = undefined
this._enable = false
this._speed = 10.0
this.type = 'snow'
this._state = State.INITIALIZED
}

set enable(enable) {
this._enable = enable
if (enable && this._viewer && !this._delegate) {
this._createPostProcessStage()
}
this._delegate && (this._delegate.enabled = enable)
return this
}

get enable() {
return this._enable
}

set speed(speed) {
this._speed = speed
this._delegate && (this._delegate.uniforms.speed = speed)
return this
}

get speed() {
return this._speed
}

/**
*
* @private
*/
_createPostProcessStage() {
this._delegate = new Cesium.PostProcessStage({
name: this._id,
fragmentShader: SnowShader,
uniforms: {
speed: this._speed
}
})
this._viewer.scene.postProcessStages.add(this._delegate)
}

/**
*
* @param viewer
* @returns {Snow}
*/
addTo(viewer) {
if (!viewer) {
return this
}
this._viewer = viewer
this._state = State.ADDED
return this
}
}

export default Snow

正在加载...
取消
保存