zhuda-laya-engine 0.0.0-stage → 2.1.0
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/AGENTS.md +236 -0
- package/CHANGELOG.md +431 -0
- package/LICENSE +41 -0
- package/README.md +247 -2
- package/assets/branding/layaair-tribute-mark.svg +5 -0
- package/assets/branding/zhuda-layaair-mark.svg +34 -0
- package/assets/prefabs/CoinFountainVfx.lh +197 -0
- package/assets/prefabs/ConfirmActionButton.lh +87 -0
- package/assets/prefabs/LevelUpAuraVfx.lh +136 -0
- package/assets/prefabs/MineExplosionParticle.lh +352 -0
- package/assets/prefabs/NineSliceDialogFrame.lh +171 -0
- package/assets/prefabs/SwordSlashTrailVfx.lh +117 -0
- package/assets/prefabs/TacticalHudBar.lh +134 -0
- package/assets/prefabs/TelemetryDashboard.lh +87 -0
- package/assets/prefabs/UnityCrystalGuardian.lh +144 -0
- package/assets/prefabs/ZhudaSpine42Mascot.lh +183 -0
- package/assets/scenes/MinesweeperStage.ls +1180 -0
- package/dist/asset-calibration-report.json +969 -0
- package/dist/build-manifest.json +254 -0
- package/dist/sdk/zhuda-laya-csg.cjs +710 -0
- package/dist/sdk/zhuda-laya-csg.esm.js +683 -0
- package/dist/sdk/zhuda-laya-csg.min.cjs +7 -0
- package/dist/sdk/zhuda-laya-csg.min.esm.js +7 -0
- package/dist/sdk/zhuda-laya-demo.cjs +5990 -0
- package/dist/sdk/zhuda-laya-demo.esm.js +5956 -0
- package/dist/sdk/zhuda-laya-demo.min.cjs +12 -0
- package/dist/sdk/zhuda-laya-demo.min.esm.js +12 -0
- package/dist/sdk/zhuda-laya-engine.cjs +52628 -0
- package/dist/sdk/zhuda-laya-engine.esm.js +51848 -0
- package/dist/sdk/zhuda-laya-engine.min.cjs +2710 -0
- package/dist/sdk/zhuda-laya-engine.min.esm.js +2710 -0
- package/docs/3d-acceptance.md +47 -0
- package/docs/3d-integration.md +190 -0
- package/docs/README.md +112 -0
- package/docs/SUPPORT.md +21 -0
- package/docs/agent-development.md +219 -0
- package/docs/animation-control.md +213 -0
- package/docs/asset-loading.md +97 -0
- package/docs/assets/reference-games.jpg +0 -0
- package/docs/assets/solana-qr.png +0 -0
- package/docs/assets/sponsor-pass.svg +32 -0
- package/docs/audio.md +319 -0
- package/docs/backend-delivery.md +218 -0
- package/docs/basis-textures.md +94 -0
- package/docs/browser-sdk-releases.json +204 -0
- package/docs/browser-sdk.md +187 -0
- package/docs/cinematics.md +267 -0
- package/docs/cloth.md +280 -0
- package/docs/combat-runtime.md +82 -0
- package/docs/consumer-route.md +169 -0
- package/docs/consumer-tooling.md +518 -0
- package/docs/crowd-runtime.md +195 -0
- package/docs/developer-tooling.md +191 -0
- package/docs/film-scenes.md +1504 -0
- package/docs/filmic-rendering.md +743 -0
- package/docs/flythrough.md +89 -0
- package/docs/gpu-depth.md +207 -0
- package/docs/grass.md +127 -0
- package/docs/hdr-environment.md +76 -0
- package/docs/instanced-meshes.md +137 -0
- package/docs/layout-and-media.md +37 -0
- package/docs/layout-wireframe.md +80 -0
- package/docs/lighting-depth.md +185 -0
- package/docs/minigame-packaging.md +79 -0
- package/docs/model-editor.md +66 -0
- package/docs/model-lod.md +122 -0
- package/docs/model-pipeline.md +74 -0
- package/docs/model-showcase.md +365 -0
- package/docs/overview.md +183 -0
- package/docs/plans/README.md +133 -0
- package/docs/plans/TASK-TEMPLATE.md +64 -0
- package/docs/plans/TASK-spine38-isolated-topology-20261106.md +89 -0
- package/docs/plans/TEMPLATE.md +44 -0
- package/docs/plans/VERIFICATION-TEMPLATE.md +45 -0
- package/docs/plans/asset-packaging.md +140 -0
- package/docs/plans/cinematic-production/task.md +97 -0
- package/docs/plans/cinematic-production/verification.md +49 -0
- package/docs/plans/consumer-delivery-followups.md +129 -0
- package/docs/plans/desktop-and-agent-gates/hardening-task.md +85 -0
- package/docs/plans/desktop-and-agent-gates/hardening-verification.md +109 -0
- package/docs/plans/desktop-and-agent-gates/task.md +94 -0
- package/docs/plans/desktop-and-agent-gates/verification.md +60 -0
- package/docs/plans/prefab-toolchain.md +73 -0
- package/docs/plans/preview-runtime-hardening.md +68 -0
- package/docs/platform-adapters.md +113 -0
- package/docs/post-quality.md +242 -0
- package/docs/prefab-editor.md +194 -0
- package/docs/prefab-library.md +94 -0
- package/docs/prefab-workflow.md +75 -0
- package/docs/procedural-assets.md +463 -0
- package/docs/procedural-prefabs.md +133 -0
- package/docs/quality-route.md +386 -0
- package/docs/release-policy.md +79 -0
- package/docs/routes/README.md +180 -0
- package/docs/routes/TEMPLATE.md +54 -0
- package/docs/routes/action-adventure-3d-minigame.md +238 -0
- package/docs/routes/apocalypse-survival-3d.md +219 -0
- package/docs/routes/aqua-freeze-2d-minigame.md +183 -0
- package/docs/routes/astral-minimal-film-look.md +141 -0
- package/docs/routes/cascade-slot.md +93 -0
- package/docs/routes/clip-free-garments.md +229 -0
- package/docs/routes/drop-merge-2d-minigame.md +200 -0
- package/docs/routes/dungeon-line-slot.md +87 -0
- package/docs/routes/film-as-data.md +131 -0
- package/docs/routes/film-fight-that-lands.md +198 -0
- package/docs/routes/film-ink-lines.md +120 -0
- package/docs/routes/floating-island.md +229 -0
- package/docs/routes/game-2d.md +168 -0
- package/docs/routes/game-rules-first.md +89 -0
- package/docs/routes/game-to-prefabs.md +249 -0
- package/docs/routes/go-game-server.md +95 -0
- package/docs/routes/good-looking-3d-scene.md +99 -0
- package/docs/routes/high-quality-character.md +263 -0
- package/docs/routes/humanoid-from-shapes.md +97 -0
- package/docs/routes/hunting-action-3d.md +76 -0
- package/docs/routes/idle-gacha-2d.md +67 -0
- package/docs/routes/interactive-film-viewer.md +141 -0
- package/docs/routes/isometric-mystery-2d.md +217 -0
- package/docs/routes/key-art-to-animated-title.md +132 -0
- package/docs/routes/laya-ide-project-cli-web.md +125 -0
- package/docs/routes/layered-cloth-on-body.md +240 -0
- package/docs/routes/low-poly-character-from-shapes.md +475 -0
- package/docs/routes/match-three-2d.md +64 -0
- package/docs/routes/merge-2d.md +59 -0
- package/docs/routes/mini-game-build.md +80 -0
- package/docs/routes/mobile-always-landscape.md +149 -0
- package/docs/routes/mobile-web-fullscreen.md +255 -0
- package/docs/routes/model-from-dcc.md +106 -0
- package/docs/routes/new-game-project.md +100 -0
- package/docs/routes/onboarding-tutorial-2d.md +54 -0
- package/docs/routes/online-lockstep-match.md +473 -0
- package/docs/routes/outdoor-landscape.md +134 -0
- package/docs/routes/outfit-swap-3d.md +144 -0
- package/docs/routes/papercraft-diorama.md +186 -0
- package/docs/routes/platform-fighter-2d.md +235 -0
- package/docs/routes/platformer-2d.md +69 -0
- package/docs/routes/pole-rescue-arcade-3d.md +198 -0
- package/docs/routes/pose-atlas-motion.md +188 -0
- package/docs/routes/prototype-to-center.md +162 -0
- package/docs/routes/raft-brawl-3d-minigame.md +205 -0
- package/docs/routes/rhythm-2d.md +82 -0
- package/docs/routes/rigged-phone-character.md +120 -0
- package/docs/routes/screen-fit-game-box.md +127 -0
- package/docs/routes/shared-islands.md +195 -0
- package/docs/routes/side-view-duel.md +266 -0
- package/docs/routes/single-html-page.md +83 -0
- package/docs/routes/smooth-character-motion.md +162 -0
- package/docs/routes/spine-character.md +175 -0
- package/docs/routes/story-counting-2d.md +219 -0
- package/docs/routes/survivors-2d.md +60 -0
- package/docs/routes/third-person-town-3d.md +240 -0
- package/docs/routes/tower-defence-3d.md +67 -0
- package/docs/routes/ui-test-by-probe.md +219 -0
- package/docs/routes/underwater.md +138 -0
- package/docs/routes/visual-content-prefab.md +101 -0
- package/docs/runtime-hardening.md +108 -0
- package/docs/shader-warm-up.md +125 -0
- package/docs/spring-bones.md +79 -0
- package/docs/stylized-shading.md +171 -0
- package/docs/terrain-shader.md +130 -0
- package/docs/vrm.md +128 -0
- package/docs/walking.md +97 -0
- package/docs/webgpu.md +225 -0
- package/docs/white-label-themes.md +212 -0
- package/docs/zh-CN/README.md +130 -0
- package/examples/browser-sdk/2d-webgpu.html +46 -0
- package/examples/browser-sdk/2d.html +46 -0
- package/examples/browser-sdk/3d-gltf-webgpu.html +50 -0
- package/examples/browser-sdk/3d-gltf.html +50 -0
- package/examples/browser-sdk/3d-webgpu.html +46 -0
- package/examples/browser-sdk/3d.html +46 -0
- package/examples/cinematic-capture/Main.ts +294 -0
- package/examples/cinematic-capture/README.md +27 -0
- package/examples/model-inspector/README.md +15 -0
- package/examples/model-inspector/asset.gltf +158 -0
- package/examples/windows-webgpu/README.md +57 -0
- package/examples/windows-webgpu/bundle.mjs +122 -0
- package/examples/windows-webgpu/main.mjs +302 -0
- package/examples/windows-webgpu/package-lock.json +2353 -0
- package/examples/windows-webgpu/package.json +40 -0
- package/examples/windows-webgpu/squirrel.mjs +66 -0
- package/package.json +259 -4
- package/packages/ci-gate/export-film.mjs +347 -0
- package/packages/ci-gate/verify-sequence.mjs +285 -0
- package/packages/compiler/agent-contract.mjs +545 -0
- package/packages/compiler/ast-scanner.mjs +254 -0
- package/packages/compiler/backend-loader.mjs +62 -0
- package/packages/compiler/branding-assets.mjs +32 -0
- package/packages/compiler/branding.mjs +324 -0
- package/packages/compiler/browser-auto-example.mjs +31 -0
- package/packages/compiler/browser-model-example.mjs +53 -0
- package/packages/compiler/browser-process.mjs +344 -0
- package/packages/compiler/browser-sdk-example.mjs +50 -0
- package/packages/compiler/browser-sdk.mjs +342 -0
- package/packages/compiler/chrome-host.mjs +196 -0
- package/packages/compiler/cli.mjs +22 -0
- package/packages/compiler/compiler-runtime.mjs +41 -0
- package/packages/compiler/consumer-accept.mjs +89 -0
- package/packages/compiler/consumer-build.mjs +641 -0
- package/packages/compiler/consumer-guards.mjs +422 -0
- package/packages/compiler/consumer-publication.mjs +56 -0
- package/packages/compiler/consumer-scripts.mjs +134 -0
- package/packages/compiler/consumer-starter-2d.mjs +54 -0
- package/packages/compiler/consumer-starter-minigame.mjs +30 -0
- package/packages/compiler/editor-http.mjs +210 -0
- package/packages/compiler/editor-request.ts +12 -0
- package/packages/compiler/film-host-client.ts +249 -0
- package/packages/compiler/flythrough-file.mjs +144 -0
- package/packages/compiler/json-locate.mjs +85 -0
- package/packages/compiler/layout-contract.mjs +472 -0
- package/packages/compiler/minigame-bootstrap.mjs +24 -0
- package/packages/compiler/minigame-bridge.mjs +66 -0
- package/packages/compiler/minigame-guards.mjs +107 -0
- package/packages/compiler/minigame-toolchain.mjs +241 -0
- package/packages/compiler/model-crown.mjs +173 -0
- package/packages/compiler/model-editor-client.ts +809 -0
- package/packages/compiler/model-editor.mjs +242 -0
- package/packages/compiler/model-lod.mjs +547 -0
- package/packages/compiler/prefab-check.mjs +288 -0
- package/packages/compiler/prefab-checks.mjs +74 -0
- package/packages/compiler/prefab-dependencies.mjs +102 -0
- package/packages/compiler/prefab-editor-client.ts +950 -0
- package/packages/compiler/prefab-editor-preview.ts +89 -0
- package/packages/compiler/prefab-editor-server.mjs +585 -0
- package/packages/compiler/prefab-editor.mjs +43 -0
- package/packages/compiler/prefab-environment.mjs +107 -0
- package/packages/compiler/prefab-lifecycle.mjs +60 -0
- package/packages/compiler/prefab-preview.mjs +159 -0
- package/packages/compiler/prefab-watch.mjs +459 -0
- package/packages/compiler/runtime3d-types.mjs +91 -0
- package/packages/compiler/stage-probe.mjs +557 -0
- package/packages/compiler/visual-asset-audit.mjs +609 -0
- package/packages/compiler/visual-contract-audit.mjs +173 -0
- package/packages/compiler/webgpu-loader.mjs +153 -0
- package/packages/compiler/zhuda-build.mjs +714 -0
- package/packages/compiler/zhuda-doctor.mjs +186 -0
- package/packages/compiler/zhuda-init.mjs +353 -0
- package/packages/compiler/zhuda-layout.mjs +163 -0
- package/packages/compiler/zhuda-minigame.mjs +636 -0
- package/packages/core/BenchmarkEngine.ts +2 -0
- package/packages/core/BitmapFontContract.ts +159 -0
- package/packages/core/DeclarativeSDK.ts +686 -0
- package/packages/core/IframeSoundAdapter.ts +1824 -0
- package/packages/core/NoSleepAdapter.ts +302 -0
- package/packages/core/PixelCalibrationEngine.ts +602 -0
- package/packages/core/Prefab.ts +314 -0
- package/packages/core/RealmGuards.ts +111 -0
- package/packages/core/ResponsiveLayout.ts +149 -0
- package/packages/core/RuntimeGuards.ts +261 -0
- package/packages/core/SpineCharacter.ts +243 -0
- package/packages/core/SpineVisualContract.ts +238 -0
- package/packages/core/SplashSignal.ts +72 -0
- package/packages/core/StaticAssetPipeline.ts +3 -0
- package/packages/core/TopologyGraph.ts +693 -0
- package/packages/core/UnityBridgePipeline.ts +2 -0
- package/packages/core/VideoSurface.ts +75 -0
- package/packages/core/ViewportMatrixEngine.ts +237 -0
- package/packages/core/VisualFocusContract.ts +76 -0
- package/packages/core/VisualMigrationPipeline.ts +245 -0
- package/packages/core/WebViewSurface.ts +116 -0
- package/packages/core/audio/OfflineMix.ts +186 -0
- package/packages/core/audio/Spatial.ts +312 -0
- package/packages/core/audio/StreamVoice.ts +161 -0
- package/packages/core/csg/Bsp.ts +310 -0
- package/packages/core/csg/index.ts +589 -0
- package/packages/core/effects/Effect2D.ts +286 -0
- package/packages/core/effects/Effect3D.ts +213 -0
- package/packages/core/effects/EffectBudget.ts +695 -0
- package/packages/core/effects/EffectDirector.ts +254 -0
- package/packages/core/effects/EffectPresets.ts +843 -0
- package/packages/core/effects/EffectPresets2D.ts +1011 -0
- package/packages/core/effects/EffectSprites.ts +143 -0
- package/packages/core/effects/ParticleEventLink.ts +53 -0
- package/packages/core/effects/README.md +108 -0
- package/packages/core/effects/VfxMaterial.ts +253 -0
- package/packages/core/film/BakeKey.ts +21 -0
- package/packages/core/film/ColorValues.ts +44 -0
- package/packages/core/film/FilmLint.ts +353 -0
- package/packages/core/film/FilmPrefab.ts +82 -0
- package/packages/core/film/FilmReview.ts +84 -0
- package/packages/core/film/FilmScene.ts +3442 -0
- package/packages/core/film/FilmStage.ts +2148 -0
- package/packages/core/film/Flythrough.ts +169 -0
- package/packages/core/film/FlythroughReport.ts +326 -0
- package/packages/core/film/FrameMetrics.ts +167 -0
- package/packages/core/film/LiveLook.ts +242 -0
- package/packages/core/film/LookQuality.ts +94 -0
- package/packages/core/film/Projection.ts +74 -0
- package/packages/core/film/SceneLook.ts +363 -0
- package/packages/core/film/index.ts +12 -0
- package/packages/core/index.ts +48 -0
- package/packages/core/particles/EffectEnvelope.ts +52 -0
- package/packages/core/particles/LayaParticleRenderer.ts +182 -0
- package/packages/core/particles/LayaParticleRenderer3D.ts +218 -0
- package/packages/core/particles/ParticleExtras.ts +163 -0
- package/packages/core/particles/ParticleMesh3D.ts +156 -0
- package/packages/core/particles/ParticleSystem.ts +324 -0
- package/packages/core/particles/ParticleSystem3D.ts +411 -0
- package/packages/core/particles/ParticleTypes.ts +230 -0
- package/packages/core/particles/ParticleTypes3D.ts +115 -0
- package/packages/core/particles/README.md +181 -0
- package/packages/core/particles/RibbonPath3D.ts +234 -0
- package/packages/core/particles/RibbonTrail.ts +145 -0
- package/packages/core/particles/RibbonTrail3D.ts +166 -0
- package/packages/core/particles/index.ts +12 -0
- package/packages/core/prefabs/PrefabLibrary.ts +692 -0
- package/packages/core/prefabs/PrefabTree3D.ts +291 -0
- package/packages/core/prefabs/index.ts +10 -0
- package/packages/core/procedural/Geometry.ts +1871 -0
- package/packages/core/procedural/Hand.ts +1510 -0
- package/packages/core/procedural/Isosurface.ts +1035 -0
- package/packages/core/procedural/ModuleHarness.ts +180 -0
- package/packages/core/procedural/Noise.ts +123 -0
- package/packages/core/procedural/PhotoMaterial.ts +283 -0
- package/packages/core/procedural/ProceduralAssetProvider.ts +105 -0
- package/packages/core/procedural/ProceduralMaterial.ts +184 -0
- package/packages/core/procedural/PropFactory.ts +307 -0
- package/packages/core/procedural/Random.ts +70 -0
- package/packages/core/procedural/SynthAudio.ts +221 -0
- package/packages/core/procedural/TerrainMesh.ts +207 -0
- package/packages/core/procedural/TextureRecipe.ts +464 -0
- package/packages/core/procedural/TriplanarMaterial.ts +169 -0
- package/packages/core/procedural/Variation.ts +94 -0
- package/packages/core/procedural/index.ts +15 -0
- package/packages/core/runtime3d/ActionFrames.ts +245 -0
- package/packages/core/runtime3d/AnimationBlend.ts +282 -0
- package/packages/core/runtime3d/AnimationBlendMath.ts +98 -0
- package/packages/core/runtime3d/AssetProvider.ts +775 -0
- package/packages/core/runtime3d/AssetShowcase.ts +190 -0
- package/packages/core/runtime3d/Assets.ts +801 -0
- package/packages/core/runtime3d/AsyncDeadline.ts +53 -0
- package/packages/core/runtime3d/AsyncOperation.ts +39 -0
- package/packages/core/runtime3d/BackendChoice.ts +359 -0
- package/packages/core/runtime3d/BakedCrowd.ts +575 -0
- package/packages/core/runtime3d/BasisTexture.ts +657 -0
- package/packages/core/runtime3d/CameraCollision.ts +69 -0
- package/packages/core/runtime3d/CameraEffects.ts +752 -0
- package/packages/core/runtime3d/CameraRig.ts +70 -0
- package/packages/core/runtime3d/CharacterController.ts +272 -0
- package/packages/core/runtime3d/CinematicCapture.ts +457 -0
- package/packages/core/runtime3d/Clocks.ts +724 -0
- package/packages/core/runtime3d/Cloth.ts +183 -0
- package/packages/core/runtime3d/Clouds.ts +462 -0
- package/packages/core/runtime3d/ColorLut.ts +351 -0
- package/packages/core/runtime3d/CombatQueries.ts +240 -0
- package/packages/core/runtime3d/ComputePass.ts +582 -0
- package/packages/core/runtime3d/CrowdPresentation.ts +368 -0
- package/packages/core/runtime3d/DebugDraw.ts +567 -0
- package/packages/core/runtime3d/DebugStats.ts +35 -0
- package/packages/core/runtime3d/Decals.ts +309 -0
- package/packages/core/runtime3d/DensityVolume.ts +238 -0
- package/packages/core/runtime3d/DistanceBricks.ts +349 -0
- package/packages/core/runtime3d/Dither.ts +199 -0
- package/packages/core/runtime3d/DrawWarmUp.ts +217 -0
- package/packages/core/runtime3d/EdgeSmoothing.ts +108 -0
- package/packages/core/runtime3d/EnvironmentProbe.ts +656 -0
- package/packages/core/runtime3d/Filmic.ts +973 -0
- package/packages/core/runtime3d/Foliage.ts +367 -0
- package/packages/core/runtime3d/GltfIntake.ts +914 -0
- package/packages/core/runtime3d/GltfSecondUv.ts +153 -0
- package/packages/core/runtime3d/GraphicsGuard.ts +171 -0
- package/packages/core/runtime3d/Grass.ts +1080 -0
- package/packages/core/runtime3d/GroundTruthAO.ts +360 -0
- package/packages/core/runtime3d/HalfFloat.ts +44 -0
- package/packages/core/runtime3d/HdrEnvironment.ts +588 -0
- package/packages/core/runtime3d/Ik.ts +392 -0
- package/packages/core/runtime3d/InputBuffer.ts +165 -0
- package/packages/core/runtime3d/InstancedMesh.ts +460 -0
- package/packages/core/runtime3d/Laya342Adapters.ts +1095 -0
- package/packages/core/runtime3d/Laya342GltfAdapter.ts +751 -0
- package/packages/core/runtime3d/LensFinish.ts +153 -0
- package/packages/core/runtime3d/LensFlare.ts +490 -0
- package/packages/core/runtime3d/LightFog.ts +1008 -0
- package/packages/core/runtime3d/LightingDepth.ts +544 -0
- package/packages/core/runtime3d/Lightmap.ts +238 -0
- package/packages/core/runtime3d/Math3D.ts +84 -0
- package/packages/core/runtime3d/MeshQuery.ts +547 -0
- package/packages/core/runtime3d/MeshoptModelDecoder.ts +354 -0
- package/packages/core/runtime3d/MiniGameSplash.ts +70 -0
- package/packages/core/runtime3d/ModelAnimationSampler.ts +97 -0
- package/packages/core/runtime3d/ModelCompatibility.ts +890 -0
- package/packages/core/runtime3d/ModelDecoder.ts +294 -0
- package/packages/core/runtime3d/ModelInspector.ts +226 -0
- package/packages/core/runtime3d/ModelLod.ts +163 -0
- package/packages/core/runtime3d/ModelPlacements.ts +426 -0
- package/packages/core/runtime3d/ObjectMotion.ts +217 -0
- package/packages/core/runtime3d/PhysicalCamera.ts +190 -0
- package/packages/core/runtime3d/PointShadow.ts +500 -0
- package/packages/core/runtime3d/PointShadowSchedule.ts +201 -0
- package/packages/core/runtime3d/Pool.ts +120 -0
- package/packages/core/runtime3d/ProbeVolume.ts +695 -0
- package/packages/core/runtime3d/QualityGovernor.ts +385 -0
- package/packages/core/runtime3d/QualityMemory.ts +200 -0
- package/packages/core/runtime3d/RenderScale.ts +356 -0
- package/packages/core/runtime3d/RenderSpaces.ts +105 -0
- package/packages/core/runtime3d/Renderer.ts +377 -0
- package/packages/core/runtime3d/ResultPlayback.ts +239 -0
- package/packages/core/runtime3d/Runtime.ts +507 -0
- package/packages/core/runtime3d/RuntimeDriver.ts +127 -0
- package/packages/core/runtime3d/SceneLifecycle.ts +211 -0
- package/packages/core/runtime3d/SceneMutation.ts +107 -0
- package/packages/core/runtime3d/SceneQueries.ts +191 -0
- package/packages/core/runtime3d/ScreenReflections.ts +595 -0
- package/packages/core/runtime3d/SelectionOutline.ts +389 -0
- package/packages/core/runtime3d/ShaderErrors.ts +104 -0
- package/packages/core/runtime3d/ShaderRegistry.ts +34 -0
- package/packages/core/runtime3d/ShaderWarmUp.ts +473 -0
- package/packages/core/runtime3d/ShadowUpdates.ts +287 -0
- package/packages/core/runtime3d/SkinBinding.ts +235 -0
- package/packages/core/runtime3d/Smaa.ts +386 -0
- package/packages/core/runtime3d/SmaaTables.ts +244 -0
- package/packages/core/runtime3d/SpringBones.ts +184 -0
- package/packages/core/runtime3d/SurfaceBuffer.ts +76 -0
- package/packages/core/runtime3d/Surfaces.ts +1105 -0
- package/packages/core/runtime3d/Themes.ts +344 -0
- package/packages/core/runtime3d/Timeline.ts +362 -0
- package/packages/core/runtime3d/ToneCurves.ts +292 -0
- package/packages/core/runtime3d/TransformGizmo.ts +539 -0
- package/packages/core/runtime3d/TriangleMask.ts +250 -0
- package/packages/core/runtime3d/Underwater.ts +778 -0
- package/packages/core/runtime3d/Utf8.ts +87 -0
- package/packages/core/runtime3d/Vrm.ts +773 -0
- package/packages/core/runtime3d/VrmDocument.ts +1830 -0
- package/packages/core/runtime3d/WalkGrid.ts +471 -0
- package/packages/core/runtime3d/WebGPU342Adapters.ts +280 -0
- package/packages/core/runtime3d/index.ts +147 -0
- package/packages/core/shaders/Laya342ShaderTemplates.ts +6 -0
- package/packages/core/shaders/LitSurfaceMaterial.ts +1076 -0
- package/packages/core/shaders/LitSurfaceTemplates.ts +13 -0
- package/packages/core/shaders/ReflectiveSurface.ts +285 -0
- package/packages/core/shaders/ShaderMaterials.ts +387 -0
- package/packages/core/shaders/ToonMaterial.ts +559 -0
- package/packages/core/shaders/WaterRipples.ts +170 -0
- package/packages/core/shaders/WaterSurface.ts +480 -0
- package/packages/core/shaders/ZhudaCommon.ts +109 -0
- package/packages/core/shaders/index.ts +6 -0
- package/packages/core/simulation/Cloth.ts +1541 -0
- package/packages/core/simulation/ParticleCore.ts +498 -0
- package/packages/core/simulation/SpringBones.ts +553 -0
- package/packages/core/simulation/index.ts +3 -0
- package/packages/core/terrain/Heightfield.ts +385 -0
- package/packages/core/terrain/Terrain2D.ts +324 -0
- package/packages/core/terrain/Terrain3D.ts +452 -0
- package/packages/core/terrain/TerrainShell.ts +94 -0
- package/packages/core/terrain/index.ts +3 -0
- package/packages/demo/BenchmarkEngine.ts +184 -0
- package/packages/demo/PixelCalibrationEngine.ts +36 -0
- package/packages/demo/ProceduralStarter.ts +312 -0
- package/packages/demo/StaticAssetPipeline.ts +1631 -0
- package/packages/demo/UnityBridgePipeline.ts +485 -0
- package/packages/demo/index.ts +6 -0
- package/packages/mcp-server/arguments.mjs +81 -0
- package/packages/mcp-server/corpus.mjs +627 -0
- package/packages/mcp-server/film-host.mjs +627 -0
- package/packages/mcp-server/film-session.mjs +95 -0
- package/packages/mcp-server/film-tools.mjs +460 -0
- package/packages/mcp-server/index.mjs +931 -0
- package/packages/mcp-server/quality-route.mjs +332 -0
- package/packages/mcp-server/roots.mjs +72 -0
- package/packages/mcp-server/runtime.mjs +81 -0
- package/packages/types/BitmapFontContract.d.ts +19 -0
- package/packages/types/DeclarativeSDK.d.ts +137 -0
- package/packages/types/IframeSoundAdapter.d.ts +173 -0
- package/packages/types/NoSleepAdapter.d.ts +50 -0
- package/packages/types/PixelCalibrationEngine.d.ts +83 -0
- package/packages/types/Prefab.d.ts +11 -0
- package/packages/types/RealmGuards.d.ts +26 -0
- package/packages/types/ResponsiveLayout.d.ts +10 -0
- package/packages/types/RuntimeGuards.d.ts +55 -0
- package/packages/types/SpineCharacter.d.ts +94 -0
- package/packages/types/SpineVisualContract.d.ts +50 -0
- package/packages/types/SplashSignal.d.ts +27 -0
- package/packages/types/TopologyGraph.d.ts +76 -0
- package/packages/types/VideoSurface.d.ts +21 -0
- package/packages/types/ViewportMatrixEngine.d.ts +27 -0
- package/packages/types/VisualFocusContract.d.ts +32 -0
- package/packages/types/VisualMigrationPipeline.d.ts +53 -0
- package/packages/types/WebViewSurface.d.ts +26 -0
- package/packages/types/audio/OfflineMix.d.ts +10 -0
- package/packages/types/audio/Spatial.d.ts +63 -0
- package/packages/types/audio/StreamVoice.d.ts +31 -0
- package/packages/types/browser.d.ts +44 -0
- package/packages/types/csg/Bsp.d.ts +85 -0
- package/packages/types/csg/index.d.ts +81 -0
- package/packages/types/demo/BenchmarkEngine.d.ts +61 -0
- package/packages/types/demo/PixelCalibrationEngine.d.ts +13 -0
- package/packages/types/demo/ProceduralStarter.d.ts +11 -0
- package/packages/types/demo/StaticAssetPipeline.d.ts +87 -0
- package/packages/types/demo/UnityBridgePipeline.d.ts +49 -0
- package/packages/types/demo/index.d.ts +7 -0
- package/packages/types/editor-preview.d.ts +27 -0
- package/packages/types/effects/Effect2D.d.ts +65 -0
- package/packages/types/effects/Effect3D.d.ts +42 -0
- package/packages/types/effects/EffectBudget.d.ts +179 -0
- package/packages/types/effects/EffectDirector.d.ts +68 -0
- package/packages/types/effects/EffectPresets.d.ts +27 -0
- package/packages/types/effects/EffectPresets2D.d.ts +32 -0
- package/packages/types/effects/EffectSprites.d.ts +23 -0
- package/packages/types/effects/ParticleEventLink.d.ts +16 -0
- package/packages/types/effects/VfxMaterial.d.ts +52 -0
- package/packages/types/film/BakeKey.d.ts +9 -0
- package/packages/types/film/ColorValues.d.ts +25 -0
- package/packages/types/film/FilmLint.d.ts +40 -0
- package/packages/types/film/FilmPrefab.d.ts +22 -0
- package/packages/types/film/FilmReview.d.ts +27 -0
- package/packages/types/film/FilmScene.d.ts +1214 -0
- package/packages/types/film/FilmStage.d.ts +237 -0
- package/packages/types/film/Flythrough.d.ts +44 -0
- package/packages/types/film/FlythroughReport.d.ts +132 -0
- package/packages/types/film/FrameMetrics.d.ts +49 -0
- package/packages/types/film/LiveLook.d.ts +60 -0
- package/packages/types/film/LookQuality.d.ts +24 -0
- package/packages/types/film/Projection.d.ts +20 -0
- package/packages/types/film/SceneLook.d.ts +122 -0
- package/packages/types/film/index.d.ts +13 -0
- package/packages/types/index.d.ts +49 -0
- package/packages/types/particles/EffectEnvelope.d.ts +18 -0
- package/packages/types/particles/LayaParticleRenderer.d.ts +30 -0
- package/packages/types/particles/LayaParticleRenderer3D.d.ts +29 -0
- package/packages/types/particles/ParticleExtras.d.ts +50 -0
- package/packages/types/particles/ParticleMesh3D.d.ts +24 -0
- package/packages/types/particles/ParticleSystem.d.ts +52 -0
- package/packages/types/particles/ParticleSystem3D.d.ts +69 -0
- package/packages/types/particles/ParticleTypes.d.ts +89 -0
- package/packages/types/particles/ParticleTypes3D.d.ts +56 -0
- package/packages/types/particles/RibbonPath3D.d.ts +32 -0
- package/packages/types/particles/RibbonTrail.d.ts +38 -0
- package/packages/types/particles/RibbonTrail3D.d.ts +22 -0
- package/packages/types/particles/index.d.ts +13 -0
- package/packages/types/prefabs/PrefabLibrary.d.ts +176 -0
- package/packages/types/prefabs/PrefabTree3D.d.ts +59 -0
- package/packages/types/prefabs/index.d.ts +4 -0
- package/packages/types/procedural/Geometry.d.ts +252 -0
- package/packages/types/procedural/Hand.d.ts +150 -0
- package/packages/types/procedural/Isosurface.d.ts +181 -0
- package/packages/types/procedural/ModuleHarness.d.ts +90 -0
- package/packages/types/procedural/Noise.d.ts +15 -0
- package/packages/types/procedural/PhotoMaterial.d.ts +49 -0
- package/packages/types/procedural/ProceduralAssetProvider.d.ts +31 -0
- package/packages/types/procedural/ProceduralMaterial.d.ts +63 -0
- package/packages/types/procedural/PropFactory.d.ts +94 -0
- package/packages/types/procedural/Random.d.ts +26 -0
- package/packages/types/procedural/SynthAudio.d.ts +49 -0
- package/packages/types/procedural/TerrainMesh.d.ts +55 -0
- package/packages/types/procedural/TextureRecipe.d.ts +125 -0
- package/packages/types/procedural/TriplanarMaterial.d.ts +27 -0
- package/packages/types/procedural/Variation.d.ts +23 -0
- package/packages/types/procedural/index.d.ts +16 -0
- package/packages/types/runtime3d/ActionFrames.d.ts +62 -0
- package/packages/types/runtime3d/AnimationBlend.d.ts +57 -0
- package/packages/types/runtime3d/AnimationBlendMath.d.ts +27 -0
- package/packages/types/runtime3d/AssetProvider.d.ts +167 -0
- package/packages/types/runtime3d/AssetShowcase.d.ts +44 -0
- package/packages/types/runtime3d/Assets.d.ts +166 -0
- package/packages/types/runtime3d/AsyncDeadline.d.ts +6 -0
- package/packages/types/runtime3d/AsyncOperation.d.ts +3 -0
- package/packages/types/runtime3d/BackendChoice.d.ts +130 -0
- package/packages/types/runtime3d/BakedCrowd.d.ts +156 -0
- package/packages/types/runtime3d/BasisTexture.d.ts +169 -0
- package/packages/types/runtime3d/CameraCollision.d.ts +20 -0
- package/packages/types/runtime3d/CameraEffects.d.ts +134 -0
- package/packages/types/runtime3d/CameraRig.d.ts +27 -0
- package/packages/types/runtime3d/CharacterController.d.ts +71 -0
- package/packages/types/runtime3d/CinematicCapture.d.ts +103 -0
- package/packages/types/runtime3d/Clocks.d.ts +200 -0
- package/packages/types/runtime3d/Cloth.d.ts +57 -0
- package/packages/types/runtime3d/Clouds.d.ts +69 -0
- package/packages/types/runtime3d/ColorLut.d.ts +73 -0
- package/packages/types/runtime3d/CombatQueries.d.ts +50 -0
- package/packages/types/runtime3d/ComputePass.d.ts +134 -0
- package/packages/types/runtime3d/CrowdPresentation.d.ts +104 -0
- package/packages/types/runtime3d/DebugDraw.d.ts +161 -0
- package/packages/types/runtime3d/DebugStats.d.ts +24 -0
- package/packages/types/runtime3d/Decals.d.ts +65 -0
- package/packages/types/runtime3d/DensityVolume.d.ts +39 -0
- package/packages/types/runtime3d/DistanceBricks.d.ts +97 -0
- package/packages/types/runtime3d/Dither.d.ts +78 -0
- package/packages/types/runtime3d/DrawWarmUp.d.ts +93 -0
- package/packages/types/runtime3d/EdgeSmoothing.d.ts +10 -0
- package/packages/types/runtime3d/EnvironmentProbe.d.ts +139 -0
- package/packages/types/runtime3d/Filmic.d.ts +243 -0
- package/packages/types/runtime3d/Foliage.d.ts +95 -0
- package/packages/types/runtime3d/GltfIntake.d.ts +236 -0
- package/packages/types/runtime3d/GltfSecondUv.d.ts +54 -0
- package/packages/types/runtime3d/GraphicsGuard.d.ts +18 -0
- package/packages/types/runtime3d/Grass.d.ts +226 -0
- package/packages/types/runtime3d/GroundTruthAO.d.ts +64 -0
- package/packages/types/runtime3d/HalfFloat.d.ts +13 -0
- package/packages/types/runtime3d/HdrEnvironment.d.ts +144 -0
- package/packages/types/runtime3d/Ik.d.ts +123 -0
- package/packages/types/runtime3d/InputBuffer.d.ts +49 -0
- package/packages/types/runtime3d/InstancedMesh.d.ts +116 -0
- package/packages/types/runtime3d/Laya342Adapters.d.ts +306 -0
- package/packages/types/runtime3d/Laya342GltfAdapter.d.ts +105 -0
- package/packages/types/runtime3d/LensFinish.d.ts +38 -0
- package/packages/types/runtime3d/LensFlare.d.ts +114 -0
- package/packages/types/runtime3d/LightFog.d.ts +190 -0
- package/packages/types/runtime3d/LightingDepth.d.ts +128 -0
- package/packages/types/runtime3d/Lightmap.d.ts +55 -0
- package/packages/types/runtime3d/Math3D.d.ts +19 -0
- package/packages/types/runtime3d/MeshQuery.d.ts +107 -0
- package/packages/types/runtime3d/MeshoptModelDecoder.d.ts +34 -0
- package/packages/types/runtime3d/MiniGameSplash.d.ts +4 -0
- package/packages/types/runtime3d/ModelAnimationSampler.d.ts +26 -0
- package/packages/types/runtime3d/ModelCompatibility.d.ts +169 -0
- package/packages/types/runtime3d/ModelDecoder.d.ts +66 -0
- package/packages/types/runtime3d/ModelInspector.d.ts +77 -0
- package/packages/types/runtime3d/ModelLod.d.ts +61 -0
- package/packages/types/runtime3d/ModelPlacements.d.ts +77 -0
- package/packages/types/runtime3d/ObjectMotion.d.ts +43 -0
- package/packages/types/runtime3d/PhysicalCamera.d.ts +51 -0
- package/packages/types/runtime3d/PointShadow.d.ts +71 -0
- package/packages/types/runtime3d/PointShadowSchedule.d.ts +74 -0
- package/packages/types/runtime3d/Pool.d.ts +33 -0
- package/packages/types/runtime3d/ProbeVolume.d.ts +123 -0
- package/packages/types/runtime3d/QualityGovernor.d.ts +201 -0
- package/packages/types/runtime3d/QualityMemory.d.ts +83 -0
- package/packages/types/runtime3d/RenderScale.d.ts +69 -0
- package/packages/types/runtime3d/RenderSpaces.d.ts +44 -0
- package/packages/types/runtime3d/Renderer.d.ts +62 -0
- package/packages/types/runtime3d/ResultPlayback.d.ts +70 -0
- package/packages/types/runtime3d/Runtime.d.ts +136 -0
- package/packages/types/runtime3d/RuntimeDriver.d.ts +42 -0
- package/packages/types/runtime3d/SceneLifecycle.d.ts +84 -0
- package/packages/types/runtime3d/SceneMutation.d.ts +34 -0
- package/packages/types/runtime3d/SceneQueries.d.ts +71 -0
- package/packages/types/runtime3d/ScreenReflections.d.ts +78 -0
- package/packages/types/runtime3d/SelectionOutline.d.ts +68 -0
- package/packages/types/runtime3d/ShaderErrors.d.ts +27 -0
- package/packages/types/runtime3d/ShaderRegistry.d.ts +10 -0
- package/packages/types/runtime3d/ShaderWarmUp.d.ts +162 -0
- package/packages/types/runtime3d/ShadowUpdates.d.ts +125 -0
- package/packages/types/runtime3d/SkinBinding.d.ts +53 -0
- package/packages/types/runtime3d/Smaa.d.ts +60 -0
- package/packages/types/runtime3d/SmaaTables.d.ts +12 -0
- package/packages/types/runtime3d/SpringBones.d.ts +69 -0
- package/packages/types/runtime3d/SurfaceBuffer.d.ts +28 -0
- package/packages/types/runtime3d/Surfaces.d.ts +326 -0
- package/packages/types/runtime3d/Themes.d.ts +70 -0
- package/packages/types/runtime3d/Timeline.d.ts +88 -0
- package/packages/types/runtime3d/ToneCurves.d.ts +59 -0
- package/packages/types/runtime3d/TransformGizmo.d.ts +139 -0
- package/packages/types/runtime3d/TriangleMask.d.ts +84 -0
- package/packages/types/runtime3d/Underwater.d.ts +120 -0
- package/packages/types/runtime3d/Utf8.d.ts +12 -0
- package/packages/types/runtime3d/Vrm.d.ts +120 -0
- package/packages/types/runtime3d/VrmDocument.d.ts +381 -0
- package/packages/types/runtime3d/WalkGrid.d.ts +94 -0
- package/packages/types/runtime3d/WebGPU342Adapters.d.ts +27 -0
- package/packages/types/runtime3d/index.d.ts +102 -0
- package/packages/types/shaders/Laya342ShaderTemplates.d.ts +3 -0
- package/packages/types/shaders/LitSurfaceMaterial.d.ts +257 -0
- package/packages/types/shaders/LitSurfaceTemplates.d.ts +7 -0
- package/packages/types/shaders/ReflectiveSurface.d.ts +58 -0
- package/packages/types/shaders/ShaderMaterials.d.ts +60 -0
- package/packages/types/shaders/ToonMaterial.d.ts +176 -0
- package/packages/types/shaders/WaterRipples.d.ts +48 -0
- package/packages/types/shaders/WaterSurface.d.ts +110 -0
- package/packages/types/shaders/ZhudaCommon.d.ts +31 -0
- package/packages/types/shaders/index.d.ts +7 -0
- package/packages/types/simulation/Cloth.d.ts +381 -0
- package/packages/types/simulation/ParticleCore.d.ts +125 -0
- package/packages/types/simulation/SpringBones.d.ts +107 -0
- package/packages/types/simulation/index.d.ts +4 -0
- package/packages/types/terrain/Heightfield.d.ts +67 -0
- package/packages/types/terrain/Terrain2D.d.ts +81 -0
- package/packages/types/terrain/Terrain3D.d.ts +80 -0
- package/packages/types/terrain/TerrainShell.d.ts +8 -0
- package/packages/types/terrain/index.d.ts +4 -0
- package/packages/types/vendor/LayaAir.runtime.d.ts +97440 -0
- package/packages/types/vendor/REPAIRS.md +30 -0
- package/packages/types/vendor/restored-3.4.2.json +142 -0
- package/packages/types/vendor/spine.runtime.d.ts +1507 -0
- package/packages/types/vendor/wechat-media.d.ts +877 -0
- package/packages/types/zhuda-2d-ui.d.ts +244 -0
- package/packages/types/zhuda-3d-physics.d.ts +34 -0
- package/packages/types/zhuda-audio.d.ts +128 -0
- package/packages/types/zhuda-bootstrap.d.ts +100 -0
- package/packages/types/zhuda-branding.d.ts +6 -0
- package/packages/types/zhuda-core.d.ts +46 -0
- package/packages/types/zhuda-declarative.d.ts +81 -0
- package/packages/types/zhuda-effects.d.ts +302 -0
- package/packages/types/zhuda-layout.d.ts +96 -0
- package/packages/types/zhuda-particles.d.ts +477 -0
- package/packages/types/zhuda-spine42.d.ts +23 -0
- package/packages/types/zhuda-static-assets.d.ts +389 -0
- package/packages/types/zhuda-video.d.ts +24 -0
- package/packages/types/zhuda-visual-contract.d.ts +52 -0
- package/packages/types/zhuda-webview.d.ts +43 -0
- package/vendor/basis/LICENSE.txt +55 -0
- package/vendor/basis/README.md +25 -0
- package/vendor/basis/basis_transcoder.js +19 -0
- package/vendor/basis/basis_transcoder.wasm +0 -0
- package/vendor/laya-3.4.2/WEBGPU-LICENSES.txt +1259 -0
- package/vendor/laya-3.4.2/libs/laya.ani.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.box2D.js +1209 -0
- package/vendor/laya-3.4.2/libs/laya.bridge.js +2273 -0
- package/vendor/laya-3.4.2/libs/laya.bullet.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.core.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.d3.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.device.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.gltf.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.ik.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.light2D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.line2D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.native-spine42.js +7672 -0
- package/vendor/laya-3.4.2/libs/laya.navMesh2d.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.navMesh3d.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.navMeshCommon.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.particle.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.particle3D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.particleCommon.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.physics2D.js +3774 -0
- package/vendor/laya-3.4.2/libs/laya.physics3D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.postProcess.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.spine.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.spine3D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.tilemap.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.trail2D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.trail3D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.trailCommon.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.ui.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.ui2.js +9904 -0
- package/vendor/laya-3.4.2/libs/laya.vfx.js +6754 -0
- package/vendor/laya-3.4.2/libs/laya.webgl_2D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.webgl_3D.js +1 -0
- package/vendor/laya-3.4.2/libs/laya.webgpu_2D.js +18576 -0
- package/vendor/laya-3.4.2/libs/laya.webgpu_3D.js +6729 -0
- package/vendor/laya-3.4.2/libs/nagabind.js +395 -0
- package/vendor/laya-3.4.2/libs/nagabind_bg.wasm +0 -0
- package/vendor/laya-3.4.2/libs/shader_compiler_web.js +22 -0
- package/vendor/laya-3.4.2/libs/shader_compiler_web.wasm +0 -0
- package/vendor/laya-3.4.2/libs/spine-core-3.8.js +1 -0
- package/vendor/laya-3.4.2/libs/spine-core-4.2.js +10216 -0
- package/vendor/laya-3.4.2/webgpu-provenance.json +41 -0
- package/vendor/meshoptimizer/LICENSE.txt +21 -0
- package/vendor/meshoptimizer/README.md +13 -0
- package/vendor/threejs-post/LICENSE.txt +53 -0
- package/vendor/threejs-post/README.md +54 -0
- package/zhuda.config.json +22 -0
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
import { zhudaUtf8Decode, zhudaUtf8Encode } from './Utf8.ts';
|
|
2
|
+
/**
|
|
3
|
+
* glTF intake steps LayaAir 3.4.2's importer lacks. They read a glTF document,
|
|
4
|
+
* or rewrite its bytes, before the native parser sees it. Nothing here makes a
|
|
5
|
+
* Laya object, fetches a URL or allocates a GPU resource.
|
|
6
|
+
*/
|
|
7
|
+
type Json = Record<string, unknown>;
|
|
8
|
+
const record = (v: unknown): Json =>
|
|
9
|
+
v !== null && typeof v === 'object' && !Array.isArray(v) ? (v as Json) : {};
|
|
10
|
+
const list = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
|
|
11
|
+
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
|
12
|
+
const indexOf = (v: unknown): number | null =>
|
|
13
|
+
Number.isSafeInteger(v) && (v as number) >= 0 ? (v as number) : null;
|
|
14
|
+
const label = (v: unknown): string => (typeof v === 'string' ? v : '');
|
|
15
|
+
function freeze<T>(value: T): T {
|
|
16
|
+
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
17
|
+
for (const child of Object.values(value)) freeze(child);
|
|
18
|
+
Object.freeze(value);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------- images ---
|
|
24
|
+
|
|
25
|
+
/** Texture-source extensions the engine resolves, in the order it prefers them. */
|
|
26
|
+
export type ZhudaGltfImageExtension =
|
|
27
|
+
| 'KHR_texture_basisu'
|
|
28
|
+
| 'EXT_texture_avif'
|
|
29
|
+
| 'EXT_texture_webp';
|
|
30
|
+
/** The picture formats the device is asked about. */
|
|
31
|
+
export type ZhudaProbedImageFormat = 'webp' | 'avif';
|
|
32
|
+
/** Whether this device decodes WebP and AVIF pictures (`probeZhudaImageDecode`). */
|
|
33
|
+
export interface ZhudaImageDecodeSupport {
|
|
34
|
+
readonly webp: boolean;
|
|
35
|
+
readonly avif: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Formats whose decoder gave no answer before the probe's deadline. They
|
|
38
|
+
* count as not decodable in this answer only; the next probe asks again.
|
|
39
|
+
* Absent when every format answered.
|
|
40
|
+
*/
|
|
41
|
+
readonly unanswered?: readonly ZhudaProbedImageFormat[];
|
|
42
|
+
/**
|
|
43
|
+
* Whether a Basis transcoder is at hand for KTX 2 images
|
|
44
|
+
* (`KHR_texture_basisu`): set by `ZhudaModelLibrary` from its own
|
|
45
|
+
* `basisTranscoder`, not by a probe of the device.
|
|
46
|
+
*/
|
|
47
|
+
readonly basis?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface ZhudaGltfTextureSource {
|
|
50
|
+
/** The image the texture loads, or null when the file offers nothing loadable. */
|
|
51
|
+
readonly image: number | null;
|
|
52
|
+
/** The extension that image came from; null for the core `source`. */
|
|
53
|
+
readonly extension: ZhudaGltfImageExtension | null;
|
|
54
|
+
/** Extensions whose image this device cannot decode. */
|
|
55
|
+
readonly unavailable: readonly ZhudaGltfImageExtension[];
|
|
56
|
+
}
|
|
57
|
+
const imageExtensions: readonly (readonly [
|
|
58
|
+
ZhudaGltfImageExtension,
|
|
59
|
+
ZhudaProbedImageFormat | 'basis',
|
|
60
|
+
])[] = [
|
|
61
|
+
['KHR_texture_basisu', 'basis'],
|
|
62
|
+
['EXT_texture_avif', 'avif'],
|
|
63
|
+
['EXT_texture_webp', 'webp'],
|
|
64
|
+
];
|
|
65
|
+
/**
|
|
66
|
+
* The image a glTF texture should load: its KTX 2 image when a Basis
|
|
67
|
+
* transcoder is at hand (the only one the graphics card samples as it is),
|
|
68
|
+
* then its AVIF, then its WebP source when the device decodes it, else the
|
|
69
|
+
* core `source` (the optional fallback). Without `support` the core source is
|
|
70
|
+
* preferred, and an extension image is used only when there is no fallback.
|
|
71
|
+
*/
|
|
72
|
+
export function resolveZhudaGltfTextureSource(
|
|
73
|
+
texture: unknown,
|
|
74
|
+
support?: ZhudaImageDecodeSupport,
|
|
75
|
+
): ZhudaGltfTextureSource {
|
|
76
|
+
const t = record(texture),
|
|
77
|
+
extensions = record(t.extensions),
|
|
78
|
+
fallback = indexOf(t.source),
|
|
79
|
+
unavailable: ZhudaGltfImageExtension[] = [];
|
|
80
|
+
for (const [name, key] of imageExtensions) {
|
|
81
|
+
const image = indexOf(record(extensions[name]).source);
|
|
82
|
+
if (image === null) continue;
|
|
83
|
+
if (support ? support[key] === true : fallback === null)
|
|
84
|
+
return { image, extension: name, unavailable };
|
|
85
|
+
if (support) unavailable.push(name);
|
|
86
|
+
}
|
|
87
|
+
return { image: fallback, extension: null, unavailable };
|
|
88
|
+
}
|
|
89
|
+
// One-pixel pictures, made with sharp 0.35.5 (libwebp 1.6.0, libheif 1.23.5 / aom 3.15.1).
|
|
90
|
+
const probes: Record<ZhudaProbedImageFormat, [string, string]> = {
|
|
91
|
+
webp: ['image/webp', 'UklGRhwAAABXRUJQVlA4TA8AAAAvAAAAAAcQ/Y/+ByKi/wEA'],
|
|
92
|
+
avif: [
|
|
93
|
+
'image/avif',
|
|
94
|
+
'AAAAHGZ0eXBhdmlmAAAAAG1pZjFhdmlmbWlhZgAAANZtZXRhAAAAAAAAACFoZGxyAAAAAAAAAABwaWN0AAAAAAAAAAAAAAAAAAAAACJpbG9jAAAAAERAAAEAAQAAAAAA+gABAAAAAAAAACYAAAAjaWluZgAAAAAAAQAAABVpbmZlAgAAAAABAABhdjAxAAAAAA5waXRtAAAAAAABAAAAVmlwcnAAAAA4aXBjbwAAAAxhdjFDgSACAAAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAABZpcG1hAAAAAAAAAAEAAQOBAgMAAAAubWRhdBIACgc4AAaQENBpMhkXgmMEwAAygAAAAEhUNmGsYoAKNr/ysfdA',
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
const probedFormats: readonly ZhudaProbedImageFormat[] = ['webp', 'avif'];
|
|
98
|
+
/** How long a probe waits for a decoder: `turns` timers of `turnMs` each. */
|
|
99
|
+
export interface ZhudaImageProbeDeadline {
|
|
100
|
+
turns?: number;
|
|
101
|
+
turnMs?: number;
|
|
102
|
+
}
|
|
103
|
+
/** True or false: the decoder's answer. Null: it gave none before the deadline. */
|
|
104
|
+
async function decodes(
|
|
105
|
+
g: any,
|
|
106
|
+
type: string,
|
|
107
|
+
base64: string,
|
|
108
|
+
turns: number,
|
|
109
|
+
turnMs: number,
|
|
110
|
+
): Promise<boolean | null> {
|
|
111
|
+
if (typeof g.Blob !== 'function' || typeof g.atob !== 'function') return false;
|
|
112
|
+
const blob = new g.Blob([Uint8Array.from(g.atob(base64), (c: string) => c.charCodeAt(0))], {
|
|
113
|
+
type,
|
|
114
|
+
});
|
|
115
|
+
// LayaAir decodes a texture through an image element (WebGL) or from one
|
|
116
|
+
// (WebGPU): ask the same decoder. A worker or mini-game host without one
|
|
117
|
+
// falls back to createImageBitmap, or reports no support.
|
|
118
|
+
if (typeof g.Image === 'function' && typeof g.URL?.createObjectURL === 'function') {
|
|
119
|
+
const url = g.URL.createObjectURL(blob);
|
|
120
|
+
try {
|
|
121
|
+
return await new Promise<boolean | null>((resolve) => {
|
|
122
|
+
const image = new g.Image();
|
|
123
|
+
// A decoder that never answers must not hold a model load for ever.
|
|
124
|
+
// The deadline counts turns of the event loop, not wall time: a main
|
|
125
|
+
// thread busy with a long parse or a shader compile holds up the
|
|
126
|
+
// picture's load event as it holds up a timer, and a timer that fires
|
|
127
|
+
// late for that reason is not the decoder's answer. IO deadline only.
|
|
128
|
+
let left = turns,
|
|
129
|
+
timer: ReturnType<typeof setTimeout>;
|
|
130
|
+
const turn = () => {
|
|
131
|
+
if (--left > 0) timer = setTimeout(turn, turnMs);
|
|
132
|
+
else resolve(null);
|
|
133
|
+
};
|
|
134
|
+
timer = setTimeout(turn, turnMs);
|
|
135
|
+
image.onload = () => {
|
|
136
|
+
clearTimeout(timer);
|
|
137
|
+
resolve(image.naturalWidth === 1 && image.naturalHeight === 1);
|
|
138
|
+
};
|
|
139
|
+
image.onerror = () => {
|
|
140
|
+
clearTimeout(timer);
|
|
141
|
+
resolve(false);
|
|
142
|
+
};
|
|
143
|
+
image.src = url;
|
|
144
|
+
});
|
|
145
|
+
} finally {
|
|
146
|
+
g.URL.revokeObjectURL(url);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (typeof g.createImageBitmap === 'function')
|
|
150
|
+
try {
|
|
151
|
+
const bitmap = await g.createImageBitmap(blob);
|
|
152
|
+
const ok = bitmap.width === 1 && bitmap.height === 1;
|
|
153
|
+
bitmap.close?.();
|
|
154
|
+
return ok;
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* A probe with its own memory, on `host`'s Image, Blob and URL (read when it is
|
|
162
|
+
* called). `probeZhudaImageDecode` is the page's one; tests make their own.
|
|
163
|
+
*/
|
|
164
|
+
export function createZhudaImageDecodeProbe(
|
|
165
|
+
host: unknown = globalThis,
|
|
166
|
+
deadline: ZhudaImageProbeDeadline = {},
|
|
167
|
+
): () => Promise<ZhudaImageDecodeSupport> {
|
|
168
|
+
const turns = deadline.turns ?? 30,
|
|
169
|
+
turnMs = deadline.turnMs ?? 100;
|
|
170
|
+
if (!Number.isInteger(turns) || turns < 1 || !finite(turnMs) || turnMs < 0)
|
|
171
|
+
throw Error('Invalid image probe deadline');
|
|
172
|
+
// Only a decoder's own answer is remembered.
|
|
173
|
+
const known: Partial<Record<ZhudaProbedImageFormat, boolean>> = {};
|
|
174
|
+
let probing: Promise<ZhudaImageDecodeSupport> | undefined;
|
|
175
|
+
return () => {
|
|
176
|
+
probing ??= Promise.all(
|
|
177
|
+
probedFormats.map(
|
|
178
|
+
async (format) => known[format] ?? decodes(host, ...probes[format], turns, turnMs),
|
|
179
|
+
),
|
|
180
|
+
).then((answers) => {
|
|
181
|
+
const unanswered = probedFormats.filter((format, i) => {
|
|
182
|
+
if (answers[i] === null) return true;
|
|
183
|
+
known[format] = answers[i] as boolean;
|
|
184
|
+
return false;
|
|
185
|
+
});
|
|
186
|
+
// A format without an answer is asked again by the next call.
|
|
187
|
+
if (unanswered.length) probing = undefined;
|
|
188
|
+
return Object.freeze({
|
|
189
|
+
webp: answers[0] === true,
|
|
190
|
+
avif: answers[1] === true,
|
|
191
|
+
...(unanswered.length ? { unanswered: Object.freeze(unanswered) } : {}),
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
return probing;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Decodes a one-pixel WebP and AVIF picture and remembers each decoder's answer
|
|
199
|
+
* for the page. A decoder that gives none in time is listed in `unanswered`,
|
|
200
|
+
* counted as no for that answer, and asked again by the next call.
|
|
201
|
+
*/
|
|
202
|
+
export const probeZhudaImageDecode: () => Promise<ZhudaImageDecodeSupport> =
|
|
203
|
+
createZhudaImageDecodeProbe();
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------- lights ---
|
|
206
|
+
|
|
207
|
+
/** One `KHR_lights_punctual` light, as the file gives it (glTF units). */
|
|
208
|
+
export interface ZhudaGltfLight {
|
|
209
|
+
readonly index: number;
|
|
210
|
+
readonly name: string;
|
|
211
|
+
readonly type: 'directional' | 'point' | 'spot';
|
|
212
|
+
/** Linear RGB, 0..1. */
|
|
213
|
+
readonly color: readonly [number, number, number];
|
|
214
|
+
/** Lux (lm/m²) for a directional light; candela (lm/sr) for point and spot. */
|
|
215
|
+
readonly intensity: number;
|
|
216
|
+
/** Metres; null when the file gives none (no cut-off). */
|
|
217
|
+
readonly range: number | null;
|
|
218
|
+
/** Spot cone half-angles in radians (glTF defaults: 0 and π/4). */
|
|
219
|
+
readonly innerConeAngle: number;
|
|
220
|
+
readonly outerConeAngle: number;
|
|
221
|
+
}
|
|
222
|
+
export interface ZhudaGltfProblem {
|
|
223
|
+
readonly index: number;
|
|
224
|
+
readonly message: string;
|
|
225
|
+
}
|
|
226
|
+
/** The file's punctual lights; an invalid one is null and named in `problems`. */
|
|
227
|
+
export function readZhudaGltfLights(document: unknown): {
|
|
228
|
+
lights: readonly (ZhudaGltfLight | null)[];
|
|
229
|
+
problems: readonly ZhudaGltfProblem[];
|
|
230
|
+
} {
|
|
231
|
+
const problems: ZhudaGltfProblem[] = [];
|
|
232
|
+
const lights = list(record(record(record(document).extensions).KHR_lights_punctual).lights).map(
|
|
233
|
+
(raw, index): ZhudaGltfLight | null => {
|
|
234
|
+
const l = record(raw),
|
|
235
|
+
spot = record(l.spot);
|
|
236
|
+
const fail = (message: string) => (problems.push({ index, message }), null);
|
|
237
|
+
if (l.type !== 'directional' && l.type !== 'point' && l.type !== 'spot')
|
|
238
|
+
return fail('type must be directional, point or spot');
|
|
239
|
+
const color = l.color === undefined ? [1, 1, 1] : list(l.color);
|
|
240
|
+
if (color.length !== 3 || !color.every((c) => finite(c) && c >= 0 && c <= 1))
|
|
241
|
+
return fail('color must be three linear values from 0 to 1');
|
|
242
|
+
const intensity = l.intensity === undefined ? 1 : l.intensity;
|
|
243
|
+
if (!finite(intensity) || intensity < 0) return fail('intensity must be 0 or more');
|
|
244
|
+
if (l.range !== undefined && (!finite(l.range) || l.range <= 0))
|
|
245
|
+
return fail('range must be more than 0');
|
|
246
|
+
const inner = spot.innerConeAngle === undefined ? 0 : spot.innerConeAngle,
|
|
247
|
+
outer = spot.outerConeAngle === undefined ? Math.PI / 4 : spot.outerConeAngle;
|
|
248
|
+
if (l.type === 'spot') {
|
|
249
|
+
if (l.spot === undefined) return fail('a spot light needs its spot object');
|
|
250
|
+
if (
|
|
251
|
+
!finite(inner) ||
|
|
252
|
+
!finite(outer) ||
|
|
253
|
+
inner < 0 ||
|
|
254
|
+
outer <= inner ||
|
|
255
|
+
outer > Math.PI / 2 + 1e-6
|
|
256
|
+
)
|
|
257
|
+
return fail('spot cone angles need 0 <= inner < outer <= π/2');
|
|
258
|
+
}
|
|
259
|
+
return freeze({
|
|
260
|
+
index,
|
|
261
|
+
name: label(l.name),
|
|
262
|
+
type: l.type,
|
|
263
|
+
color: color as [number, number, number],
|
|
264
|
+
intensity,
|
|
265
|
+
range: l.type === 'directional' || l.range === undefined ? null : (l.range as number),
|
|
266
|
+
innerConeAngle: l.type === 'spot' ? (inner as number) : 0,
|
|
267
|
+
outerConeAngle: l.type === 'spot' ? (outer as number) : 0,
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
);
|
|
271
|
+
return { lights, problems };
|
|
272
|
+
}
|
|
273
|
+
/** What `zhudaGltfLightToLaya` gives a LayaAir light component. */
|
|
274
|
+
export interface ZhudaLayaLightSettings {
|
|
275
|
+
readonly kind: 'directional' | 'point' | 'spot';
|
|
276
|
+
/** For `Light.color`, which LayaAir reads as sRGB-encoded: glTF's linear colour, encoded. */
|
|
277
|
+
readonly color: readonly [number, number, number];
|
|
278
|
+
readonly intensity: number;
|
|
279
|
+
/** Point and spot: LayaAir's `range`, where its falloff reaches zero. */
|
|
280
|
+
readonly range: number | null;
|
|
281
|
+
/** Spot: LayaAir's full cone angle in degrees (its inner edge is fixed at half of it). */
|
|
282
|
+
readonly spotAngleDeg: number | null;
|
|
283
|
+
}
|
|
284
|
+
const encode = (c: number) =>
|
|
285
|
+
c <= 0 ? 0 : c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
|
|
286
|
+
/**
|
|
287
|
+
* glTF light units to LayaAir's. LayaAir's diffuse term has no 1/π
|
|
288
|
+
* (`Fd_Lambert` returns 1), so a LayaAir intensity is radiance per unit of
|
|
289
|
+
* albedo; glTF gives lux and candela.
|
|
290
|
+
* - Directional: `scale × lux / π`.
|
|
291
|
+
* - Point and spot: LayaAir's falloff is `1/(1+25f²)` (and a fade past 0.87),
|
|
292
|
+
* f = distance/range, not glTF's inverse square. The range is the file's,
|
|
293
|
+
* or `4·√(scale·cd/π)` when it gives none: a quarter of it is where the glTF
|
|
294
|
+
* light makes a white surface white. The intensity makes LayaAir's curve equal
|
|
295
|
+
* glTF's recommended falloff at half the range. Without a file range the two
|
|
296
|
+
* stay within about 30 % from a quarter to nine tenths of it. With one,
|
|
297
|
+
* glTF's window closes faster: LayaAir's light is about 0.63 of it at a
|
|
298
|
+
* quarter of the range and about twice it at three quarters. Nearer than a
|
|
299
|
+
* quarter, LayaAir's light stays finite.
|
|
300
|
+
* - Spot: the full cone is twice glTF's outer angle (at most 179°). LayaAir puts
|
|
301
|
+
* the inner edge at half of it, whatever the file says, and dims the cone by
|
|
302
|
+
* the cosine to its axis as well.
|
|
303
|
+
* `scale` stands in for exposure: physical units can be very large.
|
|
304
|
+
*/
|
|
305
|
+
export function zhudaGltfLightToLaya(
|
|
306
|
+
light: ZhudaGltfLight,
|
|
307
|
+
options: { scale?: number } = {},
|
|
308
|
+
): ZhudaLayaLightSettings {
|
|
309
|
+
const scale = options.scale ?? 1;
|
|
310
|
+
if (!finite(scale) || scale < 0) throw Error('Invalid glTF light scale');
|
|
311
|
+
const color = freeze(light.color.map(encode) as [number, number, number]);
|
|
312
|
+
if (light.type === 'directional')
|
|
313
|
+
return freeze({
|
|
314
|
+
kind: 'directional',
|
|
315
|
+
color,
|
|
316
|
+
intensity: (scale * light.intensity) / Math.PI,
|
|
317
|
+
range: null,
|
|
318
|
+
spotAngleDeg: null,
|
|
319
|
+
});
|
|
320
|
+
const power = scale * light.intensity;
|
|
321
|
+
const range = light.range ?? Math.max(1e-3, 4 * Math.sqrt(power / Math.PI));
|
|
322
|
+
// glTF's recommended window at half the range, (1 - (1/2)^4)^2; none without a range.
|
|
323
|
+
const windowAtHalf = light.range === null ? 1 : (1 - 1 / 16) ** 2;
|
|
324
|
+
return freeze({
|
|
325
|
+
kind: light.type,
|
|
326
|
+
color,
|
|
327
|
+
intensity: (7.25 * 4 * power * windowAtHalf) / (Math.PI * range * range),
|
|
328
|
+
range,
|
|
329
|
+
spotAngleDeg:
|
|
330
|
+
light.type === 'spot' ? Math.min(179, (2 * light.outerConeAngle * 180) / Math.PI) : null,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// --------------------------------------------------------------- cameras ---
|
|
335
|
+
|
|
336
|
+
export type ZhudaGltfCamera =
|
|
337
|
+
| {
|
|
338
|
+
readonly index: number;
|
|
339
|
+
readonly name: string;
|
|
340
|
+
readonly type: 'perspective';
|
|
341
|
+
/** Vertical field of view in degrees. */
|
|
342
|
+
readonly yfovDeg: number;
|
|
343
|
+
/** Width over height when the file fixes one; LayaAir uses its viewport's. */
|
|
344
|
+
readonly aspectRatio: number | null;
|
|
345
|
+
readonly znear: number;
|
|
346
|
+
/** Null: no far plane in the file. */
|
|
347
|
+
readonly zfar: number | null;
|
|
348
|
+
}
|
|
349
|
+
| {
|
|
350
|
+
readonly index: number;
|
|
351
|
+
readonly name: string;
|
|
352
|
+
readonly type: 'orthographic';
|
|
353
|
+
/** Half width and half height of the view, in metres. */
|
|
354
|
+
readonly xmag: number;
|
|
355
|
+
readonly ymag: number;
|
|
356
|
+
readonly znear: number;
|
|
357
|
+
readonly zfar: number;
|
|
358
|
+
};
|
|
359
|
+
/** The file's cameras; an invalid one is null and named in `problems`. */
|
|
360
|
+
export function readZhudaGltfCameras(document: unknown): {
|
|
361
|
+
cameras: readonly (ZhudaGltfCamera | null)[];
|
|
362
|
+
problems: readonly ZhudaGltfProblem[];
|
|
363
|
+
} {
|
|
364
|
+
const problems: ZhudaGltfProblem[] = [];
|
|
365
|
+
const cameras = list(record(document).cameras).map((raw, index): ZhudaGltfCamera | null => {
|
|
366
|
+
const c = record(raw);
|
|
367
|
+
const fail = (message: string) => (problems.push({ index, message }), null);
|
|
368
|
+
if (c.type === 'perspective') {
|
|
369
|
+
const p = record(c.perspective);
|
|
370
|
+
if (!finite(p.yfov) || p.yfov <= 0 || p.yfov >= Math.PI)
|
|
371
|
+
return fail('perspective yfov must be between 0 and π');
|
|
372
|
+
if (!finite(p.znear) || p.znear <= 0) return fail('perspective znear must be more than 0');
|
|
373
|
+
if (p.zfar !== undefined && (!finite(p.zfar) || p.zfar <= p.znear))
|
|
374
|
+
return fail('perspective zfar must be more than znear');
|
|
375
|
+
if (p.aspectRatio !== undefined && (!finite(p.aspectRatio) || p.aspectRatio <= 0))
|
|
376
|
+
return fail('perspective aspectRatio must be more than 0');
|
|
377
|
+
return freeze({
|
|
378
|
+
index,
|
|
379
|
+
name: label(c.name),
|
|
380
|
+
type: 'perspective' as const,
|
|
381
|
+
yfovDeg: (p.yfov * 180) / Math.PI,
|
|
382
|
+
aspectRatio: (p.aspectRatio as number | undefined) ?? null,
|
|
383
|
+
znear: p.znear,
|
|
384
|
+
zfar: (p.zfar as number | undefined) ?? null,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
if (c.type === 'orthographic') {
|
|
388
|
+
const o = record(c.orthographic);
|
|
389
|
+
if (!finite(o.xmag) || !finite(o.ymag) || o.xmag === 0 || o.ymag === 0)
|
|
390
|
+
return fail('orthographic xmag and ymag must be nonzero');
|
|
391
|
+
if (!finite(o.znear) || !finite(o.zfar) || o.znear < 0 || o.zfar <= o.znear)
|
|
392
|
+
return fail('orthographic planes need 0 <= znear < zfar');
|
|
393
|
+
return freeze({
|
|
394
|
+
index,
|
|
395
|
+
name: label(c.name),
|
|
396
|
+
type: 'orthographic' as const,
|
|
397
|
+
xmag: o.xmag,
|
|
398
|
+
ymag: o.ymag,
|
|
399
|
+
znear: o.znear,
|
|
400
|
+
zfar: o.zfar,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
return fail('type must be perspective or orthographic');
|
|
404
|
+
});
|
|
405
|
+
return { cameras, problems };
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Points a LayaAir camera as a glTF camera: its lens, and, given the Laya node
|
|
409
|
+
* the camera hangs on, that node's world position and rotation (both look down
|
|
410
|
+
* their local -Z). The aspect ratio stays the viewport's; an infinite far plane
|
|
411
|
+
* keeps the camera's own.
|
|
412
|
+
*/
|
|
413
|
+
export function applyZhudaGltfCamera(
|
|
414
|
+
camera: Laya.Camera,
|
|
415
|
+
data: ZhudaGltfCamera,
|
|
416
|
+
node?: Laya.Sprite3D,
|
|
417
|
+
): void {
|
|
418
|
+
if (data.type === 'perspective') {
|
|
419
|
+
camera.orthographic = false;
|
|
420
|
+
camera.fieldOfView = data.yfovDeg;
|
|
421
|
+
camera.nearPlane = data.znear;
|
|
422
|
+
if (data.zfar !== null) camera.farPlane = data.zfar;
|
|
423
|
+
} else {
|
|
424
|
+
camera.orthographic = true;
|
|
425
|
+
camera.orthographicVerticalSize = 2 * Math.abs(data.ymag);
|
|
426
|
+
camera.nearPlane = data.znear;
|
|
427
|
+
camera.farPlane = data.zfar;
|
|
428
|
+
}
|
|
429
|
+
if (node) {
|
|
430
|
+
camera.transform.position = node.transform.position.clone();
|
|
431
|
+
camera.transform.rotation = node.transform.rotation.clone();
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ------------------------------------------------------------ node data ---
|
|
436
|
+
|
|
437
|
+
/** glTF node data LayaAir 3.4.2 drops on import: custom properties, a camera, a light. */
|
|
438
|
+
export interface ZhudaGltfNodeData {
|
|
439
|
+
readonly index: number;
|
|
440
|
+
/** The glTF name, or null (LayaAir then names the node `node_<n>`). */
|
|
441
|
+
readonly name: string | null;
|
|
442
|
+
/** A frozen copy of the node's `extras`, when it has any. */
|
|
443
|
+
readonly extras?: unknown;
|
|
444
|
+
readonly camera?: ZhudaGltfCamera;
|
|
445
|
+
readonly light?: ZhudaGltfLight;
|
|
446
|
+
/** `EXT_mesh_gpu_instancing`: the node's mesh is drawn this many times. */
|
|
447
|
+
readonly instancing?: ZhudaGltfInstancing;
|
|
448
|
+
}
|
|
449
|
+
/** Every node that carries extras, a valid camera, a valid light or copies of its mesh. */
|
|
450
|
+
export function readZhudaGltfNodeData(document: unknown): readonly ZhudaGltfNodeData[] {
|
|
451
|
+
const { lights } = readZhudaGltfLights(document),
|
|
452
|
+
{ cameras } = readZhudaGltfCameras(document),
|
|
453
|
+
copies = new Map(readZhudaGltfInstancing(document).instancing.map((i) => [i.index, i]));
|
|
454
|
+
const out: ZhudaGltfNodeData[] = [];
|
|
455
|
+
list(record(document).nodes).forEach((raw, index) => {
|
|
456
|
+
const n = record(raw);
|
|
457
|
+
const light = lights[indexOf(record(record(n.extensions).KHR_lights_punctual).light) ?? -1];
|
|
458
|
+
const camera = cameras[indexOf(n.camera) ?? -1];
|
|
459
|
+
const instancing = copies.get(index);
|
|
460
|
+
if (n.extras === undefined && !light && !camera && !instancing) return;
|
|
461
|
+
out.push(
|
|
462
|
+
freeze({
|
|
463
|
+
index,
|
|
464
|
+
name: typeof n.name === 'string' ? n.name : null,
|
|
465
|
+
...(n.extras === undefined ? {} : { extras: JSON.parse(JSON.stringify(n.extras)) }),
|
|
466
|
+
...(camera ? { camera } : {}),
|
|
467
|
+
...(light ? { light } : {}),
|
|
468
|
+
...(instancing ? { instancing } : {}),
|
|
469
|
+
}),
|
|
470
|
+
);
|
|
471
|
+
});
|
|
472
|
+
return freeze(out);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// ------------------------------------------------------- GPU instancing ---
|
|
476
|
+
|
|
477
|
+
/** A node whose mesh is drawn many times (`EXT_mesh_gpu_instancing`), and the accessors that place its copies. */
|
|
478
|
+
export interface ZhudaGltfInstancing {
|
|
479
|
+
/** The glTF node. */
|
|
480
|
+
readonly index: number;
|
|
481
|
+
/** How many copies. */
|
|
482
|
+
readonly count: number;
|
|
483
|
+
/** Accessor indices; null where the file gives none (no move, no turn, no scale). */
|
|
484
|
+
readonly translation: number | null;
|
|
485
|
+
readonly rotation: number | null;
|
|
486
|
+
readonly scale: number | null;
|
|
487
|
+
}
|
|
488
|
+
/** Most copies one node may ask for: what one instanced mesh draws. */
|
|
489
|
+
export const ZHUDA_GLTF_MAX_INSTANCES = 200000;
|
|
490
|
+
/**
|
|
491
|
+
* The nodes that use `EXT_mesh_gpu_instancing`. A node whose attributes
|
|
492
|
+
* cannot be drawn is left out and named in `problems` (its mesh is then
|
|
493
|
+
* drawn once, where the node is).
|
|
494
|
+
*/
|
|
495
|
+
export function readZhudaGltfInstancing(document: unknown): {
|
|
496
|
+
instancing: readonly ZhudaGltfInstancing[];
|
|
497
|
+
problems: readonly ZhudaGltfProblem[];
|
|
498
|
+
} {
|
|
499
|
+
const accessors = list(record(document).accessors).map(record);
|
|
500
|
+
const instancing: ZhudaGltfInstancing[] = [],
|
|
501
|
+
problems: ZhudaGltfProblem[] = [];
|
|
502
|
+
list(record(document).nodes).forEach((raw, index) => {
|
|
503
|
+
const node = record(raw);
|
|
504
|
+
const extension = record(node.extensions).EXT_mesh_gpu_instancing;
|
|
505
|
+
if (extension === undefined) return;
|
|
506
|
+
const fail = (message: string) => problems.push({ index, message });
|
|
507
|
+
if (indexOf(node.mesh) === null) return fail('the node has no mesh to draw');
|
|
508
|
+
if (node.skin !== undefined) return fail('a skinned mesh cannot be drawn as copies');
|
|
509
|
+
const attributes = record(record(extension).attributes);
|
|
510
|
+
const wanted: Record<string, { type: string; components: readonly number[] }> = {
|
|
511
|
+
TRANSLATION: { type: 'VEC3', components: [5126] },
|
|
512
|
+
// A turn may be floats, or bytes or shorts read as -1..1.
|
|
513
|
+
ROTATION: { type: 'VEC4', components: [5126, 5120, 5122] },
|
|
514
|
+
SCALE: { type: 'VEC3', components: [5126] },
|
|
515
|
+
};
|
|
516
|
+
const found: Record<string, number | null> = { TRANSLATION: null, ROTATION: null, SCALE: null };
|
|
517
|
+
let count: number | null = null;
|
|
518
|
+
for (const [name, want] of Object.entries(wanted)) {
|
|
519
|
+
if (attributes[name] === undefined) continue;
|
|
520
|
+
const at = indexOf(attributes[name]),
|
|
521
|
+
accessor = at === null ? undefined : accessors[at];
|
|
522
|
+
if (!accessor || !Object.keys(accessor).length) return fail(`${name} names no accessor`);
|
|
523
|
+
const component = accessor.componentType as number;
|
|
524
|
+
if (accessor.type !== want.type || !want.components.includes(component))
|
|
525
|
+
return fail(
|
|
526
|
+
`${name} must be ${want.type} of floats${name === 'ROTATION' ? ' (or normalized bytes or shorts)' : ''}`,
|
|
527
|
+
);
|
|
528
|
+
if (component !== 5126 && accessor.normalized !== true)
|
|
529
|
+
return fail(`${name} of integers must be normalized`);
|
|
530
|
+
const many = indexOf(accessor.count);
|
|
531
|
+
if (many === null || many < 1) return fail(`${name} has no count`);
|
|
532
|
+
if (count !== null && many !== count)
|
|
533
|
+
return fail(`${name} has ${many} values where another attribute has ${count}`);
|
|
534
|
+
count = many;
|
|
535
|
+
found[name] = at;
|
|
536
|
+
}
|
|
537
|
+
if (count === null) return fail('no TRANSLATION, ROTATION or SCALE to place the copies by');
|
|
538
|
+
if (count > ZHUDA_GLTF_MAX_INSTANCES)
|
|
539
|
+
return fail(`${count} copies is more than one node may have (${ZHUDA_GLTF_MAX_INSTANCES})`);
|
|
540
|
+
instancing.push({
|
|
541
|
+
index,
|
|
542
|
+
count,
|
|
543
|
+
translation: found.TRANSLATION,
|
|
544
|
+
rotation: found.ROTATION,
|
|
545
|
+
scale: found.SCALE,
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
return freeze({ instancing, problems });
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* The matrix of each copy (sixteen numbers, column by column, in the node's
|
|
552
|
+
* own space) from the three attributes as they lie in the file: three
|
|
553
|
+
* numbers a move, four a turn (a quaternion x, y, z, w; integers are read as
|
|
554
|
+
* -1..1 by `rotationScale`, 1/127 for bytes and 1/32767 for shorts), three a
|
|
555
|
+
* scale. A turn of no length is no turn. `mirrored` lists the copies whose
|
|
556
|
+
* scale turns the mesh inside out: one draw has one facing, so an instanced
|
|
557
|
+
* mesh cannot draw them with the rest.
|
|
558
|
+
*/
|
|
559
|
+
export function zhudaGltfInstanceMatrices(
|
|
560
|
+
count: number,
|
|
561
|
+
translation?: ArrayLike<number> | null,
|
|
562
|
+
rotation?: ArrayLike<number> | null,
|
|
563
|
+
scale?: ArrayLike<number> | null,
|
|
564
|
+
rotationScale = 1,
|
|
565
|
+
): { matrices: Float32Array; mirrored: number[] } {
|
|
566
|
+
if (!Number.isInteger(count) || count < 1) throw Error('Invalid instance count');
|
|
567
|
+
for (const [name, values, size] of [
|
|
568
|
+
['translation', translation, 3],
|
|
569
|
+
['rotation', rotation, 4],
|
|
570
|
+
['scale', scale, 3],
|
|
571
|
+
] as const)
|
|
572
|
+
if (values && values.length < count * size)
|
|
573
|
+
throw Error(`Instance ${name} holds fewer than ${count} values`);
|
|
574
|
+
const matrices = new Float32Array(count * 16),
|
|
575
|
+
mirrored: number[] = [];
|
|
576
|
+
for (let i = 0; i < count; i++) {
|
|
577
|
+
let x = 0,
|
|
578
|
+
y = 0,
|
|
579
|
+
z = 0,
|
|
580
|
+
w = 1;
|
|
581
|
+
if (rotation) {
|
|
582
|
+
x = Math.max(rotation[i * 4] * rotationScale, -1);
|
|
583
|
+
y = Math.max(rotation[i * 4 + 1] * rotationScale, -1);
|
|
584
|
+
z = Math.max(rotation[i * 4 + 2] * rotationScale, -1);
|
|
585
|
+
w = Math.max(rotation[i * 4 + 3] * rotationScale, -1);
|
|
586
|
+
const length = Math.hypot(x, y, z, w);
|
|
587
|
+
if (length > 1e-8) {
|
|
588
|
+
x /= length;
|
|
589
|
+
y /= length;
|
|
590
|
+
z /= length;
|
|
591
|
+
w /= length;
|
|
592
|
+
} else {
|
|
593
|
+
x = y = z = 0;
|
|
594
|
+
w = 1;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
const sx = scale ? scale[i * 3] : 1,
|
|
598
|
+
sy = scale ? scale[i * 3 + 1] : 1,
|
|
599
|
+
sz = scale ? scale[i * 3 + 2] : 1;
|
|
600
|
+
const m = i * 16;
|
|
601
|
+
matrices[m] = (1 - 2 * (y * y + z * z)) * sx;
|
|
602
|
+
matrices[m + 1] = 2 * (x * y + w * z) * sx;
|
|
603
|
+
matrices[m + 2] = 2 * (x * z - w * y) * sx;
|
|
604
|
+
matrices[m + 4] = 2 * (x * y - w * z) * sy;
|
|
605
|
+
matrices[m + 5] = (1 - 2 * (x * x + z * z)) * sy;
|
|
606
|
+
matrices[m + 6] = 2 * (y * z + w * x) * sy;
|
|
607
|
+
matrices[m + 8] = 2 * (x * z + w * y) * sz;
|
|
608
|
+
matrices[m + 9] = 2 * (y * z - w * x) * sz;
|
|
609
|
+
matrices[m + 10] = (1 - 2 * (x * x + y * y)) * sz;
|
|
610
|
+
if (translation) {
|
|
611
|
+
matrices[m + 12] = translation[i * 3];
|
|
612
|
+
matrices[m + 13] = translation[i * 3 + 1];
|
|
613
|
+
matrices[m + 14] = translation[i * 3 + 2];
|
|
614
|
+
}
|
|
615
|
+
matrices[m + 15] = 1;
|
|
616
|
+
if (sx * sy * sz < 0) mirrored.push(i);
|
|
617
|
+
}
|
|
618
|
+
for (let i = 0; i < matrices.length; i++)
|
|
619
|
+
if (!Number.isFinite(matrices[i])) throw Error(`Instance ${Math.floor(i / 16)} is not finite`);
|
|
620
|
+
return { matrices, mirrored };
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// ---------------------------------------------------- quantized vertices ---
|
|
624
|
+
|
|
625
|
+
const components: Record<number, { size: number; read: string; max: number }> = {
|
|
626
|
+
5120: { size: 1, read: 'getInt8', max: 127 },
|
|
627
|
+
5121: { size: 1, read: 'getUint8', max: 255 },
|
|
628
|
+
5122: { size: 2, read: 'getInt16', max: 32767 },
|
|
629
|
+
5123: { size: 2, read: 'getUint16', max: 65535 },
|
|
630
|
+
};
|
|
631
|
+
const counts: Record<string, number> = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4 };
|
|
632
|
+
// The vertex attributes LayaAir reads as four-byte floats. COLOR_0 is left to
|
|
633
|
+
// the parse-time repair (zhudaGltfColors) and JOINTS_0 stays integer.
|
|
634
|
+
const attributeTypes: Record<string, readonly string[]> = {
|
|
635
|
+
POSITION: ['VEC3'],
|
|
636
|
+
NORMAL: ['VEC3'],
|
|
637
|
+
TANGENT: ['VEC4'],
|
|
638
|
+
TEXCOORD_0: ['VEC2'],
|
|
639
|
+
TEXCOORD_1: ['VEC2'],
|
|
640
|
+
WEIGHTS_0: ['VEC4'],
|
|
641
|
+
};
|
|
642
|
+
const targetTypes: Record<string, readonly string[]> = {
|
|
643
|
+
POSITION: ['VEC3'],
|
|
644
|
+
NORMAL: ['VEC3'],
|
|
645
|
+
TANGENT: ['VEC3'],
|
|
646
|
+
};
|
|
647
|
+
/**
|
|
648
|
+
* Vertex attribute accessors LayaAir would read wrongly: integers, normalized or
|
|
649
|
+
* not (`KHR_mesh_quantization`, or core glTF's normalized texture coordinates
|
|
650
|
+
* and weights). LayaAir copies them into a float vertex buffer unscaled.
|
|
651
|
+
*/
|
|
652
|
+
export function quantizedZhudaGltfAccessors(document: unknown): number[] {
|
|
653
|
+
const accessors = list(record(document).accessors),
|
|
654
|
+
found = new Set<number>();
|
|
655
|
+
const take = (value: unknown, types: readonly string[] | undefined, where: string) => {
|
|
656
|
+
const index = indexOf(value);
|
|
657
|
+
if (index === null || !types) return;
|
|
658
|
+
const a = record(accessors[index]);
|
|
659
|
+
if (a.componentType === 5126) return;
|
|
660
|
+
if (!components[a.componentType as number] || !types.includes(String(a.type)))
|
|
661
|
+
throw Error(`${where}: accessors[${index}] has a layout no vertex attribute may have`);
|
|
662
|
+
found.add(index);
|
|
663
|
+
};
|
|
664
|
+
list(record(document).meshes).forEach((mesh, m) =>
|
|
665
|
+
list(record(mesh).primitives).forEach((primitive, p) => {
|
|
666
|
+
const where = `meshes[${m}].primitives[${p}]`;
|
|
667
|
+
for (const [name, value] of Object.entries(record(record(primitive).attributes)))
|
|
668
|
+
take(value, attributeTypes[name], `${where}.attributes.${name}`);
|
|
669
|
+
list(record(primitive).targets).forEach((target, t) => {
|
|
670
|
+
for (const [name, value] of Object.entries(record(target)))
|
|
671
|
+
take(value, targetTypes[name], `${where}.targets[${t}].${name}`);
|
|
672
|
+
});
|
|
673
|
+
}),
|
|
674
|
+
);
|
|
675
|
+
return [...found].sort((a, b) => a - b);
|
|
676
|
+
}
|
|
677
|
+
// A buffer whose bytes are in the file itself, as a base64 data URI.
|
|
678
|
+
const embeddedBuffer = /^data:(application\/(octet-stream|gltf-buffer))?;base64,/;
|
|
679
|
+
/**
|
|
680
|
+
* The buffers a file's buffer views use but keeps outside itself (a `uri` that
|
|
681
|
+
* is not an embedded base64 one). `dequantizeZhudaGltf` has no bytes for them.
|
|
682
|
+
*/
|
|
683
|
+
export function externalZhudaGltfBuffers(document: unknown): number[] {
|
|
684
|
+
const buffers = list(record(document).buffers).map(record),
|
|
685
|
+
found = new Set<number>();
|
|
686
|
+
for (const view of list(record(document).bufferViews)) {
|
|
687
|
+
const index = indexOf(record(view).buffer),
|
|
688
|
+
uri = index === null ? undefined : buffers[index]?.uri;
|
|
689
|
+
if (typeof uri === 'string' && !embeddedBuffer.test(uri)) found.add(index!);
|
|
690
|
+
}
|
|
691
|
+
return [...found].sort((a, b) => a - b);
|
|
692
|
+
}
|
|
693
|
+
export interface ZhudaDequantizeResult {
|
|
694
|
+
/** A GLB: the source data in one buffer, then the float copies. */
|
|
695
|
+
readonly bytes: Uint8Array;
|
|
696
|
+
/** Accessors turned into floats. */
|
|
697
|
+
readonly accessors: number;
|
|
698
|
+
/** Bytes of float data added. */
|
|
699
|
+
readonly floatBytes: number;
|
|
700
|
+
}
|
|
701
|
+
const align4 = (n: number) => Math.ceil(n / 4) * 4;
|
|
702
|
+
/**
|
|
703
|
+
* Turns quantized vertex attributes into 32-bit floats before LayaAir parses
|
|
704
|
+
* the file (see `quantizedZhudaGltfAccessors`). Node transforms are kept: they
|
|
705
|
+
* still carry `KHR_mesh_quantization`'s scale and offset. Needs embedded
|
|
706
|
+
* buffers. Returns null when nothing needs it. GPU memory becomes that of an
|
|
707
|
+
* unquantized export; the quantized source bytes stay in the file unread.
|
|
708
|
+
*/
|
|
709
|
+
export function dequantizeZhudaGltf(
|
|
710
|
+
input: ArrayBuffer | Uint8Array,
|
|
711
|
+
options: { maxOutputBytes?: number } = {},
|
|
712
|
+
): ZhudaDequantizeResult | null {
|
|
713
|
+
const limit = options.maxOutputBytes ?? 256 * 1024 * 1024;
|
|
714
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) throw Error('Invalid dequantize output limit');
|
|
715
|
+
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
716
|
+
const { document, bin } = readZhudaGltfBytes(bytes);
|
|
717
|
+
const targets = quantizedZhudaGltfAccessors(document);
|
|
718
|
+
if (!targets.length) return null;
|
|
719
|
+
const accessors = list(document.accessors).map(record),
|
|
720
|
+
views = list(document.bufferViews).map(record),
|
|
721
|
+
sources = list(document.buffers).map(record);
|
|
722
|
+
// Every buffer a view uses, laid end to end in the one GLB buffer.
|
|
723
|
+
const data = new Map<number, Uint8Array>(),
|
|
724
|
+
starts = new Map<number, number>();
|
|
725
|
+
let length = 0;
|
|
726
|
+
views.forEach((view, v) => {
|
|
727
|
+
const i = indexOf(view.buffer);
|
|
728
|
+
if (i === null || !sources[i]) throw Error(`bufferViews[${v}] names no buffer`);
|
|
729
|
+
if (data.has(i)) return;
|
|
730
|
+
const source = sources[i],
|
|
731
|
+
declared = indexOf(source.byteLength);
|
|
732
|
+
if (declared === null) throw Error(`buffers[${i}] has no byteLength`);
|
|
733
|
+
let bytesOf: Uint8Array;
|
|
734
|
+
if (source.uri === undefined && i === 0 && bin) bytesOf = bin;
|
|
735
|
+
else if (typeof source.uri === 'string' && embeddedBuffer.test(source.uri)) {
|
|
736
|
+
const encoded = source.uri.slice(source.uri.indexOf(',') + 1);
|
|
737
|
+
if (Math.floor((encoded.length * 3) / 4) > limit) throw Error('Oversized embedded buffer');
|
|
738
|
+
bytesOf = Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
|
|
739
|
+
} else throw Error(`Dequantizing needs embedded buffers; buffers[${i}] has no bytes here`);
|
|
740
|
+
if (bytesOf.byteLength < declared) throw Error(`buffers[${i}] is shorter than its byteLength`);
|
|
741
|
+
data.set(i, bytesOf.subarray(0, declared));
|
|
742
|
+
starts.set(i, length);
|
|
743
|
+
length = align4(length + declared);
|
|
744
|
+
});
|
|
745
|
+
/** A buffer view's bytes, bounds-checked. */
|
|
746
|
+
const viewOf = (value: unknown) => {
|
|
747
|
+
const index = indexOf(value),
|
|
748
|
+
view = index === null ? undefined : views[index];
|
|
749
|
+
if (!view) throw Error(`bufferViews[${String(value)}] is missing`);
|
|
750
|
+
const source = data.get(indexOf(view.buffer)!)!,
|
|
751
|
+
offset = indexOf(view.byteOffset ?? 0),
|
|
752
|
+
size = indexOf(view.byteLength);
|
|
753
|
+
if (offset === null || size === null || offset + size > source.byteLength)
|
|
754
|
+
throw Error(`bufferViews[${index}] lies outside its buffer`);
|
|
755
|
+
return {
|
|
756
|
+
bytes: new DataView(source.buffer, source.byteOffset + offset, size),
|
|
757
|
+
offset,
|
|
758
|
+
stride: indexOf(view.byteStride),
|
|
759
|
+
};
|
|
760
|
+
};
|
|
761
|
+
const floats = new Map<number, Float32Array>();
|
|
762
|
+
let floatBytes = 0;
|
|
763
|
+
for (const index of targets) {
|
|
764
|
+
const a = accessors[index];
|
|
765
|
+
const c = components[a.componentType as number],
|
|
766
|
+
n = counts[String(a.type)],
|
|
767
|
+
count = indexOf(a.count),
|
|
768
|
+
element = c.size * n;
|
|
769
|
+
if (count === null || count < 1) throw Error(`accessors[${index}] has no count`);
|
|
770
|
+
floatBytes += count * n * 4;
|
|
771
|
+
if (floatBytes + length > limit) throw Error('Dequantized output budget exceeded');
|
|
772
|
+
const out = new Float32Array(count * n);
|
|
773
|
+
const value = (view: DataView, at: number) => {
|
|
774
|
+
const raw = (view as any)[c.read](at, true) as number;
|
|
775
|
+
return a.normalized === true ? Math.max(raw / c.max, -1) : raw;
|
|
776
|
+
};
|
|
777
|
+
if (a.bufferView !== undefined) {
|
|
778
|
+
const start = indexOf(a.byteOffset ?? 0),
|
|
779
|
+
{ bytes: view, offset, stride } = viewOf(a.bufferView),
|
|
780
|
+
step = stride ?? element;
|
|
781
|
+
if (start === null || (offset + start) % c.size || step < element || step % c.size)
|
|
782
|
+
throw Error(`accessors[${index}] is misaligned or has an invalid stride`);
|
|
783
|
+
if (start + step * (count - 1) + element > view.byteLength)
|
|
784
|
+
throw Error(`accessors[${index}] reads past its buffer view`);
|
|
785
|
+
for (let i = 0; i < count; i++)
|
|
786
|
+
for (let k = 0; k < n; k++) out[i * n + k] = value(view, start + i * step + k * c.size);
|
|
787
|
+
}
|
|
788
|
+
if (a.sparse !== undefined) {
|
|
789
|
+
const sparse = record(a.sparse),
|
|
790
|
+
many = indexOf(sparse.count),
|
|
791
|
+
ids = record(sparse.indices),
|
|
792
|
+
values = record(sparse.values);
|
|
793
|
+
const idSize = ({ 5121: 1, 5123: 2, 5125: 4 } as Record<number, number>)[
|
|
794
|
+
ids.componentType as number
|
|
795
|
+
];
|
|
796
|
+
const idStart = indexOf(ids.byteOffset ?? 0),
|
|
797
|
+
valueStart = indexOf(values.byteOffset ?? 0);
|
|
798
|
+
if (!many || many > count || !idSize || idStart === null || valueStart === null)
|
|
799
|
+
throw Error(`accessors[${index}].sparse is invalid`);
|
|
800
|
+
const idView = viewOf(ids.bufferView).bytes,
|
|
801
|
+
valueView = viewOf(values.bufferView).bytes;
|
|
802
|
+
if (idStart + many * idSize > idView.byteLength)
|
|
803
|
+
throw Error(`accessors[${index}].sparse indices read past their view`);
|
|
804
|
+
if (valueStart + many * element > valueView.byteLength)
|
|
805
|
+
throw Error(`accessors[${index}].sparse values read past their view`);
|
|
806
|
+
let previous = -1;
|
|
807
|
+
for (let s = 0; s < many; s++) {
|
|
808
|
+
const at = idStart + s * idSize;
|
|
809
|
+
const target =
|
|
810
|
+
idSize === 1
|
|
811
|
+
? idView.getUint8(at)
|
|
812
|
+
: idSize === 2
|
|
813
|
+
? idView.getUint16(at, true)
|
|
814
|
+
: idView.getUint32(at, true);
|
|
815
|
+
if (target <= previous || target >= count)
|
|
816
|
+
throw Error(`accessors[${index}].sparse indices must rise inside the accessor`);
|
|
817
|
+
previous = target;
|
|
818
|
+
for (let k = 0; k < n; k++)
|
|
819
|
+
out[target * n + k] = value(valueView, valueStart + s * element + k * c.size);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
floats.set(index, out);
|
|
823
|
+
}
|
|
824
|
+
// Lay out: the sources (their views re-pointed to buffer 0), then one view per float copy.
|
|
825
|
+
for (const view of views) {
|
|
826
|
+
view.byteOffset = starts.get(indexOf(view.buffer)!)! + (indexOf(view.byteOffset ?? 0) ?? 0);
|
|
827
|
+
view.buffer = 0;
|
|
828
|
+
}
|
|
829
|
+
const output = new Uint8Array(length + floatBytes);
|
|
830
|
+
for (const [buffer, bytesOf] of data) output.set(bytesOf, starts.get(buffer)!);
|
|
831
|
+
let cursor = length;
|
|
832
|
+
for (const [index, values] of floats) {
|
|
833
|
+
const a = accessors[index],
|
|
834
|
+
n = counts[String(a.type)];
|
|
835
|
+
output.set(new Uint8Array(values.buffer), cursor);
|
|
836
|
+
views.push({ buffer: 0, byteOffset: cursor, byteLength: values.byteLength });
|
|
837
|
+
cursor += values.byteLength;
|
|
838
|
+
a.bufferView = views.length - 1;
|
|
839
|
+
a.componentType = 5126;
|
|
840
|
+
delete a.byteOffset;
|
|
841
|
+
delete a.normalized;
|
|
842
|
+
delete a.sparse;
|
|
843
|
+
// LayaAir takes a mesh's bounds from POSITION's min and max: give the float ones.
|
|
844
|
+
const min = Array<number>(n).fill(Infinity),
|
|
845
|
+
max = Array<number>(n).fill(-Infinity);
|
|
846
|
+
for (let i = 0; i < values.length; i++) {
|
|
847
|
+
min[i % n] = Math.min(min[i % n], values[i]);
|
|
848
|
+
max[i % n] = Math.max(max[i % n], values[i]);
|
|
849
|
+
}
|
|
850
|
+
a.min = min;
|
|
851
|
+
a.max = max;
|
|
852
|
+
}
|
|
853
|
+
document.accessors = accessors;
|
|
854
|
+
document.bufferViews = views;
|
|
855
|
+
document.buffers = [{ byteLength: output.byteLength }];
|
|
856
|
+
for (const key of ['extensionsUsed', 'extensionsRequired']) {
|
|
857
|
+
const names = list(document[key]).filter((x) => x !== 'KHR_mesh_quantization');
|
|
858
|
+
if (names.length) document[key] = names;
|
|
859
|
+
else delete document[key];
|
|
860
|
+
}
|
|
861
|
+
const glb = writeZhudaGlb(document, output);
|
|
862
|
+
if (glb.byteLength > limit) throw Error('Dequantized output budget exceeded');
|
|
863
|
+
return { bytes: glb, accessors: floats.size, floatBytes };
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// ------------------------------------------------------------------- GLB ---
|
|
867
|
+
|
|
868
|
+
/** A glTF JSON or GLB file's document and, for a GLB, its binary chunk. */
|
|
869
|
+
export function readZhudaGltfBytes(bytes: Uint8Array): { document: Json; bin?: Uint8Array } {
|
|
870
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
871
|
+
if (bytes.byteLength < 12 || view.getUint32(0, true) !== 0x46546c67)
|
|
872
|
+
return { document: record(JSON.parse(zhudaUtf8Decode(bytes))) };
|
|
873
|
+
if (view.getUint32(4, true) !== 2 || view.getUint32(8, true) !== bytes.byteLength)
|
|
874
|
+
throw Error('Invalid GLB header');
|
|
875
|
+
let offset = 12,
|
|
876
|
+
document: Json | undefined,
|
|
877
|
+
bin: Uint8Array | undefined;
|
|
878
|
+
for (let chunk = 0; offset < bytes.byteLength; chunk++) {
|
|
879
|
+
if (offset + 8 > bytes.byteLength) throw Error('Truncated GLB chunk');
|
|
880
|
+
const size = view.getUint32(offset, true),
|
|
881
|
+
kind = view.getUint32(offset + 4, true);
|
|
882
|
+
if (size % 4 || offset + 8 + size > bytes.byteLength) throw Error('Invalid GLB chunk length');
|
|
883
|
+
const data = bytes.subarray(offset + 8, offset + 8 + size);
|
|
884
|
+
if (chunk === 0) {
|
|
885
|
+
if (kind !== 0x4e4f534a) throw Error('GLB JSON must be first');
|
|
886
|
+
document = record(JSON.parse(zhudaUtf8Decode(data)));
|
|
887
|
+
} else if (chunk === 1 && kind === 0x004e4942) bin = data;
|
|
888
|
+
else throw Error('Unsupported GLB chunk');
|
|
889
|
+
offset += 8 + size;
|
|
890
|
+
}
|
|
891
|
+
if (!document) throw Error('GLB has no JSON chunk');
|
|
892
|
+
return { document, bin };
|
|
893
|
+
}
|
|
894
|
+
/** A GLB of a document and its one binary buffer. */
|
|
895
|
+
export function writeZhudaGlb(document: unknown, bin: Uint8Array): Uint8Array {
|
|
896
|
+
const json = zhudaUtf8Encode(JSON.stringify(document)),
|
|
897
|
+
jsonLength = align4(json.byteLength),
|
|
898
|
+
binLength = align4(bin.byteLength);
|
|
899
|
+
const total = 28 + jsonLength + binLength;
|
|
900
|
+
if (total > 0xffffffff) throw Error('GLB too large');
|
|
901
|
+
const glb = new Uint8Array(total),
|
|
902
|
+
header = new DataView(glb.buffer);
|
|
903
|
+
header.setUint32(0, 0x46546c67, true);
|
|
904
|
+
header.setUint32(4, 2, true);
|
|
905
|
+
header.setUint32(8, total, true);
|
|
906
|
+
header.setUint32(12, jsonLength, true);
|
|
907
|
+
header.setUint32(16, 0x4e4f534a, true);
|
|
908
|
+
glb.fill(32, 20, 20 + jsonLength);
|
|
909
|
+
glb.set(json, 20);
|
|
910
|
+
header.setUint32(20 + jsonLength, binLength, true);
|
|
911
|
+
header.setUint32(24 + jsonLength, 0x004e4942, true);
|
|
912
|
+
glb.set(bin, 28 + jsonLength);
|
|
913
|
+
return glb;
|
|
914
|
+
}
|