87 lines
3.2 KiB
GDScript
87 lines
3.2 KiB
GDScript
class_name X
|
|
|
|
static func LoadModel(filePath: String):
|
|
var sillyPath = str("res://", filePath.replace("/Map/", "/map/"))
|
|
var file = FileAccess.open(sillyPath, FileAccess.READ)
|
|
|
|
# Do a case insensitive lookup only if we have to, it's more expensive.
|
|
if file == null:
|
|
file = FileAccess.open(Utils.GetCaseiFileName(sillyPath), FileAccess.READ)
|
|
|
|
var fileLines = file.get_as_text(true).split("\n")
|
|
|
|
var meshDataStart = false
|
|
var indexDataStart = false
|
|
var meshTextureCoordsStart = false
|
|
var textureNameStart = false
|
|
var textureName = ""
|
|
var texture: Texture2D = null
|
|
var meshDataLength = -1
|
|
var uvsDataLength = -1
|
|
var indexDataLength = -1
|
|
var verts = PackedVector3Array()
|
|
var uvs = PackedVector2Array()
|
|
var indexes = PackedInt32Array()
|
|
for line in fileLines:
|
|
if meshDataStart or indexDataStart:
|
|
if meshDataStart and meshDataLength == -1:
|
|
meshDataLength = int(line.strip_edges().split(";")[0])
|
|
elif indexDataStart and indexDataLength == -1:
|
|
indexDataLength = int(line.strip_edges().split(";")[0])
|
|
elif meshDataStart:
|
|
var pointData = line.strip_edges().split(";")
|
|
if pointData.size() == 5:
|
|
meshDataStart = false
|
|
indexDataStart = true
|
|
verts.push_back(Vector3(-float(pointData[0]), float(pointData[1]), float(pointData[2])))
|
|
elif indexDataStart:
|
|
var indexParts = line.strip_edges().split(";")
|
|
if indexParts.size() == 4:
|
|
indexDataStart = false
|
|
var indexData = indexParts[1].split(",")
|
|
var amount = int(indexParts[0])
|
|
for i in range(amount):
|
|
indexes.push_back(int(indexData[i]))
|
|
elif textureNameStart:
|
|
textureName = line.strip_edges().split(";")[0].replace("\"", "")
|
|
var texturePath = str("GFX/map/Props/", textureName)
|
|
texture = Global.GetTextureFromCache(texturePath)
|
|
if not texture:
|
|
texture = Global.LoadTexture(texturePath)
|
|
textureNameStart = false
|
|
elif meshTextureCoordsStart:
|
|
if uvsDataLength == -1:
|
|
uvsDataLength = int(line.strip_edges().split(";")[0])
|
|
else:
|
|
var uvParts = line.strip_edges().split(";")
|
|
if uvParts.size() == 4:
|
|
meshTextureCoordsStart = false
|
|
uvs.push_back(Vector2(float(uvParts[0]), float(uvParts[1])))
|
|
|
|
if line.strip_edges().contains("Mesh "):
|
|
meshDataStart = true
|
|
elif line.strip_edges().contains("TextureFilename"):
|
|
textureNameStart = true
|
|
elif line.strip_edges().contains("MeshTextureCoords"):
|
|
meshTextureCoordsStart = true
|
|
|
|
var mesh = MeshInstance3D.new()
|
|
var arr_mesh = ArrayMesh.new()
|
|
var arr = []
|
|
arr.resize(Mesh.ARRAY_MAX)
|
|
|
|
arr[Mesh.ARRAY_VERTEX]=verts
|
|
arr[Mesh.ARRAY_TEX_UV]=uvs
|
|
arr[Mesh.ARRAY_INDEX]=indexes
|
|
|
|
var meshInstance = MeshInstance3D.new()
|
|
arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr)
|
|
meshInstance.mesh = arr_mesh
|
|
var mat = StandardMaterial3D.new()
|
|
#mat.albedo_color = Color(randi() % 255 / 255.0, randi() % 255 / 255.0, randi() % 255 / 255.0)
|
|
mat.albedo_texture = texture
|
|
#mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA_DEPTH_PRE_PASS if activeAlbedoHasAlpha else BaseMaterial3D.TRANSPARENCY_DISABLED
|
|
meshInstance.set_surface_override_material(0, mat)
|
|
meshInstance.create_trimesh_collision()
|
|
meshInstance.name = str(filePath, "_", Time.get_unix_time_from_system())
|
|
return meshInstance
|