Compare commits
10 Commits
wip/shadow
...
master
Author | SHA1 | Date |
---|---|---|
PrismaticFlower | 2b60f0d2a6 | |
LeovanGit | 8d5c701b86 | |
PrismaticFlower | c072c9e56d | |
PrismaticFlower | 2421ba70c2 | |
PrismaticFlower | ae42cda6ab | |
LeovanGit | 0ac921d855 | |
Will | 806a7cc060 | |
itdominator | d1d83d39af | |
itdominator | 1ec4332576 | |
itdominator | 125ad2792c |
|
@ -1,8 +1,6 @@
|
|||
.DS_Store
|
||||
*.msh
|
||||
|
||||
*.swp
|
||||
|
||||
# Created by https://www.gitignore.io/api/python,visualstudiocode
|
||||
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode
|
||||
|
||||
|
|
|
@ -32,111 +32,7 @@ def validate_segment_geometry(segment : GeometrySegment):
|
|||
return True
|
||||
|
||||
|
||||
def get_shadow_geometry(model: Model):
|
||||
for segment in model.geometry:
|
||||
if segment.shadow_geometry is not None:
|
||||
return segment.shadow_geometry
|
||||
return None
|
||||
|
||||
|
||||
|
||||
# SHDW mesh info is of a different form from
|
||||
# normal segment geometry
|
||||
def model_to_shadow_mesh(model: Model, shadow_geometry : ShadowGeometry):
|
||||
|
||||
blender_mesh = bpy.data.meshes.new(model.name)
|
||||
|
||||
# As is the case with normal geometry processing,
|
||||
# these will contain flattened lists
|
||||
vertex_positions = [convert_vector_space(position) for position in shadow_geometry.positions]
|
||||
|
||||
# Vertices
|
||||
blender_mesh.vertices.add(len(vertex_positions))
|
||||
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
||||
|
||||
|
||||
def faces_from_half_edges(half_edges : List[Tuple[int,int,int,int]]) -> List[List[int]]:
|
||||
faces = []
|
||||
visited_edges = [False] * len(half_edges)
|
||||
|
||||
for i in range(len(half_edges)):
|
||||
|
||||
if visited_edges[i]:
|
||||
continue
|
||||
|
||||
curr_edge = half_edges[i]
|
||||
|
||||
curr_index = curr_edge[0]
|
||||
starting_index = curr_index
|
||||
|
||||
face_length = 0
|
||||
face_temp = [0] * 5
|
||||
|
||||
while True:
|
||||
|
||||
if face_length + 1> len(face_temp):
|
||||
face_temp.append(curr_index)
|
||||
else:
|
||||
face_temp[face_length] = curr_index
|
||||
|
||||
face_length += 1
|
||||
|
||||
curr_edge = half_edges[curr_edge[1]]
|
||||
curr_index = curr_edge[0]
|
||||
|
||||
if (curr_index == starting_index):
|
||||
break
|
||||
|
||||
#print(f"Added a face of length: {face_length}")
|
||||
faces.append(face_temp[0:face_length])
|
||||
|
||||
return faces
|
||||
|
||||
polygons = faces_from_half_edges(shadow_geometry.edges)
|
||||
|
||||
# LOOPS
|
||||
flat_indices = [index for polygon in polygons for index in polygon]
|
||||
blender_mesh.loops.add(len(flat_indices))
|
||||
|
||||
# Position indices
|
||||
blender_mesh.loops.foreach_set("vertex_index", flat_indices)
|
||||
|
||||
|
||||
|
||||
# POLYGONS/FACES
|
||||
blender_mesh.polygons.add(len(polygons))
|
||||
|
||||
# Indices of starting loop for each polygon
|
||||
polygon_loop_start_indices = [0] * len(polygons)
|
||||
current_polygon_start_index = 0
|
||||
|
||||
# Number of loops in this polygon. Polygon i will use
|
||||
# loops from polygon_loop_start_indices[i] to
|
||||
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
|
||||
polygon_loop_totals = [0] * len(polygons)
|
||||
|
||||
for i,polygon in enumerate(polygons):
|
||||
polygon_loop_start_indices[i] = current_polygon_start_index
|
||||
|
||||
current_polygon_length = len(polygon)
|
||||
current_polygon_start_index += current_polygon_length
|
||||
|
||||
polygon_loop_totals[i] = current_polygon_length
|
||||
|
||||
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
|
||||
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
|
||||
|
||||
blender_mesh.validate(clean_customdata=False)
|
||||
blender_mesh.update()
|
||||
|
||||
#sv_name = model.name if model.name.startswith("sv_") else "sv_" + model.name
|
||||
|
||||
blender_mesh_object = bpy.data.objects.new(model.name, blender_mesh)
|
||||
|
||||
return blender_mesh_object
|
||||
|
||||
|
||||
def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
||||
def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
||||
|
||||
blender_mesh = bpy.data.meshes.new(model.name)
|
||||
|
||||
|
@ -223,7 +119,7 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
||||
|
||||
# LOOPS
|
||||
|
||||
|
||||
flat_indices = [index for polygon in polygons for index in polygon]
|
||||
|
||||
blender_mesh.loops.add(len(flat_indices))
|
||||
|
@ -246,24 +142,25 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||
|
||||
|
||||
# POLYGONS/FACES
|
||||
|
||||
blender_mesh.polygons.add(len(polygons))
|
||||
|
||||
# Indices of starting loop for each polygon
|
||||
polygon_loop_start_indices = [0] * len(polygons)
|
||||
polygon_loop_start_indices = []
|
||||
current_polygon_start_index = 0
|
||||
|
||||
# Number of loops in this polygon. Polygon i will use
|
||||
# loops from polygon_loop_start_indices[i] to
|
||||
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
|
||||
polygon_loop_totals = [0] * len(polygons)
|
||||
polygon_loop_totals = []
|
||||
|
||||
for i,polygon in enumerate(polygons):
|
||||
polygon_loop_start_indices[i] = current_polygon_start_index
|
||||
for polygon in polygons:
|
||||
polygon_loop_start_indices.append(current_polygon_start_index)
|
||||
|
||||
current_polygon_length = len(polygon)
|
||||
current_polygon_start_index += current_polygon_length
|
||||
|
||||
polygon_loop_totals[i] = current_polygon_length
|
||||
polygon_loop_totals.append(current_polygon_length)
|
||||
|
||||
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
|
||||
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
|
||||
|
@ -302,16 +199,3 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||
|
||||
return blender_mesh_object
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
||||
|
||||
shadow_geometry = get_shadow_geometry(model)
|
||||
if shadow_geometry is not None:
|
||||
return model_to_shadow_mesh(model, shadow_geometry)
|
||||
else:
|
||||
return model_to_mesh(model, scene, materials_map)
|
||||
|
||||
|
||||
|
|
|
@ -39,20 +39,6 @@ class VertexWeight:
|
|||
weight: float = 1.0
|
||||
bone: int = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
class ShadowGeometry:
|
||||
""" Class representing 'SHDW' chunks. """
|
||||
|
||||
# Perhaps I could just use the positions list in the segment
|
||||
# class, but I don't know if SHDW info can coexist with
|
||||
# a normal geometry segment...
|
||||
positions: List[Vector] = field(default_factory=list)
|
||||
|
||||
# The second two entries may not be necessary...
|
||||
edges: List[Tuple[int,int,int,int]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class GeometrySegment:
|
||||
""" Class representing a 'SEGM' section in a .msh file. """
|
||||
|
@ -70,7 +56,6 @@ class GeometrySegment:
|
|||
triangles: List[List[int]] = field(default_factory=list)
|
||||
triangle_strips: List[List[int]] = None
|
||||
|
||||
shadow_geometry: ShadowGeometry = None
|
||||
|
||||
@dataclass
|
||||
class CollisionPrimitive:
|
||||
|
|
|
@ -387,29 +387,7 @@ def _read_segm(segm: Reader, materials_list: List[Material]) -> GeometrySegment:
|
|||
# TODO: Dont know if/how to handle trailing 0 bug yet: https://schlechtwetterfront.github.io/ze_filetypes/msh.html#STRP
|
||||
#if segm.read_u16 != 0:
|
||||
# segm.skip_bytes(-2)
|
||||
|
||||
elif next_header == "SHDW":
|
||||
|
||||
shadow_geometry = ShadowGeometry()
|
||||
|
||||
with segm.read_child() as shdw:
|
||||
#print("Found shadow chunk")
|
||||
|
||||
num_positions = shdw.read_u32()
|
||||
#print(f" Num verts in shadow mesh: {num_positions}")
|
||||
shadow_geometry.positions = [shdw.read_vec() for _ in range(num_positions)]
|
||||
|
||||
num_edges = shdw.read_u32()
|
||||
#print(f" Num edges in shadow mesh: {num_edges}")
|
||||
edges = []
|
||||
for i in range(num_edges):
|
||||
edges.append(tuple(shdw.read_u16(4)))
|
||||
#print(" " + str(edges[-1]))
|
||||
shadow_geometry.edges = edges
|
||||
|
||||
geometry_seg.shadow_geometry = shadow_geometry
|
||||
|
||||
|
||||
elif next_header == "WGHT":
|
||||
with segm.read_child() as wght:
|
||||
|
||||
|
|
|
@ -44,9 +44,10 @@ def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material])
|
|||
new_obj = bpy.data.objects.new(model.name, None)
|
||||
new_obj.empty_display_size = 1
|
||||
new_obj.empty_display_type = 'PLAIN_AXES'
|
||||
new_obj.name = model.name
|
||||
|
||||
|
||||
model_map[model.name] = new_obj
|
||||
new_obj.name = model.name
|
||||
|
||||
if model.parent:
|
||||
new_obj.parent = model_map[model.parent]
|
||||
|
|
Loading…
Reference in New Issue