Răsfoiți Sursa

fix exported normals + add option to invert winding

deltanedas 3 ani în urmă
părinte
comite
b110d9c59f
3 a modificat fișierele cu 27 adăugiri și 6 ștergeri
  1. BIN
      chair.blend
  2. BIN
      chair.chr
  3. 27 6
      chair_exporter.py

BIN
chair.blend


BIN
chair.chr


+ 27 - 6
chair_exporter.py

@@ -7,7 +7,7 @@ bl_info = {
 	"blender": (3, 0, 0),
 	"blender": (3, 0, 0),
 	"location": "File > Import-Export > Chair Mesh (.chr)",
 	"location": "File > Import-Export > Chair Mesh (.chr)",
 	# Crate verion 0.2.3
 	# Crate verion 0.2.3
-	"version": (0, 2, 4),
+	"version": (0, 2, 5),
 	"category": "Import-Export"
 	"category": "Import-Export"
 }
 }
 
 
@@ -92,6 +92,9 @@ def process_scene(context, apply_modifiers, store_colours, store_normals, store_
 		if not me.loop_triangles and me.polygons:
 		if not me.loop_triangles and me.polygons:
 			me.calc_loop_triangles()
 			me.calc_loop_triangles()
 
 
+		if store_normals:
+			me.calc_normals_split()
+
 		# TODO: find texture used by this object
 		# TODO: find texture used by this object
 		vertices = []
 		vertices = []
 		vertex_ids = {}
 		vertex_ids = {}
@@ -164,9 +167,14 @@ def write_vec3_byte(f, v, scale, offset):
 	z = 128 * (v[1] - offset[1]) / scale[1]
 	z = 128 * (v[1] - offset[1]) / scale[1]
 	f.write(bytes([int(x), int(y), int(z)]))
 	f.write(bytes([int(x), int(y), int(z)]))
 
 
+tau = math.pi * 2
 def index_normal(n):
 def index_normal(n):
+	if n == 0:
+		# exact mapping of 0 to 0.0 with no error
+		return 0
+
 	# asin fairly slow but this is pyton so doesnt matter
 	# asin fairly slow but this is pyton so doesnt matter
-	return 128 + int(math.asin(n) * 255 / math.pi)
+	return 128 - int(math.asin(n) * 255 / tau)
 
 
 def write_vec3_sin(f, v):
 def write_vec3_sin(f, v):
 	x = index_normal(v[0])
 	x = index_normal(v[0])
@@ -175,7 +183,7 @@ def write_vec3_sin(f, v):
 	z = index_normal(v[1])
 	z = index_normal(v[1])
 	f.write(bytes([x, y, z]))
 	f.write(bytes([x, y, z]))
 
 
-def write_model(path, colours, 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, flip_winding):
 	f = open(path, "wb")
 	f = open(path, "wb")
 	# Chair file header
 	# Chair file header
 	f.write(bytes("CHAIR", "ascii"))
 	f.write(bytes("CHAIR", "ascii"))
@@ -259,8 +267,15 @@ def write_model(path, colours, objects, indices, export_colours, export_normals,
 	# Indices
 	# Indices
 	f.write(len(indices).to_bytes(4, "little"))
 	f.write(len(indices).to_bytes(4, "little"))
 	indsize = index_size(sum([len(obj[0]) for obj in objects]))
 	indsize = index_size(sum([len(obj[0]) for obj in objects]))
-	for i in indices:
-		f.write(i.to_bytes(indsize, "little"))
+	if flip_winding:
+		for i in range(0, int(len(indices) / 3)):
+			n = i * 3
+			f.write(indices[n + 1].to_bytes(indsize, "little"))
+			f.write(indices[n].to_bytes(indsize, "little"))
+			f.write(indices[n + 2].to_bytes(indsize, "little"))
+	else:
+		for i in indices:
+			f.write(i.to_bytes(indsize, "little"))
 
 
 	f.close()
 	f.close()
 	return
 	return
@@ -335,10 +350,16 @@ class ModelExporter(Operator, ExportHelper):
 		default = False
 		default = False
 	)
 	)
 
 
+	flip_winding: BoolProperty(
+		name = "Flip Winding",
+		description = "Flip vertex winding from CCW to CW",
+		default = False
+	)
+
 	def execute(self, context):
 	def execute(self, context):
 		colours, objects, indices = process_scene(context, self.apply_modifiers, self.export_colours, self.export_normals, self.export_uvs)
 		colours, objects, indices = process_scene(context, self.apply_modifiers, self.export_colours, self.export_normals, self.export_uvs)
 		write_model(self.filepath, colours, objects, indices, self.export_colours, self.export_normals, self.export_uvs, self.export_skeleton,
 		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)
+			self.short_pos, self.short_normals, self.short_uvs, self.short_weights, self.flip_winding)
 		return {"FINISHED"}
 		return {"FINISHED"}
 
 
 def menu_export(self, context):
 def menu_export(self, context):