-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
PickingPipelineStageSpec.js
333 lines (270 loc) · 10.7 KB
/
PickingPipelineStageSpec.js
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
import {
combine,
GltfLoader,
ModelExperimentalStatistics,
ModelExperimentalType,
PickingPipelineStage,
ShaderBuilder,
Resource,
ResourceCache,
defined,
} from "../../../Source/Cesium.js";
import createScene from "../../createScene.js";
import ShaderBuilderTester from "../../ShaderBuilderTester.js";
import waitForLoaderProcess from "../../waitForLoaderProcess.js";
describe("Scene/ModelExperimental/PickingPipelineStage", function () {
const boxVertexColors =
"./Data/Models/GltfLoader/BoxVertexColors/glTF/BoxVertexColors.gltf";
const boxInstanced =
"./Data/Models/GltfLoader/BoxInstanced/glTF/box-instanced.gltf";
const microcosm = "./Data/Models/GltfLoader/Microcosm/glTF/microcosm.gltf";
const mockIdObject = {};
let scene;
const gltfLoaders = [];
beforeAll(function () {
scene = createScene();
});
afterAll(function () {
scene.destroyForSpecs();
});
afterEach(function () {
const gltfLoadersLength = gltfLoaders.length;
for (let i = 0; i < gltfLoadersLength; ++i) {
const gltfLoader = gltfLoaders[i];
if (!gltfLoader.isDestroyed()) {
gltfLoader.destroy();
}
}
gltfLoaders.length = 0;
ResourceCache.clearForSpecs();
});
function getOptions(gltfPath, options) {
const resource = new Resource({
url: gltfPath,
});
return combine(options, {
gltfResource: resource,
incrementallyLoadTexture: false,
});
}
function loadGltf(gltfPath, options) {
const gltfLoader = new GltfLoader(getOptions(gltfPath, options));
gltfLoaders.push(gltfLoader);
gltfLoader.load();
return waitForLoaderProcess(gltfLoader, scene);
}
function mockRenderResources() {
return {
attributes: [],
attributeIndex: 1,
pickId: undefined,
shaderBuilder: new ShaderBuilder(),
model: {
_pipelineResources: [],
_pickIds: [],
statistics: new ModelExperimentalStatistics(),
type: ModelExperimentalType.GLTF,
},
runtimePrimitive: {
primitive: {},
},
runtimeNode: {
node: {},
},
uniformMap: {},
};
}
function expectUniformMap(uniformMap, expected) {
for (const key in expected) {
if (expected.hasOwnProperty(key)) {
const expectedValue = expected[key];
const uniformFunction = uniformMap[key];
expect(uniformFunction).toBeDefined();
expect(uniformFunction()).toEqual(expectedValue);
}
}
}
function verifyPickObject(pickObject, renderResources, instanceId) {
const model = renderResources.model;
expect(pickObject).toBeDefined();
if (ModelExperimentalType.is3DTiles(model.type)) {
const content = model.content;
expect(pickObject.primitive).toEqual(content.tileset);
expect(pickObject.content).toEqual(content);
} else {
expect(pickObject.primitive).toEqual(model);
}
if (defined(instanceId)) {
expect(pickObject.instanceId).toEqual(instanceId);
}
const detailPickObject = pickObject.detail;
expect(detailPickObject).toBeDefined();
expect(detailPickObject.model).toEqual(model);
expect(detailPickObject.node).toEqual(renderResources.runtimeNode);
expect(detailPickObject.primitive).toEqual(
renderResources.runtimePrimitive
);
}
it("sets the picking variables in render resources for 3D Tiles", function () {
const renderResources = mockRenderResources();
renderResources.model.type = ModelExperimentalType.TILE_GLTF;
// Setting the content property makes PickingPipelineStage handle this
// as part of a tileset.
renderResources.model.content = {
tileset: {},
};
return loadGltf(boxVertexColors).then(function (gltfLoader) {
const components = gltfLoader.components;
const primitive = components.nodes[0].primitives[0];
const frameState = scene.frameState;
const context = frameState.context;
// Reset pick objects.
context._pickObjects = [];
PickingPipelineStage.process(renderResources, primitive, frameState);
const shaderBuilder = renderResources.shaderBuilder;
ShaderBuilderTester.expectHasFragmentUniforms(shaderBuilder, [
"uniform vec4 czm_pickColor;",
]);
const pickObject =
context._pickObjects[Object.keys(context._pickObjects)[0]];
verifyPickObject(pickObject, renderResources);
const uniformMap = renderResources.uniformMap;
expect(uniformMap.czm_pickColor).toBeDefined();
expect(uniformMap.czm_pickColor()).toBeDefined();
expect(renderResources.model._pipelineResources.length).toEqual(1);
expect(renderResources.model._pickIds.length).toEqual(1);
expect(renderResources.pickId).toEqual("czm_pickColor");
});
});
it("sets the picking variables in render resources for models", function () {
const renderResources = mockRenderResources();
return loadGltf(boxVertexColors).then(function (gltfLoader) {
const components = gltfLoader.components;
const primitive = components.nodes[0].primitives[0];
const frameState = scene.frameState;
const context = frameState.context;
// Reset pick objects.
context._pickObjects = [];
PickingPipelineStage.process(renderResources, primitive, frameState);
const shaderBuilder = renderResources.shaderBuilder;
ShaderBuilderTester.expectHasFragmentUniforms(shaderBuilder, [
"uniform vec4 czm_pickColor;",
]);
const pickObject =
context._pickObjects[Object.keys(context._pickObjects)[0]];
verifyPickObject(pickObject, renderResources);
const uniformMap = renderResources.uniformMap;
expect(uniformMap.czm_pickColor).toBeDefined();
expect(uniformMap.czm_pickColor()).toBeDefined();
expect(renderResources.model._pipelineResources.length).toEqual(1);
expect(renderResources.model._pickIds.length).toEqual(1);
expect(renderResources.pickId).toEqual("czm_pickColor");
});
});
it("sets value for pick object id if model has an id defined", function () {
const renderResources = mockRenderResources();
renderResources.model.id = mockIdObject;
return loadGltf(boxVertexColors).then(function (gltfLoader) {
const components = gltfLoader.components;
const primitive = components.nodes[0].primitives[0];
const frameState = scene.frameState;
const context = frameState.context;
// Reset pick objects.
context._pickObjects = [];
PickingPipelineStage.process(renderResources, primitive, frameState);
const shaderBuilder = renderResources.shaderBuilder;
ShaderBuilderTester.expectHasFragmentUniforms(shaderBuilder, [
"uniform vec4 czm_pickColor;",
]);
const pickObject =
context._pickObjects[Object.keys(context._pickObjects)[0]];
verifyPickObject(pickObject, renderResources, undefined);
expect(pickObject.id).toBe(mockIdObject);
const uniformMap = renderResources.uniformMap;
expect(uniformMap.czm_pickColor).toBeDefined();
expect(uniformMap.czm_pickColor()).toBeDefined();
expect(renderResources.model._pipelineResources.length).toEqual(1);
expect(renderResources.model._pickIds.length).toEqual(1);
expect(renderResources.pickId).toEqual("czm_pickColor");
});
});
it("sets the picking variables in render resources with instancing", function () {
const renderResources = mockRenderResources();
renderResources.instanceCount = 4;
renderResources.runtimeNode.node.instances = {
featureIds: [{}, {}],
};
return loadGltf(boxInstanced).then(function (gltfLoader) {
const components = gltfLoader.components;
const primitive = components.nodes[0].primitives[0];
renderResources.runtimeNode.node = components.nodes[0];
const frameState = scene.frameState;
const context = frameState.context;
// Reset pick objects.
context._pickObjects = [];
PickingPipelineStage.process(renderResources, primitive, frameState);
const shaderBuilder = renderResources.shaderBuilder;
ShaderBuilderTester.expectHasAttributes(shaderBuilder, undefined, [
"attribute vec4 a_pickColor;",
]);
ShaderBuilderTester.expectHasVaryings(shaderBuilder, [
"varying vec4 v_pickColor;",
]);
let i = 0;
for (const key in context._pickObjects) {
if (context._pickObjects.hasOwnProperty(key)) {
const pickObject = context._pickObjects[key];
verifyPickObject(pickObject, renderResources, i++);
}
}
const pickIdAttribute = renderResources.attributes[0];
expect(pickIdAttribute).toBeDefined();
expect(pickIdAttribute.index).toEqual(1);
// Each time an attribute is added, the attribute index should be incremented.
expect(renderResources.attributeIndex).toEqual(2);
expect(pickIdAttribute.vertexBuffer).toBeDefined();
expect(pickIdAttribute.vertexBuffer.sizeInBytes).toEqual(
renderResources.instanceCount * 4
);
expect(pickIdAttribute.instanceDivisor).toEqual(1);
expect(renderResources.model._pipelineResources.length).toEqual(5);
expect(renderResources.model._pickIds.length).toEqual(4);
const statistics = renderResources.model.statistics;
expect(statistics.geometryByteLength).toBe(
renderResources.instanceCount * 4
);
expect(renderResources.pickId).toEqual("v_pickColor");
});
});
it("sets the picking variables in render resources with feature ID textures", function () {
const mockModelFeatureTable = {
batchTexture: {
pickTexture: "mockPickTexture",
},
};
const renderResources = mockRenderResources();
renderResources.hasPropertyTable = true;
renderResources.model.featureIdLabel = "featureId_0";
renderResources.model.featureTables = [mockModelFeatureTable];
return loadGltf(microcosm).then(function (gltfLoader) {
const components = gltfLoader.components;
const primitive = components.nodes[0].primitives[0];
const frameState = scene.frameState;
const context = frameState.context;
// Reset pick objects.
context._pickObjects = [];
PickingPipelineStage.process(renderResources, primitive, frameState);
const expectedUniforms = {
model_pickTexture: mockModelFeatureTable.batchTexture.pickTexture,
};
expectUniformMap(renderResources.uniformMap, expectedUniforms);
const shaderBuilder = renderResources.shaderBuilder;
ShaderBuilderTester.expectHasFragmentUniforms(shaderBuilder, [
"uniform sampler2D model_pickTexture;",
]);
expect(renderResources.pickId).toEqual(
"((selectedFeature.id < int(model_featuresLength)) ? texture2D(model_pickTexture, selectedFeature.st) : vec4(0.0))"
);
});
});
});