Skip to content

Commit ff54823

Browse files
Moved GetBisector and OffsetPolygon from RoofGenerator to Geometry
1 parent 7ec0139 commit ff54823

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

Examples/Buildings/RoofGenerator.cs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4-
using UnityEngine.Assertions;
54

65
namespace ProceduralToolkit.Examples
76
{
@@ -15,7 +14,7 @@ public static MeshDraft Generate(
1514
float roofHeight,
1615
RoofConfig roofConfig)
1716
{
18-
List<Vector2> roofPolygon = OffsetPolygon(foundationPolygon, roofConfig.overhang);
17+
List<Vector2> roofPolygon = Geometry.OffsetPolygon(foundationPolygon, roofConfig.overhang);
1918

2019
MeshDraft roofDraft;
2120
switch (roofConfig.type)
@@ -90,6 +89,7 @@ public static MeshDraft GenerateHipped(List<Vector2> roofPolygon, RoofConfig roo
9089
Vector3 ridgeOffset = (b - a).normalized*2;
9190
Vector3 ridge0 = (a + d)/2 + ridgeHeight + ridgeOffset;
9291
Vector3 ridge1 = (b + c)/2 + ridgeHeight - ridgeOffset;
92+
9393
var roofDraft = new MeshDraft();
9494
roofDraft.AddQuad(a, ridge0, ridge1, b);
9595
roofDraft.AddTriangle(b, ridge1, c);
@@ -102,52 +102,14 @@ private static MeshDraft GenerateBorder(List<Vector2> roofPolygon, RoofConfig ro
102102
{
103103
List<Vector3> lowerRing = roofPolygon.ConvertAll(v => v.ToVector3XZ());
104104
List<Vector3> upperRing = roofPolygon.ConvertAll(v => v.ToVector3XZ() + Vector3.up*roofConfig.thickness);
105-
var border = new MeshDraft().AddFlatQuadBand(lowerRing, upperRing, false);
106-
return border;
105+
return new MeshDraft().AddFlatQuadBand(lowerRing, upperRing, false);
107106
}
108107

109108
private static MeshDraft GenerateOverhang(List<Vector2> foundationPolygon, List<Vector2> roofPolygon)
110109
{
111110
List<Vector3> lowerRing = foundationPolygon.ConvertAll(v => v.ToVector3XZ());
112111
List<Vector3> upperRing = roofPolygon.ConvertAll(v => v.ToVector3XZ());
113-
var overhang = new MeshDraft().AddFlatQuadBand(lowerRing, upperRing, false);
114-
return overhang;
115-
}
116-
117-
private static List<Vector2> OffsetPolygon(List<Vector2> polygon, float distance)
118-
{
119-
var newPolygon = new List<Vector2>();
120-
for (int i = 0; i < polygon.Count; i++)
121-
{
122-
var previous = polygon.GetLooped(i - 1);
123-
var current = polygon[i];
124-
var next = polygon.GetLooped(i + 1);
125-
float angle;
126-
Vector2 bisector = GetBisector(previous, current, next, out angle);
127-
float hypotenuse = distance/GetBisectorSin(angle);
128-
129-
newPolygon.Add(current + bisector*hypotenuse);
130-
}
131-
return newPolygon;
132-
}
133-
134-
private static Vector2 GetBisector(Vector2 previous, Vector2 current, Vector2 next, out float angle)
135-
{
136-
Vector2 toPrevious = (previous - current).normalized;
137-
Vector2 toNext = (next - current).normalized;
138-
139-
angle = VectorE.SignedAngle(toPrevious, toNext);
140-
Assert.IsFalse(float.IsNaN(angle));
141-
return toPrevious.RotateCW(angle/2);
142-
}
143-
144-
private static float GetBisectorSin(float angle)
145-
{
146-
if (angle > 180)
147-
{
148-
angle = 360 - angle;
149-
}
150-
return Mathf.Sin(angle/2*Mathf.Deg2Rad);
112+
return new MeshDraft().AddFlatQuadBand(lowerRing, upperRing, false);
151113
}
152114
}
153115

Scripts/Geometry/Geometry.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3+
using UnityEngine.Assertions;
34

45
namespace ProceduralToolkit
56
{
@@ -10,6 +11,8 @@ public static class Geometry
1011
{
1112
public const float Epsilon = 0.00001f;
1213

14+
#region Point samplers
15+
1316
/// <summary>
1417
/// Returns a point on a circle in the XY plane
1518
/// </summary>
@@ -175,5 +178,52 @@ public static Vector3 PointOnTeardrop(float radius, float height, float horizont
175178
y: height*sinVertical,
176179
z: radius*Mathf.Cos(horizontalRadians)*teardrop);
177180
}
181+
182+
#endregion Point samplers
183+
184+
/// <summary>
185+
/// Returns the bisector of an angle
186+
/// </summary>
187+
/// <param name="previous">Previous vertex</param>
188+
/// <param name="current">Current vertex</param>
189+
/// <param name="next">Next vertex</param>
190+
/// <param name="degrees">Value of an angle in degrees</param>
191+
public static Vector2 GetAngleBisector(Vector2 previous, Vector2 current, Vector2 next, out float degrees)
192+
{
193+
Vector2 toPrevious = (previous - current).normalized;
194+
Vector2 toNext = (next - current).normalized;
195+
196+
degrees = VectorE.SignedAngle(toPrevious, toNext);
197+
Assert.IsFalse(float.IsNaN(degrees));
198+
return toPrevious.RotateCW(degrees/2);
199+
}
200+
201+
/// <summary>
202+
/// Offsets the supplied polygon. Does not handle intersections.
203+
/// </summary>
204+
/// <param name="polygon">Vertices of the polygon</param>
205+
/// <param name="distance">Offset distance. Positive values offset inside, negative outside.</param>
206+
public static List<Vector2> OffsetPolygon(List<Vector2> polygon, float distance)
207+
{
208+
var newPolygon = new List<Vector2>();
209+
for (int i = 0; i < polygon.Count; i++)
210+
{
211+
Vector2 previous = polygon.GetLooped(i - 1);
212+
Vector2 current = polygon[i];
213+
Vector2 next = polygon.GetLooped(i + 1);
214+
215+
float angle;
216+
Vector2 bisector = GetAngleBisector(previous, current, next, out angle);
217+
218+
if (angle > 180)
219+
{
220+
angle = 360 - angle;
221+
}
222+
float hypotenuse = distance/Mathf.Sin(angle/2*Mathf.Deg2Rad);
223+
224+
newPolygon.Add(current + bisector*hypotenuse);
225+
}
226+
return newPolygon;
227+
}
178228
}
179229
}

0 commit comments

Comments
 (0)