-
-
Notifications
You must be signed in to change notification settings - Fork 397
Replace refId with url for asset reference resolution #2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9f4ea52
98deada
8c914ae
d04e57a
f8ef31d
5de1828
b4a2831
e28c482
e342936
56de240
a97dd8f
2a357e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AssetPromise, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AssetType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ContentRestorer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LoadItem, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Loader, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RequestConfig, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -12,65 +13,117 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TextureWrapMode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceLoader | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "@galacean/engine-core"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Texture2DContentRestorer } from "./Texture2DContentRestorer"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { decode } from "./resource-deserialize"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { FileHeader } from "./resource-deserialize/utils/FileHeader"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @resourceLoader(AssetType.Texture2D, ["png", "jpg", "webp", "jpeg"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function loadImageFromBuffer(buffer: ArrayBuffer): AssetPromise<HTMLImageElement> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new AssetPromise((resolve, reject) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const blob = new Blob([buffer]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const img = new Image(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| img.onload = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| URL.revokeObjectURL(img.src); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(img); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| img.onerror = reject; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| img.src = URL.createObjectURL(blob); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revoke the blob URL on The helper only releases the object URL in 🧹 Suggested fix function loadImageFromBuffer(buffer: ArrayBuffer): AssetPromise<HTMLImageElement> {
return new AssetPromise((resolve, reject) => {
const blob = new Blob([buffer]);
const img = new Image();
+ const objectURL = URL.createObjectURL(blob);
img.onload = () => {
- URL.revokeObjectURL(img.src);
+ URL.revokeObjectURL(objectURL);
resolve(img);
};
- img.onerror = reject;
- img.src = URL.createObjectURL(blob);
+ img.onerror = (event) => {
+ URL.revokeObjectURL(objectURL);
+ reject(event);
+ };
+ img.src = objectURL;
});
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @resourceLoader(AssetType.Texture2D, ["png", "jpg", "webp", "jpeg", "tex"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Texture2DLoader extends Loader<Texture2D> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| override load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Texture2D> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const url = item.url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestConfig = <RequestConfig>{ ...item, type: "arraybuffer" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new AssetPromise((resolve, reject, setTaskCompleteProgress, setTaskDetailProgress) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const url = item.url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const requestConfig = <RequestConfig>{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...item, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: "image" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceManager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ._request<HTMLImageElement>(url, requestConfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ._request<ArrayBuffer>(url, requestConfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .onProgress(setTaskCompleteProgress, setTaskDetailProgress) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then((image) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format = TextureFormat.R8G8B8A8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anisoLevel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrapModeU, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrapModeV, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterMode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSRGBColorSpace = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mipmap = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } = (item.params as Partial<Texture2DParams>) ?? {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { width, height } = image; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const engine = resourceManager.engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then((buffer) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (FileHeader.checkMagic(buffer)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| decode<Texture2D>(buffer, resourceManager.engine).then((texture) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceManager.addContentRestorer(new Texture2DContentRestorer(texture, url, requestConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(texture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, reject); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| loadImageFromBuffer(buffer).then((img) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the no-magic serialized-texture path reachable.
💡 One straightforward fix- if (FileHeader.checkMagic(buffer)) {
+ const isSerializedTexture = FileHeader.checkMagic(buffer) || /\.tex(?:$|[?#])/i.test(url);
+ if (isSerializedTexture) {- if (FileHeader.checkMagic(buffer)) {
+ const isSerializedTexture = FileHeader.checkMagic(buffer) || /\.tex(?:$|[?#])/i.test(this.url);
+ if (isSerializedTexture) {Also applies to: 111-114 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const texture = this._createTexture(img, item, resourceManager); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceManager.addContentRestorer(new Texture2DContentRestorer(texture, url, requestConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(texture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, reject); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .catch(reject); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const generateMipmap = TextureUtils.supportGenerateMipmapsWithCorrection( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| engine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mipmap, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSRGBColorSpace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private _createTexture(img: HTMLImageElement, item: LoadItem, resourceManager: ResourceManager): Texture2D { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format = TextureFormat.R8G8B8A8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anisoLevel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrapModeU, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrapModeV, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filterMode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSRGBColorSpace = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mipmap = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } = (item.params as Partial<Texture2DParams>) ?? {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { width, height } = img; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const engine = resourceManager.engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const texture = new Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const generateMipmap = TextureUtils.supportGenerateMipmapsWithCorrection( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| engine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mipmap, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isSRGBColorSpace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.anisoLevel = anisoLevel ?? texture.anisoLevel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.filterMode = filterMode ?? texture.filterMode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.wrapModeU = wrapModeU ?? texture.wrapModeU; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.wrapModeV = wrapModeV ?? texture.wrapModeV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const texture = new Texture2D(engine, width, height, format, generateMipmap, isSRGBColorSpace); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.anisoLevel = anisoLevel ?? texture.anisoLevel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.filterMode = filterMode ?? texture.filterMode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.wrapModeU = wrapModeU ?? texture.wrapModeU; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.wrapModeV = wrapModeV ?? texture.wrapModeV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.setImageSource(img); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| generateMipmap && texture.generateMipmaps(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.setImageSource(image); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| generateMipmap && texture.generateMipmaps(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const url = item.url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (url.indexOf("data:") !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.name = url.substring(url.lastIndexOf("/") + 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (url.indexOf("data:") !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const index = url.lastIndexOf("/"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.name = url.substring(index + 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return texture; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Texture2DContentRestorer extends ContentRestorer<Texture2D> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resource: Texture2D, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public url: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public requestConfig: RequestConfig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super(resource); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resourceManager.addContentRestorer(new Texture2DContentRestorer(texture, url, requestConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve(texture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| override restoreContent(): AssetPromise<Texture2D> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const texture = this.resource; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const engine = texture.engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| engine.resourceManager | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // @ts-ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ._request<ArrayBuffer>(this.url, this.requestConfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then((buffer) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (FileHeader.checkMagic(buffer)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return decode<Texture2D>(buffer, engine, texture); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return loadImageFromBuffer(buffer).then((img) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.setImageSource(img); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| texture.generateMipmaps(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return texture; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .catch((e) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| reject(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Treat
keyas part of the cached ref.packages/loader/src/resource-deserialize/resources/schema/BasicSchema.ts:104now defines refs as{ url, key? }, sokeyis the only discriminator for sibling sub-assets. The fast path still returns_objectPool[url]directly, which can make{ url: "foo", key: "textures[0]" }and{ url: "foo", key: "textures[1]" }resolve to the same cached object once the pool is warm. Canonicalize the full keyed URL and use it consistently for cache lookup/loading, or resolve the keyed sub-resource from the cached root before returning.🛠️ Proposed fix
🤖 Prompt for AI Agents