File size: 5,561 Bytes
6b56a65
 
 
 
 
9dfa216
 
 
 
 
 
6b56a65
 
 
 
 
 
 
 
 
 
 
9dfa216
 
 
 
 
 
 
 
 
 
 
 
 
6b56a65
9dfa216
 
6b56a65
9dfa216
 
 
 
 
 
6b56a65
9dfa216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b56a65
 
 
 
 
 
 
 
 
 
9dfa216
 
 
 
 
 
 
6b56a65
9dfa216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b56a65
9dfa216
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
@tool
extends EditorScript

var data:Dictionary = { "bathroom1": [8.52112676056338, 8.52112676056338, 6.408450704225352, 8.52112676056338, 6.408450704225352, 6.408450704225352, 8.52112676056338, 6.408450704225352], "bedroom1": [9.507042253521128, 5.422535211267606, 6.408450704225352, 5.422535211267606, 6.408450704225352, 3.3098591549295775, 9.507042253521128, 3.3098591549295775], "living_room": [14.71830985915493, 10.563380281690142, 10.563380281690142, 10.563380281690142, 10.563380281690142, 7.464788732394367, 14.71830985915493, 7.464788732394367], "kitchen": [9.507042253521128, 12.605633802816902, 4.366197183098592, 12.605633802816902, 4.366197183098592, 10.563380281690142, 9.507042253521128, 10.563380281690142], "bedroom2": [13.661971830985916, 7.464788732394367, 10.563380281690142, 7.464788732394367, 10.563380281690142, 6.408450704225352, 9.507042253521128, 6.408450704225352, 9.507042253521128, 4.366197183098592, 13.661971830985916, 4.366197183098592], "bedroom3": [12.605633802816902, 14.71830985915493, 9.507042253521128, 14.71830985915493, 9.507042253521128, 11.619718309859156, 10.563380281690142, 11.619718309859156, 10.563380281690142, 10.563380281690142, 12.605633802816902, 10.563380281690142], "bathroom2": [9.507042253521128, 10.563380281690142, 6.408450704225352, 10.563380281690142, 6.408450704225352, 8.52112676056338, 9.507042253521128, 8.52112676056338], "bedroom4": [5.422535211267606, 9.507042253521128, 3.3098591549295775, 9.507042253521128, 3.3098591549295775, 5.422535211267606, 5.422535211267606, 5.422535211267606], "corridor": [9.507042253521128, 11.619718309859156, 9.507042253521128, 8.52112676056338, 8.52112676056338, 8.52112676056338, 8.52112676056338, 6.408450704225352, 6.408450704225352, 6.408450704225352, 6.408450704225352, 9.507042253521128, 5.422535211267606, 9.507042253521128, 5.422535211267606, 5.422535211267606, 9.507042253521128, 5.422535211267606, 9.507042253521128, 6.408450704225352, 10.563380281690142, 6.408450704225352, 10.563380281690142, 11.619718309859156]}

					
const architext_colors = [[0, 0, 0], [249, 222, 182], [195, 209, 217], [250, 120, 128], [126, 202, 234], [190, 0, 198], [255, 255, 255], 
				   [6, 53, 17], [17, 33, 58], [132, 151, 246], [197, 203, 159], [6, 53, 17],]
const housegan_labels = {"living_room": 1, "kitchen": 2, "bedroom": 3, "bathroom": 4, "missing": 5, "closet": 6, 
						 "balcony": 7, "hallway": 8, "dining_room": 9, "laundry_room": 10, "corridor": 8}
	
func create_room(name: String, coords: Array, parent: Node, root: Node) -> void:
	# Create vertices for the room
	var verts = PackedVector2Array()
	for i in range(0, coords.size(), 2):
		verts.append(Vector2(coords[i], coords[i+1]))
	
	# Create a new CSGPolygon3D with given name
	var polygon = CSGPolygon3D.new()
	polygon.polygon = verts
	polygon.name = name
	polygon.mode = CSGPolygon3D.MODE_DEPTH
	polygon.depth = 2.0  # Set the depth of the room

	# Apply rotation
	polygon.rotate_x(deg_to_rad(-90))

	# Apply color coding
	var material: StandardMaterial3D = StandardMaterial3D.new()
	for room_name in housegan_labels.keys():
		if not name.begins_with(room_name):
			continue
		var color_values = architext_colors[housegan_labels[room_name]]
		material.albedo_color = Color(color_values[0]/255.0, color_values[1]/255.0, color_values[2]/255.0)
	polygon.material = material
	
	# Add polygons to the parent node
	parent.add_child(polygon)
	polygon.owner = root

func create_corridor(name: String, coords: Array, parent: Node, root: Node) -> void:
	# Create vertices for the corridor
	var verts = PackedVector2Array()
	for i in range(0, coords.size(), 2):
		verts.append(Vector2(coords[i], coords[i+1]))
	
	# Create a new CSGPolygon3D with given name
	var polygon = CSGPolygon3D.new()
	polygon.polygon = verts
	polygon.name = name
	polygon.mode = CSGPolygon3D.MODE_DEPTH
	polygon.depth = 2.0  # Set the depth of the corridor
	polygon.operation = CSGPolygon3D.OPERATION_SUBTRACTION  # Set operation to subtraction

	# Apply rotation
	polygon.rotate_x(deg_to_rad(-90))

	# Apply color coding
	var material = StandardMaterial3D.new()
	material.albedo_color = Color(architext_colors[housegan_labels[name]][0]/255.0, architext_colors[housegan_labels[name]][1]/255.0, architext_colors[housegan_labels[name]][2]/255.0)
	polygon.material = material
	
	# Add polygons to the parent node
	parent.add_child(polygon)
	polygon.owner = root


func _run():
	var root = get_scene()
	var csg_combiner: CSGCombiner3D = CSGCombiner3D.new()
	csg_combiner.name = "CSGCombiner3D"
	root.add_child(csg_combiner, true)
	csg_combiner.owner = root
	
	# Create rooms from the data
	for room_name in data.keys():
		if room_name == "corridor":
			create_corridor(room_name, data[room_name], csg_combiner, root)
		else:
			create_room(room_name, data[room_name], csg_combiner, root)
			
	# After creating all rooms and corridors
	var aabb : AABB = csg_combiner.get_aabb()

	# Expand the aabb slightly on x and z axis (width and depth)
	aabb.expand(Vector3(0.1, 0, 0.1))

	# Create a new CSGBox for the baseplate
	var baseplate: CSGBox3D = CSGBox3D.new()
	baseplate.name = "Baseplate"

	# Set the size of the baseplate to match the expanded aabb
	baseplate.size.x = aabb.size.x
	baseplate.size.z = aabb.size.z
	# You can adjust the height as per your requirement
	baseplate.size.y = 0.1

	# Position the baseplate at the center of the aabb
	baseplate.transform.origin = aabb.position + aabb.size / 2 - Vector3(0, baseplate.size.y / 2, 0)

	# Add the baseplate to the root node
	csg_combiner.add_child(baseplate)
	baseplate.owner = root