GiantsEditReloaded/terrainmanager.gd

135 lines
3.8 KiB
GDScript

extends Node
func get_terrain(gck_map: String) -> MeshInstance:
var finalmesh = MeshInstance.new()
var gdunzip = load("res://addons/gdunzip/gdunzip.gd").new()
var loaded = gdunzip.load(gck_map)
if loaded:
var gti_file = null
for f in gdunzip.files:
if f.ends_with(".gti"):
gti_file = f
if not gti_file:
print("Could not find any gti file in "+gck_map)
return finalmesh
var terrain_bytes = gdunzip.uncompress(gti_file)
if not terrain_bytes:
print("Failed to decompress default.gti")
return finalmesh
var terrain = load("res://terrain.gd").new()
var stream = StreamPeerBuffer.new()
stream.data_array = terrain_bytes
terrain._readgti(stream)
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
for v in terrain.triangles:
st.add_color(Color(v[4] / 255.0, v[5] / 255.0, v[6] / 255.0))
st.add_vertex(Vector3(v[0], v[2], v[1]))
var m = st.commit()
finalmesh.mesh = m
# create heightmap and colormap
var heightmap = Image.new()
var colormap = Image.new()
heightmap.create(terrain.xverticesnumber, terrain.yverticesnumber, false, Image.FORMAT_RGBA8)
colormap.create(terrain.xverticesnumber, terrain.yverticesnumber, false, Image.FORMAT_RGBA8)
heightmap.lock()
colormap.lock()
var minheight = terrain.minheight
var maxheight = terrain.maxheight
for x in range(terrain.xverticesnumber):
for y in range(terrain.yverticesnumber):
var height = terrain.vertices[y * terrain.xverticesnumber + x][2]
var greypercentage = (height-minheight)/(maxheight-minheight)
heightmap.set_pixel(x, y, Color(greypercentage, greypercentage , greypercentage))
var r = terrain.vertices[y * terrain.xverticesnumber + x][4]
var g = terrain.vertices[y * terrain.xverticesnumber + x][5]
var b = terrain.vertices[y * terrain.xverticesnumber + x][6]
colormap.set_pixel(x, y, Color8(r, g, b))
heightmap.unlock()
colormap.unlock()
heightmap.save_png("res://heightmap.png")
colormap.save_png("res://colormap.png")
else:
print("Could not open map "+gck_map)
return finalmesh
var mat = SpatialMaterial.new()
mat.vertex_color_use_as_albedo = true
mat.params_cull_mode = SpatialMaterial.CULL_DISABLED
finalmesh.material_override = mat
return finalmesh
func read_pstring(s: StreamPeerBuffer) -> String:
var res = ''
var c = s.get_u8()
while c != 0:
var a = PoolByteArray([c])
res+=a.get_string_from_ascii()
return res
func load_bin(stream: StreamPeerBuffer) -> void:
pass
func read_chunk(stream: StreamPeerBuffer, chunktype: int):
match(chunktype):
0x17:
var scale = stream.get_float()
0x1d:
var outdome_texture = stream.get_string(16)
0x1e:
var dome_texture = stream.get_string(16)
0x1f:
var dome_edge_texture = stream.get_string(16)
0x20:
var WFall1Tex = stream.get_string(16)
0x21:
var WFall2Tex = stream.get_string(16)
0x22:
var WFall3Tex = stream.get_string(16)
0x23:
var space_line_texture = stream.get_string(16)
0x24:
var space_texture = stream.get_string(16)
0x25:
var sea_texture = stream.get_string(16)
0x26:
var glow_texture = stream.get_string(16)
0x27:
var teleport = {}
teleport["Index"] = stream.get_u8()
teleport["X"] = stream.get_float()
teleport["Y"] = stream.get_float()
teleport["Z"] = stream.get_float()
teleport["Angle"] = stream.get_float()
0x28:
var r = stream.get_u8()
var g = stream.get_u8()
var b = stream.get_u8()
var sun_color = [r, g, b]
0x29:
var fog = {}
fog["NearDistance"] = stream.get_float()
fog["FarDistance"] = stream.get_float()
var r = stream.get_u8()
var g = stream.get_u8()
var b = stream.get_u8()
fog["Color"] = [r, g, b]
0x2a:
var type = stream.get_32()
var x = stream.get_float()
var y = stream.get_float()
var z = stream.get_float()
var angle = stream.get_float()
0x2c:
var music = stream.get_string(32)