44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
export type ModelAssetFormat = "gltf" | "glb" | "usdz" | "fbx" | "obj" | "stl" | "3mf" | "3d";
|
|
|
|
export function normalizeModelAssetUrl(value: unknown): string | null {
|
|
if (typeof value !== "string") return null;
|
|
const normalized = value.trim();
|
|
return normalized || null;
|
|
}
|
|
|
|
export function extractModelAssetFormat(modelUrl?: string | null): ModelAssetFormat {
|
|
const value = String(modelUrl || "").toLowerCase();
|
|
if (!value) return "3d";
|
|
if (value.includes(".gltf")) return "gltf";
|
|
if (value.includes(".glb")) return "glb";
|
|
if (value.includes(".usdz")) return "usdz";
|
|
if (value.includes(".fbx")) return "fbx";
|
|
if (value.includes(".obj")) return "obj";
|
|
if (value.includes(".stl")) return "stl";
|
|
if (value.includes(".3mf")) return "3mf";
|
|
return "3d";
|
|
}
|
|
|
|
export function resolvePreferredModelAssetUrl(params: {
|
|
primaryUrl?: string | null;
|
|
sourceUrl?: string | null;
|
|
fallbackUrl?: string | null;
|
|
}) {
|
|
const primaryUrl = normalizeModelAssetUrl(params.primaryUrl);
|
|
const sourceUrl = normalizeModelAssetUrl(params.sourceUrl);
|
|
const fallbackUrl = normalizeModelAssetUrl(params.fallbackUrl);
|
|
|
|
if (sourceUrl) {
|
|
if (!primaryUrl) return sourceUrl;
|
|
if (primaryUrl !== sourceUrl) {
|
|
const primaryFormat = extractModelAssetFormat(primaryUrl);
|
|
const sourceFormat = extractModelAssetFormat(sourceUrl);
|
|
|
|
if (primaryFormat === "gltf") return sourceUrl;
|
|
if (primaryFormat !== "glb" && sourceFormat === "glb") return sourceUrl;
|
|
}
|
|
}
|
|
|
|
return primaryUrl || sourceUrl || fallbackUrl || null;
|
|
}
|