;============================================================ ; iron_vrm.hsp — VRM 1.0 メタデータローダ (最小実装) ; ; VRM は glTF 2.0 の拡張 (extensions.VRMC_vrm)。本モジュールは ; iron_gltf でメッシュ読込、加えて VRM のメタ情報 (キャラクター名 / ; 作者 / ライセンス / サムネイル index / version) を JSON から抽出。 ; ; 注意: ; .vrm ファイルの実体は .glb (glTF Binary)。本モジュールは ; .glb / .vrm / .gltf(JSON) のいずれも受け取り、内部で JSON chunk を ; 自動抽出してメタ情報を返す。メッシュ本体のロードは iron_gltf.hsp で。 ; ; API: ; vrm_load_meta "model.vrm" (.vrm/.glb/.gltf いずれも可), ; var_name, var_author, var_license, var_version ; → stat 0=OK ; ; vrm_describe "path", var_text ; JSON から抽出した全 VRM メタを "key=val" の改行区切りで返す ; ; 依存: iron_json.hsp ;============================================================ #ifndef __iron_vrm_hsp__ #define __iron_vrm_hsp__ #include "iron_json.hsp" #module iron_vrm ;--------------------------------------------------------- ; internal: .vrm/.glb/.gltf ファイルから JSON 本体を var_out に書き出す ;--------------------------------------------------------- #deffunc _vrm_extract_json str path, var v_out, \ local _size, local _fb, local _magic, local _total_len, \ local _cp, local _clen, local _ctype sdim v_out, 1 exist path _size = strsize if _size <= 0 : return -1 sdim _fb, _size + 16 bload path, _fb, _size if _size < 12 { sdim v_out, _size + 16 memcpy v_out, _fb, _size return _size } _magic = lpeek(_fb, 0) if _magic != 0x46546C67 { ; 非 GLB (plain .gltf JSON) sdim v_out, _size + 16 memcpy v_out, _fb, _size return _size } ; GLB: JSON chunk を取り出す _total_len = lpeek(_fb, 8) if _total_len > _size : _total_len = _size _cp = 12 repeat if _cp + 8 > _total_len : return -2 _clen = lpeek(_fb, _cp) _ctype = lpeek(_fb, _cp + 4) _cp = _cp + 8 if _ctype = 0x4E4F534A { ; "JSON" sdim v_out, _clen + 16 memcpy v_out, _fb, _clen, 0, _cp poke v_out, _clen, 0 return _clen } _cp = _cp + _clen if _cp >= _total_len : break loop return -3 #deffunc vrm_load_meta str path, var v_name, var v_author, var v_license, var v_version, \ local _buf, local _hid, local _n sdim v_name, 256 sdim v_author, 256 sdim v_license, 256 sdim v_version, 64 _vrm_extract_json path, _buf _n = stat if _n <= 0 : return -1 _hid = json_load(_buf) if _hid < 0 : return -2 ; VRM 1.0 v_name = json_str(_hid, "extensions.VRMC_vrm.meta.name") v_author = json_str(_hid, "extensions.VRMC_vrm.meta.authors[0]") v_license = json_str(_hid, "extensions.VRMC_vrm.meta.licenseUrl") v_version = json_str(_hid, "extensions.VRMC_vrm.specVersion") ; VRM 0.x フォールバック if strlen(v_name) = 0 : v_name = json_str(_hid, "extensions.VRM.meta.title") if strlen(v_author) = 0 : v_author = json_str(_hid, "extensions.VRM.meta.author") if strlen(v_license) = 0 : v_license = json_str(_hid, "extensions.VRM.meta.licenseName") if strlen(v_version) = 0 : v_version = json_str(_hid, "extensions.VRM.exporterVersion") json_release _hid return 0 #deffunc vrm_describe str path, var v_out, \ local _buf, local _hid, local _n, local _sb, \ local _name, local _author, local _lic, local _ver, \ local _contact, local _allow_sexual, local _allow_violent, \ local _allow_commercial, local _thumb_idx sdim v_out, 4096 v_out = "" _vrm_extract_json path, _buf _n = stat if _n <= 0 : return -1 _hid = json_load(_buf) if _hid < 0 : return -2 _name = json_str(_hid, "extensions.VRMC_vrm.meta.name") _author = json_str(_hid, "extensions.VRMC_vrm.meta.authors[0]") _lic = json_str(_hid, "extensions.VRMC_vrm.meta.licenseUrl") _ver = json_str(_hid, "extensions.VRMC_vrm.specVersion") _contact= json_str(_hid, "extensions.VRMC_vrm.meta.contactInformation") _allow_sexual = json_str(_hid, "extensions.VRMC_vrm.meta.allowExcessivelySexualUsage") _allow_violent = json_str(_hid, "extensions.VRMC_vrm.meta.allowExcessivelyViolentUsage") _allow_commercial= json_str(_hid, "extensions.VRMC_vrm.meta.commercialUsage") _thumb_idx = json_int(_hid, "extensions.VRMC_vrm.meta.thumbnailImage") if strlen(_name) = 0 : _name = json_str(_hid, "extensions.VRM.meta.title") if strlen(_author) = 0 : _author = json_str(_hid, "extensions.VRM.meta.author") if strlen(_lic) = 0 : _lic = json_str(_hid, "extensions.VRM.meta.licenseName") _sb = "" _sb = _sb + "name=" + _name + "\n" _sb = _sb + "author=" + _author + "\n" _sb = _sb + "licenseUrl=" + _lic + "\n" _sb = _sb + "specVersion=" + _ver + "\n" _sb = _sb + "contactInformation=" + _contact + "\n" _sb = _sb + "allowExcessivelySexualUsage=" + _allow_sexual + "\n" _sb = _sb + "allowExcessivelyViolentUsage=" + _allow_violent + "\n" _sb = _sb + "commercialUsage=" + _allow_commercial + "\n" _sb = _sb + "thumbnailImage=" + _thumb_idx + "\n" v_out = _sb json_release _hid return 0 #global #endif