Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import math
- # Create new mesh and a new object
- def create_mesh(ob_name, coords, edges, faces, vertex_groups):
- me = bpy.data.meshes.new(ob_name + "Mesh")
- ob = bpy.data.objects.new(ob_name, me)
- me.from_pydata(coords, edges, faces)
- me.update()
- bpy.context.collection.objects.link(ob)
- return ob
- from cmath import sqrt
- from dis import dis
- from io import SEEK_CUR
- import struct
- SCENE_FILE = "C:\\Users\\chand\\Desktop\\3DS\\hakadan_ch_info.zsi"
- with open(SCENE_FILE, 'rb') as scene_file:
- magic = scene_file.read(4)
- if magic != b"ZSI\x01":
- print("Invalid ZSI header")
- exit(1)
- # going to ignore alternate setups for now
- scene_file.seek(0x10)
- while(1):
- cmd1 = struct.unpack('<i', scene_file.read(4))[0]
- cmd2 = struct.unpack('<i', scene_file.read(4))[0]
- cmdType = cmd1 & 0xFF
- if cmdType == 0x14:
- print("found the break at " + format(scene_file.tell() - 8, '#X'))
- break
- if cmdType == 0x03:
- print("found the collision cmd at " + format(scene_file.tell() - 8, '#x'))
- print("collision header is at " + format(cmd2 + 0x10, '#x'))
- scene_file.seek(cmd2 + 0x10)
- box_min_x = struct.unpack('<h', scene_file.read(2))[0]
- box_min_y = struct.unpack('<h', scene_file.read(2))[0]
- box_min_z = struct.unpack('<h', scene_file.read(2))[0]
- box_max_x = struct.unpack('<h', scene_file.read(2))[0]
- box_max_y = struct.unpack('<h', scene_file.read(2))[0]
- box_max_z = struct.unpack('<h', scene_file.read(2))[0]
- numVtxs = struct.unpack('<h', scene_file.read(2))[0]
- numPolygons = struct.unpack('<h', scene_file.read(2))[0]
- print(str(numVtxs) + " vertices (" + format(numVtxs, '#x') + ")")
- print(str(numPolygons) + " polygons (" + format(numPolygons, '#x') + ")")
- numSurfaceTypes = struct.unpack('<h', scene_file.read(2))[0]
- numCamDataList = struct.unpack('<h', scene_file.read(2))[0]
- scene_file.seek(0x04, SEEK_CUR)
- vtxsOffset = struct.unpack('<i', scene_file.read(4))[0]
- polygonsOffset = struct.unpack('<i', scene_file.read(4))[0]
- #read the vtx data
- scene_file.seek(vtxsOffset + 0x10)
- vtxs = []
- for i in range(0, numVtxs):
- vtx_x = struct.unpack('<h', scene_file.read(2))[0]
- vtx_y = struct.unpack('<h', scene_file.read(2))[0]
- vtx_z = struct.unpack('<h', scene_file.read(2))[0]
- vtxs.append((- vtx_x / 100.0, vtx_z / 100.0, vtx_y / 100.0))
- #read the polys data
- scene_file.seek(polygonsOffset + 0x10)
- faces = []
- extensions = []
- num_small_y = 0
- for i in range(0, numPolygons):
- polyType = struct.unpack('<h', scene_file.read(2))[0]
- vtx1 = struct.unpack('<h', scene_file.read(2))[0] & 0x1FFF
- vtx2 = struct.unpack('<h', scene_file.read(2))[0] & 0x1FFF
- vtx3 = struct.unpack('<h', scene_file.read(2))[0]
- unk_08 = struct.unpack('<h', scene_file.read(2))[0]
- normal_x = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
- normal_y = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
- normal_z = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
- dist = struct.unpack('<f', scene_file.read(4))[0]
- faces.append((vtx1, vtx2, vtx3))
- #let's try to compute the rectangles that extend the face
- tri_vtxs = [vtx1, vtx2, vtx3]
- if normal_y > 0.00008:
- hull_points = []
- for (a, b) in ((0, 1), (1, 2), (2, 0)):
- vtx_1 = vtxs[tri_vtxs[a]]
- vtx_2 = vtxs[tri_vtxs[b]]
- vtx_1_x = - vtx_1[0] * 100.0
- vtx_1_z = vtx_1[1] * 100.0
- vtx_2_x = - vtx_2[0] * 100.0
- vtx_2_z = vtx_2[1] * 100.0
- edge_d_z = vtx_2_z - vtx_1_z
- edge_d_x = vtx_2_x - vtx_1_x
- if edge_d_z != 0 and edge_d_x != 0:
- edge_xz_slope = float(edge_d_z)/edge_d_x
- extend_rect_xz_slope = -1.0/edge_xz_slope
- extend_delta_z = 0.0 * math.sin(math.atan(extend_rect_xz_slope))
- extend_delta_x = 0.0 * math.cos(math.atan(extend_rect_xz_slope))
- extend_1_x = vtx_1_x + extend_delta_x
- extend_1_z = vtx_1_z + extend_delta_z
- extend_1_x_b = vtx_1_x - extend_delta_x
- extend_1_z_b = vtx_1_z - extend_delta_z
- extend_2_x = vtx_2_x + extend_delta_x
- extend_2_z = vtx_2_z + extend_delta_z
- extend_2_x_b = vtx_2_x - extend_delta_x
- extend_2_z_b = vtx_2_z - extend_delta_z
- extend_1_y = (((-(normal_x * extend_1_x)) - (normal_z * extend_1_z)) - dist) / normal_y
- extend_2_y = (((-(normal_x * extend_2_x)) - (normal_z * extend_2_z)) - dist) / normal_y
- extend_1_y_b = (((-(normal_x * extend_1_x_b)) - (normal_z * extend_1_z_b)) - dist) / normal_y
- extend_2_y_b = (((-(normal_x * extend_2_x_b)) - (normal_z * extend_2_z_b)) - dist) / normal_y
- extensions.append((tri_vtxs[a], tri_vtxs[b], (extend_1_x, extend_1_y, extend_1_z), (extend_2_x, extend_2_y, extend_2_z)))
- extensions.append((tri_vtxs[a], tri_vtxs[b], (extend_1_x_b, extend_1_y_b, extend_1_z_b), (extend_2_x_b, extend_2_y_b, extend_2_z_b)))
- # mag_sq = ((normal_x**2) + (normal_y**2) + (normal_z**2)) / (32767**2)
- # # print("type: {}".format(polyType))
- # # print("\tvertices: {}, {}, {}".format(vtx1, vtx2, vtx3))
- # # print("\tnormal: {}, {}, {}".format(normal_x, normal_y, normal_z))
- # # print("\tmag: {}".format(sqrt(mag_sq)))
- # # print("\tdist: {}\n".format(dist))
- # if normal_y > 0 and normal_y < 0x100:
- # num_small_y = num_small_y + 1
- # # print(dist, normal_y)
- # print("num small y: {}".format(num_small_y))
- # #render extensions
- print(len(extensions))
- for (v1, v2, (extend_1_x, extend_1_y, extend_1_z), (extend_2_x, extend_2_y, extend_2_z)) in extensions:
- # ext_index_1 = len(vtxs)
- # vtxs.append((extend_1_x_b / 100.0, extend_1_z_b / 100.0, extend_1_y_b / 100.0))
- ext_index_2 = len(vtxs)
- vtxs.append((- extend_1_x / 100.0, extend_1_z / 100.0, extend_1_y / 100.0))
- ext_index_3 = len(vtxs)
- vtxs.append((- extend_2_x / 100.0, extend_2_z / 100.0, extend_2_y / 100.0))
- # ext_index_4 = len(vtxs)
- # vtxs.append((extend_2_x_b / 100.0, extend_2_z_b / 100.0, extend_2_y_b / 100.0))
- # print("new face: ", v1, ext_index_2, ext_index_3, v2)
- # print("\t", extend_1_x / 100.0, extend_1_z / 100.0, extend_1_y / 100.0)
- # print("\t", extend_2_x / 100.0, extend_2_z / 100.0, extend_2_y / 100.0)
- faces.append((v1, ext_index_2, ext_index_3, v2))
- print(len(faces))
- create_mesh("botw", vtxs, [], faces, vertex_groups)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement