tools: obj2dl: Support vertex color commands

Vertex color commands override normal commands, so the only way to
export color commands is to use the new "--use-vertex-color" option.
This will skip exporting normal commands and it will only export color
commands.
This commit is contained in:
Antonio Niño Díaz 2023-01-18 01:41:55 +00:00
parent 9f6c719373
commit 207feca7fc

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
# #
# Copyright (c) 2022 Antonio Niño Díaz <antonio_nd@outlook.com> # Copyright (c) 2022-2023 Antonio Niño Díaz <antonio_nd@outlook.com>
from display_list import DisplayList from display_list import DisplayList
@ -15,7 +15,7 @@ def is_valid_texture_size(size):
return size in VALID_TEXTURE_SIZES return size in VALID_TEXTURE_SIZES
def convert_obj(input_file, output_file, texture_size, def convert_obj(input_file, output_file, texture_size,
model_scale, model_translation): model_scale, model_translation, use_vertex_color):
vertices = [] vertices = []
texcoords = [] texcoords = []
@ -38,7 +38,15 @@ def convert_obj(input_file, output_file, texture_size,
tokens = tokens[1:] tokens = tokens[1:]
if cmd == 'v': # Vertex if cmd == 'v': # Vertex
v = (float(tokens[0]), float(tokens[1]), float(tokens[2])) if len(tokens) == 3: # (x, y, z)
if use_vertex_color:
raise OBJFormatError(f"Found vertex with no color info: {tokens}")
v = [float(tokens[i]) for i in range(3)]
elif len(tokens) == 6: # (x, y, z, r, g, b)
v = [float(tokens[i]) for i in range(6)]
else:
raise OBJFormatError(f"Unsupported vertex command: {tokens}")
vertices.append(v) vertices.append(v)
elif cmd == 'vt': # Texture coordinate elif cmd == 'vt': # Texture coordinate
@ -95,6 +103,9 @@ def convert_obj(input_file, output_file, texture_size,
vertex_index = int(tokens[0]) vertex_index = int(tokens[0])
if len(tokens[1]) > 0: if len(tokens[1]) > 0:
texcoord_index = int(tokens[1]) texcoord_index = int(tokens[1])
# Only generate normal commands if there is no vertex color, as
# it would overwrite it.
if not use_vertex_color:
normal_index = int(tokens[2]) normal_index = int(tokens[2])
else: else:
raise OBJFormatError(f"Invalid face {face}") raise OBJFormatError(f"Invalid face {face}")
@ -131,6 +142,10 @@ def convert_obj(input_file, output_file, texture_size,
v *= model_scale v *= model_scale
vtx.append(v) vtx.append(v)
if use_vertex_color:
rgb = [vertices[vertex_index][i] for i in range(3, 6)]
dl.color(*rgb)
# Let the DisplayList class pick the best vtx command # Let the DisplayList class pick the best vtx command
dl.vtx(vtx[0], vtx[1], vtx[2]) dl.vtx(vtx[0], vtx[1], vtx[2])
@ -144,8 +159,8 @@ if __name__ == "__main__":
import sys import sys
import traceback import traceback
print("obj2dl v0.1.0") print("obj2dl v0.1.1")
print("Copyright (c) 2022 Antonio Niño Díaz <antonio_nd@outlook.com>") print("Copyright (c) 2022-2023 Antonio Niño Díaz <antonio_nd@outlook.com>")
print("All rights reserved") print("All rights reserved")
print("") print("")
@ -167,6 +182,9 @@ if __name__ == "__main__":
help="translate model by this value") help="translate model by this value")
parser.add_argument("--scale", default=1.0, type=float, parser.add_argument("--scale", default=1.0, type=float,
help="scale model by this value (after the translation)") help="scale model by this value (after the translation)")
parser.add_argument("--use-vertex-color", required=False,
action='store_true',
help="use vertex colors instead of normals")
args = parser.parse_args() args = parser.parse_args()
@ -187,7 +205,7 @@ if __name__ == "__main__":
try: try:
convert_obj(args.input, args.output, args.texture, convert_obj(args.input, args.output, args.texture,
args.scale, args.translation) args.scale, args.translation, args.use_vertex_color)
except BaseException as e: except BaseException as e:
print("ERROR: " + str(e)) print("ERROR: " + str(e))
traceback.print_exc() traceback.print_exc()