diff options
Diffstat (limited to 'extractor/gnd_format.gd')
-rw-r--r-- | extractor/gnd_format.gd | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/extractor/gnd_format.gd b/extractor/gnd_format.gd index ea8d965..65635cf 100644 --- a/extractor/gnd_format.gd +++ b/extractor/gnd_format.gd @@ -1,6 +1,9 @@ class_name GNDFormat +const MAP_TILE_SIZE := 10.0 + + ## Byte Length: 4 [br] ## GRAT var signature: String = "GRGN" @@ -120,6 +123,67 @@ static func from_bytes(bytes: ByteStream) -> GNDFormat: return gnd_format +func get_cubes() -> Array: + var cubes := [] + for cube in ground_mesh_cubes: + cubes.append({ + "cube": cube, + Vector3(0, 1, 0): { + "surface": surfaces[cube.upwards_facing_surface_index], + "mesh": surfaces[cube.upwards_facing_surface_index].get_mesh({ + Vector2(0, 0): Vector3(0, cube.top_left_height, 0), + Vector2(1, 0): Vector3(MAP_TILE_SIZE, cube.top_right_height, 0), + Vector2(1, 1): Vector3(MAP_TILE_SIZE, cube.bottom_right_height, MAP_TILE_SIZE), + Vector2(0, 1): Vector3(0, cube.bottom_left_height, MAP_TILE_SIZE), + }), + }, + }) + + return cubes + + +func convert(data_path: String) -> GridMap: + var grid_map := GridMap.new() + grid_map.cell_size = Vector3(GNDFormat.MAP_TILE_SIZE, GNDFormat.MAP_TILE_SIZE, GNDFormat.MAP_TILE_SIZE) + grid_map.cell_center_x = false + grid_map.cell_center_y = false + grid_map.cell_center_z = false + + grid_map.position = Vector3( + (width * MAP_TILE_SIZE) / 2.0, + 0, + -(height * MAP_TILE_SIZE) / 2.0, + ) + + var library := MeshLibrary.new() + grid_map.mesh_library = library + + var cubes := get_cubes() + + for x in width: + for y in height: + var cube = cubes[x + y * width] + for surface_type in [Vector3(0, 1, 0)]: + var mesh: ArrayMesh = cube[surface_type].mesh + var material := StandardMaterial3D.new() + material.albedo_texture = load( + "%s/data/texture/%s" % [ + data_path, + texture_paths[cube[surface_type].surface.texture_index] + ] + ) + + mesh.surface_set_material(0, material) + + var id := library.get_last_unused_item_id() + library.create_item(id) + library.set_item_mesh(id, mesh) + + grid_map.set_cell_item(Vector3(-x, 0, y), id) + + return grid_map + + class LightMapSlice: ## Byte Type: i32 [br] ## Byte Length: 4 @@ -229,6 +293,61 @@ class Surface: ) + func get_mesh(vertices: Dictionary[Vector2, Vector3]) -> ArrayMesh: + var surface_tool := SurfaceTool.new() + surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES) + + var uvs = get_uvs() + var color = get_vertex_color() + + var tiles_per_surface := 2 * 2 + + surface_tool.add_triangle_fan( + PackedVector3Array([ + vertices[Vector2(0, 0)], + vertices[Vector2(1, 0)], + vertices[Vector2(1, 1)], + ]), + PackedVector2Array([ + uvs[Vector2(0, 0)] * tiles_per_surface, + uvs[Vector2(1, 0)] * tiles_per_surface, + uvs[Vector2(1, 1)] * tiles_per_surface, + ]), + PackedColorArray([ + color, color, color, + ]) + ) + surface_tool.add_triangle_fan( + PackedVector3Array([ + vertices[Vector2(0, 0)], + vertices[Vector2(1, 1)], + vertices[Vector2(0, 1)], + ]), + PackedVector2Array([ + uvs[Vector2(0, 0)] * tiles_per_surface, + uvs[Vector2(1, 1)] * tiles_per_surface, + uvs[Vector2(0, 1)] * tiles_per_surface, + ]), + PackedColorArray([ + color, color, color, + ]) + ) + + return surface_tool.commit() + + + func get_uv_id() -> String: + # TODO: add more data? + return ( + "%d %d %d %d %d %d %d %d" % [ + u_top_left, v_top_left, + u_top_right, v_top_right, + u_bottom_right, v_bottom_right, + u_bottom_left, v_bottom_left + ] + ).md5_text() + + static func from_bytes(bytes: ByteStream) -> Surface: var surface = Surface.new() |