无人机管理后台前端(已迁走)
张含笑
2025-09-01 2ca94de8ede18ac07ccfd8dec7b6f6a707adde9b
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// * 创建视锥
import * as Cesium from 'cesium'
 
function computeFarCenter (position, hpr, far) {
    const quaternion = Cesium.Transforms.headingPitchRollQuaternion(position, hpr)
    const rotMat = Cesium.Matrix3.fromQuaternion(quaternion)
    const dirLocal = new Cesium.Cartesian3(0, 0, far)
    const dirWorld = Cesium.Matrix3.multiplyByVector(rotMat, dirLocal, new Cesium.Cartesian3())
    return Cesium.Cartesian3.add(position, dirWorld, new Cesium.Cartesian3())
}
 
/* eslint-disable new-cap */
/* eslint-disable no-undef */
export default class CreateFrustum {
    constructor(viewer, options) {
        this._isShowVideoPlan = options.isShowVideoPlan || false
        this.position = options.position
        this._viewer = viewer
 
        this.fov = options.fov || 0
        this.near = options.near || 0.01
        this.far = options.far || 0
 
        this.heading = options.heading || 0
        this.pitch = options.pitch || 0
        this.roll = options.roll || 0
 
        this.width = options.width
        this.height = options.height
 
        this.videoScreen = null
 
 
        this.cameraPosition = Cesium.Cartesian3.fromDegrees(
            this.position.longitude,
            this.position.latitude,
            this.position.altitude
        )
        this.hpr = Cesium.HeadingPitchRoll.fromDegrees(this.heading, this.pitch, this.roll)
        this.orientation = Cesium.Transforms.headingPitchRollQuaternion(this.cameraPosition, this.hpr)
 
        this._entityVideoDataSource = null
        this._entityVideoDataSource = new Cesium.CustomDataSource('entityVideoDataSource')
        this._viewer.dataSources.add(this._entityVideoDataSource)
 
        this._primitiveCollection = null
        this._primitiveCollection = new Cesium.PrimitiveCollection()
        this._viewer.scene.primitives.add(this._primitiveCollection)
 
        this.add()
    }
 
    // 创建视锥体和轮廓线
    add () {
        this.clear()
 
        this._viewer.scene.globe.depthTestAgainstTerrain = true
 
        this.addFrustum()
        this.addOutline()
        this.addCenterline()
    }
 
