Skip to content

Commit

Permalink
Improve handling of texture names in glTF loader (#15709)
Browse files Browse the repository at this point in the history
  • Loading branch information
bghgary authored Oct 17, 2024
1 parent 7e12d90 commit b9c17a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/dev/core/src/Materials/Textures/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,9 @@ export class Texture extends BaseTexture {
serializationObject._creationFlags = this._creationFlags;
serializationObject._useSRGBBuffer = this._useSRGBBuffer;
if (Texture._SerializeInternalTextureUniqueId) {
serializationObject.internalTextureUniqueId = this._texture?.uniqueId ?? undefined;
serializationObject.internalTextureUniqueId = this._texture?.uniqueId;
}
serializationObject.internalTextureLabel = this._texture?.label;
serializationObject.noMipmap = this._noMipmap;

this.name = savedName;
Expand Down Expand Up @@ -1003,8 +1004,12 @@ export class Texture extends BaseTexture {
}
}

if (hasInternalTextureUniqueId && !internalTexture) {
texture?._texture?._setUniqueId(parsedTexture.internalTextureUniqueId);
if (texture && texture._texture) {
if (hasInternalTextureUniqueId && !internalTexture) {
texture._texture._setUniqueId(parsedTexture.internalTextureUniqueId);
}

texture._texture.label = parsedTexture.internalTextureLabel;
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/dev/core/src/Materials/Textures/textureSampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class TextureSampler {
public _comparisonFunction: number = 0;

/**
* Used for debugging purpose only
* General label used for debugging or storing a name.
*/
public label?: string;

Expand Down
10 changes: 10 additions & 0 deletions packages/dev/loaders/src/glTF/2.0/glTFLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2430,13 +2430,23 @@ export class GLTFLoader implements IGLTFLoader {
const name = image.uri || `${this._fileName}#image${image.index}`;
const dataUrl = `data:${this._uniqueRootUrl}${name}`;
babylonTexture.updateURL(dataUrl, data);

// Set the internal texture label.
const internalTexture = babylonTexture.getInternalTexture();
if (internalTexture) {
internalTexture.label = image.name;
}
})
);

babylonTexture.wrapU = samplerData.wrapU;
babylonTexture.wrapV = samplerData.wrapV;
assign(babylonTexture);

if (this._parent.useGltfTextureNames) {
babylonTexture.name = image.name || image.uri || `image${image.index}`;
}

return Promise.all(promises).then(() => {
return babylonTexture;
});
Expand Down
7 changes: 7 additions & 0 deletions packages/dev/loaders/src/glTF/glTFFileLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ abstract class GLTFLoaderOptions {
this.useSRGBBuffers = options.useSRGBBuffers ?? this.useSRGBBuffers;
this.targetFps = options.targetFps ?? this.targetFps;
this.alwaysComputeSkeletonRootNode = options.alwaysComputeSkeletonRootNode ?? this.alwaysComputeSkeletonRootNode;
this.useGltfTextureNames = options.useGltfTextureNames ?? this.useGltfTextureNames;
this.preprocessUrlAsync = options.preprocessUrlAsync ?? this.preprocessUrlAsync;
this.customRootNode = options.customRootNode;
this.onMeshLoaded = options.onMeshLoaded;
Expand Down Expand Up @@ -329,6 +330,12 @@ abstract class GLTFLoaderOptions {
*/
public alwaysComputeSkeletonRootNode = false;

/**
* If true, the loader will derive the name for Babylon textures from the glTF texture name, image name, or image url. Defaults to false.
* Note that it is possible for multiple Babylon textures to share the same name when the Babylon textures load from the same glTF texture or image.
*/
public useGltfTextureNames = false;

/**
* Function called before loading a url referenced by the asset.
* @param url url referenced by the asset
Expand Down

0 comments on commit b9c17a2

Please sign in to comment.