generate mesh for doors

This commit is contained in:
Nitwel 2024-05-29 17:58:46 +02:00
parent f3e2d2e9ff
commit cd38985510
10 changed files with 326 additions and 16 deletions

View File

@ -1,6 +1,8 @@
extends Node3D
const ConstructRoomMesh = preload ("res://lib/utils/mesh/construct_room_mesh.gd")
const ConstructDoorMesh = preload ("res://lib/utils/mesh/construct_door_mesh.gd")
const RoomMaterial = preload ("res://content/system/house/room/states/room.tres")
@onready var wall_corners = $Ceiling/WallCorners
@onready var wall_edges = $Ceiling/WallEdges
@ -10,6 +12,7 @@ const ConstructRoomMesh = preload ("res://lib/utils/mesh/construct_room_mesh.gd"
@onready var room_floor = $Floor
@onready var room_ceiling = $Ceiling
@onready var doors = $Doors
@onready var state_machine: StateMachine = $StateMachine
@ -84,3 +87,29 @@ static func generate_ceiling_mesh(room: Dictionary):
var corners = room.corners
return ConstructRoomMesh.generate_ceiling_mesh(corners)
func generate_doors_mesh():
for door in Store.house.state.doors:
if door.room1 != name:
continue
var door_mesh = MeshInstance3D.new()
doors.add_child(door_mesh)
if Geometry2D.is_polygon_clockwise([
Vector2(door.room1_position1.x, door.room1_position1.z),
Vector2(door.room2_position1.x, door.room2_position1.z),
Vector2(door.room2_position2.x, door.room2_position2.z),
Vector2(door.room1_position2.x, door.room1_position2.z)
]):
door_mesh.mesh = ConstructDoorMesh.generate_door_mesh(door.room1_position2, door.room2_position2, door.room2_position1, door.room1_position1)
else:
door_mesh.mesh = ConstructDoorMesh.generate_door_mesh(door.room1_position1, door.room2_position1, door.room2_position2, door.room1_position2)
door_mesh.material_override = RoomMaterial
func clear_doors_mesh():
for door in doors.get_children():
doors.remove_child(door)
door.queue_free()

View File

@ -1,24 +1,14 @@
[gd_scene load_steps=12 format=3 uid="uid://bswgmclohuqui"]
[gd_scene load_steps=9 format=3 uid="uid://bswgmclohuqui"]
[ext_resource type="Script" path="res://content/system/house/room/room.gd" id="1_fccq0"]
[ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_ugebq"]
[ext_resource type="Material" uid="uid://xehjkoeofqc" path="res://content/system/house/room/room_next.tres" id="3_4q8ik"]
[ext_resource type="Shader" path="res://content/system/house/room/room.gdshader" id="3_lwge0"]
[ext_resource type="Material" uid="uid://d0i5oae6evn08" path="res://content/system/house/room/states/room.tres" id="3_fmi1g"]
[ext_resource type="Script" path="res://lib/utils/state_machine/state_machine.gd" id="4_nbbo6"]
[ext_resource type="Script" path="res://content/system/house/room/states/view.gd" id="6_g066t"]
[ext_resource type="Script" path="res://content/system/house/room/states/edit.gd" id="7_ap14h"]
[sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_08sv0"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_rqpss"]
render_priority = -100
next_pass = ExtResource("3_4q8ik")
shader = ExtResource("3_lwge0")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_1hn7n"]
render_priority = -100
shader = ExtResource("3_lwge0")
[sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ap613"]
plane = Plane(0, -1, 0, 0)
@ -36,10 +26,10 @@ shape = SubResource("WorldBoundaryShape3D_08sv0")
script = ExtResource("1_ugebq")
[node name="WallMesh" type="MeshInstance3D" parent="."]
material_override = SubResource("ShaderMaterial_rqpss")
material_override = ExtResource("3_fmi1g")
[node name="CeilingMesh" type="MeshInstance3D" parent="."]
material_override = SubResource("ShaderMaterial_1hn7n")
material_override = ExtResource("3_fmi1g")
[node name="WallCollision" type="StaticBody3D" parent="."]
collision_layer = 24
@ -76,4 +66,6 @@ script = ExtResource("6_g066t")
[node name="Edit" type="Node" parent="StateMachine"]
script = ExtResource("7_ap14h")
[node name="Doors" type="Node3D" parent="."]
[node name="Entities" type="Node3D" parent="."]

View File

@ -0,0 +1,9 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://d0i5oae6evn08"]
[ext_resource type="Material" uid="uid://xehjkoeofqc" path="res://content/system/house/room/room_next.tres" id="1_irw15"]
[ext_resource type="Shader" path="res://content/system/house/room/room.gdshader" id="2_hhffe"]
[resource]
render_priority = -100
next_pass = ExtResource("1_irw15")
shader = ExtResource("2_hhffe")

View File

@ -30,10 +30,14 @@ func _on_enter():
wall_collisions.backface_collision = true
room.wall_collision.shape = wall_collisions
room.generate_doors_mesh()
func _on_leave():
room.room_ceiling.get_node("CollisionShape3D").disabled = true
room.room_floor.get_node("CollisionShape3D").disabled = true
room.wall_collision.shape = null
room.wall_mesh.mesh = null
room.ceiling_mesh.mesh = null
room.ceiling_mesh.mesh = null
room.clear_doors_mesh()

View File

@ -5,3 +5,8 @@
[resource]
render_priority = 0
shader = ExtResource("1_sbr3e")
shader_parameter/data = null
shader_parameter/data_size = null
shader_parameter/alpha = 0.3
shader_parameter/roughness = 0.85
shader_parameter/edge_color = Color(0, 0, 0, 1)

View File

@ -1,6 +1,7 @@
extends Node3D
const ConstructRoomMesh = preload ("res://lib/utils/mesh/construct_room_mesh.gd")
const ConstructDoorMesh = preload ("res://lib/utils/mesh/construct_door_mesh.gd")
const wall_material = preload ("./mini_wall.tres")
const humidity_gradient = preload ("./humid_gradient.tres")
const temperature_gradient = preload ("./temp_gradient.tres")
@ -81,6 +82,25 @@ func _ready():
walls_mesh.set_layer_mask_value(2, true)
floor_mesh.set_layer_mask_value(2, true)
for door in Store.house.state.doors:
var door_mesh=MeshInstance3D.new()
model.add_child(door_mesh)
if Geometry2D.is_polygon_clockwise([
Vector2(door.room1_position1.x, door.room1_position1.z),
Vector2(door.room2_position1.x, door.room2_position1.z),
Vector2(door.room2_position2.x, door.room2_position2.z),
Vector2(door.room1_position2.x, door.room1_position2.z)
]):
door_mesh.mesh=ConstructDoorMesh.generate_door_mesh(door.room1_position2, door.room2_position2, door.room2_position1, door.room1_position1)
else:
door_mesh.mesh=ConstructDoorMesh.generate_door_mesh(door.room1_position1, door.room2_position1, door.room2_position2, door.room1_position2)
door_mesh.material_override=wall_material
door_mesh.set_layer_mask_value(2, true)
)
# Update Size

View File

@ -0,0 +1,163 @@
## Generate a door mesh between two rooms based on their upper left and right corners
static func generate_door_mesh(room1_pos1: Vector3, room2_pos1: Vector3, room2_pos2: Vector3, room1_pos2: Vector3):
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT)
var points = [room1_pos1, room2_pos1, room2_pos2, room1_pos2, Vector3(room1_pos1.x, 0, room1_pos1.z), Vector3(room2_pos1.x, 0, room2_pos1.z), Vector3(room2_pos2.x, 0, room2_pos2.z), Vector3(room1_pos2.x, 0, room1_pos2.z)]
# Add top face
add_quad(st, points, 0, 1, 2, 3)
# Add bottom face
add_quad(st, points, 5, 4, 7, 6)
# Add side faces
add_quad(st, points, 0, 4, 5, 1)
add_quad(st, points, 2, 6, 7, 3)
st.deindex()
st.generate_normals()
st.generate_tangents()
return st.commit()
static func generate_door_mesh_grid(room1_pos1: Vector3, room2_pos1: Vector3, room2_pos2: Vector3, room1_pos2: Vector3, grid:=0.1):
var st = SurfaceTool.new()
var room1_pos1_bottom = Vector3(room1_pos1.x, 0, room1_pos1.z)
var room2_pos1_bottom = Vector3(room2_pos1.x, 0, room2_pos1.z)
var room2_pos2_bottom = Vector3(room2_pos2.x, 0, room2_pos2.z)
var room1_pos2_bottom = Vector3(room1_pos2.x, 0, room1_pos2.z)
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT)
# Add top
add_quad_grid(st, room1_pos1, room1_pos2, room2_pos2, room2_pos1, false, grid)
# Add bottom
add_quad_grid(st, room1_pos1_bottom, room1_pos2_bottom, room2_pos2_bottom, room2_pos1_bottom, true, grid)
# Add side faces
add_quad_grid(st, room1_pos1, room1_pos1_bottom, room2_pos1_bottom, room2_pos1, true, grid)
add_quad_grid(st, room1_pos2, room1_pos2_bottom, room2_pos2_bottom, room2_pos2, false, grid)
st.deindex()
st.generate_normals()
st.generate_tangents()
return st.commit()
static func add_quad(st: SurfaceTool, points, index1: int, index2: int, index3: int, index4: int):
st.set_uv(Vector2(0.0, 0.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index1])
st.set_uv(Vector2(1.0, 1.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index3])
st.set_uv(Vector2(1.0, 0.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index2])
st.set_uv(Vector2(0.0, 0.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index1])
st.set_uv(Vector2(0.0, 1.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index4])
st.set_uv(Vector2(1.0, 1.0))
st.set_custom(0, Color(2.0, 2.0, 0, 0))
st.add_vertex(points[index3])
static func add_quad_grid(st: SurfaceTool, p1: Vector3, p2: Vector3, p3: Vector3, p4: Vector3, flipped: bool=false, grid:=1.0):
var d1 = (p2 - p1).normalized()
var d2 = (p4 - p1).normalized()
var n1 = d1.cross(d2).normalized()
var transform_local = Transform3D(d1, n1, d2, p1)
var lp1 = transform_local.affine_inverse() * p1
var lp2 = transform_local.affine_inverse() * p2
var lp3 = transform_local.affine_inverse() * p3
var lp4 = transform_local.affine_inverse() * p4
lp1 = Vector2(lp1.x, lp1.z)
lp2 = Vector2(lp2.x, lp2.z)
lp3 = Vector2(lp3.x, lp3.z)
lp4 = Vector2(lp4.x, lp4.z)
var points = PackedVector2Array()
# Generate edges
var edge_points = [lp1, lp2, lp3, lp4]
for i in range(4):
var delta: Vector2 = edge_points[(i + 1) % 4] - edge_points[i]
var steps = ceil(delta.length() / grid)
for j in range(steps):
var p = edge_points[i] + delta.normalized() * j * grid
points.push_back(p)
var edges = PackedInt32Array()
for k in range(points.size()):
edges.append(k)
edges.append((k + 1) % points.size())
# Generate points
var rect = Rect2()
rect = rect.expand(lp1)
rect = rect.expand(lp2)
rect = rect.expand(lp3)
rect = rect.expand(lp4)
var ls = rect.position
var ld1 = rect.size.x
var ld2 = rect.size.y
var steps = Vector2(ld1 / grid, ld2 / grid).ceil() + Vector2(2, 2)
for i in range(steps.x):
for j in range(steps.y):
var p = Vector2(i, j) * grid + ls - Vector2(0.5, 0.5) * grid
points.push_back(p)
# Triangulate
var cdt: ConstrainedTriangulation = ConstrainedTriangulation.new()
cdt.init(true, true, 0.01)
cdt.insert_vertices(points)
cdt.insert_edges(edges)
cdt.erase_outer_triangles()
points = cdt.get_all_vertices()
var triangles: PackedInt32Array = cdt.get_all_triangles()
var points3D = PackedVector3Array()
for i in range(points.size()):
var pos3D = transform_local * Vector3(points[i].x, 0, points[i].y)
points3D.push_back(pos3D)
if flipped:
for i in range(triangles.size() / 3):
var temp = triangles[i * 3]
triangles[i * 3] = triangles[i * 3 + 1]
triangles[i * 3 + 1] = temp
for i in range(triangles.size()):
st.set_uv(Vector2(0.5, 0.5))
st.set_custom(0, Color(1.0, 1.0, 0, 0))
st.add_vertex(points3D[triangles[i]])

View File

@ -46,7 +46,8 @@ enabled=PackedStringArray("res://addons/godot-xr-tools/plugin.cfg")
folder_colors={
"res://assets/": "blue",
"res://content/": "teal",
"res://lib/": "yellow"
"res://lib/": "yellow",
"res://test/": "purple"
}
[filesystem]

View File

@ -0,0 +1,15 @@
@tool
extends Node3D
const ConstructDoorMesh = preload ("res://lib/utils/mesh/construct_door_mesh.gd")
@onready var door_mesh = $DoorMesh
@onready var door_mesh_grid = $DoorMeshGrid
@onready var room1_pos1 = $Room1Pos1
@onready var room1_pos2 = $Room1Pos2
@onready var room2_pos1 = $Room2Pos1
@onready var room2_pos2 = $Room2Pos2
func _ready():
door_mesh.mesh = ConstructDoorMesh.generate_door_mesh(room1_pos1.position, room2_pos1.position, room2_pos2.position, room1_pos2.position)
door_mesh_grid.mesh = ConstructDoorMesh.generate_door_mesh_grid(room1_pos1.position, room2_pos1.position, room2_pos2.position, room1_pos2.position)

File diff suppressed because one or more lines are too long