@galacean/engine-loader 2.0.0-alpha.56 → 2.0.0-alpha.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -1198,6 +1198,72 @@ HDRDecoder._float2HalfTables = HDRDecoder._generateFloat2HalfTables();
1198
1198
  HDRDecoder._floatView = new Float32Array(1);
1199
1199
  HDRDecoder._uint32View = new Uint32Array(HDRDecoder._floatView.buffer);
1200
1200
 
1201
+ function _instanceof(left, right) {
1202
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
1203
+ return !!right[Symbol.hasInstance](left);
1204
+ } else return left instanceof right;
1205
+ }
1206
+
1207
+ /**
1208
+ * Decodes encoded image bytes (PNG, JPEG, WebP, ...) into an image element.
1209
+ *
1210
+ * The blob handed to the image carries the declared `mimeType` or the MIME type detected from the bytes:
1211
+ * browsers sniff an untyped blob, but some platforms (for example mini-game runtimes) only decode images
1212
+ * whose type is declared.
1213
+ * @internal
1214
+ */ var ImageDecoder = /*#__PURE__*/ function() {
1215
+ function ImageDecoder() {}
1216
+ ImageDecoder._decode = function _decode(data, url, mimeType) {
1217
+ return new engineCore.AssetPromise(function(resolve, reject) {
1218
+ var objectUrl = URL.createObjectURL(new Blob([
1219
+ data
1220
+ ], {
1221
+ type: mimeType || ImageDecoder._getMimeType(data)
1222
+ }));
1223
+ var image = new Image();
1224
+ image.onload = function() {
1225
+ var complete = function() {
1226
+ ImageDecoder._releaseObjectUrl(image, objectUrl);
1227
+ resolve(image);
1228
+ };
1229
+ if ((engineCore.SystemInfo.platform === engineCore.Platform.IPhone || engineCore.SystemInfo.platform === engineCore.Platform.IPad) && typeof requestAnimationFrame !== "undefined") {
1230
+ // Defer on iOS WebKit so the decoded image surface is ready before revoking the blob URL.
1231
+ requestAnimationFrame(complete);
1232
+ } else {
1233
+ complete();
1234
+ }
1235
+ };
1236
+ image.onerror = image.onabort = function() {
1237
+ ImageDecoder._releaseObjectUrl(image, objectUrl);
1238
+ reject(new Error('TextureLoader: failed to decode texture "' + url + '" (' + data.byteLength + " bytes)."));
1239
+ };
1240
+ image.crossOrigin = "anonymous";
1241
+ image.src = objectUrl;
1242
+ });
1243
+ };
1244
+ ImageDecoder._getMimeType = function _getMimeType(data) {
1245
+ var bytes = _instanceof(data, Uint8Array) ? data : ArrayBuffer.isView(data) ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength) : new Uint8Array(data);
1246
+ if (bytes.length >= 4 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) {
1247
+ return "image/png";
1248
+ }
1249
+ if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
1250
+ return "image/jpeg";
1251
+ }
1252
+ // "RIFF" .... "WEBP"
1253
+ if (bytes.length >= 12 && bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
1254
+ return "image/webp";
1255
+ }
1256
+ return "";
1257
+ };
1258
+ ImageDecoder._releaseObjectUrl = function _releaseObjectUrl(image, objectUrl) {
1259
+ image.onload = null;
1260
+ image.onerror = null;
1261
+ image.onabort = null;
1262
+ URL.revokeObjectURL(objectUrl);
1263
+ };
1264
+ return ImageDecoder;
1265
+ }();
1266
+
1201
1267
  /**
1202
1268
  * Data format: [url] [mipmap(1B)] [filterMode(1B)] [anisoLevel(1B)] [wrapModeU(1B)] [wrapModeV(1B)]
1203
1269
  * [format(1B)] [width(2B)] [height(2B)] [isSRGBColorSpace(1B)] [Uint32(imageSize) + imageBytes]
@@ -1205,7 +1271,7 @@ HDRDecoder._uint32View = new Uint32Array(HDRDecoder._floatView.buffer);
1205
1271
  function Texture2DDecoder() {}
1206
1272
  Texture2DDecoder.decode = function decode(engine, bufferReader, restoredTexture) {
1207
1273
  return new engineCore.AssetPromise(function(resolve, reject) {
1208
- bufferReader.nextStr();
1274
+ var url = bufferReader.nextStr();
1209
1275
  var mipmap = !!bufferReader.nextUint8();
1210
1276
  var filterMode = bufferReader.nextUint8();
1211
1277
  var anisoLevel = bufferReader.nextUint8();
@@ -1229,21 +1295,11 @@ HDRDecoder._uint32View = new Uint32Array(HDRDecoder._floatView.buffer);
1229
1295
  mipmap && texture.generateMipmaps();
1230
1296
  resolve(texture);
1231
1297
  } else {
1232
- var blob = new Blob([
1233
- imageData
1234
- ]);
1235
- var img = new Image();
1236
- img.onload = function() {
1237
- URL.revokeObjectURL(img.src);
1298
+ ImageDecoder._decode(imageData, url).then(function(img) {
1238
1299
  texture.setImageSource(img);
1239
1300
  mipmap && texture.generateMipmaps();
1240
1301
  resolve(texture);
1241
- };
1242
- img.onerror = function(e) {
1243
- URL.revokeObjectURL(img.src);
1244
- reject(e);
1245
- };
1246
- img.src = URL.createObjectURL(blob);
1302
+ }, reject);
1247
1303
  }
1248
1304
  });
1249
1305
  };
@@ -1260,7 +1316,7 @@ Texture2DDecoder = __decorate([
1260
1316
  function TextureCubeDecoder() {}
1261
1317
  TextureCubeDecoder.decode = function decode(engine, bufferReader, restoredTexture) {
1262
1318
  return new engineCore.AssetPromise(function(resolve, reject) {
1263
- bufferReader.nextStr();
1319
+ var url = bufferReader.nextStr();
1264
1320
  var mipmap = !!bufferReader.nextUint8();
1265
1321
  var filterMode = bufferReader.nextUint8();
1266
1322
  var anisoLevel = bufferReader.nextUint8();
@@ -1287,23 +1343,13 @@ Texture2DDecoder = __decorate([
1287
1343
  resolve(texture);
1288
1344
  } else {
1289
1345
  var _loop = function(i1) {
1290
- var blob = new Blob([
1291
- facesData[i1]
1292
- ]);
1293
- var img = new Image();
1294
- img.onload = function() {
1295
- URL.revokeObjectURL(img.src);
1346
+ ImageDecoder._decode(facesData[i1], url).then(function(img) {
1296
1347
  texture.setImageSource(engineCore.TextureCubeFace.PositiveX + i1, img);
1297
1348
  if (++loadedCount === 6) {
1298
1349
  mipmap && texture.generateMipmaps();
1299
1350
  resolve(texture);
1300
1351
  }
1301
- };
1302
- img.onerror = function(e) {
1303
- URL.revokeObjectURL(img.src);
1304
- reject(e);
1305
- };
1306
- img.src = URL.createObjectURL(blob);
1352
+ }, reject);
1307
1353
  };
1308
1354
  var loadedCount = 0;
1309
1355
  for(var i1 = 0; i1 < 6; i1++)_loop(i1);
@@ -1316,12 +1362,6 @@ TextureCubeDecoder = __decorate([
1316
1362
  decoder("TextureCube")
1317
1363
  ], TextureCubeDecoder);
1318
1364
 
1319
- function _instanceof(left, right) {
1320
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
1321
- return !!right[Symbol.hasInstance](left);
1322
- } else return left instanceof right;
1323
- }
1324
-
1325
1365
  /**
1326
1366
  * The Prefab resource.
1327
1367
  */ var PrefabResource = /*#__PURE__*/ function(ReferResource1) {
@@ -3756,30 +3796,7 @@ function registerGLTFParser(pipeline) {
3756
3796
  /**
3757
3797
  * Load image buffer
3758
3798
  */ GLTFUtils.loadImageBuffer = function loadImageBuffer(imageBuffer, type) {
3759
- return new Promise(function(resolve, reject) {
3760
- var blob = new window.Blob([
3761
- imageBuffer
3762
- ], {
3763
- type: type
3764
- });
3765
- var img = new Image();
3766
- img.onerror = function() {
3767
- URL.revokeObjectURL(img.src);
3768
- reject(new Error("Failed to load image buffer"));
3769
- };
3770
- img.onload = function() {
3771
- // Call requestAnimationFrame to avoid iOS's bug.
3772
- requestAnimationFrame(function() {
3773
- URL.revokeObjectURL(img.src);
3774
- resolve(img);
3775
- img.onload = null;
3776
- img.onerror = null;
3777
- img.onabort = null;
3778
- });
3779
- };
3780
- img.crossOrigin = "anonymous";
3781
- img.src = URL.createObjectURL(blob);
3782
- });
3799
+ return Promise.resolve(ImageDecoder._decode(imageBuffer, type, type));
3783
3800
  };
3784
3801
  /**
3785
3802
  * Parse the glb format.
@@ -3918,7 +3935,7 @@ function registerGLTFParser(pipeline) {
3918
3935
  }
3919
3936
  });
3920
3937
  } else {
3921
- return GLTFUtils.loadImageBuffer(bufferData, textureRestoreInfo.mimeType).then(function(image) {
3938
+ return ImageDecoder._decode(bufferData, _this.resource.url, textureRestoreInfo.mimeType).then(function(image) {
3922
3939
  texture.setImageSource(image);
3923
3940
  texture.generateMipmaps();
3924
3941
  });
@@ -5186,8 +5203,9 @@ exports.GLTFTextureParser = /*#__PURE__*/ function(GLTFParser1) {
5186
5203
  var bufferView = glTF.bufferViews[bufferViewIndex];
5187
5204
  texture = context.get(GLTFParserType.Buffer).then(function(buffers) {
5188
5205
  var buffer = buffers[bufferView.buffer];
5189
- var imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);
5190
- return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then(function(image) {
5206
+ var _bufferView_byteOffset;
5207
+ var imageBuffer = new Uint8Array(buffer, (_bufferView_byteOffset = bufferView.byteOffset) != null ? _bufferView_byteOffset : 0, bufferView.byteLength);
5208
+ return ImageDecoder._decode(imageBuffer, imageName || textureName || url, mimeType).then(function(image) {
5191
5209
  var width = image.width, height = image.height;
5192
5210
  var format = engineCore.TextureFormat.R8G8B8A8;
5193
5211
  var _samplerInfo_mipmap;
@@ -5201,8 +5219,6 @@ exports.GLTFTextureParser = /*#__PURE__*/ function(GLTFParser1) {
5201
5219
  context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);
5202
5220
  return texture;
5203
5221
  });
5204
- }).catch(function(e) {
5205
- engineCore.Logger.error("GLTFTextureParser: image buffer error", e);
5206
5222
  });
5207
5223
  }
5208
5224
  return texture;
@@ -5926,26 +5942,102 @@ var PrimitiveMeshLoader = /*#__PURE__*/ function(Loader) {
5926
5942
  }
5927
5943
  var _proto = PrimitiveMeshLoader.prototype;
5928
5944
  _proto.load = function load(item, resourceManager) {
5929
- var engine = resourceManager.engine;
5945
+ var _this = this;
5930
5946
  return resourceManager._request(item.url, _extends({}, item, {
5931
5947
  type: "json"
5932
5948
  })).then(function(data) {
5933
- switch(data.type){
5934
- case "sphere":
5935
- return engineCore.PrimitiveMesh.createSubdivisionSurfaceSphere(engine, data.sphereRadius, data.sphereStep);
5936
- case "capsule":
5937
- return engineCore.PrimitiveMesh.createCapsule(engine, data.capsuleRadius, data.capsuleHeight, data.capsuleRadialSegments, data.capsuleHeightSegments);
5938
- case "cone":
5939
- return engineCore.PrimitiveMesh.createCone(engine, data.coneRadius, data.coneHeight, data.coneRadialSegment, data.coneHeightSegment);
5940
- case "cuboid":
5941
- return engineCore.PrimitiveMesh.createCuboid(engine, data.cuboidWidth, data.cuboidHeight, data.cuboidDepth);
5942
- case "cylinder":
5943
- return engineCore.PrimitiveMesh.createCylinder(engine, data.cylinderRadiusTop, data.cylinderRadiusBottom, data.cylinderHeight, data.cylinderRadialSegment, data.cylinderHeightSegment);
5944
- case "plane":
5945
- return engineCore.PrimitiveMesh.createPlane(engine, data.planeWidth, data.planeHeight, data.planeHorizontalSegments, data.planeVerticalSegments);
5946
- case "torus":
5947
- return engineCore.PrimitiveMesh.createTorus(engine, data.torusRadius, data.torusTubeRadius, data.torusRadialSegments, data.torusTubularSegments, data.torusArc);
5949
+ return _this._createPrimitiveMesh(resourceManager.engine, data);
5950
+ });
5951
+ };
5952
+ _proto._createPrimitiveMesh = function _createPrimitiveMesh(engine, data) {
5953
+ var type = data.type;
5954
+ switch(type){
5955
+ case "sphere":
5956
+ return engineCore.PrimitiveMesh.createSphere(engine, data.sphereRadius, this._parseRangeOptions(data.sphereOptions));
5957
+ case "cuboid":
5958
+ return engineCore.PrimitiveMesh.createCuboid(engine, data.cuboidWidth, data.cuboidHeight, data.cuboidDepth, data.cuboidOptions);
5959
+ case "plane":
5960
+ return engineCore.PrimitiveMesh.createPlane(engine, data.planeWidth, data.planeDepth, data.planeOptions);
5961
+ case "cylinder":
5962
+ return engineCore.PrimitiveMesh.createCylinder(engine, data.cylinderRadius, data.cylinderHeight, this._parseRangeOptions(data.cylinderOptions));
5963
+ case "truncatedCone":
5964
+ return engineCore.PrimitiveMesh.createTruncatedCone(engine, data.truncatedConeTopRadius, data.truncatedConeBottomRadius, data.truncatedConeHeight, this._parseRangeOptions(data.truncatedConeOptions));
5965
+ case "cone":
5966
+ return engineCore.PrimitiveMesh.createCone(engine, data.coneRadius, data.coneHeight, this._parseRangeOptions(data.coneOptions));
5967
+ case "capsule":
5968
+ return engineCore.PrimitiveMesh.createCapsule(engine, data.capsuleRadius, data.capsuleCylinderHeight, data.capsuleOptions);
5969
+ case "torus":
5970
+ return engineCore.PrimitiveMesh.createTorus(engine, data.torusRadius, data.torusTubeRadius, this._parseRangeOptions(data.torusOptions));
5971
+ case "circle":
5972
+ return engineCore.PrimitiveMesh.createCircle(engine, data.circleRadius, this._parseRangeOptions(data.circleOptions));
5973
+ case "ring":
5974
+ return engineCore.PrimitiveMesh.createRing(engine, data.ringOuterRadius, data.ringInnerRadius, this._parseRangeOptions(data.ringOptions));
5975
+ case "tetrahedron":
5976
+ return engineCore.PrimitiveMesh.createTetrahedron(engine, data.tetrahedronRadius, data.tetrahedronOptions);
5977
+ case "octahedron":
5978
+ return engineCore.PrimitiveMesh.createOctahedron(engine, data.octahedronRadius, data.octahedronOptions);
5979
+ case "icosahedron":
5980
+ return engineCore.PrimitiveMesh.createIcosahedron(engine, data.icosahedronRadius, data.icosahedronOptions);
5981
+ case "dodecahedron":
5982
+ return engineCore.PrimitiveMesh.createDodecahedron(engine, data.dodecahedronRadius, data.dodecahedronOptions);
5983
+ case "revolvedSurface":
5984
+ return engineCore.PrimitiveMesh.createRevolvedSurface(engine, this._parseVector2s(data.revolvedSurfaceProfile), this._parseRangeOptions(data.revolvedSurfaceOptions));
5985
+ case "tube":
5986
+ return engineCore.PrimitiveMesh.createTube(engine, this._parseVector3s(data.tubePath), data.tubeRadius, data.tubeOptions);
5987
+ case "polygon":
5988
+ return engineCore.PrimitiveMesh.createPolygon(engine, this._parseShapes(data.polygonShapes), data.polygonOptions);
5989
+ case "extrudedPolygon":
5990
+ if ("extrudedPolygonPath" in data) {
5991
+ return engineCore.PrimitiveMesh.createExtrudedPolygon(engine, this._parseShapes(data.extrudedPolygonShapes), this._parseVector3s(data.extrudedPolygonPath), data.extrudedPolygonOptions);
5992
+ }
5993
+ return engineCore.PrimitiveMesh.createExtrudedPolygon(engine, this._parseShapes(data.extrudedPolygonShapes), data.extrudedPolygonDepth, data.extrudedPolygonOptions);
5994
+ case "torusKnot":
5995
+ return engineCore.PrimitiveMesh.createTorusKnot(engine, data.torusKnotRadius, data.torusKnotTubeRadius, data.torusKnotAxisTurns, data.torusKnotCrossSectionTurns, data.torusKnotOptions);
5996
+ case "roundedCuboid":
5997
+ return engineCore.PrimitiveMesh.createRoundedCuboid(engine, data.roundedCuboidWidth, data.roundedCuboidHeight, data.roundedCuboidDepth, data.roundedCuboidCornerRadius, data.roundedCuboidOptions);
5998
+ default:
5999
+ {
6000
+ var unsupportedType = type;
6001
+ throw new Error("Unknown primitive mesh type: " + unsupportedType);
6002
+ }
6003
+ }
6004
+ };
6005
+ _proto._parseRangeOptions = function _parseRangeOptions(options) {
6006
+ if (!options) {
6007
+ return;
6008
+ }
6009
+ var result;
6010
+ for(var key in options){
6011
+ var value = options[key];
6012
+ if (value && (typeof value === "undefined" ? "undefined" : _type_of(value)) === "object") {
6013
+ var range = value;
6014
+ (result != null ? result : result = _extends({}, options))[key] = new engineMath.Vector2(range.x, range.y);
5948
6015
  }
6016
+ }
6017
+ return result != null ? result : options;
6018
+ };
6019
+ _proto._parseVector2s = function _parseVector2s(points) {
6020
+ return points.map(function(param) {
6021
+ var x = param.x, y = param.y;
6022
+ return new engineMath.Vector2(x, y);
6023
+ });
6024
+ };
6025
+ _proto._parseVector3s = function _parseVector3s(points) {
6026
+ return points.map(function(param) {
6027
+ var x = param.x, y = param.y, z = param.z;
6028
+ return new engineMath.Vector3(x, y, z);
6029
+ });
6030
+ };
6031
+ _proto._parseShapes = function _parseShapes(shapes) {
6032
+ var _this = this;
6033
+ return shapes.map(function(param) {
6034
+ var points = param.points, holes = param.holes;
6035
+ return {
6036
+ points: _this._parseVector2s(points),
6037
+ holes: holes == null ? void 0 : holes.map(function(hole) {
6038
+ return _this._parseVector2s(hole);
6039
+ })
6040
+ };
5949
6041
  });
5950
6042
  };
5951
6043
  return PrimitiveMeshLoader;
@@ -6249,7 +6341,7 @@ var TextureLoader = /*#__PURE__*/ function(Loader) {
6249
6341
  };
6250
6342
  _proto._decodeImage = function _decodeImage(buffer, item, resourceManager) {
6251
6343
  var _this = this;
6252
- return decodeImage(buffer, item.url).then(function(img) {
6344
+ return ImageDecoder._decode(buffer, item.url).then(function(img) {
6253
6345
  var _item_params;
6254
6346
  var _ref = (_item_params = item.params) != null ? _item_params : {}, _ref_format = _ref.format, format = _ref_format === void 0 ? engineCore.TextureFormat.R8G8B8A8 : _ref_format, _ref_isSRGBColorSpace = _ref.isSRGBColorSpace, isSRGBColorSpace = _ref_isSRGBColorSpace === void 0 ? true : _ref_isSRGBColorSpace, _ref_mipmap = _ref.mipmap, mipmap = _ref_mipmap === void 0 ? true : _ref_mipmap;
6255
6347
  var engine = resourceManager.engine;
@@ -6308,7 +6400,7 @@ var TextureContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
6308
6400
  texture.mipmapCount > 1 && texture.generateMipmaps();
6309
6401
  return texture;
6310
6402
  }
6311
- return decodeImage(buffer, _this.url).then(function(img) {
6403
+ return ImageDecoder._decode(buffer, _this.url).then(function(img) {
6312
6404
  texture.setImageSource(img);
6313
6405
  texture.mipmapCount > 1 && texture.generateMipmaps();
6314
6406
  return texture;
@@ -6317,26 +6409,6 @@ var TextureContentRestorer = /*#__PURE__*/ function(ContentRestorer) {
6317
6409
  };
6318
6410
  return TextureContentRestorer;
6319
6411
  }(engineCore.ContentRestorer);
6320
- function decodeImage(buffer, url) {
6321
- return new engineCore.AssetPromise(function(resolve, reject) {
6322
- var objectUrl = URL.createObjectURL(new Blob([
6323
- buffer
6324
- ]));
6325
- var image = new Image();
6326
- var releaseObjectUrl = function() {
6327
- return URL.revokeObjectURL(objectUrl);
6328
- };
6329
- image.onload = function() {
6330
- releaseObjectUrl();
6331
- resolve(image);
6332
- };
6333
- image.onerror = function() {
6334
- releaseObjectUrl();
6335
- reject(new Error('TextureLoader: failed to decode texture "' + url + '" (' + buffer.byteLength + " bytes)."));
6336
- };
6337
- image.src = objectUrl;
6338
- });
6339
- }
6340
6412
 
6341
6413
  var AudioLoader = /*#__PURE__*/ function(Loader) {
6342
6414
  _inherits(AudioLoader, Loader);
@@ -6733,6 +6805,25 @@ KHR_materials_clearcoat = __decorate([
6733
6805
  registerGLTFExtension("KHR_materials_clearcoat", GLTFExtensionMode.AdditiveParse)
6734
6806
  ], KHR_materials_clearcoat);
6735
6807
 
6808
+ var KHR_materials_emissive_strength = /*#__PURE__*/ function(GLTFExtensionParser) {
6809
+ _inherits(KHR_materials_emissive_strength, GLTFExtensionParser);
6810
+ function KHR_materials_emissive_strength() {
6811
+ return GLTFExtensionParser.apply(this, arguments) || this;
6812
+ }
6813
+ var _proto = KHR_materials_emissive_strength.prototype;
6814
+ _proto.additiveParse = function additiveParse(_, material, schema) {
6815
+ var _schema_emissiveStrength = schema.emissiveStrength, emissiveStrength = _schema_emissiveStrength === void 0 ? 1 : _schema_emissiveStrength;
6816
+ if (emissiveStrength !== 1 && _instanceof(material, engineCore.PBRMaterial)) {
6817
+ var color = material.emissiveColor;
6818
+ color.set(color.r * emissiveStrength, color.g * emissiveStrength, color.b * emissiveStrength, color.a);
6819
+ }
6820
+ };
6821
+ return KHR_materials_emissive_strength;
6822
+ }(GLTFExtensionParser);
6823
+ KHR_materials_emissive_strength = __decorate([
6824
+ registerGLTFExtension("KHR_materials_emissive_strength", GLTFExtensionMode.AdditiveParse)
6825
+ ], KHR_materials_emissive_strength);
6826
+
6736
6827
  var KHR_materials_ior = /*#__PURE__*/ function(GLTFExtensionParser) {
6737
6828
  _inherits(KHR_materials_ior, GLTFExtensionParser);
6738
6829
  function KHR_materials_ior() {