immersive-home/app/content/system/house/room/room.gd

130 lines
2.8 KiB
GDScript3
Raw Normal View History

extends Node3D
2023-12-19 00:15:48 +02:00
@onready var wall_corners = $Ceiling/WallCorners
@onready var wall_edges = $Ceiling/WallEdges
2023-12-20 02:54:22 +02:00
@onready var wall_mesh: MeshInstance3D = $WallMesh
@onready var ceiling_mesh: MeshInstance3D = $CeilingMesh
@onready var wall_collisions = $WallCollisions
2023-12-19 00:15:48 +02:00
@onready var room_floor = $Floor
@onready var room_ceiling = $Ceiling
2023-12-19 00:15:48 +02:00
@onready var state_machine = $StateMachine
2023-12-19 00:15:48 +02:00
var editable: bool = false:
set(value):
2024-03-21 11:51:58 +02:00
editable = value
2024-03-21 14:29:10 +02:00
if !is_inside_tree(): return
if editable:
2023-12-19 00:15:48 +02:00
state_machine.change_to("Edit")
else:
2023-12-19 00:15:48 +02:00
state_machine.change_to("View")
2024-03-21 14:29:10 +02:00
func _ready():
Update.props(self, ["editable"])
2023-12-20 02:54:22 +02:00
func has_point(point: Vector3) -> bool:
2024-01-16 16:00:30 +02:00
return get_aabb().has_point(point)
2024-01-16 16:00:30 +02:00
func get_aabb():
2024-01-27 16:13:43 +02:00
var room_store = Store.house.get_room(name)
2024-03-12 12:35:15 +02:00
if room_store == null:
return AABB()
2024-01-27 16:13:43 +02:00
var corners = room_store.corners
if corners.size() == 0:
2024-01-16 16:00:30 +02:00
return AABB()
2024-01-27 16:13:43 +02:00
var min_pos = Vector3(corners[0].x, 0, corners[0].y)
var max_pos = min_pos
2024-01-16 16:00:30 +02:00
2024-01-27 16:13:43 +02:00
for corner in corners:
min_pos.x = min(min_pos.x, corner.x)
min_pos.z = min(min_pos.z, corner.y)
2024-01-16 16:00:30 +02:00
2024-01-27 16:13:43 +02:00
max_pos.x = max(max_pos.x, corner.x)
max_pos.z = max(max_pos.z, corner.y)
2024-01-16 16:00:30 +02:00
2024-01-27 16:13:43 +02:00
min_pos.y = 0
max_pos.y = room_store.height
2024-01-16 16:00:30 +02:00
2024-01-25 17:29:33 +02:00
return AABB(to_global(min_pos), to_global(max_pos) - to_global(min_pos))
2024-01-27 16:13:43 +02:00
static func generate_wall_mesh(room_store: Dictionary):
if room_store.corners.size() < 2:
return null
var st = SurfaceTool.new()
var wall_up = Vector3.UP * room_store.height
st.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP)
for corner in room_store.corners:
var corner3D = Vector3(corner.x, 0, corner.y)
st.add_vertex(corner3D)
st.add_vertex(corner3D + wall_up)
var first_corner = Vector3(room_store.corners[0].x, 0, room_store.corners[0].y)
st.add_vertex(first_corner)
st.add_vertex(first_corner + wall_up)
st.index()
st.generate_normals()
st.generate_tangents()
var mesh = st.commit()
return mesh
static func generate_ceiling_mesh(room_store: Dictionary):
var points: PackedVector2Array = PackedVector2Array()
var edges: PackedInt32Array = PackedInt32Array()
var triangles: PackedInt32Array
var corners = room_store.corners
if corners.size() < 3:
return null
for i in range(corners.size()):
var corner = corners[i]
points.append(Vector2(corner.x, corner.y))
edges.append(i)
edges.append((i + 1) % corners.size())
var cdt: ConstrainedTriangulation = ConstrainedTriangulation.new()
cdt.init(true, true, 0.1)
cdt.insert_vertices(points)
cdt.insert_edges(edges)
cdt.erase_outer_triangles()
points = cdt.get_all_vertices()
triangles = cdt.get_all_triangles()
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
for i in range(points.size()):
st.add_vertex(Vector3(points[i].x, 0, points[i].y))
for i in range(triangles.size()):
st.add_index(triangles[i])
2024-01-25 17:29:33 +02:00
2024-01-27 16:13:43 +02:00
st.index()
st.generate_normals()
st.generate_tangents()
2024-01-25 17:29:33 +02:00
2024-01-27 16:13:43 +02:00
var mesh = st.commit()
2024-01-25 17:29:33 +02:00
2024-01-27 16:13:43 +02:00
return mesh