15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
406
407
408
409
410
411
412
413
414
415
416
417
418
import {
  GltfLoaderUtil,
  Matrix3,
  PixelDatatype,
  PixelFormat,
  SupportedImageFormats,
  Texture,
  TextureWrap,
  TextureMagnificationFilter,
  TextureMinificationFilter,
} from "../../Source/Cesium.js";
import createContext from "../createContext.js";
 
describe("Scene/GltfLoaderUtil", function () {
  var gltfWithTextures = {
    images: [
      {
        uri: "image.png",
      },
      {
        mimeType: "image/jpeg",
        bufferView: 0,
      },
      {
        uri: "image.webp",
      },
      {
        uri: "image.ktx2",
      },
    ],
    textures: [
      {
        source: 0,
      },
      {
        source: 0,
        extensions: {
          EXT_texture_webp: {
            source: 2,
          },
        },
      },
      {
        source: 0,
        extensions: {
          KHR_texture_basisu: {
            source: 3,
          },
        },
      },
    ],
  };
 
  it("getImageIdFromTexture throws if gltf is undefined", function () {
    expect(function () {
      GltfLoaderUtil.getImageIdFromTexture({
        gltf: undefined,
        textureId: 0,
        supportedImageFormats: new SupportedImageFormats(),
      });
    }).toThrowDeveloperError();
  });
 
  it("getImageIdFromTexture throws if textureId is undefined", function () {
    expect(function () {
      GltfLoaderUtil.getImageIdFromTexture({
        gltf: gltfWithTextures,
        textureId: undefined,
        supportedImageFormats: new SupportedImageFormats(),
      });
    }).toThrowDeveloperError();
  });
 
  it("getImageIdFromTexture throws if supportedImageFormats is undefined", function () {
    expect(function () {
      GltfLoaderUtil.getImageIdFromTexture({
        gltf: gltfWithTextures,
        textureId: 0,
        supportedImageFormats: undefined,
      });
    }).toThrowDeveloperError();
  });
 
  it("getImageIdFromTexture gets image id", function () {
    var imageId = GltfLoaderUtil.getImageIdFromTexture({
      gltf: gltfWithTextures,
      textureId: 0,
      supportedImageFormats: new SupportedImageFormats(),
    });
    expect(imageId).toBe(0);
  });
 
  it("getImageIdFromTexture gets WebP image when EXT_texture_webp extension is supported", function () {
    var imageId = GltfLoaderUtil.getImageIdFromTexture({
      gltf: gltfWithTextures,
      textureId: 1,
      supportedImageFormats: new SupportedImageFormats({
        webp: true,
      }),
    });
    expect(imageId).toBe(2);
  });
 
  it("getImageIdFromTexture gets default image when EXT_texture_webp extension is not supported", function () {
    var imageId = GltfLoaderUtil.getImageIdFromTexture({
      gltf: gltfWithTextures,
      textureId: 1,
      supportedImageFormats: new SupportedImageFormats({
        webp: false,
      }),
    });
    expect(imageId).toBe(0);
  });
 
  it("getImageIdFromTexture gets KTX2 image when KHR_texture_basisu extension is supported", function () {
    var imageId = GltfLoaderUtil.getImageIdFromTexture({
      gltf: gltfWithTextures,
      textureId: 2,
      supportedImageFormats: new SupportedImageFormats({
        basis: true,
      }),
    });
    expect(imageId).toBe(3);
  });
 
  it("getImageIdFromTexture gets default image when KHR_texture_basisu extension is not supported", function () {
    var imageId = GltfLoaderUtil.getImageIdFromTexture({
      gltf: gltfWithTextures,
      textureId: 2,
      supportedImageFormats: new SupportedImageFormats({
        basis: false,
      }),
    });
    expect(imageId).toBe(0);
  });
 
  it("createSampler throws if gltf is undefined", function () {
    expect(function () {
      GltfLoaderUtil.getImageIdFromTexture({
        gltf: undefined,
        textureInfo: {
          index: 0,
        },
      });
    }).toThrowDeveloperError();
  });
 
  it("createSampler throws if textureInfo is undefined", function () {
    expect(function () {
      GltfLoaderUtil.getImageIdFromTexture({
        gltf: gltfWithTextures,
        textureInfo: undefined,
      });
    }).toThrowDeveloperError();
  });
 
  it("createSampler gets default sampler when texture does not have a sampler", function () {
    var sampler = GltfLoaderUtil.createSampler({
      gltf: {
        textures: [
          {
            source: 0,
          },
        ],
      },
      textureInfo: {
        index: 0,
      },
    });
    expect(sampler.wrapS).toBe(TextureWrap.REPEAT);
    expect(sampler.wrapT).toBe(TextureWrap.REPEAT);
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.LINEAR);
    expect(sampler.magnificationFilter).toBe(TextureMagnificationFilter.LINEAR);
  });
 
  it("createSampler uses NEAREST when compressedTextureNoMipmap is true and the minFilter uses nearest mipmap filtering", function () {
    var sampler = GltfLoaderUtil.createSampler({
      gltf: {
        textures: [
          {
            source: 0,
            sampler: 0,
          },
        ],
        samplers: [
          {
            magFilter: 9729,
            minFilter: 9986,
            wrapS: 10497,
            wrapT: 10497,
          },
        ],
      },
      textureInfo: {
        index: 0,
      },
      compressedTextureNoMipmap: true,
    });
    expect(sampler.wrapS).toBe(TextureWrap.REPEAT);
    expect(sampler.wrapT).toBe(TextureWrap.REPEAT);
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.NEAREST);
    expect(sampler.magnificationFilter).toBe(TextureMagnificationFilter.LINEAR);
  });
 
  it("createSampler uses LINEAR when compressedTextureNoMipmap is true and the minFilter uses linear mipmap filtering", function () {
    var sampler = GltfLoaderUtil.createSampler({
      gltf: {
        textures: [
          {
            source: 0,
            sampler: 0,
          },
        ],
        samplers: [
          {
            magFilter: 9729,
            minFilter: 9987,
            wrapS: 10497,
            wrapT: 10497,
          },
        ],
      },
      textureInfo: {
        index: 0,
      },
      compressedTextureNoMipmap: true,
    });
    expect(sampler.wrapS).toBe(TextureWrap.REPEAT);
    expect(sampler.wrapT).toBe(TextureWrap.REPEAT);
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.LINEAR);
    expect(sampler.magnificationFilter).toBe(TextureMagnificationFilter.LINEAR);
  });
 
  function createSampler(options) {
    var gltf = {
      textures: [
        {
          source: 0,
          sampler: 0,
        },
      ],
      samplers: [
        {
          minFilter: options.minFilter,
          magFilter: options.magFilter,
          wrapS: options.wrapS,
          wrapT: options.wrapT,
        },
      ],
    };
 
    var textureInfo = {
      index: 0,
    };
 
    if (options.useTextureTransform) {
      textureInfo.extensions = {
        KHR_texture_transform: {},
      };
    }
 
    return GltfLoaderUtil.createSampler({
      gltf: gltf,
      textureInfo: textureInfo,
    });
  }
 
  it("createSampler fills in undefined sampler properties", function () {
    var sampler = createSampler({
      wrapS: TextureWrap.CLAMP_TO_EDGE,
    });
 
    expect(sampler.wrapS).toBe(TextureWrap.CLAMP_TO_EDGE);
    expect(sampler.wrapT).toBe(TextureWrap.REPEAT);
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.LINEAR);
    expect(sampler.magnificationFilter).toBe(TextureMagnificationFilter.LINEAR);
  });
 
  it("createSampler creates non-mipmap sampler for KHR_texture_transform", function () {
    var sampler = createSampler({
      minFilter: TextureMinificationFilter.NEAREST_MIPMAP_NEAREST,
      useTextureTransform: true,
    });
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.NEAREST);
 
    sampler = createSampler({
      minFilter: TextureMinificationFilter.NEAREST_MIPMAP_LINEAR,
      useTextureTransform: true,
    });
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.NEAREST);
 
    sampler = createSampler({
      minFilter: TextureMinificationFilter.LINEAR_MIPMAP_NEAREST,
      useTextureTransform: true,
    });
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.LINEAR);
 
    sampler = createSampler({
      minFilter: TextureMinificationFilter.LINEAR_MIPMAP_LINEAR,
      useTextureTransform: true,
    });
    expect(sampler.minificationFilter).toBe(TextureMinificationFilter.LINEAR);
  });
 
  it("createModelTextureReader creates texture with default values", function () {
    var textureInfo = {
      index: 0,
    };
 
    var modelTexture = GltfLoaderUtil.createModelTextureReader({
      textureInfo: textureInfo,
    });
 
    expect(modelTexture.texture).toBeUndefined();
    expect(modelTexture.texCoord).toBe(0);
    expect(modelTexture.transform).toBeUndefined();
    expect(modelTexture.channels).toBeUndefined();
  });
 
  it("createModelTextureReader creates texture with KHR_texture_transform extension", function () {
    var textureInfo = {
      index: 0,
      texCoord: 0,
      extensions: {
        KHR_texture_transform: {
          offset: [0.5, 0.5],
          scale: [0.1, 0.2],
          texCoord: 1,
        },
      },
    };
 
    // prettier-ignore
    var expectedTransform = new Matrix3(
      0.1, 0.0, 0.5,
      0.0, 0.2, 0.5,
      0.0, 0.0, 1.0
    );
 
    var modelTexture = GltfLoaderUtil.createModelTextureReader({
      textureInfo: textureInfo,
    });
 
    expect(modelTexture.texCoord).toBe(1);
    expect(modelTexture.transform).toEqual(expectedTransform);
  });
 
  it("createModelTextureReader handles KHR_texture_transform rotation correctly", function () {
    var angle = Math.PI / 2.0;
    var textureInfo = {
      index: 0,
      texCoord: 0,
      extensions: {
        KHR_texture_transform: {
          rotation: angle,
          texCoord: 1,
        },
      },
    };
 
    // glTF requires texture coordinates to start in the top left corner.
    // This reverses the orientation of the uv coordinate space, so the angle
    // must be reversed.
    // prettier-ignore
    var expectedTransform = new Matrix3(
      Math.cos(-angle), -Math.sin(-angle), 0.0,
      Math.sin(-angle), Math.cos(-angle), 0.0,
      0.0, 0.0, 1.0
    );
 
    var modelTexture = GltfLoaderUtil.createModelTextureReader({
      textureInfo: textureInfo,
    });
 
    expect(modelTexture.texCoord).toBe(1);
    expect(modelTexture.transform).toEqual(expectedTransform);
  });
 
  it("createModelTextureReader creates texture", function () {
    var context = createContext();
 
    var texture = new Texture({
      context: context,
      pixelFormat: PixelFormat.RGBA,
      pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
      source: {
        arrayBufferView: new Uint8Array([1, 2, 3, 4]),
        width: 1,
        height: 1,
      },
    });
 
    var textureInfo = {
      index: 0,
      texCoord: 1,
    };
 
    var modelTexture = GltfLoaderUtil.createModelTextureReader({
      textureInfo: textureInfo,
      channels: "r",
      texture: texture,
    });
 
    expect(modelTexture.texture).toBe(texture);
    expect(modelTexture.texCoord).toBe(1);
    expect(modelTexture.transform).toBeUndefined();
    expect(modelTexture.channels).toBe("r");
 
    texture.destroy();
    context.destroyForSpecs();
  });
 
  it("createModelTextureReader throws if textureInfo is undefined", function () {
    expect(function () {
      GltfLoaderUtil.createModelTextureReader();
    }).toThrowDeveloperError();
  });
});