Created
July 28, 2018 16:29
-
-
Save oshow/e566b1630bda8df11c9d7c49f3a712f9 to your computer and use it in GitHub Desktop.
parse VRM file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'pp' | |
def parse_glb(f) | |
magic = f.read(4) | |
if magic != 'glTF' | |
raise "magic not found: #{magic}" | |
end | |
version = f.read(4).unpack('I').first | |
if version != 2 | |
raise "version: #{version} is not 2" | |
end | |
file_size = f.read(4).unpack('I').first | |
json_str = nil | |
body = nil | |
while !(f.eof) | |
chunk_size = f.read(4).unpack('I').first | |
chunk_type = f.read(4) | |
chunk_data = f.read(chunk_size) | |
case chunk_type | |
when "BIN\x00" | |
body = chunk_data | |
when "JSON" | |
json_str = chunk_data | |
else | |
raise "unknown chunk_type: #{chunk_type}" | |
end | |
end | |
[JSON.parse(json_str), body] | |
end | |
open('AliciaSolid.vrm', 'rb') {|f| | |
parsed, body = parse_glb(f) | |
pp parsed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment