From be9ff66eebe676c859eddfd93d6cb0d8c9c5e81f Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Fri, 13 Mar 2020 19:23:49 +0900 Subject: [PATCH] Added support concatinating `materials`, `images`, `textures` and `samplers`. --- examples/mesh-conv/sandbox/README.md | 2 +- examples/mesh-conv/sandbox/concat_mesh.py | 113 +++++++++++++++++++++- 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/examples/mesh-conv/sandbox/README.md b/examples/mesh-conv/sandbox/README.md index df01840..53f149c 100644 --- a/examples/mesh-conv/sandbox/README.md +++ b/examples/mesh-conv/sandbox/README.md @@ -19,7 +19,7 @@ $ python concat_mesh.py source0.gltf target.gltf * [x] Support multiple glTFs to merge(concat) * [ ] Support merging skin * [ ] Support merging different node hierarchies -* [ ] Support `images`, `textures`, `materials` +* [x] Support `images`, `textures`, `materials`, `samplers` * [ ] Support other glTF info # replace_attrib.py diff --git a/examples/mesh-conv/sandbox/concat_mesh.py b/examples/mesh-conv/sandbox/concat_mesh.py index a84c8cd..1296b1e 100644 --- a/examples/mesh-conv/sandbox/concat_mesh.py +++ b/examples/mesh-conv/sandbox/concat_mesh.py @@ -1,11 +1,41 @@ # concat mesh to glTF # support multiple source glTF to me concatenated to target.gltf +# TODO: skins, nodes, scenes + import json import sys, os g_prefix = "added/" +def adjustMaterialTextureIndex(mat, texture_index_offset): + # Args: mat(dict) + # texture_index_offset(int) Index offset + + if "normalTexture" in mat: + if "index" in mat["normalTexture"]: + mat["normalTexture"]["index"] += texture_index_offset + + if "occlusionTexture" in mat: + if "index" in mat["occlusionTexture"]: + mat["occlusionTexture"]["index"] += texture_index_offset + + if "emissiveTexture" in mat: + if "index" in mat["emissiveTexture"]: + mat["emissiveTexture"]["index"] += texture_index_offset + + if "pbrMetallicRoughness" in mat: + if "baseColorTexture" in mat["pbrMetallicRoughness"]: + if "index" in mat["pbrMetallicRoughness"]["baseColorTexture"]: + mat["pbrMetallicRoughness"]["baseColorTexture"]["index"] += texture_index_offset + + if "metallicRoughnessTexture" in mat["pbrMetallicRoughness"]: + if "index" in mat["pbrMetallicRoughness"]["metallicRoughnessTexture"]: + mat["pbrMetallicRoughness"]["metallicRoughnessTexture"]["index"] += texture_index_offset + + return mat + + def concat(source, target, offsets, prefix, extraInfo): # Args: # source: dict(modified), target: dict(modified), @@ -18,16 +48,22 @@ def concat(source, target, offsets, prefix, extraInfo): num_source_bufferViews = len(source["bufferViews"]) num_source_accessors = len(source["accessors"]) num_source_materials = len(source["materials"]) if "materials" in source else 0 + num_source_samplers = len(source["samplers"]) if "samplers" in source else 0 + num_source_images = len(source["images"]) if "images" in source else 0 + num_source_textures = len(source["textures"]) if "textures" in source else 0 print("src =====") print("num_src_meshes: ", num_source_meshes) print("num_src_buffers: ", num_source_buffers) print("num_src_bufferViews: ", num_source_bufferViews) print("num_src_accessors: ", num_source_accessors) print("num_src_materials: ", num_source_materials) + print("num_src_textures: ", num_source_textures) + print("num_src_images: ", num_source_images) + print("num_src_samplers: ", num_source_samplers) # - # Adjust name and index offset + # Modify name and adjust index offset # for i in range(len(source["buffers"])): if "name" in source["buffers"][i]: @@ -62,6 +98,29 @@ def concat(source, target, offsets, prefix, extraInfo): source["meshes"][i]["primitives"][primid]["material"] += offsets["materials"] + if "materials" in source: + for i in range(len(source["materials"])): + if "name" in source["materials"][i]: + source["materials"][i]["name"] = prefix + source["materials"][i]["name"] + source["materials"][i] = adjustMaterialTextureIndex(source["materials"][i], offsets["textures"]) + + if "images" in source: + for i in range(len(source["images"])): + if "name" in source["images"][i]: + source["images"][i]["name"] = prefix + source["images"][i]["name"] + + if "samplers" in source: + for i in range(len(source["samplers"])): + if "name" in source["samplers"][i]: + source["samplers"][i]["name"] = prefix + source["samplers"][i]["name"] + + if "textures" in source: + for i in range(len(source["textures"])): + if "name" in source["textures"][i]: + source["textures"][i]["name"] = prefix + source["textures"][i]["name"] + source["textures"][i]["sampler"] += offsets["samplers"] + source["textures"][i]["source"] += offsets["images"] + # # Append mesh info # @@ -70,7 +129,25 @@ def concat(source, target, offsets, prefix, extraInfo): target["meshes"] += source["meshes"] target["accessors"] += source["accessors"] if "materials" in source: - target["materials"] += source["materials"] + if "materials" in target: + target["materials"] += source["materials"] + else: + target["materials"] = source["materials"] + if "images" in source: + if "images" in target: + target["images"] += source["images"] + else: + target["images"] = source["images"] + if "textures" in source: + if "textures" in target: + target["textures"] += source["textures"] + else: + target["textures"] = source["textures"] + if "samplers" in source: + if "samplers" in target: + target["samplers"] += source["samplers"] + else: + target["samplers"] = source["samplers"] # assume `prefix` is unique extraInfo["num_{}_meshes".format(prefix)] = num_source_meshes @@ -78,6 +155,9 @@ def concat(source, target, offsets, prefix, extraInfo): extraInfo["num_{}_bufferViews".format(prefix)] = num_source_bufferViews extraInfo["num_{}_accessors".format(prefix)] = num_source_accessors extraInfo["num_{}_materials".format(prefix)] = num_source_materials + extraInfo["num_{}_images".format(prefix)] = num_source_images + extraInfo["num_{}_samplers".format(prefix)] = num_source_samplers + extraInfo["num_{}_textures".format(prefix)] = num_source_textures # update offsets offsets["meshes"] += num_source_meshes @@ -85,6 +165,9 @@ def concat(source, target, offsets, prefix, extraInfo): offsets["bufferViews"] += num_source_bufferViews offsets["accessors"] += num_source_accessors offsets["materials"] += num_source_materials + offsets["textures"] += num_source_textures + offsets["images"] += num_source_images + offsets["samplers"] += num_source_samplers def main(): @@ -121,11 +204,26 @@ def main(): if "materials" in target: num_target_materials = len(target["materials"]) + num_target_images = 0 + if "images" in target: + num_target_images = len(target["images"]) + + num_target_samplers = 0 + if "samplers" in target: + num_target_samplers = len(target["samplers"]) + + num_target_textures = 0 + if "textures" in target: + num_target_textures = len(target["textures"]) + print("num_target_meshes: ", num_target_meshes) print("num_target_buffers: ", num_target_buffers) print("num_target_bufferViews: ", num_target_bufferViews) print("num_target_accessors: ", num_target_accessors) - print("num_target_materials: ", num_target_accessors) + print("num_target_materials: ", num_target_materials) + print("num_target_textures: ", num_target_textures) + print("num_target_images: ", num_target_images) + print("num_target_samplers: ", num_target_samplers) # # add some info to asset.extras @@ -136,6 +234,9 @@ def main(): extraInfo["num_target_bufferViews"] = num_target_bufferViews extraInfo["num_target_accessors"] = num_target_accessors extraInfo["num_target_materials"] = num_target_materials + extraInfo["num_target_textures"] = num_target_textures + extraInfo["num_target_images"] = num_target_images + extraInfo["num_target_samplers"] = num_target_samplers offsets = {} offsets["meshes"] = num_target_meshes @@ -143,6 +244,9 @@ def main(): offsets["bufferViews"] = num_target_bufferViews offsets["accessors"] = num_target_accessors offsets["materials"] = num_target_materials + offsets["textures"] = num_target_textures + offsets["samplers"] = num_target_samplers + offsets["images"] = num_target_images for i in range(num_srcs): source = sources[i] @@ -155,6 +259,9 @@ def main(): extraInfo["num_total_bufferViews"] = offsets["bufferViews"] extraInfo["num_total_accessors"] = offsets["accessors"] extraInfo["num_total_materials"] = offsets["materials"] + extraInfo["num_total_textures"] = offsets["textures"] + extraInfo["num_total_images"] = offsets["images"] + extraInfo["num_total_samplers"] = offsets["samplers"] target["asset"]["extras"] = extraInfo