Skip to content

Commit eac6471

Browse files
authored
Uniquify gltf texture asset names (playcanvas#3390)
1 parent ff57d3b commit eac6471

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

src/resources/parser/glb-parser.js

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,8 @@ const applySampler = function (texture, gltfSampler) {
17771777
}
17781778
};
17791779

1780+
let gltfTextureUniqueId = 0;
1781+
17801782
// load an image
17811783
const loadImageAsync = function (gltfImage, index, bufferViews, urlBase, registry, options, callback) {
17821784
const preprocess = options && options.image && options.image.preprocess;
@@ -1800,39 +1802,16 @@ const loadImageAsync = function (gltfImage, index, bufferViews, urlBase, registr
18001802
'image/vnd-ms.dds': 'dds'
18011803
};
18021804

1803-
const loadTexture = function (url, mimeType, crossOrigin, isBlobUrl) {
1804-
const name = gltfImage.name || 'gltf-texture';
1805-
1806-
// construct the asset file
1807-
const file = { url: url };
1808-
if (mimeType) {
1809-
const extension = mimeTypeFileExtensions[mimeType];
1810-
if (extension) {
1811-
file.filename = name + '-' + index + '.' + extension;
1812-
}
1813-
}
1814-
1815-
// create and load the asset
1816-
const asset = new Asset('texture_' + index, 'texture', file, null, { crossOrigin: crossOrigin });
1817-
asset.on('load', function () {
1818-
if (isBlobUrl) {
1819-
URL.revokeObjectURL(url);
1820-
}
1821-
onLoad(asset);
1822-
});
1823-
asset.on('error', callback);
1824-
registry.add(asset);
1825-
registry.load(asset);
1826-
};
1827-
1828-
const loadTextureFromData = function (mimeType, bufferView) {
1829-
const name = gltfImage.name || 'gltf-texture';
1805+
const loadTexture = function (url, bufferView, mimeType, options) {
1806+
const name = (gltfImage.name || 'gltf-texture') + '-' + gltfTextureUniqueId++;
18301807

18311808
// construct the asset file
18321809
const file = {
1833-
url: name + '-' + index,
1834-
contents: bufferView
1810+
url: url || name
18351811
};
1812+
if (bufferView) {
1813+
file.contents = bufferView;
1814+
}
18361815
if (mimeType) {
18371816
const extension = mimeTypeFileExtensions[mimeType];
18381817
if (extension) {
@@ -1841,7 +1820,7 @@ const loadImageAsync = function (gltfImage, index, bufferViews, urlBase, registr
18411820
}
18421821

18431822
// create and load the asset
1844-
const asset = new Asset(name, 'texture', file, null);
1823+
const asset = new Asset(name, 'texture', file, null, options);
18451824
asset.on('load', onLoad);
18461825
asset.on('error', callback);
18471826
registry.add(asset);
@@ -1861,13 +1840,13 @@ const loadImageAsync = function (gltfImage, index, bufferViews, urlBase, registr
18611840
if (gltfImage.hasOwnProperty('uri')) {
18621841
// uri specified
18631842
if (isDataURI(gltfImage.uri)) {
1864-
loadTexture(gltfImage.uri, getDataURIMimeType(gltfImage.uri));
1843+
loadTexture(gltfImage.uri, null, getDataURIMimeType(gltfImage.uri), null);
18651844
} else {
1866-
loadTexture(path.join(urlBase, gltfImage.uri), null, "anonymous");
1845+
loadTexture(path.join(urlBase, gltfImage.uri), null, null, { crossOrigin: "anonymous" });
18671846
}
18681847
} else if (gltfImage.hasOwnProperty('bufferView') && gltfImage.hasOwnProperty('mimeType')) {
18691848
// bufferview
1870-
loadTextureFromData(gltfImage.mimeType, bufferViews[gltfImage.bufferView]);
1849+
loadTexture(null, bufferViews[gltfImage.bufferView], gltfImage.mimeType, null);
18711850
} else {
18721851
// fail
18731852
callback("Invalid image found in gltf (neither uri or bufferView found). index=" + index);

0 commit comments

Comments
 (0)