    // 创建视锥体
    addFrustum () {
        const frustum = new Cesium.PerspectiveFrustum({
            // 查看的视场角,绕Z轴旋转,以弧度方式输入
            fov: Cesium.Math.toRadians(this.fov),
            // 视锥体的宽度/高度
            aspectRatio: this.width / this.height,
            // 近面距视点的距离
            near: this.near,
            // 远面距视点的距离
            far: this.far,
        })
 
        const frustumGeometry = new Cesium.FrustumGeometry({
            frustum: frustum,
            origin: this.cameraPosition,
            orientation: this.orientation,
            vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
        })
 
        const instance = new Cesium.GeometryInstance({
            geometry: frustumGeometry,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    Cesium.Color.fromBytes(0, 213, 144, 20)
                ),
            },
        })
 
        const primitive = new Cesium.Primitive({
            geometryInstances: instance,
            releaseGeometryInstances: false,
            appearance: new Cesium.PerInstanceColorAppearance({
                closed: true,
                flat: true,
                translucent: true
            }),
            asynchronous: false,
        })
 
        this._primitiveCollection.add(primitive)
 
        // if (this.roll > 135) {
        //     this.initVideoPolygon(frustumGeometry)
        // }
    }
 
    // 创建轮廓线
    addOutline () {
        const frustum = new Cesium.PerspectiveFrustum({
            // 查看的视场角度,绕Z轴旋转,以弧度方式输入
            // The angle of the field of view (FOV), in radians.
            // This angle will be used as the horizontal FOV if the width is greater than the height, otherwise it will be the vertical FOV.
            fov: Cesium.Math.toRadians(this.fov),
            // 视锥体的宽度/高度
            aspectRatio: this.width / this.height,
            // 近面距视点的距离
            near: this.near,
            // 远面距视点的距离
            far: this.far,
        })
 
        const frustumGeometry = new Cesium.FrustumOutlineGeometry({
            frustum: frustum,
            origin: this.cameraPosition,
            orientation: this.orientation,
            vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
        })
 
        const instance = new Cesium.GeometryInstance({
            geometry: frustumGeometry,
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    Cesium.Color.fromBytes(0, 213, 144, 255)
                ),
            },
        })
 
        const primitive = new Cesium.Primitive({
            geometryInstances: instance,
            appearance: new Cesium.PerInstanceColorAppearance({
                closed: true,
                flat: true,
                translucent: true
            }),
            asynchronous: false,
        })
 
        this._primitiveCollection.add(primitive)
    }
 
    addCenterline () {
        let matrix = Cesium.Matrix3.fromQuaternion(
            this.orientation,
            new Cesium.Matrix3()
        )
 
        let right1 = Cesium.Matrix3.getColumn(matrix, 0, new Cesium.Cartesian3())
        let up1 = Cesium.Matrix3.getColumn(matrix, 1, new Cesium.Cartesian3())
        let direction = Cesium.Matrix3.getColumn(matrix, 2, new Cesium.Cartesian3())
 
        let frontDirect = Cesium.Cartesian3.multiplyByScalar(
            direction,
            this.far,
            new Cesium.Cartesian3()
        )
        let bottomCenter = Cesium.Cartesian3.add(this.cameraPosition, frontDirect, new Cesium.Cartesian3())
 
        // 创建Entity来显示中心线
        this._entityVideoDataSource?.entities.add({
            name: 'Center Line of Frustum',
            polyline: {
                positions: [this.cameraPosition, bottomCenter],
                width: 1,
                // material: Cesium.Color.RED.withAlpha(0.5)
                material: new Cesium.PolylineDashMaterialProperty({
                    color: Cesium.Color.fromBytes(0, 213, 144, 255),
                    dashLength: 30.0,
                    gapColor: Cesium.Color.TRANSPARENT,
                    gapWidth: 1.0,
                }),
            },
        })
 
        const ray = new Cesium.Ray()
        ray.origin = this.cameraPosition
        // 计算方向向量(终点 - 起点)并归一化
        ray.direction = Cesium.Cartesian3.normalize(
            Cesium.Cartesian3.subtract(bottomCenter, this.cameraPosition, new Cesium.Cartesian3()),
            new Cesium.Cartesian3()
        )
 
        const pickPosition = this._viewer.scene.globe.pick(ray, this._viewer.scene)
        let distance = null
 
        if (pickPosition) {
            // 计算交点与A、B的距离
            distance = Cesium.Cartesian3.distance(this.cameraPosition, pickPosition)
        }
 
        // 视椎体投放视频
        // if (this.roll <= 135) {
 
        // this._isShowVideoPlan && this.initVideoPlan(distance)
 
        // }
    }
 
    initVideoPlan (distance = null) {
        const element = document.querySelector('.video-player')
 
        if (element === '') return
 
 
        let curFar = distance != null && distance < this.far ? distance - 10 : this.far
 
        let width = curFar * Math.tan(Cesium.Math.toRadians(this.fov) * 0.5) * 2
        let height = width / (this.width / this.height)
 
        this._entityVideoDataSource.entities.add({
            position: new Cesium.CallbackProperty(() => {
                return computeFarCenter(this.cameraPosition, this.hpr, curFar)
            }, false),
            orientation: new Cesium.CallbackProperty(() => {
                return this.orientation
            }, false),
            plane: {
                plane: new Cesium.Plane(new Cesium.Cartesian3(0, 0, -1), 0),
                dimensions: new Cesium.Cartesian2(width, height),
                material: element,
            },
        })
    }
 
    initVideoPolygon (frustumGeometry) {
        const element = document.querySelector('.video-player')
 
        if (element === '') return
 
        // 取出视锥体的顶点坐标
        const geometry = Cesium.FrustumOutlineGeometry.createGeometry(frustumGeometry)
 
        const result = []
        for (let i = 0; i < geometry.attributes.position.values.length; i += 3) {
            result.push(Array.from(geometry.attributes.position.values).slice(i, i + 3))
        }
 
        // 将顶点分为近面(前4个)和远面(后4个)
        const nearVertices = result.slice(0, 4)
        const farVertices = result.slice(4, 8)
 
        let videoPolygon = []
        let videoIntersectionPolygon = []
 
        // 处理四条棱线(近面顶点到远面对应顶点)
        for (let i = 0; i < 4; i++) {
            const nearVertex = new Cesium.Cartesian3(...nearVertices[i])
            const farVertex = new Cesium.Cartesian3(...farVertices[i])
 
            const ray = new Cesium.Ray()
            ray.origin = nearVertex
            // 计算方向向量(终点 - 起点)并归一化
            ray.direction = Cesium.Cartesian3.normalize(
                Cesium.Cartesian3.subtract(farVertex, nearVertex, new Cesium.Cartesian3()),
                new Cesium.Cartesian3()
            )
 
            // 检测射线与地球表面交点
            const pickPosition = this._viewer.scene.globe.pick(ray, this._viewer.scene)
 
            // 有交点则使用交点,否则使用远面顶点
            if (pickPosition) {
                videoIntersectionPolygon.push(pickPosition)
            }
 
            videoPolygon.push(farVertex)
        }
 
        // 获得视锥体和地面的交点,构成平面,并使用video标签做为材质
        if (videoPolygon.length === 4) {
            if (videoIntersectionPolygon.length < 4) {
                this._entityVideoDataSource.entities.add({
                    polygon: {
                        hierarchy: new Cesium.PolygonHierarchy(videoPolygon),
                        material: element,
                        stRotation: +Cesium.Math.toRadians(this.heading),
                        perPositionHeight: true,
                        heightReference: Cesium.HeightReference.NONE
                    },
 
                    orientation: new Cesium.CallbackProperty(() => {
                        return this.orientation
                    }, false),
                })
            } else {
                this._entityVideoDataSource.entities.add({
                    polygon: {
                        hierarchy: new Cesium.PolygonHierarchy(videoIntersectionPolygon),
                        material: element,
                        stRotation: Cesium.Math.toRadians(135),
                        perPositionHeight: false,
                        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                    },
 
                    // orientation: new Cesium.CallbackProperty(() => {
                    //     return this.orientation
                    // }, false),
                })
            }
        }
 
    }
 
    // 清除视锥体和轮廓线
    clear () {
        if (this._viewer) {
            this._viewer.scene.globe.depthTestAgainstTerrain = false
 
            if (this._primitiveCollection) {
                this._primitiveCollection.removeAll()
            }
 
            if (this._entityVideoDataSource) {
                this._entityVideoDataSource?.entities.removeAll()
            }
        }
    }
 
    destroyed () {
        if (this._viewer) {
            this._viewer.scene.globe.depthTestAgainstTerrain = false
 
            if (this._primitiveCollection) {
                this._primitiveCollection.removeAll()
                this._viewer.scene.primitives.remove(this._primitiveCollection)
                this._primitiveCollection = null
            }
 
            if (this._entityVideoDataSource) {
                this._entityVideoDataSource?.entities.removeAll()
                this._viewer.dataSources.remove(this._entityVideoDataSource, true)
                this._entityVideoDataSource = null
            }
        }
    }
 
    UpdateWidth (value) {
        this.width = value
        this.add()
    }
    UpdateHeight (value) {
        this.height = value
        this.add()
    }
    UpdateFov (value) {
        this.fov = value
        this.add()
    }
    UpdateNear (value) {
        this.near = value
        this.add()
    }
    UpdateFar (value) {
        this.far = value
        this.add()
    }
    UpdateHeading (value) {
        this.heading = value
        this.add()
    }
    UpdatePitch (value) {
        this.pitch = value
        this.add()
    }
    UpdateRoll (value) {
        this.roll = value
        this.add()
    }
    UpdataPosition (position) {
        if (!position) return
        this.position = position
        this.add()
    }
 
    // 更新视锥体的姿态
    Update (position, headingPitchRoll) {
        this.position = Cesium.Cartesian3.fromDegrees(
            position.longitude,
            position.latitude,
            position.altitude
        )
        this.orientation = Cesium.Transforms.headingPitchRollQuaternion(
            this.position,
            new Cesium.HeadingPitchRoll.fromDegrees(
                headingPitchRoll.heading,
                headingPitchRoll.pitch,
                headingPitchRoll.roll
            )
        )
        this.add()
    }
}