yzt
2023-05-08 24e1c6a1c3d5331b5a4f1111dcbae3ef148eda1a
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
import { Cartesian3 } from "../../Source/Cesium.js";
import { Cartographic } from "../../Source/Cesium.js";
import { Color } from "../../Source/Cesium.js";
import { ColorGeometryInstanceAttribute } from "../../Source/Cesium.js";
import { destroyObject } from "../../Source/Cesium.js";
import { Ellipsoid } from "../../Source/Cesium.js";
import { GeometryInstance } from "../../Source/Cesium.js";
import { HeadingPitchRange } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
import { Matrix4 } from "../../Source/Cesium.js";
import { Rectangle } from "../../Source/Cesium.js";
import { RectangleGeometry } from "../../Source/Cesium.js";
import { Resource } from "../../Source/Cesium.js";
import { Pass } from "../../Source/Cesium.js";
import { RenderState } from "../../Source/Cesium.js";
import { ClassificationModel } from "../../Source/Cesium.js";
import { ClassificationType } from "../../Source/Cesium.js";
import { PerInstanceColorAppearance } from "../../Source/Cesium.js";
import { Primitive } from "../../Source/Cesium.js";
import { StencilConstants } from "../../Source/Cesium.js";
import createScene from "../createScene.js";
import pollToPromise from "../pollToPromise.js";
import { addDefaults } from "../../Source/Cesium.js";
import { parseGlb } from "../../Source/Cesium.js";
import { updateVersion } from "../../Source/Cesium.js";
 
