GiantsEditReloaded/MeshInstance.gd

144 lines
4.2 KiB
GDScript3
Raw Normal View History

2020-04-15 13:45:56 +02:00
extends MeshInstance
var signature
var u0
var xoffset
var yoffset
var minheight
var maxheight
2020-04-17 17:39:48 +02:00
var heightscale
2020-04-15 13:45:56 +02:00
var xverticesnumber
var yverticesnumber
var stretch
var u1
var u2
var u3
var u4
var version
var u6
var u7
var u8
var tex
var edges
var file = File.new()
var vertices = []
var triangles = []
func _on_MeshInstance_ready():
readgti("res://default.gti")
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
for v in 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()
mesh = m
func fill():
# fill read values
2020-04-17 17:39:48 +02:00
# print("fill")
2020-04-15 13:45:56 +02:00
var p = 0
while p < xverticesnumber * yverticesnumber:
var b = file.get_8()
if b >= 0x80:
while b != 0x00:
if p >= xverticesnumber * yverticesnumber:
return
b = (b + 1) & 0xFF
p+=1
else:
while b != 0xff:
if p >= xverticesnumber * yverticesnumber:
return
if version != 7:
2020-04-17 17:39:48 +02:00
vertices[p][2] = file.get_float() * heightscale # height
2020-04-15 13:45:56 +02:00
vertices[p][3] = file.get_8() # triangulation
vertices[p][4] = file.get_8() # R
vertices[p][5] = file.get_8() # G
vertices[p][6] = file.get_8() # B
else:
2020-04-17 17:39:48 +02:00
vertices[p][2] = file.get_float() * heightscale # height
2020-04-15 13:45:56 +02:00
vertices[p][3] = file.get_8() # triangulation
vertices[p][4] = file.get_8() # R
vertices[p][5] = file.get_8() # G
vertices[p][6] = file.get_8() # B
var unk = file.get_8() # unk
# print("["+ str(vertices[p][0]) + ", "+ str(vertices[p][1])+", "+ str(vertices[p][2])+", "+ str(vertices[p][3])+", "+ str(vertices[p][4])+", "+ str(vertices[p][5])+", "+ str(vertices[p][6])+", "+str(b)+"]")
p+=1
b = (b - 1) & 0xFF
func readgti(filename):
file.open(filename, File.READ)
if not file.is_open():
get_tree().quit()
return
signature = file.get_32()
u0 = file.get_32()
xoffset = file.get_float()
yoffset = file.get_float()
minheight = file.get_float()
maxheight = file.get_float()
xverticesnumber = file.get_32()
yverticesnumber = file.get_32()
stretch = file.get_float()
2020-04-17 17:39:48 +02:00
stretch = 40.0
heightscale = stretch/40.0 # normal stretch
2020-04-15 13:45:56 +02:00
u1 = file.get_float()
u2 = file.get_float()
u3 = file.get_float()
u4 = file.get_float()
version = file.get_16()
u6 = file.get_16()
u7 = file.get_float()
u8 = file.get_float()
tex = file.get_buffer(32).get_string_from_ascii()
vertices.resize(xverticesnumber * yverticesnumber)
# initialize empty terrain
2020-04-17 17:39:48 +02:00
# print("init")
2020-04-15 13:45:56 +02:00
for y in range(yverticesnumber):
for x in range(xverticesnumber):
vertices[y * xverticesnumber + x] = []
vertices[y * xverticesnumber + x].append(xoffset + x * stretch) # 0: x
vertices[y * xverticesnumber + x].append(yoffset + y * stretch) # 1: y
vertices[y * xverticesnumber + x].append(minheight) # 2: height
vertices[y * xverticesnumber + x].append(0) # 3: triangulation value
vertices[y * xverticesnumber + x].append(0) # 4: R
vertices[y * xverticesnumber + x].append(0) # 5: G
vertices[y * xverticesnumber + x].append(0) # 6: B
fill()
file.close()
# sort triangles
2020-04-17 17:39:48 +02:00
# print("sort")
2020-04-15 13:45:56 +02:00
var i = 0
# triangles.resize(2*len(vertices))
for y in range(yverticesnumber-1):
for x in range(xverticesnumber-1):
var b = (vertices[y * xverticesnumber + x][3] & 7) % 128
if b == 4 or b == 5 or b == 7:
triangles.append(vertices[(y+1) * xverticesnumber + x])
triangles.append(vertices[y * xverticesnumber + x])
triangles.append(vertices[(y+1) * xverticesnumber + (x+1)])
if b == 5 or b == 3:
triangles.append(vertices[y * xverticesnumber + (x + 1)])
triangles.append(vertices[y * xverticesnumber + x])
triangles.append(vertices[(y + 1) * xverticesnumber + (x + 1)])
for y in range (yverticesnumber-1):
for x in range(xverticesnumber-1):
var b = (vertices[y * xverticesnumber + x][3] & 7) % 128
if b == 1 or b == 6:
triangles.append(vertices[(y + 1) * xverticesnumber + x])
triangles.append(vertices[y * xverticesnumber + x])
triangles.append(vertices[y * xverticesnumber + (x + 1)])
if b == 2 or b == 6:
triangles.append(vertices[y * xverticesnumber + (x + 1)])
triangles.append(vertices[(y + 1) * xverticesnumber + x])
triangles.append(vertices[(y + 1) * xverticesnumber + (x + 1)])