--!strict -- model/pmx: MikuMikuDance models, PMX 2.0 / 2.1 (PmxEditor's specification notes) and PMD 1.0 (the original MMD -- format, as documented by the MMD community). New code, no third-party code. -- Reads: UTF-16 / UTF-8 text (PMD: Shift-JIS names kept when ASCII, else the English names), vertices with BDEF1 / -- BDEF2 / BDEF4 / SDEF / QDEF weights -> JOINTS_0 / WEIGHTS_0 (normalized; SDEF C / R0 / R1 as _SDEF_C / _SDEF_R0 / -- _SDEF_R1, the deform type as _DEFORM, additional UVs as _UVA1.._UVA4 vec4, edge scale as _EDGE_SCALE), faces (one -- primitive per material), textures (uri, data through opts.files / opts.resolve), materials (diffuse -> baseColor, -- specular power -> roughness, no-cull -> doubleSided; diffuse / specular / ambient, edge, sphere map and mode, toon -- texture or shared toon index, memo in material.extras.pmx), bones (nodes with parent-relative translations, one skin; -- tail, inherit, axes, IK in node.extras.pmx), vertex morphs -> morph targets (POSITION deltas, mesh.extras.targetNames), -- the other morphs (group, bone, UV, material, flip, impulse), display frames, rigid bodies and joints as extras -- (mesh.extras.pmx.morphs, meta.source). MMD's left-handed coordinates become right-handed: z is negated and the -- winding reversed (extras keep the file's values and 0-based indices). meta.up = "y" (one unit is about 8 cm). -- Not supported: writing, PMX 2.1 soft bodies (counted in meta.source.softBodies), VMD motions / VPD poses. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Types = require("@formats/Types") local Read = require("@formats/model/pmx/Read") local Pmd = require("@formats/model/pmx/Pmd") local Build = require("@formats/model/pmx/Build") -- ------------------------------------------------------------------------------------------------ contract local Pmx = { name = "pmx", category = "model", extensions = { "pmx", "pmd" }, mime = { "model/x-pmx", "model/x-pmd" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Text" }, caps = { decode = true, encode = false, materials = true, skins = true, morphTargets = true, pmd = true }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { files: { [string]: any }?, -- texture files by name (buffer or string; "/" or "\" separators, or the base name) resolve: ((uri: string) -> any)?, -- or: reads a texture file (the api sets it for paths) morphs: boolean?, -- vertex morphs as morph targets (default true) maxVertices: number?, -- default 2^24 maxBytes: number?, -- morph target bytes (default 2^28) } -- ------------------------------------------------------------------------------------------------ sniff function Pmx.sniff(r: Bytes.Reader): number local head = r:peek(8) if string.sub(head, 1, 4) == "PMX " then return 1 end if string.sub(head, 1, 7) == "Pmd\0\0\128\63" then return 1 end return 0 end -- ------------------------------------------------------------------------------------------------ decode function Pmx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?) local pmd = r:peek(3) == "Pmd" local m: any, msg: string?, kind: string? if pmd then m, msg, kind = Pmd.parse(r, opts) else m, msg, kind = Read.parse(r, opts) end if not m then return nil, Err.from(msg, r.pos - r.base, kind) end local scene, berr, bkind = Build.scene(m, opts) if not scene then return nil, Err.from(berr, nil, bkind) end if pmd and scene.meta.source then scene.meta.source.pmd = true end return scene, nil end return Pmx