2023西安数博会CIM演示-【前端】-Web
AdaKing88
2023-08-21 bc03b832caa49bbcd2674fe4cae3701b5059bf95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
 
 
 
function addMaterial() {
 
    Cesium.Material._materialCache.addMaterial("SEWater",
        {
            fabric: {
                type: "SEWater",
                uniforms: {
                    baseWaterColor: new Cesium.Color(0.2, 0.3, 0.6, 0x1),
                    blendColor: new Cesium.Color(0.5, 0.5, 0.5, 0.7),
                    specularMap: Cesium.Material.DefaultImageId,
                    normalMap: Cesium.Material.DefaultImageId,
                    frequency: 0x1388,
                    animationSpeed: 0.05,
                    amplitude: 0x2,
                    specularIntensity: 0.5,
                    fadeFactor: 0x1,
                    sizeAndVelocity: new Cesium.Cartesian4(0x64, 0x64, 0xa, 0x0)
                },
                source: waterSource,
            },
            translucent: function(data) {
                var uniforms = data.uniforms;
                return uniforms.baseWaterColor.alpha < 1 || uniforms.blendColor.alpha < 1;
 
            },
        });
}
let viewShadowMap;
let _createShadowMap = function() {
    let Camera = new Cesium.Camera(Viewer.scene);
 
    Camera.frustum.fov = Cesium.Math.toRadians(60);
    Camera.frustum.aspectRatio = 60 / 40;
    Camera.frustum.near = 1;
    Camera.frustum.far = 30;
 
 
    //设置方向角
    Camera.setView({
        destination: Cesium.Cartesian3.fromDegrees(108.958524, 34.220926, 1000.0), // 设置位置
        orientation: {
            heading: Cesium.Math.toRadians(45), // 方向角
            pitch: Cesium.Math.toRadians(0),// 俯仰角
            roll: 0   //翻滚角
        }
    });
 
 
    let scratchRight = new Cesium.Cartesian3();
    let scratchRotation = new Cesium.Matrix3();
    let scratchOrientation = new Cesium.Quaternion();
 
    let up = Camera.upWC;
    let right = Camera.rightWC;
    right = Cesium.Cartesian3.negate(right, scratchRight);
 
    let rotation = scratchRotation;
    Cesium.Matrix3.setColumn(rotation, 0, right, rotation);
    Cesium.Matrix3.setColumn(rotation, 1, up, rotation);
    Cesium.Matrix3.setColumn(rotation, 2, Camera.direction, rotation);
 
    //计算视锥姿态
    let orientationData = Cesium.Quaternion.fromRotationMatrix(rotation, scratchOrientation);
 
    let positions = new Float64Array(3 * 4 * 2);
    Cesium.FrustumGeometry._computeNearFarPlanes(
        Camera.positionWC,
        orientationData,
        0,
        Camera.frustum,
        positions
    );
    let _position = new Cesium.Cartesian3();
    for (let i = 12; i < positions.length; i += 3) {
        _position.x += positions[i]
        _position.y += positions[i + 1]
        _position.z += positions[i + 2]
    }
    Cesium.Cartesian3.multiplyByScalar(_position, 1 / 4, _position);
 
 
    viewShadowMap = new Cesium.ShadowMap({
        lightCamera: Camera,
        enabled: false,
        isPointLight: false,
        isSpotLight: true,
        cascadesEnabled: !1,
        context: Viewer.scene.context,
        pointLightRadius: 30
    })
    //viewShadowMap.guid = Cesium.createGuid();
    Viewer.scene.frameState.shadowMaps.push(viewShadowMap) // *重点* 多投影
    //  return viewShadowMap._shadowMapTexture;
}
 
