Skip to content

Instantly share code, notes, and snippets.

@yakumo-proj
Created December 13, 2020 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakumo-proj/9bc9ac8241f7d7bdd4588cce727cef45 to your computer and use it in GitHub Desktop.
Save yakumo-proj/9bc9ac8241f7d7bdd4588cce727cef45 to your computer and use it in GitHub Desktop.
VRM image file extract tools (Ruby)
#!/usr/bin/ruby -Ks
# Copyright (C) 2020 Yakumo Sayo. All rights reserved.
# Dual Licence (BSD 2-Crause or GPLv3)
require 'pathname'
require 'JSON'
buf = File.open(ARGV[0], "rb")
def read_uint32(buffer)
buffer.read(4).bytes.reverse.inject(0){|s,i| s*256 + i}
end
def chunk_read(buffer)
chunk_length = read_uint32(buffer)
chunk_type = read_uint32(buffer)
buffer.read(chunk_length)
end
magic = read_uint32(buf)
version = read_uint32(buf)
file_length = read_uint32(buf)
puts("magic:#{magic}, version:#{version}, length:#{file_length}")
json = chunk_read(buf)
basename = File.basename(ARGV[0])
json_file = "#{basename}.json"
File.open(json_file, mode = "w"){|f|
f.write json
}
puts "written: #{json_file}"
bin = chunk_read(buf)
=begin
bin_name = "#{basename}.01.bin"
File.open(json_file, mode = "wb"){|f|
f.write(bin, bin.size)
}
puts "written: #{bin_name}"
=end
gltf = JSON.parse(json)
gltf['images'].map{|x|
x['bufferView']
}.map{|i|
view = gltf['bufferViews'][i]
png_data = bin[ view['byteOffset'], ]
snum = format("%02d", i)
filename = "#{basename}-#{snum}.png"
File.open(filename, mode = "wb"){|f|
f.write(bin[view['byteOffset'], view['byteLength']], view['byteLength'])
}
puts "written: #{filename}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment