179 lines
4.9 KiB
TypeScript
179 lines
4.9 KiB
TypeScript
function normalizeSplatLabel(label: string) {
|
|
return String(label || "").trim().toLowerCase();
|
|
}
|
|
|
|
function parseResolutionScore(label: string) {
|
|
const normalized = normalizeSplatLabel(label);
|
|
if (!normalized) return 0;
|
|
|
|
if (
|
|
normalized.includes("full") ||
|
|
normalized.includes("origin") ||
|
|
normalized.includes("source") ||
|
|
normalized.includes("max")
|
|
) {
|
|
return Number.MAX_SAFE_INTEGER;
|
|
}
|
|
|
|
const match = normalized.match(/(\d+(?:\.\d+)?)\s*([km]?)/);
|
|
if (!match) return 0;
|
|
|
|
const value = Number(match[1]);
|
|
if (!Number.isFinite(value)) return 0;
|
|
|
|
const unit = match[2];
|
|
if (unit === "m") return Math.round(value * 1_000_000);
|
|
if (unit === "k") return Math.round(value * 1_000);
|
|
return Math.round(value);
|
|
}
|
|
|
|
function compareSplatEntries(
|
|
a: [string, string],
|
|
b: [string, string]
|
|
) {
|
|
const scoreDelta = parseResolutionScore(b[0]) - parseResolutionScore(a[0]);
|
|
if (scoreDelta !== 0) return scoreDelta;
|
|
return a[0].localeCompare(b[0], "en");
|
|
}
|
|
|
|
export function getSortedWorldLabsSplatEntries(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return Object.entries(spzUrls || {})
|
|
.filter(
|
|
(entry): entry is [string, string] =>
|
|
Boolean(entry[0]) && typeof entry[1] === "string" && entry[1].trim().length > 0
|
|
)
|
|
.sort(compareSplatEntries);
|
|
}
|
|
|
|
export function pickBestWorldLabsSplatEntry(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getSortedWorldLabsSplatEntries(spzUrls)[0] || null;
|
|
}
|
|
|
|
export function pickBestWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return pickBestWorldLabsSplatEntry(spzUrls)?.[1] || null;
|
|
}
|
|
|
|
function compareInlineSplatEntries(
|
|
left: [string, string],
|
|
right: [string, string]
|
|
) {
|
|
// World Labs exposes multiple SPZ tiers; the canvas should show the highest
|
|
// quality available first and keep lower tiers only as load fallbacks.
|
|
return compareSplatEntries(left, right);
|
|
}
|
|
|
|
const KNOWN_ARCHIVED_ASSET_HOSTS = [
|
|
"blob.vercel-storage.com",
|
|
"s3.1080p.hk",
|
|
"img.yydd.ai",
|
|
];
|
|
|
|
function isKnownArchivedAssetHost(hostname: string) {
|
|
return KNOWN_ARCHIVED_ASSET_HOSTS.some(
|
|
(host) => hostname === host || hostname.endsWith(`.${host}`)
|
|
);
|
|
}
|
|
|
|
export function isLikelyWorldLabsTemporaryAssetUrl(value?: string | null) {
|
|
const raw = String(value || "").trim();
|
|
if (!raw) return false;
|
|
if (raw.startsWith("/") || raw.startsWith("blob:") || raw.startsWith("data:")) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const parsed = new URL(raw);
|
|
const hostname = parsed.hostname.toLowerCase();
|
|
const pathname = parsed.pathname.toLowerCase();
|
|
|
|
if (isKnownArchivedAssetHost(hostname)) {
|
|
return false;
|
|
}
|
|
|
|
if (hostname.includes("worldlabs") || hostname.includes("marble")) {
|
|
return true;
|
|
}
|
|
|
|
const likelyWorldAsset =
|
|
pathname.includes("worldlabs") ||
|
|
pathname.includes("marble") ||
|
|
pathname.includes("spz") ||
|
|
pathname.includes("splat") ||
|
|
pathname.includes("mesh") ||
|
|
pathname.endsWith(".glb") ||
|
|
pathname.endsWith(".gltf");
|
|
|
|
return (
|
|
likelyWorldAsset &&
|
|
(hostname.includes("googleapis.com") ||
|
|
hostname.includes("googleusercontent.com") ||
|
|
hostname.includes("cloudfront.net"))
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isArchivedWorldLabsAssetUrl(value?: string | null) {
|
|
const raw = String(value || "").trim();
|
|
return Boolean(raw) && !isLikelyWorldLabsTemporaryAssetUrl(raw);
|
|
}
|
|
|
|
export function getArchivedWorldLabsSplatEntries(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getSortedWorldLabsSplatEntries(spzUrls).filter(([, url]) =>
|
|
isArchivedWorldLabsAssetUrl(url)
|
|
);
|
|
}
|
|
|
|
export function pickBestArchivedWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return getArchivedWorldLabsSplatEntries(spzUrls)[0]?.[1] || null;
|
|
}
|
|
|
|
export function pickPreferredInlineWorldLabsSplatEntry(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
const archivedEntries = getArchivedWorldLabsSplatEntries(spzUrls);
|
|
const sourceEntries =
|
|
archivedEntries.length > 0
|
|
? archivedEntries
|
|
: getSortedWorldLabsSplatEntries(spzUrls);
|
|
return sourceEntries.slice().sort(compareInlineSplatEntries)[0] || null;
|
|
}
|
|
|
|
export function pickPreferredInlineWorldLabsSplatUrl(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
return pickPreferredInlineWorldLabsSplatEntry(spzUrls)?.[1] || null;
|
|
}
|
|
|
|
export function getPreferredInlineWorldLabsSplatUrls(
|
|
spzUrls?: Record<string, string> | null
|
|
) {
|
|
const archivedEntries = getArchivedWorldLabsSplatEntries(spzUrls);
|
|
const sourceEntries =
|
|
archivedEntries.length > 0
|
|
? archivedEntries
|
|
: getSortedWorldLabsSplatEntries(spzUrls);
|
|
const orderedEntries = sourceEntries.slice().sort(compareInlineSplatEntries);
|
|
const seen = new Set<string>();
|
|
|
|
return orderedEntries
|
|
.map(([, url]) => url)
|
|
.filter((url) => {
|
|
const normalized = String(url || "").trim();
|
|
if (!normalized || seen.has(normalized)) return false;
|
|
seen.add(normalized);
|
|
return true;
|
|
});
|
|
}
|