-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
BuildingGenerator.cs
112 lines (98 loc) · 3.59 KB
/
BuildingGenerator.cs
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
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
namespace ProceduralToolkit.Buildings
{
public class BuildingGenerator
{
private IFacadePlanner facadePlanner;
private IFacadeConstructor facadeConstructor;
private IRoofPlanner roofPlanner;
private IRoofConstructor roofConstructor;
public void SetFacadePlanner(IFacadePlanner facadePlanner)
{
this.facadePlanner = facadePlanner;
}
public void SetFacadeConstructor(IFacadeConstructor facadeConstructor)
{
this.facadeConstructor = facadeConstructor;
}
public void SetRoofPlanner(IRoofPlanner roofPlanner)
{
this.roofPlanner = roofPlanner;
}
public void SetRoofConstructor(IRoofConstructor roofConstructor)
{
this.roofConstructor = roofConstructor;
}
/// <summary>
/// Generates a new building from the input polygon and config.
/// Foundation polygon vertices should be in clockwise order.
/// Returns Transform of the generated building.
/// </summary>
/// <param name="foundationPolygon">Vertices of the foundation polygon in clockwise order.</param>
/// <param name="config">Generator config.</param>
/// <param name="parent">Parent transform of the generated building. Will be created if null.</param>
public Transform Generate(List<Vector2> foundationPolygon, Config config, Transform parent = null)
{
Assert.IsTrue(config.floors > 0);
Assert.IsTrue(config.entranceInterval > 0);
List<ILayout> facadeLayouts = facadePlanner.Plan(foundationPolygon, config);
float height = facadeLayouts[0].height;
if (parent == null)
{
parent = new GameObject("Building").transform;
}
facadeConstructor.Construct(foundationPolygon, facadeLayouts, parent);
if (roofPlanner != null && roofConstructor != null)
{
var roofConstructible = roofPlanner.Plan(foundationPolygon, config);
var roof = new GameObject("Roof").transform;
roof.SetParent(parent, false);
roof.localPosition = new Vector3(0, height, 0);
roof.localRotation = Quaternion.identity;
roofConstructor.Construct(roofConstructible, roof);
}
return parent;
}
[Serializable]
public class Config
{
public int floors = 5;
public float entranceInterval = 12;
public bool hasAttic = true;
public RoofConfig roofConfig = new RoofConfig
{
type = RoofType.Flat,
thickness = 0.2f,
overhang = 0.2f,
};
public Palette palette = new Palette();
}
}
[Serializable]
public class RoofConfig
{
public RoofType type = RoofType.Flat;
public float thickness;
public float overhang;
}
[Serializable]
public class Palette
{
public Color socleColor = ColorE.silver;
public Color socleWindowColor = (ColorE.silver/2).WithA(1);
public Color doorColor = (ColorE.silver/2).WithA(1);
public Color wallColor = ColorE.white;
public Color frameColor = ColorE.silver;
public Color glassColor = ColorE.white;
public Color roofColor = (ColorE.gray/4).WithA(1);
}
public enum RoofType
{
Flat,
Hipped,
Gabled,
}
}