|
| 1 | +# VRMフォーマット |
| 2 | + |
| 3 | +VRM は、[Khronos](https://www.khronos.org/) の開発している 3D Format である [glTF](https://www.khronos.org/Gltf) の version `2.0` をベースにしています。 |
| 4 | + |
| 5 | +:::note VRM file |
| 6 | +VRM は、glTF の バイナリーバージョンである glb のファイル拡張子を `.vrm` に変更します。 |
| 7 | + |
| 8 | +`.vrm` を `.glb` に戻すことで `vrm` 未対応の `glTF` 対応アプリケーションでも、3D Model としてロードできることが期待できます。 |
| 9 | +::: |
| 10 | + |
| 11 | +- https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html |
| 12 | + |
| 13 | +## vrm-0.x |
| 14 | + |
| 15 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/0.0 |
| 16 | + |
| 17 | +## vrm-1.0 |
| 18 | + |
| 19 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_vrm-1.0 |
| 20 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_materials_mtoon-1.0 |
| 21 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_springBone-1.0 |
| 22 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_node_constraint-1.0 |
| 23 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_springBone_extended_collider-1.0 |
| 24 | + |
| 25 | +VRM と 並用することが想定される他の glTF extension |
| 26 | + |
| 27 | +- [KHR_materials_unlit](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_unlit/README.md) |
| 28 | +- KHR_texture_transform |
| 29 | +- KHR_materials_emissive_strength |
| 30 | + |
| 31 | +## vrma |
| 32 | + |
| 33 | +- https://github.com/vrm-c/vrm-specification/tree/master/specification/VRMC_vrm_animation-1.0 |
| 34 | + |
| 35 | +## glb フォーマット概説 |
| 36 | + |
| 37 | +:::note 仕様 |
| 38 | +https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#glb-file-format-specification |
| 39 | +::: |
| 40 | + |
| 41 | +シーンヒエラルキーをJSONで記述するテキスト部と、画像や頂点配列を記録するバイナリ部の2つの部分からなります。 |
| 42 | + |
| 43 | +`glTFヘッダ + JSON CHUNk + BINARY CHUNK`という構成です。 |
| 44 | + |
| 45 | +### glTFヘッダ部 |
| 46 | + |
| 47 | +| 長さ | 内容 | 型 | 値 | |
| 48 | +| :--- | :------------- | :---- | :----- | |
| 49 | +| 4 | | ascii | "glTF" | |
| 50 | +| 4 | gltfバージョン | int32 | 2 | |
| 51 | +| 4 | file size | int32 | | |
| 52 | + |
| 53 | +### チャンク部 |
| 54 | + |
| 55 | +| 長さ | 内容 | 型 | 値 | |
| 56 | +| :--------- | :--------- | :------- | :------------------ | |
| 57 | +| 4 | chunk size | int32 | | |
| 58 | +| 4 | chunk type | ascii | "JSON" or "BIN\x00" | |
| 59 | +| chunk size | chunk body | バイト列 | | |
| 60 | + |
| 61 | +```python title="python3によるパース例" |
| 62 | +import struct |
| 63 | +import json |
| 64 | + |
| 65 | +class Reader: |
| 66 | + def __init__(self, data: bytes)->None: |
| 67 | + self.data = data |
| 68 | + self.pos = 0 |
| 69 | + |
| 70 | + def read_str(self, size): |
| 71 | + result = self.data[self.pos: self.pos + size] |
| 72 | + self.pos += size |
| 73 | + return result.strip() |
| 74 | + |
| 75 | + def read(self, size): |
| 76 | + result = self.data[self.pos: self.pos + size] |
| 77 | + self.pos += size |
| 78 | + return result |
| 79 | + |
| 80 | + def read_uint(self): |
| 81 | + result = struct.unpack('I', self.data[self.pos:self.pos + 4])[0] |
| 82 | + self.pos += 4 |
| 83 | + return result |
| 84 | + |
| 85 | + |
| 86 | +def parse_glb(data: bytes): |
| 87 | + reader = Reader(data) |
| 88 | + magic = reader.read_str(4) |
| 89 | + if magic != b'glTF': |
| 90 | + raise Exception(f'magic not found: #{magic}') |
| 91 | + |
| 92 | + version = reader.read_uint() |
| 93 | + if version != 2: |
| 94 | + raise Exception(f'version:#{version} is not 2') |
| 95 | + |
| 96 | + size = reader.read_uint() |
| 97 | + size -= 12 |
| 98 | + |
| 99 | + json_str = None |
| 100 | + body = None |
| 101 | + while size > 0: |
| 102 | + #print(size) |
| 103 | + |
| 104 | + chunk_size = reader.read_uint() |
| 105 | + size -= 4 |
| 106 | + |
| 107 | + chunk_type = reader.read_str(4) |
| 108 | + size -= 4 |
| 109 | + |
| 110 | + chunk_data = reader.read(chunk_size) |
| 111 | + size -= chunk_size |
| 112 | + |
| 113 | + if chunk_type == b'BIN\x00': |
| 114 | + body = chunk_data |
| 115 | + elif chunk_type == b'JSON': |
| 116 | + json_str = chunk_data |
| 117 | + else: |
| 118 | + raise Exception(f'unknown chunk_type: {chunk_type}') |
| 119 | + |
| 120 | + return json.loads(json_str), body |
| 121 | + |
| 122 | + |
| 123 | +with open('AliciaSolid.vrm', 'rb') as f: |
| 124 | + parsed, body = parse_glb(f.read()) |
| 125 | +``` |
0 commit comments