oda-engine 0.0.0-stage → 0.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/README.md +151 -2
- package/bin/nowhere.mjs +352 -0
- package/lib/account.mjs +57 -0
- package/lib/agentModules.mjs +57 -0
- package/lib/devServer.mjs +351 -0
- package/lib/headless.mjs +231 -0
- package/lib/headlessPreview.mjs +212 -0
- package/lib/keys.mjs +517 -0
- package/lib/make.mjs +855 -0
- package/lib/mcp.mjs +537 -0
- package/lib/open.mjs +22 -0
- package/lib/preview.mjs +87 -0
- package/lib/previewRpc.mjs +134 -0
- package/lib/project.mjs +359 -0
- package/lib/publishFlow.mjs +90 -0
- package/lib/resources.mjs +50 -0
- package/lib/rules.mjs +60 -0
- package/lib/scaffold.mjs +42 -0
- package/lib/setup.mjs +59 -0
- package/lib/tools.mjs +668 -0
- package/package.json +51 -4
- package/template/game.config.json +9 -0
- package/template/scene.json +4 -0
- package/template/src/main.ts +11 -0
- package/vendor/DOCS.md +447 -0
- package/vendor/agent/appRun.mjs +520 -0
- package/vendor/agent/assets/brief.mjs +242 -0
- package/vendor/agent/assets/collider.mjs +531 -0
- package/vendor/agent/assets/environments/index.mjs +12 -0
- package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
- package/vendor/agent/assets/fetch.mjs +168 -0
- package/vendor/agent/assets/footprint.mjs +13 -0
- package/vendor/agent/assets/gltf.mjs +547 -0
- package/vendor/agent/assets/hdr.mjs +269 -0
- package/vendor/agent/assets/imageOps.mjs +171 -0
- package/vendor/agent/assets/images/codex.mjs +307 -0
- package/vendor/agent/assets/images/fal.mjs +75 -0
- package/vendor/agent/assets/images/gemini.mjs +84 -0
- package/vendor/agent/assets/images/index.mjs +25 -0
- package/vendor/agent/assets/images/openai.mjs +75 -0
- package/vendor/agent/assets/images/stability.mjs +71 -0
- package/vendor/agent/assets/images/views.mjs +31 -0
- package/vendor/agent/assets/import.mjs +1352 -0
- package/vendor/agent/assets/inspect.mjs +465 -0
- package/vendor/agent/assets/jobs.mjs +196 -0
- package/vendor/agent/assets/ktx2.mjs +290 -0
- package/vendor/agent/assets/ledger.mjs +466 -0
- package/vendor/agent/assets/net.mjs +254 -0
- package/vendor/agent/assets/prices.json +39 -0
- package/vendor/agent/assets/providers/fal.mjs +115 -0
- package/vendor/agent/assets/providers/index.mjs +22 -0
- package/vendor/agent/assets/providers/meshy.mjs +119 -0
- package/vendor/agent/assets/providers/tripo.mjs +188 -0
- package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
- package/vendor/agent/assets/sources/index.mjs +23 -0
- package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
- package/vendor/agent/assets/sources/polypizza.mjs +121 -0
- package/vendor/agent/assets/splat.mjs +631 -0
- package/vendor/agent/assets/targets.mjs +122 -0
- package/vendor/agent/assets/tile.mjs +261 -0
- package/vendor/agent/assets/unzip.mjs +180 -0
- package/vendor/agent/blender/cleanup.py +360 -0
- package/vendor/agent/blender/convert.py +167 -0
- package/vendor/agent/blender/find.mjs +192 -0
- package/vendor/agent/blender/nw_export.py +104 -0
- package/vendor/agent/blender/nw_rubric.py +426 -0
- package/vendor/agent/blender/run.mjs +193 -0
- package/vendor/agent/blender/runner.py +252 -0
- package/vendor/agent/blender/sheet.py +743 -0
- package/vendor/agent/depth.mjs +134 -0
- package/vendor/agent/director/board.mjs +451 -0
- package/vendor/agent/director/contracts.mjs +663 -0
- package/vendor/agent/director/director.mjs +2365 -0
- package/vendor/agent/director/estimate.mjs +200 -0
- package/vendor/agent/director/gate.mjs +802 -0
- package/vendor/agent/director/limits.json +16 -0
- package/vendor/agent/director/methods.mjs +457 -0
- package/vendor/agent/director/policy.json +33 -0
- package/vendor/agent/director/prompts.mjs +244 -0
- package/vendor/agent/director/reading.mjs +341 -0
- package/vendor/agent/director/router.mjs +184 -0
- package/vendor/agent/director/seams.mjs +243 -0
- package/vendor/agent/director/session.mjs +643 -0
- package/vendor/agent/director/store.mjs +119 -0
- package/vendor/agent/director/taskGate.mjs +110 -0
- package/vendor/agent/director/teamLedger.mjs +118 -0
- package/vendor/agent/director/triage.mjs +63 -0
- package/vendor/agent/downloads.cjs +363 -0
- package/vendor/agent/flow.mjs +78 -0
- package/vendor/agent/guard.mjs +613 -0
- package/vendor/agent/host.mjs +1736 -0
- package/vendor/agent/language.mjs +58 -0
- package/vendor/agent/ledger.mjs +360 -0
- package/vendor/agent/locale.cjs +186 -0
- package/vendor/agent/loop/capacity.mjs +74 -0
- package/vendor/agent/loop/compose.mjs +127 -0
- package/vendor/agent/loop/gate.mjs +205 -0
- package/vendor/agent/loop/judge.mjs +189 -0
- package/vendor/agent/loop/levels.mjs +273 -0
- package/vendor/agent/loop/state.mjs +358 -0
- package/vendor/agent/notes.cjs +278 -0
- package/vendor/agent/pace.mjs +258 -0
- package/vendor/agent/panels.mjs +431 -0
- package/vendor/agent/prompt.mjs +342 -0
- package/vendor/agent/reference/board.mjs +410 -0
- package/vendor/agent/reference/compose.mjs +364 -0
- package/vendor/agent/reference/decode.mjs +56 -0
- package/vendor/agent/reference/facts.mjs +1874 -0
- package/vendor/agent/reference/png.mjs +107 -0
- package/vendor/agent/reference/records.mjs +63 -0
- package/vendor/agent/reference/web.mjs +252 -0
- package/vendor/agent/roles/artist.mjs +69 -0
- package/vendor/agent/roles/builder.mjs +79 -0
- package/vendor/agent/roles/checks.mjs +223 -0
- package/vendor/agent/roles/common.mjs +188 -0
- package/vendor/agent/roles/critic.mjs +171 -0
- package/vendor/agent/roles/index.mjs +39 -0
- package/vendor/agent/roles/modeller.mjs +142 -0
- package/vendor/agent/roles/qa.mjs +58 -0
- package/vendor/agent/roles/rubrics.mjs +79 -0
- package/vendor/agent/roles/sound.mjs +69 -0
- package/vendor/agent/rounds.cjs +333 -0
- package/vendor/agent/shared.mjs +284 -0
- package/vendor/agent/stopGate.mjs +574 -0
- package/vendor/agent/subagents.mjs +129 -0
- package/vendor/agent/tools/assets.mjs +499 -0
- package/vendor/agent/tools/blender.mjs +754 -0
- package/vendor/agent/tools/cards.mjs +479 -0
- package/vendor/agent/tools/choices.mjs +479 -0
- package/vendor/agent/tools/compose.mjs +74 -0
- package/vendor/agent/tools/environment.mjs +488 -0
- package/vendor/agent/tools/eyes.mjs +733 -0
- package/vendor/agent/tools/generate.mjs +338 -0
- package/vendor/agent/tools/guide.mjs +46 -0
- package/vendor/agent/tools/harness.mjs +193 -0
- package/vendor/agent/tools/image.mjs +643 -0
- package/vendor/agent/tools/index.mjs +162 -0
- package/vendor/agent/tools/look.mjs +578 -0
- package/vendor/agent/tools/loop.mjs +858 -0
- package/vendor/agent/tools/others.mjs +123 -0
- package/vendor/agent/tools/pictures.mjs +535 -0
- package/vendor/agent/tools/reference.mjs +366 -0
- package/vendor/agent/transcripts.mjs +118 -0
- package/vendor/agent-loop.mjs +602 -0
- package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
- package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
- package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
- package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
- package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
- package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
- package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
- package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
- package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
- package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
- package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
- package/vendor/auth.cjs +570 -0
- package/vendor/build.cjs +953 -0
- package/vendor/catalog.json +455 -0
- package/vendor/connectors.cjs +688 -0
- package/vendor/eyes.mjs +733 -0
- package/vendor/gameExports.json +1390 -0
- package/vendor/git.cjs +222 -0
- package/vendor/models.json +57 -0
- package/vendor/publish.cjs +1018 -0
- package/vendor/refBoard.cjs +301 -0
- package/vendor/schema/dist/assets.d.ts +335 -0
- package/vendor/schema/dist/assets.js +439 -0
- package/vendor/schema/dist/brief.d.ts +357 -0
- package/vendor/schema/dist/brief.js +302 -0
- package/vendor/schema/dist/common.d.ts +9 -0
- package/vendor/schema/dist/common.js +13 -0
- package/vendor/schema/dist/config.d.ts +406 -0
- package/vendor/schema/dist/config.js +355 -0
- package/vendor/schema/dist/contract.d.ts +599 -0
- package/vendor/schema/dist/contract.js +1146 -0
- package/vendor/schema/dist/index.d.ts +14 -0
- package/vendor/schema/dist/index.js +21 -0
- package/vendor/schema/dist/look.d.ts +463 -0
- package/vendor/schema/dist/look.js +681 -0
- package/vendor/schema/dist/lookStats.d.ts +230 -0
- package/vendor/schema/dist/lookStats.js +768 -0
- package/vendor/schema/dist/pilot.d.ts +32 -0
- package/vendor/schema/dist/pilot.js +44 -0
- package/vendor/schema/dist/publish.d.ts +214 -0
- package/vendor/schema/dist/publish.js +291 -0
- package/vendor/schema/dist/scene.d.ts +75 -0
- package/vendor/schema/dist/scene.js +120 -0
- package/vendor/schema/dist/skyLight.d.ts +141 -0
- package/vendor/schema/dist/skyLight.js +436 -0
- package/vendor/schema/dist/style.d.ts +131 -0
- package/vendor/schema/dist/style.js +155 -0
- package/vendor/schema/package.json +1 -0
- package/vendor/selectionPrompt.mjs +51 -0
- package/vendor/useTools.mjs +195 -0
- package/vendor/userMessage.mjs +26 -0
- package/web-dist/assets/GaussianSplatUtils-CB77cP9U.js +1 -0
- package/web-dist/assets/KTX2Loader-6OIWUF7s.js +2 -0
- package/web-dist/assets/SPZLoader-zYj0Alfv.js +1 -0
- package/web-dist/assets/gaussianSplat-CJTI1sEh.js +1 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/index-DMyJcoZJ.js +693 -0
- package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
- package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
- package/web-dist/index.html +34 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// Tripo: text, one picture or several views to a textured 3D model, through
|
|
2
|
+
// its OpenAPI v3 with the creator's key (connector tripo, env TRIPO_API_KEY,
|
|
3
|
+
// Authorization: Bearer). The shapes below are Tripo's documented v3 ones
|
|
4
|
+
// (developers.tripo3d.ai, read 2026-10-22; the envelope, the error codes and
|
|
5
|
+
// the task id format were checked live with free GETs), NOT exercised with a
|
|
6
|
+
// paid call from here: no check ever spends, and the checks use a fake.
|
|
7
|
+
// Verify one real job before release.
|
|
8
|
+
//
|
|
9
|
+
// upload POST /v3/files multipart, one part `file` -> { code: 0, data: { file_token } }
|
|
10
|
+
// text-to-3d POST /v3/generation/text-to-model { prompt, model, texture, pbr, texture_quality, texture_version, delight }
|
|
11
|
+
// image-to-3d POST /v3/generation/image-to-model { input: file_token, ... }
|
|
12
|
+
// multi-image-to-3d POST /v3/generation/multiview-to-model { inputs: [{ front }, { right }, { back }, { left }], ... }
|
|
13
|
+
// each -> { code: 0, data: { task_id } }
|
|
14
|
+
// poll GET /v3/tasks/{uuid} -> { code: 0, data: { status, progress, output: { model_url, rendered_image_url }, error_code, error_message } }
|
|
15
|
+
//
|
|
16
|
+
// Delighting (developers.tripo3d.ai, the H and P series text, image and
|
|
17
|
+
// multiview pages, read 2026-10-24): delight "removes baked-in lighting from
|
|
18
|
+
// the reference image before texturing", and only the v3.5-20260815 texture
|
|
19
|
+
// model reads it; the texture model a task gets when it names none
|
|
20
|
+
// (v3.0-20250812 for v3.x and the P series) ignores it. So every task names
|
|
21
|
+
// that texture model and asks for delight, as Meshy is asked for
|
|
22
|
+
// remove_lighting: a prop's colour must not carry the reference's shadows.
|
|
23
|
+
// The docs give no price for the texture model of its own, so the estimate
|
|
24
|
+
// stays the geometry's and the texture tier's; the first real job confirms
|
|
25
|
+
// the credits it bills.
|
|
26
|
+
//
|
|
27
|
+
// One task makes the finished textured GLB (there is no preview and refine
|
|
28
|
+
// step), so poll() never hands on to a next stage. Task ids are bare UUIDs.
|
|
29
|
+
// Pictures reach Tripo only as file tokens: a data URI in a task body is
|
|
30
|
+
// refused (400), so each picture is uploaded first. quad is never sent (its
|
|
31
|
+
// output is FBX, which jobs.mjs refuses), nor smart_low_poly, generate_parts,
|
|
32
|
+
// geometry_quality, compress or export_orientation: the P series rejects
|
|
33
|
+
// them even as false, and compress makes a GLB a planet cannot read.
|
|
34
|
+
//
|
|
35
|
+
// Output on paid credits belongs to the creator (API terms 5.2.2, last
|
|
36
|
+
// updated 2025-07-11: LicenseRef-Generated-Owned); a Free User's output stays
|
|
37
|
+
// Tripo's (5.2.1). The output's signed URL may live only minutes, so jobs.mjs
|
|
38
|
+
// asks the task again for a fresh one when the file host refuses it.
|
|
39
|
+
|
|
40
|
+
import { fetchJson } from '../net.mjs'
|
|
41
|
+
|
|
42
|
+
const API = 'https://openapi.tripo3d.ai/v3'
|
|
43
|
+
/** The only host a request with the key goes to: a redirect from the API to
|
|
44
|
+
* anywhere else (a CloudFront distribution anyone can own) is refused, so
|
|
45
|
+
* the key never follows it. tripo.hosts, with the file hosts, is for the
|
|
46
|
+
* downloads, which carry no key. */
|
|
47
|
+
const API_HOSTS = ['openapi.tripo3d.ai']
|
|
48
|
+
|
|
49
|
+
/** The model ids asset_generate may run on Tripo, each with its price family in prices.json. */
|
|
50
|
+
export const MODELS = { 'v3.1-20260211': 'h', 'v3.0-20250812': 'h', 'v2.5-20250123': 'h', 'P1-20260311': 'p1', 'P2-20260801': 'p2' }
|
|
51
|
+
const DEFAULT_MODEL = 'v3.1-20260211'
|
|
52
|
+
/** The texture tier every task asks for. price() bills its add-on, so the two stay in step. */
|
|
53
|
+
const TEXTURE_QUALITY = 'standard'
|
|
54
|
+
/** The texture model that reads delight (see the header). */
|
|
55
|
+
const DELIGHT_TEXTURE = 'v3.5-20260815'
|
|
56
|
+
/** The face_limit each family takes (Tripo's ranges; the H cap is v2.5's, the lowest of its versions). */
|
|
57
|
+
const FACE_LIMIT = { h: [48, 500000], p1: [48, 20000], p2: [48, 50000] }
|
|
58
|
+
/** Tripo's view keys in the engine's view order (images/views.mjs VIEW_WORDS:
|
|
59
|
+
* the front, its right side, the back, its left side). Whether Tripo's left
|
|
60
|
+
* and right are the object's sides or the camera's is not documented: confirm
|
|
61
|
+
* it on the first real multiview job. */
|
|
62
|
+
const VIEW_KEYS = ['front', 'right', 'back', 'left']
|
|
63
|
+
const ENDPOINT = { 'text-to-3d': 'text-to-model', 'image-to-3d': 'image-to-model', 'multi-image-to-3d': 'multiview-to-model' }
|
|
64
|
+
/** Tripo's task states as the job's; anything else (failed, cancelled,
|
|
65
|
+
* banned, expired, unknown) is final and failed. */
|
|
66
|
+
const STATUS = { queued: 'pending', running: 'running', success: 'succeeded' }
|
|
67
|
+
|
|
68
|
+
/** Tripo's envelope opened: its data, or a throw that says what Tripo said. */
|
|
69
|
+
function dataOf(r, what) {
|
|
70
|
+
if (r?.code !== 0 || !r.data) throw new Error(`Tripo ${what}: ${r?.message ?? `code ${r?.code ?? 'unknown'}`}`.slice(0, 300))
|
|
71
|
+
return r.data
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** One picture uploaded, for the file token a task body names it by. */
|
|
75
|
+
async function upload(dataUri, { key, fetch, ua }) {
|
|
76
|
+
const m = /^data:(image\/(png|jpeg));base64,(.+)$/s.exec(String(dataUri ?? ''))
|
|
77
|
+
if (!m) throw new Error('Tripo takes PNG or JPEG pictures, and this one is neither.')
|
|
78
|
+
const form = new FormData()
|
|
79
|
+
form.append('file', new Blob([Buffer.from(m[3], 'base64')], { type: m[1] }), m[2] === 'png' ? 'view.png' : 'view.jpg')
|
|
80
|
+
// No content-type here: fetch writes the multipart one with its boundary.
|
|
81
|
+
const r = await fetchJson(`${API}/files`, { fetch, hosts: API_HOSTS, ua, method: 'POST', headers: { authorization: `Bearer ${key}` }, body: form })
|
|
82
|
+
const token = dataOf(r, 'did not take the picture').file_token
|
|
83
|
+
if (typeof token !== 'string' || !token) throw new Error('Tripo did not give a file token.')
|
|
84
|
+
return token
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const tripo = {
|
|
88
|
+
id: 'tripo',
|
|
89
|
+
label: 'Tripo',
|
|
90
|
+
connector: 'tripo',
|
|
91
|
+
envVar: 'TRIPO_API_KEY',
|
|
92
|
+
// The API host, and the hosts the output's signed URLs use. The first real
|
|
93
|
+
// job (2026-10-22, an international key) was served from
|
|
94
|
+
// tripo-data.rg1.data.tripo3d.com, on .com where the API is on .ai; the docs'
|
|
95
|
+
// examples say cdn.tripo3d.ai. The key never goes to the file hosts: the
|
|
96
|
+
// keyed requests are held to API_HOSTS, and jobs.mjs downloads with no headers.
|
|
97
|
+
hosts: ['openapi.tripo3d.ai', 'cdn.tripo3d.ai', '*.tripo3d.ai', '*.data.tripo3d.com'],
|
|
98
|
+
tasks: ['text-to-3d', 'image-to-3d', 'multi-image-to-3d'],
|
|
99
|
+
|
|
100
|
+
defaultModel() {
|
|
101
|
+
return DEFAULT_MODEL
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/** The licence what this vendor makes carries: the creator's, on paid credits (API terms 5.2.2). */
|
|
105
|
+
licenseOf() {
|
|
106
|
+
return { spdx: 'LicenseRef-Generated-Owned', url: 'https://developers.tripo3d.ai/en/terms' }
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
/** Lower and upper price of one candidate, from prices.json: the family's
|
|
110
|
+
* credits for the task, plus the texture tier's add-on, at the credit's price. */
|
|
111
|
+
price(task, model, prices) {
|
|
112
|
+
const t = prices?.tripo
|
|
113
|
+
const base = t?.[MODELS[model]]?.[task]
|
|
114
|
+
const add = TEXTURE_QUALITY === 'standard' ? 0 : TEXTURE_QUALITY === 'detailed' ? t?.addons?.detailedTexture : t?.addons?.extremeTexture
|
|
115
|
+
const [low, high] = t?.creditUsd ?? []
|
|
116
|
+
if (![base, add, low, high].every(Number.isFinite)) return null
|
|
117
|
+
const credits = base + add
|
|
118
|
+
return { low: credits * low, high: credits * high, unit: `${credits} credits` }
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
/** Why a model cannot run, or null: only the models in MODELS have a price and a known request shape. */
|
|
122
|
+
refuse(_task, model) {
|
|
123
|
+
if (!MODELS[model]) return `Tripo model ${model} is not in the studio's table, so its price and what its request takes are unknown. Use one of: ${Object.keys(MODELS).join(', ')}.`
|
|
124
|
+
return null
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
/** Starts one candidate: the pictures uploaded first, then one task. */
|
|
128
|
+
async submit({ task, model, prompt, imageDataUri, imageDataUris, quality, key, fetch, ua }) {
|
|
129
|
+
const family = MODELS[model]
|
|
130
|
+
if (!family) throw new Error(tripo.refuse(task, model))
|
|
131
|
+
const [lo, hi] = FACE_LIMIT[family]
|
|
132
|
+
// The model always: Tripo's own migration guide says a task without one
|
|
133
|
+
// is refused. Nothing the P series rejects (see the header).
|
|
134
|
+
const shared = {
|
|
135
|
+
model,
|
|
136
|
+
texture: true,
|
|
137
|
+
pbr: quality?.pbr !== false,
|
|
138
|
+
texture_quality: TEXTURE_QUALITY,
|
|
139
|
+
texture_version: DELIGHT_TEXTURE,
|
|
140
|
+
delight: true,
|
|
141
|
+
...(Number.isFinite(quality?.polycount) ? { face_limit: Math.min(hi, Math.max(lo, Math.round(quality.polycount))) } : {}),
|
|
142
|
+
}
|
|
143
|
+
let body
|
|
144
|
+
if (task === 'text-to-3d') {
|
|
145
|
+
body = { prompt: String(prompt ?? '').slice(0, 1024), ...shared }
|
|
146
|
+
} else if (task === 'image-to-3d') {
|
|
147
|
+
// No prompt: Tripo's image task has no field for one.
|
|
148
|
+
body = { input: await upload(imageDataUri, { key, fetch, ua }), ...shared }
|
|
149
|
+
} else {
|
|
150
|
+
// Only the views given, each under Tripo's key for it; the first is the front, which Tripo requires.
|
|
151
|
+
const uris = (imageDataUris ?? []).slice(0, VIEW_KEYS.length)
|
|
152
|
+
if (uris.length < 2) throw new Error('Tripo multiview needs 2 to 4 views, the front first.')
|
|
153
|
+
const inputs = []
|
|
154
|
+
for (let k = 0; k < uris.length; k++) inputs.push({ [VIEW_KEYS[k]]: await upload(uris[k], { key, fetch, ua }) })
|
|
155
|
+
body = { inputs, ...shared }
|
|
156
|
+
}
|
|
157
|
+
const r = await fetchJson(`${API}/generation/${ENDPOINT[task]}`, {
|
|
158
|
+
fetch,
|
|
159
|
+
hosts: API_HOSTS,
|
|
160
|
+
ua,
|
|
161
|
+
method: 'POST',
|
|
162
|
+
headers: { authorization: `Bearer ${key}`, 'content-type': 'application/json' },
|
|
163
|
+
body: JSON.stringify(body),
|
|
164
|
+
})
|
|
165
|
+
const id = dataOf(r, 'did not take the job').task_id
|
|
166
|
+
if (typeof id !== 'string' || !id) throw new Error('Tripo did not give a task id.')
|
|
167
|
+
// A bare UUID, as it came: the server refuses a task_ prefix.
|
|
168
|
+
return { vendorId: id, stage: 'generate' }
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
/** Where a candidate is now. A non-zero code throws, so pollJob only notes it. */
|
|
172
|
+
async poll(candidate, { key, fetch, ua }) {
|
|
173
|
+
const r = await fetchJson(`${API}/tasks/${encodeURIComponent(candidate.vendorId)}`, { fetch, hosts: API_HOSTS, ua, headers: { authorization: `Bearer ${key}` } })
|
|
174
|
+
const d = dataOf(r, 'could not say where the task is')
|
|
175
|
+
const status = STATUS[String(d.status ?? '').toLowerCase()] ?? 'failed'
|
|
176
|
+
if (status === 'failed') {
|
|
177
|
+
// Tripo's words, with its code (2008 is the content policy, 2018 a model too complex or expired).
|
|
178
|
+
const why = String(d.error_message || d.status || 'Tripo reported a failure')
|
|
179
|
+
return { status, error: `${why}${d.error_code ? ` (code ${d.error_code})` : ''}`.slice(0, 300) }
|
|
180
|
+
}
|
|
181
|
+
if (status !== 'succeeded') return { status, progress: Math.max(0, Math.min(100, Math.round(Number(d.progress) || 0))) }
|
|
182
|
+
// v3 gives one model_url; the V2 names are read in case they come back.
|
|
183
|
+
const out = d.output ?? {}
|
|
184
|
+
const glb = out.model_url ?? out.pbr_model ?? out.model ?? out.base_model
|
|
185
|
+
if (!glb) return { status: 'failed', error: 'Tripo finished but gave no model URL.' }
|
|
186
|
+
return { status: 'succeeded', progress: 100, glbUrl: String(glb), thumbnailUrl: out.rendered_image_url ?? out.rendered_image ?? null }
|
|
187
|
+
},
|
|
188
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// ambientCG: CC0 PBR materials (and HDRIs), one zip per resolution. Verified
|
|
2
|
+
// by hand on 2026-10-20: API v2's full_json answers with the download list;
|
|
3
|
+
// v3 answers too but lists ids only without its include options, so v2 is the
|
|
4
|
+
// one used. Everything that knows the API's shape is in this file. Trimmed
|
|
5
|
+
// answers are the fixtures in scripts/fixtures/assets/ambientcg-*.json.
|
|
6
|
+
//
|
|
7
|
+
// GET https://ambientcg.com/api/v2/full_json?type=Material&q=...&limit=...&include=downloadData,previewData,tagData
|
|
8
|
+
// GET https://ambientcg.com/api/v2/full_json?id=<assetId>&include=downloadData
|
|
9
|
+
//
|
|
10
|
+
// A download link (ambientcg.com/get?file=...) redirects to
|
|
11
|
+
// acg-download.struffelproductions.com; thumbnails are on
|
|
12
|
+
// acg-media.struffelproductions.com, from 64 to 2048 px. No md5 is given,
|
|
13
|
+
// only sizes. The 1K, 2K and 4K JPG sets are taken (a 4K set runs to about
|
|
14
|
+
// 130 MB, inside the 300 MB a material may download); the 8K PNG set of one
|
|
15
|
+
// material passes 1 GB. dimensionX and dimensionY are one tile's real size
|
|
16
|
+
// in centimetres, and maps lists what the set holds.
|
|
17
|
+
//
|
|
18
|
+
// An HDRI's zips are named by resolution alone ('1K', '2K', not '1K-HDR'),
|
|
19
|
+
// checked on 2026-10-24 on HDRIs from 2021 and 2026; each holds the sky as
|
|
20
|
+
// an EXR (<id>_1K_HDR.exr) beside a tonemapped JPG, a .blend and a .usdc.
|
|
21
|
+
// No .hdr is offered, so an ambientCG sky comes in as .exr.
|
|
22
|
+
|
|
23
|
+
import { fetchJson } from '../net.mjs'
|
|
24
|
+
|
|
25
|
+
const API = 'https://ambientcg.com/api/v2/full_json'
|
|
26
|
+
const TYPES = { material: 'Material', hdri: 'HDRI' }
|
|
27
|
+
const CC0 = { spdx: 'CC0-1.0', url: 'https://creativecommons.org/publicdomain/zero/1.0/' }
|
|
28
|
+
const RESOLUTIONS = { '1k': '1K', '2k': '2K', '4k': '4K' }
|
|
29
|
+
|
|
30
|
+
/** Every zip an asset offers, as { attribute, url, bytes, name }. */
|
|
31
|
+
function downloadsOf(asset) {
|
|
32
|
+
const out = []
|
|
33
|
+
for (const folder of Object.values(asset?.downloadFolders ?? {})) {
|
|
34
|
+
for (const category of Object.values(folder?.downloadFiletypeCategories ?? {})) {
|
|
35
|
+
for (const d of category?.downloads ?? []) {
|
|
36
|
+
if (d?.downloadLink && d?.attribute) out.push({ attribute: String(d.attribute), url: d.fullDownloadPath ?? d.downloadLink, bytes: d.size, name: d.fileName })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The zip for a resolution: '1K-JPG' for a material; for an HDRI '1K'
|
|
44
|
+
* (the name ambientCG gives it), or '1K-HDR' and '1K-EXR' should it ever
|
|
45
|
+
* name them so. */
|
|
46
|
+
function zipFor(asset, kind, resolution, format) {
|
|
47
|
+
const res = RESOLUTIONS[resolution ?? '1k']
|
|
48
|
+
if (!res) return null
|
|
49
|
+
const want = kind === 'hdri' ? [...(format === 'hdr' || format === 'exr' ? [`${res}-${format.toUpperCase()}`] : []), `${res}-EXR`, `${res}-HDR`, res] : [`${res}-${format === 'png' ? 'PNG' : 'JPG'}`]
|
|
50
|
+
const all = downloadsOf(asset)
|
|
51
|
+
for (const w of want) {
|
|
52
|
+
const hit = all.find((d) => d.attribute.toUpperCase() === w)
|
|
53
|
+
if (hit) return hit
|
|
54
|
+
}
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** ambientCG's map names as the roles asset_import gives files. */
|
|
59
|
+
const ROLE_NAMES = { color: 'color', normal: 'normal', roughness: 'rough', 'ambient-occlusion': 'ao', metalness: 'metal', displacement: 'displacement', opacity: 'opacity' }
|
|
60
|
+
const mapRoles = (maps) => (Array.isArray(maps) ? maps.map((m) => ROLE_NAMES[m] ?? m) : [])
|
|
61
|
+
|
|
62
|
+
/** One tile's real size in metres, when the asset gives it (centimetres). */
|
|
63
|
+
const coversOf = (a) => (Number(a?.dimensionX) > 0 && Number(a?.dimensionY) > 0 ? [Number(a.dimensionX) / 100, Number(a.dimensionY) / 100] : null)
|
|
64
|
+
|
|
65
|
+
export const ambientcg = {
|
|
66
|
+
id: 'ambientcg',
|
|
67
|
+
label: 'ambientCG',
|
|
68
|
+
kinds: ['material', 'hdri'],
|
|
69
|
+
needsKey: null,
|
|
70
|
+
hosts: ['ambientcg.com', 'acg-download.struffelproductions.com', 'acg-media.struffelproductions.com'],
|
|
71
|
+
|
|
72
|
+
async search(query, opts) {
|
|
73
|
+
const type = TYPES[opts.kind]
|
|
74
|
+
const url = `${API}?type=${type}&q=${encodeURIComponent(query)}&limit=${Math.min(24, opts.limit ?? 8)}&include=downloadData,previewData,tagData`
|
|
75
|
+
const data = await fetchJson(url, { fetch: opts.fetch, hosts: ambientcg.hosts, ua: opts.ua })
|
|
76
|
+
const found = Array.isArray(data?.foundAssets) ? data.foundAssets : []
|
|
77
|
+
return found.slice(0, opts.limit ?? 8).map((a) => ({
|
|
78
|
+
provider: 'ambientcg',
|
|
79
|
+
id: a.assetId,
|
|
80
|
+
title: a.displayName || a.assetId,
|
|
81
|
+
kind: opts.kind,
|
|
82
|
+
author: 'ambientCG',
|
|
83
|
+
license: ambientcg.licenseOf(a),
|
|
84
|
+
url: a.shortLink || `https://ambientcg.com/view?id=${encodeURIComponent(a.assetId)}`,
|
|
85
|
+
thumbnail: a.previewImage?.['128-PNG'] ?? a.previewImage?.['256-PNG'] ?? null,
|
|
86
|
+
preview: a.previewImage?.['512-PNG'] ?? a.previewImage?.['256-PNG'] ?? null,
|
|
87
|
+
tags: (a.tags ?? []).slice(0, 8),
|
|
88
|
+
maps: mapRoles(a.maps),
|
|
89
|
+
...(coversOf(a) ? { covers_m: coversOf(a) } : {}),
|
|
90
|
+
sizes: Object.keys(RESOLUTIONS)
|
|
91
|
+
.map((r) => {
|
|
92
|
+
const z = zipFor(a, opts.kind, r)
|
|
93
|
+
return z ? { label: opts.kind === 'hdri' && z.attribute === RESOLUTIONS[r] ? `exr ${r} zip` : `${z.attribute} zip`, res: r, bytes: z.bytes } : null
|
|
94
|
+
})
|
|
95
|
+
.filter(Boolean),
|
|
96
|
+
}))
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
async files(id, { resolution, format, fetch, ua }) {
|
|
100
|
+
if (resolution && !RESOLUTIONS[resolution]) {
|
|
101
|
+
return { error: `ambientCG sets are taken at 1k, 2k or 4k (its 8K zips run to hundreds of megabytes); ask for "1k", "2k" or "4k".` }
|
|
102
|
+
}
|
|
103
|
+
const data = await fetchJson(`${API}?id=${encodeURIComponent(id)}&include=downloadData,tagData`, { fetch, hosts: ambientcg.hosts, ua })
|
|
104
|
+
const asset = (data?.foundAssets ?? []).find((a) => a.assetId === id)
|
|
105
|
+
if (!asset) return { error: `ambientCG has no asset called "${id}". Search with asset_search first and use an id it lists.` }
|
|
106
|
+
const kind = /hdri/i.test(asset.dataType ?? '') ? 'hdri' : 'material'
|
|
107
|
+
const zip = zipFor(asset, kind, resolution, format)
|
|
108
|
+
if (!zip) return { error: `ambientCG has no ${(resolution ?? '1k').toUpperCase()} ${kind === 'hdri' ? 'HDRI' : 'JPG'} download for ${id}.` }
|
|
109
|
+
// Its zip holds an EXR whatever was asked: said here, not found at import.
|
|
110
|
+
const note = kind === 'hdri' && format === 'hdr' && !/HDR$/i.test(zip.attribute) ? 'ambientCG offers its skies as EXR only, so this is the EXR.' : null
|
|
111
|
+
return {
|
|
112
|
+
kind,
|
|
113
|
+
title: asset.displayName || id,
|
|
114
|
+
author: 'ambientCG',
|
|
115
|
+
url: asset.shortLink || `https://ambientcg.com/view?id=${encodeURIComponent(id)}`,
|
|
116
|
+
license: ambientcg.licenseOf(asset),
|
|
117
|
+
format: 'zip',
|
|
118
|
+
// The size it lists is a guide, not a checksum: it is used for the cap
|
|
119
|
+
// before the download, not to refuse one that differs by a few bytes.
|
|
120
|
+
files: [{ path: zip.name || `${id}_${zip.attribute}.zip`, url: zip.url, bytes: zip.bytes, sizeExact: false }],
|
|
121
|
+
meta: { maps: mapRoles(asset.maps), resolution: resolution ?? '1k', ...(kind === 'hdri' ? { format: /HDR$/i.test(zip.attribute) ? 'hdr' : 'exr' } : {}), ...(coversOf(asset) ? { dimensions_cm: [Number(asset.dimensionX), Number(asset.dimensionY)] } : {}) },
|
|
122
|
+
...(note ? { note } : {}),
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
licenseOf(_item) {
|
|
127
|
+
return { ...CC0 }
|
|
128
|
+
},
|
|
129
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The asset libraries asset_search and asset_fetch can reach, and the ones
|
|
2
|
+
// they cannot, with the reason, so the agent can tell the creator why a site
|
|
3
|
+
// it knows is not on the list.
|
|
4
|
+
|
|
5
|
+
import { ambientcg } from './ambientcg.mjs'
|
|
6
|
+
import { polyhaven } from './polyhaven.mjs'
|
|
7
|
+
import { polypizza } from './polypizza.mjs'
|
|
8
|
+
|
|
9
|
+
export const SOURCES = { polyhaven, ambientcg, polypizza }
|
|
10
|
+
export const SOURCE_IDS = Object.keys(SOURCES)
|
|
11
|
+
|
|
12
|
+
/** Every host a source may reach, for the guard and the fetch allowlist. */
|
|
13
|
+
export const SOURCE_HOSTS = [...new Set(Object.values(SOURCES).flatMap((s) => s.hosts))]
|
|
14
|
+
|
|
15
|
+
/** Libraries that are not adapters, and why. */
|
|
16
|
+
export const NOT_SOURCES = [
|
|
17
|
+
{ name: 'Freesound', why: 'its API is for non-commercial use only, and a planet is published to everyone' },
|
|
18
|
+
{ name: 'Sketchfab', why: "downloads need the creator's own Sketchfab sign-in, which the studio does not do yet; the creator can download a model themselves and you bring it in with asset_import" },
|
|
19
|
+
{ name: 'ArtStation, Pinterest and Behance', why: 'their terms forbid feeding their images to AI tools; the studio never fetches from them' },
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
/** The sources that serve a kind, in the order they are asked. */
|
|
23
|
+
export const sourcesFor = (kind) => Object.values(SOURCES).filter((s) => s.kinds.includes(kind))
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Poly Haven: CC0 models, textures and HDRIs, through its public API only
|
|
2
|
+
// (never the website), with a User-Agent that names the studio, as its API
|
|
3
|
+
// asks. Verified by hand on 2026-10-20 against api.polyhaven.com; trimmed
|
|
4
|
+
// answers are the fixtures in scripts/fixtures/assets/polyhaven-*.json.
|
|
5
|
+
//
|
|
6
|
+
// GET https://api.polyhaven.com/assets?t=models|textures|hdris every asset of a type, by id
|
|
7
|
+
// GET https://api.polyhaven.com/files/{id} every file, by map, resolution, format
|
|
8
|
+
//
|
|
9
|
+
// Files live on dl.polyhaven.org with a size and an md5 each; a glTF model
|
|
10
|
+
// lists the files it needs beside it (its .bin and textures) in `include`.
|
|
11
|
+
// Thumbnails come from cdn.polyhaven.com, as PNG when asked without a format,
|
|
12
|
+
// at any width asked for (128 for the list, 512 for a closer look).
|
|
13
|
+
//
|
|
14
|
+
// A card says what the item is made of before anything is downloaded: its
|
|
15
|
+
// maps, the 1k, 2k and 4k downloads with their sizes, the real size of a
|
|
16
|
+
// model or of one tile of a texture, and a model's texel density at each
|
|
17
|
+
// resolution (the listing gives it at the largest).
|
|
18
|
+
|
|
19
|
+
import { fetchJson } from '../net.mjs'
|
|
20
|
+
|
|
21
|
+
const API = 'https://api.polyhaven.com'
|
|
22
|
+
const TYPES = { model: 'models', material: 'textures', hdri: 'hdris' }
|
|
23
|
+
const CC0 = { spdx: 'CC0-1.0', url: 'https://creativecommons.org/publicdomain/zero/1.0/' }
|
|
24
|
+
/** The maps a material is made of, in the names Poly Haven gives them. GL
|
|
25
|
+
* normals, because three reads normal maps the OpenGL way. Metal and
|
|
26
|
+
* displacement come when the texture has them; arm packs ao, rough and
|
|
27
|
+
* metal into one picture the separate maps already are. */
|
|
28
|
+
const MAPS = [
|
|
29
|
+
['Diffuse', 'color'],
|
|
30
|
+
['nor_gl', 'normal'],
|
|
31
|
+
['Rough', 'rough'],
|
|
32
|
+
['AO', 'ao'],
|
|
33
|
+
['Metal', 'metal'],
|
|
34
|
+
['Displacement', 'displacement'],
|
|
35
|
+
]
|
|
36
|
+
/** The resolutions a card lists and asset_fetch takes. */
|
|
37
|
+
export const RESOLUTIONS = ['1k', '2k', '4k']
|
|
38
|
+
const RES_PX = { '1k': 1024, '2k': 2048, '4k': 4096, '8k': 8192 }
|
|
39
|
+
|
|
40
|
+
/** The maps a files listing has, as roles. A model's are the textures its
|
|
41
|
+
* glTF names (diff, nor_gl, arm: ambient occlusion, roughness, metalness). */
|
|
42
|
+
export function mapsOf(kind, files) {
|
|
43
|
+
if (kind === 'material') return MAPS.filter(([name]) => files?.[name]).map(([, role]) => role)
|
|
44
|
+
if (kind !== 'model') return []
|
|
45
|
+
const block = Object.values(files?.gltf ?? {})[0]?.gltf
|
|
46
|
+
const names = Object.keys(block?.include ?? {}).map((n) => n.toLowerCase())
|
|
47
|
+
const roles = []
|
|
48
|
+
if (names.some((n) => /_diff/.test(n))) roles.push('color')
|
|
49
|
+
if (names.some((n) => /_nor_gl|_nor/.test(n))) roles.push('normal')
|
|
50
|
+
if (names.some((n) => /_arm/.test(n))) roles.push('ao+rough+metal')
|
|
51
|
+
else {
|
|
52
|
+
if (names.some((n) => /_rough/.test(n))) roles.push('rough')
|
|
53
|
+
if (names.some((n) => /_ao/.test(n))) roles.push('ao')
|
|
54
|
+
if (names.some((n) => /_metal/.test(n))) roles.push('metal')
|
|
55
|
+
}
|
|
56
|
+
return roles
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A model's texel density at a resolution: the listing's, given at its
|
|
60
|
+
* largest, scaled to that resolution's side. */
|
|
61
|
+
export function densityAt(a, res) {
|
|
62
|
+
const max = Array.isArray(a?.max_resolution) ? Math.max(...a.max_resolution) : null
|
|
63
|
+
if (!a?.texel_density || !max || !RES_PX[res]) return null
|
|
64
|
+
return Math.round((a.texel_density * Math.min(RES_PX[res], max)) / max)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The asset lists are large (half a megabyte for the models) and change a
|
|
68
|
+
// few times a week, so one copy per process is kept for ten minutes.
|
|
69
|
+
const LIST_MS = 10 * 60_000
|
|
70
|
+
const lists = new Map()
|
|
71
|
+
|
|
72
|
+
async function listOf(kind, opts) {
|
|
73
|
+
const type = TYPES[kind]
|
|
74
|
+
const hit = lists.get(type)
|
|
75
|
+
if (hit && Date.now() - hit.at < LIST_MS && hit.fetch === opts.fetch) return hit.data
|
|
76
|
+
const data = await fetchJson(`${API}/assets?t=${type}`, { fetch: opts.fetch, hosts: polyhaven.hosts, ua: opts.ua })
|
|
77
|
+
if (!data || typeof data !== 'object') throw new Error('Poly Haven answered with something that is not an asset list.')
|
|
78
|
+
lists.set(type, { at: Date.now(), data, fetch: opts.fetch })
|
|
79
|
+
return data
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const words = (s) => String(s ?? '').toLowerCase().split(/[^a-z0-9]+/).filter(Boolean)
|
|
83
|
+
/** A word without its plural or adjective ending, so "mossy rocks" finds "moss rock". */
|
|
84
|
+
const stem = (w) => {
|
|
85
|
+
let s = w.length > 3 && w.endsWith('s') && !w.endsWith('ss') ? w.slice(0, -1) : w
|
|
86
|
+
if (s.length > 3 && /[ye]$/.test(s)) s = s.slice(0, -1)
|
|
87
|
+
return s
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** How well an asset matches the words asked for: every word must appear in
|
|
91
|
+
* its id, name, tags or categories (or share their stem); more downloads
|
|
92
|
+
* rank higher among equals. */
|
|
93
|
+
function score(id, a, query) {
|
|
94
|
+
const hay = new Set([...words(id), ...words(a.name), ...(a.tags ?? []).flatMap(words), ...(a.categories ?? []).flatMap(words)])
|
|
95
|
+
const stems = new Set([...hay].map(stem))
|
|
96
|
+
const q = words(query)
|
|
97
|
+
if (q.length === 0) return 0
|
|
98
|
+
let hits = 0
|
|
99
|
+
for (const w of q) {
|
|
100
|
+
if (hay.has(w)) hits += 2
|
|
101
|
+
else if (stems.has(stem(w))) hits += 2
|
|
102
|
+
else if ([...hay].some((h) => h.startsWith(w) || (w.length > 3 && h.includes(w)))) hits += 1
|
|
103
|
+
else return 0
|
|
104
|
+
}
|
|
105
|
+
return hits
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const authorsOf = (a) => Object.keys(a?.authors ?? {}).join(', ') || 'Poly Haven'
|
|
109
|
+
|
|
110
|
+
/** The files of one item, for a resolution and format, as
|
|
111
|
+
* [{ path, url, bytes, md5, role? }] (paths relative to the item's folder). */
|
|
112
|
+
function pick(kind, files, { resolution = '1k', format } = {}) {
|
|
113
|
+
const res = resolution
|
|
114
|
+
if (kind === 'model') {
|
|
115
|
+
const fmt = format ?? 'gltf'
|
|
116
|
+
const block = files?.[fmt]?.[res]?.[fmt]
|
|
117
|
+
if (!block?.url) return null
|
|
118
|
+
const main = { path: block.url.split('/').pop(), url: block.url, bytes: block.size, md5: block.md5 }
|
|
119
|
+
const include = Object.entries(block.include ?? {}).map(([rel, f]) => ({ path: rel, url: f.url, bytes: f.size, md5: f.md5 }))
|
|
120
|
+
return { files: [main, ...include], format: fmt }
|
|
121
|
+
}
|
|
122
|
+
if (kind === 'hdri') {
|
|
123
|
+
const fmt = format ?? 'hdr'
|
|
124
|
+
const f = files?.hdri?.[res]?.[fmt]
|
|
125
|
+
if (!f?.url) return null
|
|
126
|
+
return { files: [{ path: f.url.split('/').pop(), url: f.url, bytes: f.size, md5: f.md5 }], format: fmt }
|
|
127
|
+
}
|
|
128
|
+
const fmt = format === 'png' ? 'png' : 'jpg'
|
|
129
|
+
const out = []
|
|
130
|
+
for (const [map, role] of MAPS) {
|
|
131
|
+
const f = files?.[map]?.[res]?.[fmt]
|
|
132
|
+
if (f?.url) out.push({ path: f.url.split('/').pop(), url: f.url, bytes: f.size, md5: f.md5, role })
|
|
133
|
+
}
|
|
134
|
+
return out.length ? { files: out, format: fmt } : null
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const total = (files) => files.reduce((n, f) => n + (Number(f.bytes) || 0), 0)
|
|
138
|
+
|
|
139
|
+
export const polyhaven = {
|
|
140
|
+
id: 'polyhaven',
|
|
141
|
+
label: 'Poly Haven',
|
|
142
|
+
kinds: ['model', 'material', 'hdri'],
|
|
143
|
+
needsKey: null,
|
|
144
|
+
hosts: ['api.polyhaven.com', 'dl.polyhaven.org', 'cdn.polyhaven.com'],
|
|
145
|
+
|
|
146
|
+
/** Cards for the best matches, each with the sizes of its 1k and 2k files. */
|
|
147
|
+
async search(query, opts) {
|
|
148
|
+
const list = await listOf(opts.kind, opts)
|
|
149
|
+
const ranked = Object.entries(list)
|
|
150
|
+
.map(([id, a]) => ({ id, a, s: score(id, a, query) }))
|
|
151
|
+
.filter((r) => r.s > 0)
|
|
152
|
+
.sort((x, y) => y.s - x.s || (y.a.download_count ?? 0) - (x.a.download_count ?? 0))
|
|
153
|
+
.slice(0, opts.limit ?? 8)
|
|
154
|
+
const cards = []
|
|
155
|
+
for (const [i, { id, a }] of ranked.entries()) {
|
|
156
|
+
const card = {
|
|
157
|
+
provider: 'polyhaven',
|
|
158
|
+
id,
|
|
159
|
+
title: a.name ?? id,
|
|
160
|
+
kind: opts.kind,
|
|
161
|
+
author: authorsOf(a),
|
|
162
|
+
license: polyhaven.licenseOf(a),
|
|
163
|
+
url: `https://polyhaven.com/a/${id}`,
|
|
164
|
+
thumbnail: `https://cdn.polyhaven.com/asset_img/thumbs/${encodeURIComponent(id)}.png?width=128&height=128`,
|
|
165
|
+
preview: `https://cdn.polyhaven.com/asset_img/thumbs/${encodeURIComponent(id)}.png?width=512&height=512`,
|
|
166
|
+
tags: (a.tags ?? []).slice(0, 8),
|
|
167
|
+
sizes: [],
|
|
168
|
+
maps: [],
|
|
169
|
+
}
|
|
170
|
+
// Poly Haven states models in millimetres, width x depth x height.
|
|
171
|
+
if (opts.kind === 'model' && Array.isArray(a.dimensions) && a.dimensions.length === 3) card.dims_m = a.dimensions.map((d) => Math.round(d) / 1000)
|
|
172
|
+
if (opts.kind === 'material' && Array.isArray(a.dimensions) && a.dimensions.length >= 2) card.covers_m = a.dimensions.slice(0, 2).map((d) => Math.round(d) / 1000)
|
|
173
|
+
if (opts.kind === 'model' && a.polycount) card.triangles = a.polycount
|
|
174
|
+
if (opts.kind === 'model' && a.texel_density) card.density = Object.fromEntries(RESOLUTIONS.map((r) => [r, densityAt(a, r)]).filter(([, d]) => d))
|
|
175
|
+
// The sizes take one more request per card, so only the first eight get them.
|
|
176
|
+
if (i < 8) {
|
|
177
|
+
try {
|
|
178
|
+
const files = await fetchJson(`${API}/files/${encodeURIComponent(id)}`, { fetch: opts.fetch, hosts: polyhaven.hosts, ua: opts.ua })
|
|
179
|
+
for (const res of RESOLUTIONS) {
|
|
180
|
+
const p = pick(opts.kind, files, { resolution: res })
|
|
181
|
+
// largest: the heaviest single file, which asset_import weighs
|
|
182
|
+
// a material's map or a sky by (the studio's browser says so first).
|
|
183
|
+
if (p) card.sizes.push({ label: `${p.format === 'gltf' ? 'glTF' : p.format} ${res}`, res, bytes: total(p.files), largest: Math.max(0, ...p.files.map((f) => Number(f.bytes) || 0)) })
|
|
184
|
+
}
|
|
185
|
+
card.maps = mapsOf(opts.kind, files)
|
|
186
|
+
} catch {
|
|
187
|
+
// a card without sizes is still a card
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
cards.push(card)
|
|
191
|
+
}
|
|
192
|
+
return cards
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
/** What to download for an item: its files, the card for its source record,
|
|
196
|
+
* and its measurements. Ids are unique across Poly Haven's three types,
|
|
197
|
+
* and /info says which one an id is (type 0 HDRI, 1 texture, 2 model). */
|
|
198
|
+
async files(id, { resolution, format, fetch, ua }) {
|
|
199
|
+
let a
|
|
200
|
+
try {
|
|
201
|
+
a = await fetchJson(`${API}/info/${encodeURIComponent(id)}`, { fetch, hosts: polyhaven.hosts, ua })
|
|
202
|
+
} catch (err) {
|
|
203
|
+
if (err?.code === 'status') return { error: `Poly Haven has no asset called "${id}". Search with asset_search first and use an id it lists.` }
|
|
204
|
+
throw err
|
|
205
|
+
}
|
|
206
|
+
const kind = { 0: 'hdri', 1: 'material', 2: 'model' }[a?.type]
|
|
207
|
+
if (!a || !kind) return { error: `Poly Haven has no asset called "${id}". Search with asset_search first and use an id it lists.` }
|
|
208
|
+
const files = await fetchJson(`${API}/files/${encodeURIComponent(id)}`, { fetch, hosts: polyhaven.hosts, ua })
|
|
209
|
+
const chosen = pick(kind, files, { resolution, format })
|
|
210
|
+
if (!chosen) {
|
|
211
|
+
const have = kind === 'model' ? Object.keys(files ?? {}).filter((k) => ['gltf', 'fbx', 'blend', 'usd'].includes(k)) : kind === 'hdri' ? Object.keys(files?.hdri ?? {}) : ['jpg', 'png']
|
|
212
|
+
return { error: `Poly Haven has no ${format ?? 'default'} ${resolution ?? '1k'} files for ${id}. It has: ${have.join(', ') || 'nothing usable'}.` }
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
kind,
|
|
216
|
+
title: a.name ?? id,
|
|
217
|
+
author: authorsOf(a),
|
|
218
|
+
url: `https://polyhaven.com/a/${id}`,
|
|
219
|
+
license: polyhaven.licenseOf(a),
|
|
220
|
+
format: chosen.format,
|
|
221
|
+
files: chosen.files,
|
|
222
|
+
meta: {
|
|
223
|
+
...(Array.isArray(a.dimensions) ? { dimensions_mm: a.dimensions } : {}),
|
|
224
|
+
...(a.polycount ? { polycount: a.polycount } : {}),
|
|
225
|
+
// Poly Haven models are modelled in metres, so their size is kept.
|
|
226
|
+
...(kind === 'model' ? { units: 'm' } : {}),
|
|
227
|
+
resolution: resolution ?? '1k',
|
|
228
|
+
maps: mapsOf(kind, files),
|
|
229
|
+
...(kind === 'model' && densityAt(a, resolution ?? '1k') ? { texel_density: densityAt(a, resolution ?? '1k') } : {}),
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
licenseOf(_item) {
|
|
235
|
+
return { ...CC0 }
|
|
236
|
+
},
|
|
237
|
+
}
|