ansel0926
2022-05-16 e6c25766b95dfd792009423a5972cd43cbd7c3ce
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
//操作 倾斜影像数据的js 类库
function remove3dtileByPath(path) {
 
    if (base3dtilesPrimitives != []) {
        for (var i = 0; i < base3dtilesPrimitives.length; i++) {
            path2 = serverIP + path;
            if (base3dtilesPrimitives[i]._url == path2) {
                viewer.scene.primitives.remove(base3dtilesPrimitives[i]);
                //从数组中移除当前路径
                base3dtilesPrimitives.splice(i, 1);
            }
        }
    }
}
 
//卸载所有的3dtiles实景三维数据底图
function remove3dtilesmap() {
    if (base3dtilesPrimitives != []) {
        for (var i = 0; i < base3dtilesPrimitives.length; i++) {
            viewer.scene.primitives.remove(base3dtilesPrimitives[i]);
        }
        base3dtilesPrimitives = [];
    }
}
 
//判断是否存在
function is3dtileExsit(id,path) {
    if (base3dtilesPrimitives != []) {
        for (var i = 0; i < base3dtilesPrimitives.length; i++) {
            if (base3dtilesPrimitives[i]._id == id && base3dtilesPrimitives[i]._url == path) {
                return true;
            }
 
        }
    }
    return false;
}
/**
 * 添加 3dties
 * @returns options
 */
function add3dtiles(options) {
    var id = options.id;
    var path = options.url;
    if (is3dtileExsit(id,path)) {
        return;
    }
    var modelMatrix = Cesium.Matrix4.fromArray([
        1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0
    ]);
    //配置数据
    var tilesetOption = {
        id: options.id,
        url: options.url,
        maximumScreenSpaceError: 16,
        maximumNumberOfLoadedTiles: 2000,
        maximumMemoryUsage: 8192,
        skipLevels: 1,
        modelMatrix: modelMatrix//引入转移矩阵
    };
    var tileset = new Cesium.Cesium3DTileset(tilesetOption);
    tileset._id = options.id;
    tileset._tilesType = options.tilesType;
    tileset.loadJson = function (url) {
        var resource = Resource.createIfNeeded(tilesetUrl);
        return resource;
    }
    tileset.readyPromise.then(function () {
        var a = tileset.boundingSphere.center.clone();
        a.x += Number(options.x);
        a.y += Number(options.y);
        a.z += Number(options.z);
        var cartographic = Cesium.Cartographic.fromCartesian(a);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
        var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
        const cartographic2 = Cesium.Cartographic.fromCartesian(
            tileset.boundingSphere.center
        );
        const surface = Cesium.Cartesian3.fromRadians(
            cartographic2.longitude,
            cartographic2.latitude,
            0
        );
        const offset = Cesium.Cartesian3.fromRadians(
            cartographic.longitude,
            cartographic.latitude,
            options.z
        );
        const translation = Cesium.Cartesian3.subtract(
            offset,
            surface,
            new Cesium.Cartesian3()
        );
        var m = Cesium.Matrix4.fromTranslation(translation);
        tileset._root.transform[12] += m[12];
        tileset._root.transform[13] += m[13];
        tileset._root.transform[14] += m[14];
    })
    // console.log(tileset._root);
    //加载3dtiles数据
    base3dtilesPrimitives.push(viewer.scene.primitives.add(tileset));
    return tileset;
}
/**
 * 更新位置
 * @param {*} model 
 * @param {*} options 
 */
function update3dtilesMaxtrixByXYZ(model, options) {
 
    var mx = Cesium.Matrix4.fromArray([
        1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        options.x, options.y, options.z, 1.0
    ]);
    model._modelMatrix = mx;
}
 
 
//随机颜色
function randomRgbaColor() { //随机生成RGBA颜色
    var r = Math.floor(Math.random() * 256); //随机生成256以内r值
    var g = Math.floor(Math.random() * 256); //随机生成256以内g值
    var b = Math.floor(Math.random() * 256); //随机生成256以内b值
    var alpha = Math.random(); //随机生成1以内a值
    return "rgba(" + r + "," + g + "," + b + "," + 1 + ")"; //返回rgba(r,g,b,a)格式颜色
}
 
 
 
function update(url, lng, lat, height, head, pitch, roll, scaleparam) {
 
    var modelMatrix = Cesium.Matrix4.fromArray([
        1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0
    ]);
 
    var tilesetOption = {
        url: url,
        maximumScreenSpaceError: 16,
        maximumNumberOfLoadedTiles: 2000,
        maximumMemoryUsage: 8192,
        skipLevels: 1,
        modelMatrix: modelMatrix//引入转移矩阵
    };
    var tileset = new Cesium.Cesium3DTileset(tilesetOption);
 
    let mx = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(head));
    let my = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(pitch));
    let mz = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(roll));
    let rotationX = Cesium.Matrix4.fromRotationTranslation(mx);
    let rotationY = Cesium.Matrix4.fromRotationTranslation(my);
    let rotationZ = Cesium.Matrix4.fromRotationTranslation(mz);
    //平移
    let position = Cesium.Cartesian3.fromDegrees(lng, lat, height);
    let m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
 
    let scale = Cesium.Matrix4.fromUniformScale(scaleparam);
    // //缩放
    Cesium.Matrix4.multiply(m, scale, m);
    //旋转、平移矩阵相乘
    Cesium.Matrix4.multiply(m, rotationX, m);
    Cesium.Matrix4.multiply(m, rotationY, m);
    Cesium.Matrix4.multiply(m, rotationZ, m);
 
    tileset.readyPromise.then(function () {
 
    }).then(function () {
        if (tileset._root.transform == undefined) {
            console.log("瓦片未变换");
        }
        tileset._root.transform = m;
 
    });
    base3dtilesPrimitives.push(viewer.scene.primitives.add(tileset));
}