Skip to content

[codex] resolve asset dependencies by virtual path - #3090

Closed
cptbtptpbcptdtptp wants to merge 6 commits into
galacean:dev/2.0from
cptbtptpbcptdtptp:fix/virtual-path-loading
Closed

[codex] resolve asset dependencies by virtual path#3090
cptbtptpbcptdtptp wants to merge 6 commits into
galacean:dev/2.0from
cptbtptpbcptdtptp:fix/virtual-path-loading

Conversation

@cptbtptpbcptdtptp

@cptbtptpbcptdtptp cptbtptpbcptdtptp commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

要解决的问题

Galacean 编辑器导出的 project.json 中同时记录了资产的 virtualPath 和真实远端 path。其中:

  • virtualPath 是资产在项目文件系统中的唯一身份,也是所有资产引用的基准。
  • path 只是该资产最终请求时使用的物理地址,可能是经过 hash 处理、没有原始目录结构的 CDN 地址。
  • Atlas、Font、glTF 等资产内部既可能保存完整的 virtualPath,也可能保存相对于当前资产 virtualPath 的相对路径。

此前 runtime 在资产进入 Loader 之前就过早地把 virtualPath 替换成了远端 path。这样一来,如果 Assets/UI/Hero.atlas 被映射到一个没有目录结构的 CDN 地址,Atlas 中的 ../Textures/Hero.png 就会基于 CDN 地址解析,而不是基于 Assets/UI/Hero.atlas 解析,最终无法定位到 Assets/Textures/Hero.png,也就无法通过 project.json 找到图片真正的远端地址。

同样的问题还影响了:

  • glTF 外部 buffer、普通纹理以及 KHR_texture_basisu 纹理的相对路径。
  • Font 描述文件引用的字体文件,以及直接加载的 SourceFont。
  • glTF 内容丢失后的恢复请求。
  • 资源缓存、正在加载的 Promise、子资产回调和 getAssetPath:这些流程使用远端 path 作为身份时,两个不同 virtualPath 如果映射到同一个远端地址,会被错误地视为同一个资产。
  • 非 HTTP 路径解析时,首段路径可能被 URL 规则转换为小写,例如 Assets 被解析成 assets,导致无法命中区分大小写的 virtualPath 映射。

根因

Runtime 混用了两套路径语义:

  1. virtualPath 本应负责资产身份、缓存和相对引用解析。
  2. remote path 本应只在真正发起网络请求时使用。

但原有流程在 ResourceManager._loadSingleItem 中提前完成了 virtualPath 到 remote path 的转换,后续 Loader、缓存和内容恢复流程只能看到物理地址,因此无法再按照编辑器虚拟文件系统的目录关系解析依赖。

修复方案

  • Loader、缓存、加载中 Promise、子资产回调和 getAssetPath 统一使用 virtualPath 作为逻辑身份。
  • 新增虚拟文件系统路径解析:优先按当前资产的 virtualPath 解析相对路径,同时兼容资产内部已经保存完整 virtualPath 的情况。
  • virtualPath 到 remote path 的转换延迟到 _request 等实际请求边界。
  • 修复非 HTTP 路径解析,保留首段大小写和开头的 /
  • SpriteAtlas 内部图片引用改为先解析出图片 virtualPath,再通过 ResourceManager 加载。
  • Font 和 SourceFont 只在 FontFace 请求边界使用 remote path,字体资源身份仍保持为 virtualPath。
  • glTF schema、外部 buffer、普通纹理、KTX2 纹理和内容恢复信息全部保留 virtualPath,在每次实际请求时重新映射当前远端地址。

修复后的完整链路为:

资产内部相对路径
  → 基于当前资产 virtualPath 解析
  → 得到依赖资产 virtualPath
  → 通过 project.json 映射 remote path
  → 发起实际请求

验证

  • pnpm run b:types 通过。
  • virtualPath 定向测试:30 个用例通过。
  • Loader 回归测试:107 个用例通过。
  • 完整 Vitest 测试:123 个测试文件、1660 个用例全部通过。
  • Pre-commit ESLint 和 Prettier 检查通过。
  • git diff --check 通过。

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9acc6ab8-831e-47b8-a295-a47eb77f86b9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.57%. Comparing base (bd34daa) to head (83900d6).

Files with missing lines Patch % Lines
packages/loader/src/FontLoader.ts 87.50% 1 Missing ⚠️
packages/loader/src/GLTFContentRestorer.ts 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           dev/2.0    #3090      +/-   ##
===========================================
+ Coverage    85.42%   85.57%   +0.15%     
===========================================
  Files          811      811              
  Lines        94654    94680      +26     
  Branches     11512    11559      +47     
===========================================
+ Hits         80854    81022     +168     
+ Misses       13710    13568     -142     
  Partials        90       90              
Flag Coverage Δ
unittests 85.57% <92.85%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cptbtptpbcptdtptp

Copy link
Copy Markdown
Collaborator Author

关闭此 PR:相关问题的大部分修复已与 #3084 重复,后续以 #3084 为准。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant