imporove mikunyan-image

This commit is contained in:
Ishotihadus
2020-06-20 18:50:02 +09:00
parent 9a520637e9
commit 387a683ff0
+36 -18
View File
@@ -44,48 +44,66 @@ unless File.file?(arg)
exit(1) exit(1)
end end
assets = opts[:as_asset] ? [Mikunyan::Asset.file(arg)] : Mikunyan::AssetBundle.file(arg).assets if opts[:as_asset]
assets = [Mikunyan::Asset.file(arg)]
else
bundle = Mikunyan::AssetBundle.file(arg)
assets = bundle.assets
end
outdir = opts[:outputdir] || File.basename(arg, '.*') outdir = opts[:outputdir] || File.basename(arg, '.*')
FileUtils.mkpath(outdir) FileUtils.mkpath(outdir)
assets.each do |asset| if opts[:sprite]
if opts[:sprite] textures = {}
textures_meta = {}
assets.each do |asset|
json = {} json = {}
textures = {}
asset.each_object do |obj| asset.each_object do |obj|
next unless obj.type == 'Sprite' next unless obj.type == 'Sprite'
obj = obj.parse obj = obj.parse
next unless obj next unless obj&.m_RD&.texture
texture_id = obj.m_RD&.texture&.m_PathID&.value file_id = obj.m_RD.texture.m_FileID.value
next unless texture_id texture_asset = file_id == 0 ? asset : bundle && bundle[asset.references[file_id - 1].file_path]
texture_id = obj.m_RD.texture.m_PathID.value
next unless texture_asset && texture_id
unless textures.key?(texture_id) unless textures.dig(texture_asset, texture_id)
texture_obj = asset.parse_object(texture_id) texture_obj = texture_asset.parse_object(texture_id)
if texture_obj.is_a?(Mikunyan::CustomTypes::Texture2D) if texture_obj.is_a?(Mikunyan::CustomTypes::Texture2D)
textures[texture_id] = texture_obj.generate_png textures[texture_asset] ||= {}
json[texture_id] = { textures[texture_asset][texture_id] = texture_obj.generate_png
textures_meta[texture_asset] ||= {}
textures_meta[texture_asset][texture_id] = {
name: texture_obj.m_Name&.value, width: texture_obj.m_Width&.value, height: texture_obj.m_Height&.value, name: texture_obj.m_Name&.value, width: texture_obj.m_Width&.value, height: texture_obj.m_Height&.value,
format: texture_obj.m_TextureFormat&.value, path_id: texture_id, sprites: [] format: texture_obj.m_TextureFormat&.value, asset: texture_asset.name, path_id: texture_id
} }
end end
end end
next unless textures_meta[texture_asset][texture_id]
unless json.key?([file_id, texture_id])
json[[file_id, texture_id]] = textures_meta[texture_asset][texture_id].dup
json[[file_id, texture_id]][:sprites] = []
end
x = obj.m_Rect&.x&.value x = obj.m_Rect&.x&.value
y = obj.m_Rect&.y&.value y = obj.m_Rect&.y&.value
width = obj.m_Rect&.width&.value width = obj.m_Rect&.width&.value
height = obj.m_Rect&.height&.value height = obj.m_Rect&.height&.value
json[texture_id][:sprites] << { name: obj.object_name, x: x, y: y, width: width, height: height, path_id: obj.path_id } json[[file_id, texture_id]][:sprites] << { name: obj.object_name, x: x, y: y, width: width, height: height, path_id: obj.path_id }
next unless textures[texture_id] && x && y && width && height texture = textures[texture_asset][texture_id]
textures[texture_id].crop( next unless texture && x && y && width && height
x.round, (textures[texture_id].height - height - y).round, width.round, height.round texture.crop(x.round, (texture.height - height - y).round, width.round, height.round).save("#{outdir}/#{obj.object_name}.png")
).save("#{outdir}/#{obj.object_name}.png")
end end
puts opts[:pretty] ? JSON.pretty_generate(json.values) : JSON.generate(json.values) puts opts[:pretty] ? JSON.pretty_generate(json.values) : JSON.generate(json.values)
else end
else
assets.each do |asset|
json = [] json = []
asset.each_object do |obj| asset.each_object do |obj|
next unless obj.type == 'Texture2D' next unless obj.type == 'Texture2D'