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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import { defined, RuntimeError } from "../../Source/Cesium.js";
import findAccessorMinMax from "../../Source/Scene/GltfPipeline/findAccessorMinMax.js";
 
/**
 * A fluent interface for programmatically building a glTF.
 * @alias GltfBuilder
 * @constructor
 * @private
 */
function GltfBuilder() {
  this.gltf = {
    asset: {
      generator: "cesium-tests",
      version: "2.0",
    },
    extensionsUsed: [],
    accessors: [],
    buffers: [],
    bufferViews: [],
    materials: [],
    meshes: [],
    nodes: [
      {
        mesh: 0,
      },
    ],
    scenes: [
      {
        nodes: [0],
      },
    ],
    scene: 0,
  };
 
  this.bufferBuilders = [];
}
 
/**
 * Creates a new buffer.
 * @param {string} [name] The name of the buffer.
 * @returns {GltfBufferBuilder}
 */
GltfBuilder.prototype.buffer = function (name) {
  var index =
    this.gltf.buffers.push({
      name: name,
      byteLength: 0,
    }) - 1;
  var bufferBuilder = new GltfBufferBuilder(this, index);
  this.bufferBuilders.push(bufferBuilder);
  return bufferBuilder;
};
 
/**
 * Creates a new mesh.
 * @param {string} [name] The name of the mesh.
 * @returns {GltfMeshBuilder}
 */
GltfBuilder.prototype.mesh = function (name) {
  var index =
    this.gltf.meshes.push({
      name: name,
      primitives: [],
    }) - 1;
 
  var meshBuilder = new GltfMeshBuilder(this, index);
  return meshBuilder;
};
 
/**
 * Creates a new material.
 * @param {string} [name] The name of the material.
 * @returns {GltfMaterialBuilder}
 */
GltfBuilder.prototype.material = function (name) {
  var index =
    this.gltf.materials.push({
      name: name,
    }) - 1;
  var materialBuilder = new GltfMaterialBuilder(this, index);
  return materialBuilder;
};
 
/**
 * Gets the built glTF JSON from this builder. Calling this a second
 * time will cause the glTF returned in the first call to be invalidated.
 * Specifically, the `uri` properties of its buffers will no longer be
 * resolvable.
 *
 * After calling this method, be sure to call {@link GltfBuilder#destroy}
 * when done with the glTF, to a void leaking buffer memory.
 */
GltfBuilder.prototype.toGltf = function () {
  for (var i = 0; i < this.bufferBuilders.length; ++i) {
    var bufferBuilder = this.bufferBuilders[i];
 
    var byteLength = bufferBuilder.viewBuilders.reduce(function (
      byteLength,
      viewBuilder
    ) {
      return byteLength + viewBuilder.bufferView.byteLength;
    },
    0);
 
    var buffer = new ArrayBuffer(byteLength);
    var nextStart = 0;
 
    for (var j = 0; j < bufferBuilder.viewBuilders.length; ++j) {
      var viewBuilder = bufferBuilder.viewBuilders[j];
      viewBuilder.bufferView.byteOffset = nextStart;
      var destBuffer =
        viewBuilder.componentType === 5126
          ? new Float32Array(buffer, nextStart, viewBuilder._data.length)
          : new Uint16Array(buffer, nextStart, viewBuilder._data.length);
      destBuffer.set(viewBuilder._data);
      nextStart += viewBuilder.bufferView.byteLength;
    }
 
    bufferBuilder.buffer.byteLength = byteLength;
 
    if (bufferBuilder.buffer.uri) {
      URL.revokeObjectURL(bufferBuilder.buffer.uri);
    }
 
    bufferBuilder.buffer.uri = URL.createObjectURL(new Blob([buffer]));
 
    bufferBuilder.buffer.extras = {
      _pipeline: {
        source: new Uint8Array(buffer, 0, buffer.byteLength),
      },
    };
  }
 
  var gltf = this.gltf;
  gltf.accessors.forEach(function (accessor) {
    var minMax = findAccessorMinMax(gltf, accessor);
    accessor.min = minMax.min;
    accessor.max = minMax.max;
  });
 
  this.bufferBuilders.forEach(function (builder) {
    delete builder.buffer.extras;
  });
 
  return this.gltf;
};
 
/**
 * Frees memory allocated for buffers in {@link GltfBuilder@toGltf}.
 */
GltfBuilder.prototype.destroy = function () {
  this.bufferBuilders.forEach(function (bufferBuilder) {
    URL.revokeObjectURL(bufferBuilder.buffer.uri);
    bufferBuilder.buffer.uri = undefined;
  });
};
 
/**
 * A fluent interface for programmatically building a glTF material.
 * @param {GltfBuilder} gltfBuilder The glTF builder.
 * @param {number} materialIndex The index of this material within the glTF.
 * @private
 */
function GltfMaterialBuilder(gltfBuilder, materialIndex) {
  this.gltfBuilder = gltfBuilder;
  this.materialIndex = materialIndex;
  this.material = this.gltfBuilder.gltf.materials[materialIndex];
}
 
/**
 * Defines the material using JSON.
 * @param {*} json The JSON definition of the material.
 * @returns {GltfMaterialBuilder}
 */
GltfMaterialBuilder.prototype.json = function (json) {
  for (var property in json) {
    if (!json.hasOwnProperty(property)) {
      continue;
    }
 
    this.material[property] = json[property];
  }
 
  return this;
};
 
/**
 * A fluent interface for building a glTF mesh.
 * @param {GltfBuilder} gltfBuilder The glTF builder.
 * @param {number} meshIndex The index of this mesh within the glTF.
 * @private
 */
function GltfMeshBuilder(gltfBuilder, meshIndex) {
  this.gltfBuilder = gltfBuilder;
  this.meshIndex = meshIndex;
  this.mesh = gltfBuilder.gltf.meshes[this.meshIndex];
}
 
/**
 * Creates a new primitive within this mesh.
 * @param {string} [name] The name of the primitive.
 * @returns {GltfPrimitiveBuilder}
 */
GltfMeshBuilder.prototype.primitive = function (name) {
  var index =
    this.mesh.primitives.push({
      name: name,
      attributes: {},
    }) - 1;
 
  var meshBuilder = new GltfPrimitiveBuilder(this, index);
  return meshBuilder;
};
 
/**
 * A fluent interface for building a glTF primitive.
 * @param {GltfMeshBuilder} gltfMeshBuilder The mesh builder.
 * @param {number} primitiveIndex The index of this primitive within the mesh.
 * @private
 */
function GltfPrimitiveBuilder(gltfMeshBuilder, primitiveIndex) {
  this.gltfMeshBuilder = gltfMeshBuilder;
  this.primitiveIndex = primitiveIndex;
  this.primitive = this.gltfMeshBuilder.mesh.primitives[this.primitiveIndex];
}
 
/**
 * Adds a new attribute to the primitive.
 * @param {string} semantic The semantic of the attribute.
 * @param {string} accessorName The name of the accessor referenced by this attribute.
 * @returns {GltfPrimitiveBuilder}
 */
GltfPrimitiveBuilder.prototype.attribute = function (semantic, accessorName) {
  var gltf = this.gltfMeshBuilder.gltfBuilder.gltf;
  var accessorId = findAccessorByName(gltf, accessorName);
  if (accessorId < 0) {
    throw new RuntimeError("Accessor named " + accessorName + " not found.");
  }
 
  this.primitive.attributes[semantic] = accessorId;
  return this;
};
 
/**
 * Sets the name of the accessor providing this primitive's indices.
 * @param {string} accessorName The name of the accessor providing the indices.
 * @returns {GltfPrimitiveBuilder}
 */
GltfPrimitiveBuilder.prototype.indices = function (accessorName) {
  var gltf = this.gltfMeshBuilder.gltfBuilder.gltf;
  var accessorId = findAccessorByName(gltf, accessorName);
  if (accessorId < 0) {
    throw new RuntimeError("Accessor named " + accessorName + " not found.");
  }
 
  this.primitive.indices = accessorId;
  return this;
};
 
/**
 * Indicates that this primitive is TRIANGLES.
 * @returns {GltfPrimitiveBuilder}
 */
GltfPrimitiveBuilder.prototype.triangles = function () {
  this.primitive.mode = 4;
  return this;
};
 
/**
 * Sets the material applied to this primitive.
 * @param {string} materialName The name of the material.
 * @returns {GltfPrimitiveBuilder}
 */
GltfPrimitiveBuilder.prototype.material = function (materialName) {
  var gltf = this.gltfMeshBuilder.gltfBuilder.gltf;
 
  for (var i = 0; i < gltf.materials.length; ++i) {
    if (gltf.materials[i].name === materialName) {
      this.primitive.material = i;
      return this;
    }
  }
 
  throw new RuntimeError("Material named " + materialName + " not found.");
};
 
/**
 * A fluent interface for building a glTF buffer.
 * @param {GltfBuilder} gltfBuilder The glTF builder.
 * @param {number} bufferIndex The index of this buffer in the glTF.
 * @private
 */
function GltfBufferBuilder(gltfBuilder, bufferIndex) {
  this.gltfBuilder = gltfBuilder;
  this.bufferIndex = bufferIndex;
  this.buffer = this.gltfBuilder.gltf.buffers[bufferIndex];
  this.viewBuilders = [];
}
 
/**
 * Creates a new vertex bufferView in this buffer.
 * @param {string} name The name of the bufferView.
 * @returns {GltfBufferViewBuilder}
 */
GltfBufferBuilder.prototype.vertexBuffer = function (name) {
  var index =
    this.gltfBuilder.gltf.bufferViews.push({
      name: name,
      buffer: this.bufferIndex,
      byteLength: 0,
      target: 34962,
    }) - 1;
  var viewBuilder = new GltfBufferViewBuilder(this, index, 5126);
  this.viewBuilders.push(viewBuilder);
  return viewBuilder;
};
 
/**
 * Creates a new index bufferView in this buffer.
 * @param {string} name The name of the bufferView.
 * @returns {GltfBufferViewBuilder}
 */
GltfBufferBuilder.prototype.indexBuffer = function (name) {
  var index =
    this.gltfBuilder.gltf.bufferViews.push({
      name: name,
      buffer: this.bufferIndex,
      byteLength: 0,
      target: 34963,
    }) - 1;
  var viewBuilder = new GltfBufferViewBuilder(this, index, 5123);
  this.viewBuilders.push(viewBuilder);
  return viewBuilder;
};
 
/**
 * A fluent interface for building a glTF bufferView.
 * @param {GltfBufferBuilder} bufferBuilder The buffer builder.
 * @param {GltfBufferViewBuilder} bufferViewIndex The bufferView builder.
 * @param {number} componentType The glTF `componentType` of this bufferView.
 * @private
 */
function GltfBufferViewBuilder(bufferBuilder, bufferViewIndex, componentType) {
  this.bufferBuilder = bufferBuilder;
  this.bufferViewIndex = bufferViewIndex;
  this.bufferView = this.bufferBuilder.gltfBuilder.gltf.bufferViews[
    this.bufferViewIndex
  ];
  this.componentType = componentType;
  this.elementStride = 0;
  this.nextOffset = 0;
  this._data = undefined;
}
 
Object.defineProperties(GltfBufferViewBuilder.prototype, {
  /**
   * Gets the number of bytes in each numeric element of this bufferView.
   */
  elementByteLength: {
    get: function () {
      return this.componentType === 5126 ? 4 : 2;
    },
  },
});
 
/**
 * Defines a `VEC3` element in this bufferView.
 * @param {string} name The name of the accessor for this element.
 * @returns {GltfBufferViewBuilder}
 */
GltfBufferViewBuilder.prototype.vec3 = function (name) {
  var gltf = this.bufferBuilder.gltfBuilder.gltf;
  gltf.accessors.push({
    name: name,
    bufferView: this.bufferViewIndex,
    byteOffset: this.nextOffset,
    componentType: this.componentType,
    count: 0,
    type: "VEC3",
  });
 
  this.elementStride += 3;
 
  var newStride = 3 * this.elementByteLength;
  if (!defined(this.bufferView.byteStride)) {
    this.bufferView.byteStride = newStride;
  } else {
    this.bufferView.byteStride += newStride;
  }
 
  this.nextOffset += newStride;
 
  return this;
};
 
/**
 * Defines a `SCALAR` element in this bufferView.
 * @param {string} name The name of the accessor for this element.
 * @returns {GltfBufferViewBuilder}
 */
GltfBufferViewBuilder.prototype.scalar = function (name) {
  var gltf = this.bufferBuilder.gltfBuilder.gltf;
  gltf.accessors.push({
    name: name,
    bufferView: this.bufferViewIndex,
    byteOffset: this.nextOffset,
    componentType: this.componentType,
    count: 0,
    type: "SCALAR",
  });
 
  this.elementStride += 1;
 
  var newStride = this.elementByteLength;
  if (!defined(this.bufferView.byteStride)) {
    this.bufferView.byteStride = newStride;
  } else {
    this.bufferView.byteStride += newStride;
  }
 
  this.nextOffset += newStride;
 
  return this;
};
 
/**
 * Provides this bufferView's data. All elements should be defined with
 * {@link GltfBufferViewBuilder#vec3} and {@link GltfBufferViewBuilder#scalar}
 * before calling this method.
 * @param {Array|Float32Array|Uint16Array|Uint32Array} data The data.
 * @returns {GltfBufferViewBuilder}
 */
GltfBufferViewBuilder.prototype.data = function (data) {
  this.bufferView.byteLength = data.length * this.elementByteLength;
  this._data = data;
 
  var count = data.length / this.elementStride;
 
  var gltf = this.bufferBuilder.gltfBuilder.gltf;
  var bufferViewIndex = this.bufferViewIndex;
 
  gltf.accessors
    .filter(function (accessor) {
      return accessor.bufferView === bufferViewIndex;
    })
    .forEach(function (accessor) {
      accessor.count = count;
    });
 
  return this;
};
 
function findAccessorByName(gltf, accessorName) {
  var accessors = gltf.accessors;
 
  for (var i = 0; i < accessors.length; ++i) {
    if (accessors[i].name === accessorName) {
      return i;
    }
  }
 
  return -1;
}
 
export default GltfBuilder;