GodotSectors/Scripts/Space/SpaceRegion.gd

65 lines
1.7 KiB
GDScript

extends SpaceEntity
class_name SpaceRegion
@export var elevation: float = 0.0
@export var height: float = 100.0
func generate_geometry(space: Space) -> Node3D:
var geometry := MeshInstance3D.new()
geometry.name = name
var polygon = self.polygon
var polygon_pairs = Array()
for i in range(-1, polygon.size() - 1):
polygon_pairs.append([polygon[i], polygon[i+1]])
var vertices = PackedVector3Array()
var uvs = PackedVector2Array()
for line in polygon_pairs:
vertices.append(Vector3(
line[0][0] * space.unit_scale, elevation * space.unit_scale,
line[0][1] * space.unit_scale
))
vertices.append(Vector3(
line[1][0] * space.unit_scale, (elevation + height) * space.unit_scale,
line[1][1] * space.unit_scale,
))
vertices.append(Vector3(
line[1][0] * space.unit_scale, elevation * space.unit_scale,
line[1][1] * space.unit_scale,
))
vertices.append(Vector3(
line[1][0] * space.unit_scale, (elevation + height) * space.unit_scale,
line[1][1] * space.unit_scale,
))
vertices.append(Vector3(
line[0][0] * space.unit_scale, elevation * space.unit_scale,
line[0][1] * space.unit_scale,
))
vertices.append(Vector3(
line[0][0] * space.unit_scale, (elevation + height) * space.unit_scale,
line[0][1] * space.unit_scale,
))
uvs.append(Vector2(0, 1))
uvs.append(Vector2(1, 0))
uvs.append(Vector2(1, 1))
uvs.append(Vector2(1, 0))
uvs.append(Vector2(0, 1))
uvs.append(Vector2(0, 0))
var arrays = Array()
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices
arrays[Mesh.ARRAY_TEX_UV] = uvs
var mesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
mesh.surface_set_material(0, preload("res://Scenes/Material.tres"))
geometry.mesh = mesh
return geometry