Advertisement
Gamestabled

blender_collision_script

Oct 28th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. import bpy
  2. import math
  3.  
  4. # Create new mesh and a new object
  5. def create_mesh(ob_name, coords, edges, faces, vertex_groups):
  6. me = bpy.data.meshes.new(ob_name + "Mesh")
  7. ob = bpy.data.objects.new(ob_name, me)
  8.  
  9. me.from_pydata(coords, edges, faces)
  10. me.update()
  11.  
  12. bpy.context.collection.objects.link(ob)
  13. return ob
  14.  
  15. from cmath import sqrt
  16. from dis import dis
  17. from io import SEEK_CUR
  18. import struct
  19.  
  20. SCENE_FILE = "C:\\Users\\chand\\Desktop\\3DS\\hakadan_ch_info.zsi"
  21.  
  22. with open(SCENE_FILE, 'rb') as scene_file:
  23. magic = scene_file.read(4)
  24. if magic != b"ZSI\x01":
  25. print("Invalid ZSI header")
  26. exit(1)
  27.  
  28. # going to ignore alternate setups for now
  29. scene_file.seek(0x10)
  30. while(1):
  31. cmd1 = struct.unpack('<i', scene_file.read(4))[0]
  32. cmd2 = struct.unpack('<i', scene_file.read(4))[0]
  33. cmdType = cmd1 & 0xFF
  34.  
  35. if cmdType == 0x14:
  36. print("found the break at " + format(scene_file.tell() - 8, '#X'))
  37. break
  38.  
  39. if cmdType == 0x03:
  40. print("found the collision cmd at " + format(scene_file.tell() - 8, '#x'))
  41. print("collision header is at " + format(cmd2 + 0x10, '#x'))
  42. scene_file.seek(cmd2 + 0x10)
  43.  
  44. box_min_x = struct.unpack('<h', scene_file.read(2))[0]
  45. box_min_y = struct.unpack('<h', scene_file.read(2))[0]
  46. box_min_z = struct.unpack('<h', scene_file.read(2))[0]
  47.  
  48. box_max_x = struct.unpack('<h', scene_file.read(2))[0]
  49. box_max_y = struct.unpack('<h', scene_file.read(2))[0]
  50. box_max_z = struct.unpack('<h', scene_file.read(2))[0]
  51.  
  52. numVtxs = struct.unpack('<h', scene_file.read(2))[0]
  53. numPolygons = struct.unpack('<h', scene_file.read(2))[0]
  54.  
  55. print(str(numVtxs) + " vertices (" + format(numVtxs, '#x') + ")")
  56. print(str(numPolygons) + " polygons (" + format(numPolygons, '#x') + ")")
  57.  
  58. numSurfaceTypes = struct.unpack('<h', scene_file.read(2))[0]
  59. numCamDataList = struct.unpack('<h', scene_file.read(2))[0]
  60.  
  61. scene_file.seek(0x04, SEEK_CUR)
  62. vtxsOffset = struct.unpack('<i', scene_file.read(4))[0]
  63. polygonsOffset = struct.unpack('<i', scene_file.read(4))[0]
  64.  
  65. #read the vtx data
  66. scene_file.seek(vtxsOffset + 0x10)
  67. vtxs = []
  68. for i in range(0, numVtxs):
  69. vtx_x = struct.unpack('<h', scene_file.read(2))[0]
  70. vtx_y = struct.unpack('<h', scene_file.read(2))[0]
  71. vtx_z = struct.unpack('<h', scene_file.read(2))[0]
  72. vtxs.append((- vtx_x / 100.0, vtx_z / 100.0, vtx_y / 100.0))
  73.  
  74. #read the polys data
  75. scene_file.seek(polygonsOffset + 0x10)
  76. faces = []
  77.  
  78. extensions = []
  79. num_small_y = 0
  80.  
  81. for i in range(0, numPolygons):
  82. polyType = struct.unpack('<h', scene_file.read(2))[0]
  83. vtx1 = struct.unpack('<h', scene_file.read(2))[0] & 0x1FFF
  84. vtx2 = struct.unpack('<h', scene_file.read(2))[0] & 0x1FFF
  85. vtx3 = struct.unpack('<h', scene_file.read(2))[0]
  86. unk_08 = struct.unpack('<h', scene_file.read(2))[0]
  87. normal_x = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
  88. normal_y = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
  89. normal_z = struct.unpack('<h', scene_file.read(2))[0] * (1.0 / 32767.0)
  90. dist = struct.unpack('<f', scene_file.read(4))[0]
  91.  
  92. faces.append((vtx1, vtx2, vtx3))
  93.  
  94. #let's try to compute the rectangles that extend the face
  95. tri_vtxs = [vtx1, vtx2, vtx3]
  96. if normal_y > 0.00008:
  97.  
  98. hull_points = []
  99.  
  100. for (a, b) in ((0, 1), (1, 2), (2, 0)):
  101. vtx_1 = vtxs[tri_vtxs[a]]
  102. vtx_2 = vtxs[tri_vtxs[b]]
  103.  
  104. vtx_1_x = - vtx_1[0] * 100.0
  105. vtx_1_z = vtx_1[1] * 100.0
  106. vtx_2_x = - vtx_2[0] * 100.0
  107. vtx_2_z = vtx_2[1] * 100.0
  108.  
  109. edge_d_z = vtx_2_z - vtx_1_z
  110. edge_d_x = vtx_2_x - vtx_1_x
  111.  
  112. if edge_d_z != 0 and edge_d_x != 0:
  113. edge_xz_slope = float(edge_d_z)/edge_d_x
  114. extend_rect_xz_slope = -1.0/edge_xz_slope
  115. extend_delta_z = 0.0 * math.sin(math.atan(extend_rect_xz_slope))
  116. extend_delta_x = 0.0 * math.cos(math.atan(extend_rect_xz_slope))
  117.  
  118. extend_1_x = vtx_1_x + extend_delta_x
  119. extend_1_z = vtx_1_z + extend_delta_z
  120.  
  121. extend_1_x_b = vtx_1_x - extend_delta_x
  122. extend_1_z_b = vtx_1_z - extend_delta_z
  123.  
  124. extend_2_x = vtx_2_x + extend_delta_x
  125. extend_2_z = vtx_2_z + extend_delta_z
  126.  
  127. extend_2_x_b = vtx_2_x - extend_delta_x
  128. extend_2_z_b = vtx_2_z - extend_delta_z
  129.  
  130. extend_1_y = (((-(normal_x * extend_1_x)) - (normal_z * extend_1_z)) - dist) / normal_y
  131. extend_2_y = (((-(normal_x * extend_2_x)) - (normal_z * extend_2_z)) - dist) / normal_y
  132.  
  133. extend_1_y_b = (((-(normal_x * extend_1_x_b)) - (normal_z * extend_1_z_b)) - dist) / normal_y
  134. extend_2_y_b = (((-(normal_x * extend_2_x_b)) - (normal_z * extend_2_z_b)) - dist) / normal_y
  135.  
  136. 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)))
  137. 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)))
  138.  
  139.  
  140.  
  141.  
  142.  
  143. # mag_sq = ((normal_x**2) + (normal_y**2) + (normal_z**2)) / (32767**2)
  144.  
  145. # # print("type: {}".format(polyType))
  146. # # print("\tvertices: {}, {}, {}".format(vtx1, vtx2, vtx3))
  147. # # print("\tnormal: {}, {}, {}".format(normal_x, normal_y, normal_z))
  148. # # print("\tmag: {}".format(sqrt(mag_sq)))
  149. # # print("\tdist: {}\n".format(dist))
  150.  
  151. # if normal_y > 0 and normal_y < 0x100:
  152. # num_small_y = num_small_y + 1
  153. # # print(dist, normal_y)
  154.  
  155. # print("num small y: {}".format(num_small_y))
  156.  
  157. # #render extensions
  158. print(len(extensions))
  159. for (v1, v2, (extend_1_x, extend_1_y, extend_1_z), (extend_2_x, extend_2_y, extend_2_z)) in extensions:
  160. # ext_index_1 = len(vtxs)
  161. # vtxs.append((extend_1_x_b / 100.0, extend_1_z_b / 100.0, extend_1_y_b / 100.0))
  162. ext_index_2 = len(vtxs)
  163. vtxs.append((- extend_1_x / 100.0, extend_1_z / 100.0, extend_1_y / 100.0))
  164. ext_index_3 = len(vtxs)
  165. vtxs.append((- extend_2_x / 100.0, extend_2_z / 100.0, extend_2_y / 100.0))
  166. # ext_index_4 = len(vtxs)
  167. # vtxs.append((extend_2_x_b / 100.0, extend_2_z_b / 100.0, extend_2_y_b / 100.0))
  168.  
  169.  
  170. # print("new face: ", v1, ext_index_2, ext_index_3, v2)
  171. # print("\t", extend_1_x / 100.0, extend_1_z / 100.0, extend_1_y / 100.0)
  172. # print("\t", extend_2_x / 100.0, extend_2_z / 100.0, extend_2_y / 100.0)
  173. faces.append((v1, ext_index_2, ext_index_3, v2))
  174.  
  175. print(len(faces))
  176. create_mesh("botw", vtxs, [], faces, vertex_groups)
  177. break
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement