Explorar o código

exporting colours works

deltanedas %!s(int64=3) %!d(string=hai) anos
pai
achega
e0bae6e539
Modificáronse 2 ficheiros con 36 adicións e 20 borrados
  1. 1 1
      Cargo.toml
  2. 35 19
      chair_exporter.py

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "chair"
-version = "0.2.2"
+version = "0.2.3"
 edition = "2021"
 description = "An efficient binary mesh format which is both small and extremely fast to read."
 readme = "README.md"

+ 35 - 19
chair_exporter.py

@@ -1,5 +1,6 @@
 # Blender addon to export an object as a lib.rs/chair mesh file
 # Based on the Raw mesh exporter distributed with blender
+# FIXME: Currently might produce bloated mesh if not all features are used, the deduplication doesn't take what will actually be used into account
 
 bl_info = {
 	"name": "Chair Mesh Exporter",
@@ -7,7 +8,7 @@ bl_info = {
 	"blender": (3, 0, 0),
 	"location": "File > Import-Export > Chair Mesh (.chr)",
 	# Same as the crate version
-	"version": (0, 2, 2),
+	"version": (0, 2, 3),
 	"category": "Import-Export"
 }
 
@@ -16,14 +17,27 @@ import math
 # For writing floats
 import struct
 
-def face_to_tris(face, mesh, matrix, vertices, vertex_ids, indices):
+def face_to_tris(face, mesh, matrix, vertices, vertex_ids, colours, colour_ids, indices):
 	uvs = mesh.uv_layers.active.data
+	cols = mesh.vertex_colors.active.data
 	for n in range(3):
 		verti = face.vertices[n]
 		loopi = face.loops[n]
 		vert = mesh.vertices[verti]
 		loop = mesh.loops[loopi]
-		v = [(matrix @ vert.co)[:], (loop.normal)[:], (uvs[loopi].uv)[:]]
+
+		col = (cols[loopi].color)[:]
+		# really inefficient but this is pyton so
+		colkey = str(col)
+		if colkey in colour_ids:
+			coli = colour_ids[colkey]
+		else:
+			# new colour, push to list
+			coli = len(colours)
+			colours.append(col)
+			colour_ids[colkey] = coli
+
+		v = [(matrix @ vert.co)[:], coli, (loop.normal)[:], (uvs[loopi].uv)[:]]
 		# really inefficient but this is pyton so
 		key = str(v)
 		if key in vertex_ids:
@@ -37,6 +51,9 @@ def face_to_tris(face, mesh, matrix, vertices, vertex_ids, indices):
 		indices.append(i)
 
 def process_scene(context, apply_modifiers):
+	colours = []
+	colour_ids = {}
+	# TODO: texture names
 	objects = []
 	indices = []
 
@@ -59,12 +76,12 @@ def process_scene(context, apply_modifiers):
 		if not me.loop_triangles and me.polygons:
 			me.calc_loop_triangles()
 
-		# TODO: texture used by this object
+		# TODO: find texture used by this object
 		vertices = []
 		vertex_ids = {}
 		matrix = obj.matrix_world.copy()
 		for face in me.loop_triangles:
-			face_to_tris(face, me, matrix, vertices, vertex_ids, indices)
+			face_to_tris(face, me, matrix, vertices, vertex_ids, colours, colour_ids, indices)
 
 		min_pos = [
 			min(vertices, key=lambda v: v[0][0])[0][0],
@@ -87,7 +104,7 @@ def process_scene(context, apply_modifiers):
 		if is_tmp_mesh:
 			obj.to_mesh_clear()
 
-	return objects, indices
+	return colours, objects, indices
 
 def index_size(vertices):
 	if vertices < 256:
@@ -142,7 +159,7 @@ def write_vec3_sin(f, v):
 	z = index_normal(v[1])
 	f.write(bytes([x, y, z]))
 
-def write_model(path, objects, indices, export_colours, export_normals, export_uvs, export_skeleton, short_pos, short_normals, short_uvs, short_weights):
+def write_model(path, colours, objects, indices, export_colours, export_normals, export_uvs, export_skeleton, short_pos, short_normals, short_uvs, short_weights):
 	f = open(path, "wb")
 	# Chair file header
 	f.write(bytes("CHAIR", "ascii"))
@@ -169,11 +186,12 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
 		features |= 0b10000000
 	f.write(bytes([features]))
 
-	# TODO: export unique vertex colours from objects
 	if export_colours:
-		f.write((1).to_bytes(2, "little"))
-		f.write(bytes([255, 255, 255, 255]))
-	if 1 > 255:
+		f.write((len(colours)).to_bytes(2, "little"))
+		for col in colours:
+			for chan in col:
+				f.write(bytes([int(chan * 255)]))
+	if len(colours) > 255:
 		colour_size = 2
 	else:
 		colour_size = 1
@@ -205,8 +223,7 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
 			write_normals = write_vec3_float
 
 		if export_uvs:
-			# TODO: actual texture index
-			f.write((0).to_bytes(colour_size, "little"))
+			f.write(bytes([0]))
 		if short_uvs:
 			write_uvs = write_vec2_byte
 		else:
@@ -216,12 +233,11 @@ def write_model(path, objects, indices, export_colours, export_normals, export_u
 		for v in obj[0]:
 			write_pos(f, v[0])
 			if export_colours:
-				# TODO: colour index
-				f.write(bytes([0]))
+				f.write(v[1].to_bytes(colour_size, "little"))
 			if export_normals:
-				write_normals(f, v[1])
+				write_normals(f, v[2])
 			if export_uvs:
-				write_uvs(f, v[2])
+				write_uvs(f, v[3])
 			# TODO: bone weights
 
 	# Indices
@@ -304,8 +320,8 @@ class ModelExporter(Operator, ExportHelper):
 	)
 
 	def execute(self, context):
-		objects, indices = process_scene(context, self.apply_modifiers)
-		write_model(self.filepath, objects, indices, self.export_colours, self.export_normals, self.export_uvs, self.export_skeleton,
+		colours, objects, indices = process_scene(context, self.apply_modifiers)
+		write_model(self.filepath, colours, objects, indices, self.export_colours, self.export_normals, self.export_uvs, self.export_skeleton,
 			self.short_pos, self.short_normals, self.short_uvs, self.short_weights)
 		return {"FINISHED"}