let perspectiveFrustum = new Cesium.PerspectiveFrustum();
let perspectiveOffCenterFrustum = new Cesium.PerspectiveOffCenterFrustum();
let orthographicFrustum = new Cesium.OrthographicFrustum();
let orthographicOffCenterFrustum = new Cesium.OrthographicOffCenterFrustum();
function executeDrawCommand(drawCommand, scene, context, passState, state) {
    let frameState = scene._frameState;
    if (!Cesium.defined(scene.debugCommandFilter) || scene.debugCommandFilter(drawCommand)) {
        if (drawCommand instanceof Cesium.ClearCommand) {
            drawCommand.execute(context, passState);
        } else {
            if (frameState.useLogDepth && Cesium.defined(drawCommand.derivedCommands.logDepth)) {
                drawCommand = drawCommand.derivedCommands.logDepth.command;
            }
            let passes = orthographicFrustum.passes;
            if (!passes.pick && scene._hdr && Cesium.defined(drawCommand.derivedCommands) && Cesium.defined(drawCommand.derivedCommands.hdr)) {
                drawCommand = drawCommand.derivedCommands.hdr.command;
            }
            if (!passes.pick && !passes.depth && !scene.debugShowCommands && !scene.debugShowFrustums) {
                drawCommand.execute(context, passState);
            }
        }
    }
 
    var _0x5364b6 = scene._frameState
    if (!Cesium.defined(scene.debugCommandFilter) || scene.debugCommandFilter(drawCommand))
        if (drawCommand instanceof Cesium.ClearCommand) drawCommand.execute(context, _0x24cd9d)
        else {
            _0x5364b6.useLogDepth && Cesium.defined(drawCommand.derivedCommands.logDepth) && (drawCommand = drawCommand.derivedCommands.logDepth.command)
            var _0x3efffa = _0x5364b6.passes
            !_0x3efffa.pick && scene._hdr && Cesium.defined(drawCommand.derivedCommands) && Cesium.defined(drawCommand.derivedCommands.hdr) && (drawCommand = drawCommand.derivedCommands.hdr.command), _0x3efffa.pick || _0x3efffa.depth || scene.debugShowCommands || scene.debugShowFrustums || drawCommand.execute(context, _0x24cd9d)
        }
 
 
}
function createTexture() {
 
    let wheight = 425;
 
 
    let modelTileState = Cesium.Cesium3DTilePassState && new Cesium.Cesium3DTilePassState({ pass: Cesium.Cesium3DTilePass.RENDER });
    let update = Cesium.OctahedralProjectedCubeMap.prototype.update;
 
 
 
 
 
    let scene = Viewer.scene;
    let camera = new Cesium.Camera(scene);
    let context = scene.context;
    let passState = scene._view.passState;
    camera = Cesium.Camera.clone(scene.camera, camera);
    let nPositionCartographic = camera.positionCartographic;
    let nPitch = camera.pitch;
    let nHeading = camera.heading;
    let nRoll = camera.roll;
 
    camera.setView({
        destination: Cesium.Cartesian3.fromRadians(nPositionCartographic.longitude, nPositionCartographic.latitude, wheight + wheight - nPositionCartographic.height),
        orientation: { heading: nHeading, pitch: -nPitch, roll: nRoll }
    });
 
 
    let dbWidth = 0.5 * scene.context.drawingBufferWidth;
    let dbHeight = 0.5 * scene.context.drawingBufferHeight;
    passState.viewport.x = 0;
    passState.viewport.y = 0;
    passState.viewport.width = dbWidth;
    passState.viewport.height = dbHeight;
    //_0x3c58e5.update(context, dbWidth, dbHeight),
    //    _0x3c58e5._colorTexture
    //passState.framebuffer = _0x3c58e5._framebuffer;
 
 
    let clearColorCommand = scene._clearColorCommand;
    Cesium.Color.multiplyByScalar(Cesium.Color.DEEPSKYBLUE, 0.1, clearColorCommand.color);
    clearColorCommand.color.alpha = 1;
    clearColorCommand.execute(context, passState);
 
 
    let depthClearCommand = scene._depthClearCommand;
    let uniformState = context.uniformState;
 
    uniformState.updateCamera(camera);
    uniformState.waterHeight = wheight;
    let frustum;
    if (Cesium.defined(camera.frustum.fov)) {
        frustum = camera.frustum.clone(perspectiveFrustum);
    } else {
        if (Cesium.defined(camera.frustum.infiniteProjectionMatrix)) {
            frustum = camera.frustum.clone(perspectiveOffCenterFrustum);
        } else {
            if (Cesium.defined(camera.frustum.width)) {
                frustum = camera.frustum.clone(orthographicFrustum);
            } else {
                frustum = camera.frustum.clone(orthographicOffCenterFrustum);
            }
        }
    }
    frustum.near = camera.frustum.near;
    frustum.far = camera.frustum.far;
 
    uniformState.updateFrustum(frustum);
 
 
    let frameState = scene._frameState
    frameState.passes.render = true;
    frameState.tilesetPassState = modelTileState;
    scene.frameState.commandList.length = 0;
    Cesium.OctahedralProjectedCubeMap.prototype.update = function() { };
    scene._primitives.update(frameState);
    Cesium.OctahedralProjectedCubeMap.prototype.update = update;
    scene._view.createPotentiallyVisibleSet(scene);
 
 
    let commands;
    let indices;
    let frustumCommandsList = scene._view.frustumCommandsList;
 
    for (let i = 0; i < frustumCommandsList.length; i++) {
        let index = frustumCommandsList.length - i - 1;
        let frustumCommands = frustumCommandsList[index];
        if (index !== 0) {
            frustum.near = frustumCommands.near * scene.opaqueFrustumNearOffset;
        } else {
            frustum.near = frustumCommands.near;
        }
        frustum.far = frustumCommands.far;
        uniformState.updateFrustum(frustum);
        depthClearCommand.execute(context, passState);
        uniformState.updatePass(Cesium.Pass.CESIUM_3D_TILE);
        commands = frustumCommands.commands[Cesium.Pass.CESIUM_3D_TILE];
        indices = frustumCommands.indices[Cesium.Pass.CESIUM_3D_TILE];
 
        for (let j = 0; j < indices.length; j++) {
            executeDrawCommand(commands[j], scene, context, passState);
        }
    }
    passState.framebuffer = 0;
    uniformState.xbsjWaterHeight = -5000;
}
function createMaterial() {
 
    Viewer._wrfbo = {
 
    };
    _createShadowMap();
    setInterval(() => {
        if (viewShadowMap) {
            Viewer._wrfbo._colorTexture = viewShadowMap._shadowMapTexture;
        }
 
    }, 1000);
 
    debugger;
    var waterMaterial = new Cesium.Material({
        fabric: {
            type: 'SEWater',
            uniforms: {
                specularMap: Cesium.buildModuleUrl('Assets/Textures/waterNormals.jpg'),
                normalMap: Cesium.buildModuleUrl('Assets/Textures/waterNormals.jpg'),
                frequency: 0x1388,
                animationSpeed: 0.05,
                amplitude: 0x2,
                width: 0x64,
                height: 0x64,
            },
        },
    });
 
    waterMaterial._uniforms['specularMap_0'] = function() {
        return Viewer._wrfbo._colorTexture || Viewer.scene.context.defaultTexture
    }
    return waterMaterial;
 
}
function createWaterPolygon(positions) {
    let pos = Cesium.Cartesian3.fromDegreesArrayHeights(positions);
 
    let geom = Cesium.CoplanarPolygonGeometry.fromPositions({
        vertexFormat: Cesium.MaterialAppearance.MaterialSupport.ALL.vertexFormat,
        positions: pos
    })
 
 
    let polygonInstance = new Cesium.GeometryInstance({
        geometry: geom
    });
    let primitive = new Cesium.Primitive({
        geometryInstances: polygonInstance,
        appearance: new Cesium.MaterialAppearance({ materialSupport: Cesium.MaterialAppearance.MaterialSupport.ALL })
    })
 
    Viewer.scene.primitives.add(primitive);
    primitive.appearance.material = createMaterial();
}
function createWater(positions) {
    createWaterPolygon(positions);
 
}