describe(
  "Scene/ClassificationModel",
  function () {
    var scene;
    var modelMatrix;
    var centerLongitude = -1.31968;
    var centerLatitude = 0.698874;
 
    var batchedModel = "./Data/Models/Classification/batched.glb";
    var quantizedModel = "./Data/Models/Classification/batchedQuantization.glb";
 
    var globePrimitive;
    var tilesetPrimitive;
    var reusableGlobePrimitive;
    var reusableTilesetPrimitive;
 
    function setCamera(longitude, latitude) {
      // One feature is located at the center, point the camera there
      var center = Cartesian3.fromRadians(longitude, latitude);
      scene.camera.lookAt(center, new HeadingPitchRange(0.0, -1.57, 15.0));
    }
 
    function createPrimitive(rectangle, pass) {
      var renderState;
      if (pass === Pass.CESIUM_3D_TILE) {
        renderState = RenderState.fromCache({
          stencilTest: StencilConstants.setCesium3DTileBit(),
          stencilMask: StencilConstants.CESIUM_3D_TILE_MASK,
          depthTest: {
            enabled: true,
          },
        });
      }
      var depthColorAttribute = ColorGeometryInstanceAttribute.fromColor(
        new Color(0.0, 0.0, 0.0, 1.0)
      );
      return new Primitive({
        geometryInstances: new GeometryInstance({
          geometry: new RectangleGeometry({
            ellipsoid: Ellipsoid.WGS84,
            rectangle: rectangle,
          }),
          id: "depth rectangle",
          attributes: {
            color: depthColorAttribute,
          },
        }),
        appearance: new PerInstanceColorAppearance({
          translucent: false,
          flat: true,
          renderState: renderState,
        }),
        asynchronous: false,
      });
    }
 
    function MockPrimitive(primitive, pass) {
      this._primitive = primitive;
      this._pass = pass;
      this.show = true;
    }
 
    MockPrimitive.prototype.update = function (frameState) {
      if (!this.show) {
        return;
      }
 
      var commandList = frameState.commandList;
      var startLength = commandList.length;
      this._primitive.update(frameState);
 
      for (var i = startLength; i < commandList.length; ++i) {
        var command = commandList[i];
        command.pass = this._pass;
      }
    };
 
    MockPrimitive.prototype.isDestroyed = function () {
      return false;
    };
 
    MockPrimitive.prototype.destroy = function () {
      return destroyObject(this);
    };
 
    beforeAll(function () {
      scene = createScene();
 
      var translation = Ellipsoid.WGS84.geodeticSurfaceNormalCartographic(
        new Cartographic(centerLongitude, centerLatitude)
      );
      Cartesian3.multiplyByScalar(translation, -5.0, translation);
      modelMatrix = Matrix4.fromTranslation(translation);
 
      var offset = CesiumMath.toRadians(0.01);
      var rectangle = new Rectangle(
        centerLongitude - offset,
        centerLatitude - offset,
        centerLongitude + offset,
        centerLatitude + offset
      );
      reusableGlobePrimitive = createPrimitive(rectangle, Pass.GLOBE);
      reusableTilesetPrimitive = createPrimitive(
        rectangle,
        Pass.CESIUM_3D_TILE
      );
    });
 
    afterAll(function () {
      reusableGlobePrimitive.destroy();
      reusableTilesetPrimitive.destroy();
      scene.destroyForSpecs();
    });
 
    beforeEach(function () {
      setCamera(centerLongitude, centerLatitude);
 
      // wrap rectangle primitive so it gets executed during the globe pass and 3D Tiles pass to lay down depth
      globePrimitive = new MockPrimitive(reusableGlobePrimitive, Pass.GLOBE);
      tilesetPrimitive = new MockPrimitive(
        reusableTilesetPrimitive,
        Pass.CESIUM_3D_TILE
      );
 
      scene.primitives.add(globePrimitive);
      scene.primitives.add(tilesetPrimitive);
    });
 
    afterEach(function () {
      scene.primitives.removeAll();
      globePrimitive =
        globePrimitive &&
        !globePrimitive.isDestroyed() &&
        globePrimitive.destroy();
      tilesetPrimitive =
        tilesetPrimitive &&
        !tilesetPrimitive.isDestroyed() &&
        tilesetPrimitive.destroy();
    });
 
    function expectRender(scene, model) {
      model.show = false;
      expect(scene).toRender([0, 0, 0, 255]);
      model.show = true;
      expect(scene).toRenderAndCall(function (rgba) {
        expect(rgba).not.toEqual([0, 0, 0, 255]);
      });
    }
 
    function expectRenderBlank(scene, model) {
      model.show = false;
      expect(scene).toRender([0, 0, 0, 255]);
      model.show = true;
      expect(scene).toRender([0, 0, 0, 255]);
    }
 
    function loadGltf(model) {
      return Resource.fetchArrayBuffer(model).then(function (arrayBuffer) {
        var gltf = new Uint8Array(arrayBuffer);
        gltf = parseGlb(gltf);
        updateVersion(gltf);
        addDefaults(gltf);
        return gltf;
      });
    }
 
    function loadClassificationModel(url, classificationType) {
      return Resource.fetchArrayBuffer(url).then(function (arrayBuffer) {
        var model = scene.primitives.add(
          new ClassificationModel({
            gltf: arrayBuffer,
            classificationType: classificationType,
            modelMatrix: modelMatrix,
          })
        );
 
        var ready = false;
        model.readyPromise.then(function () {
          ready = true;
        });
 
        return pollToPromise(function () {
          scene.renderForSpecs();
          return ready;
        }).then(function () {
          return model;
        });
      });
    }
 
    it("classifies 3D Tiles", function () {
      return loadClassificationModel(
        batchedModel,
        ClassificationType.CESIUM_3D_TILE
      ).then(function (model) {
        globePrimitive.show = false;
        tilesetPrimitive.show = true;
        expectRender(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = false;
        expectRenderBlank(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = true;
      });
    });
 
    it("classifies globe", function () {
      return loadClassificationModel(
        batchedModel,
        ClassificationType.TERRAIN
      ).then(function (model) {
        globePrimitive.show = false;
        tilesetPrimitive.show = true;
        expectRenderBlank(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = false;
        expectRender(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = true;
      });
    });
 
    it("classifies both 3D Tiles and globe", function () {
      return loadClassificationModel(
        batchedModel,
        ClassificationType.BOTH
      ).then(function (model) {
        globePrimitive.show = false;
        tilesetPrimitive.show = true;
        expectRender(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = false;
        expectRender(scene, model);
        globePrimitive.show = true;
        tilesetPrimitive.show = true;
      });
    });
 
    it("renders batched model", function () {
      return loadClassificationModel(
        batchedModel,
        ClassificationType.BOTH
      ).then(function (model) {
        expectRender(scene, model);
      });
    });
 
    it("renders batched model with quantization", function () {
      return loadClassificationModel(
        quantizedModel,
        ClassificationType.BOTH
      ).then(function (model) {
        expectRender(scene, model);
      });
    });
 
    it("throws with invalid number of nodes", function () {
      return loadGltf(batchedModel).then(function (gltf) {
        gltf.nodes.push({});
        expect(function () {
          return new ClassificationModel({
            gltf: gltf,
          });
        }).toThrowRuntimeError();
      });
    });
 
    it("throws with invalid number of meshes", function () {
      return loadGltf(batchedModel).then(function (gltf) {
        gltf.meshes.push({});
        expect(function () {
          return new ClassificationModel({
            gltf: gltf,
          });
        }).toThrowRuntimeError();
      });
    });
 
    it("throws with invalid number of primitives", function () {
      return loadGltf(batchedModel).then(function (gltf) {
        gltf.meshes[0].primitives.push({});
        expect(function () {
          return new ClassificationModel({
            gltf: gltf,
          });
        }).toThrowRuntimeError();
      });
    });
 
    it("throws with position semantic", function () {
      return loadGltf(batchedModel).then(function (gltf) {
        gltf.meshes[0].primitives[0].attributes.POSITION = undefined;
        expect(function () {
          return new ClassificationModel({
            gltf: gltf,
          });
        }).toThrowRuntimeError();
      });
    });
 
    it("throws with batch id semantic", function () {
      return loadGltf(batchedModel).then(function (gltf) {
        gltf.meshes[0].primitives[0].attributes._BATCHID = undefined;
        expect(function () {
          return new ClassificationModel({
            gltf: gltf,
          });
        }).toThrowRuntimeError();
      });
    });
  },
  "WebGL"
);