Fixed normals for generated meshes

This commit is contained in:
Tiberiu Chibici 2015-06-10 12:46:04 +03:00
parent 4b61f0bdb5
commit 3ab95e13fe
6 changed files with 123 additions and 73 deletions

View File

@ -80,7 +80,6 @@
<Compile Include="Assets\Scripts\Utils\ColorHelper.cs" />
<Compile Include="Assets\Scripts\Utils\Expression.cs" />
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
<Compile Include="Assets\Scripts\Utils\MeshExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\Range.cs" />

View File

@ -80,7 +80,6 @@
<Compile Include="Assets\Scripts\Utils\ColorHelper.cs" />
<Compile Include="Assets\Scripts\Utils\Expression.cs" />
<Compile Include="Assets\Scripts\Utils\Logger.cs" />
<Compile Include="Assets\Scripts\Utils\MeshExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\QuadTree.cs" />
<Compile Include="Assets\Scripts\Utils\RandomExtensions.cs" />
<Compile Include="Assets\Scripts\Utils\Range.cs" />

View File

@ -55,22 +55,27 @@ namespace TransportGame.Unity
{
int k = (j + 1) % pts.Length;
// Side
vertices.Add(new Vector3(pts[j].Y, minY, pts[j].X));
vertices.Add(new Vector3(pts[k].Y, minY, pts[k].X));
vertices.Add(new Vector3(pts[k].Y, maxY, pts[k].X));
vertices.Add(new Vector3(pts[j].Y, maxY, pts[j].X));
vertices.Add(new Vector3(center.Y, maxY, center.X));
uv.Add(new UnityEngine.Vector2(0, 0));
uv.Add(new UnityEngine.Vector2(0, 1));
uv.Add(new UnityEngine.Vector2(1, 1));
uv.Add(new UnityEngine.Vector2(1, 0));
uv.Add(new UnityEngine.Vector2(0.5f, 0.5f));
triangles.AddRange(new[] { vIndex, vIndex + 1, vIndex + 2 });
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 3 });
triangles.AddRange(new[] { vIndex + 4, vIndex + 3, vIndex + 2 }); // top side
vIndex = vertices.Count;
// Top side
vertices.Add(new Vector3(pts[k].Y, maxY, pts[k].X));
vertices.Add(new Vector3(pts[j].Y, maxY, pts[j].X));
vertices.Add(new Vector3(center.Y, maxY, center.X));
uv.Add(new UnityEngine.Vector2(0, 0));
uv.Add(new UnityEngine.Vector2(0, 1));
uv.Add(new UnityEngine.Vector2(1, 1));
triangles.AddRange(new[] { vIndex + 2, vIndex + 1, vIndex + 0 });
vIndex = vertices.Count;
}
}

View File

@ -259,48 +259,68 @@ namespace TransportGame.Unity
}
// Build road
// Right side
// Right sidewalk cover
addV(p[furth, i], SidewalkHeight); // 0 - p0 raised
addV(sideCrns[cornerRight], SidewalkHeight); // 1 - side corner raised
addV(sideCrns[cornerRight], SidewalkHeight - SideCoverHeight); // 2 - side corner cover
addV(p[furth, i], SidewalkHeight - SideCoverHeight); // 3 - p0 cover
addV(p[furth + 1, i], SidewalkHeight); // 4 - p1 raised
addV(strCrns[cornerRight], SidewalkHeight); // 5 - street corner raised
addV(strCrns[cornerRight], 0); // 6 - street corner
addV(p[furth + 1, i], 0); // 7 - p1
// Left side
addV(p[furth + 2, i], 0); // 8 - p2
addV(strCrns[cornerLeft], 0); // 9 - street corner
addV(strCrns[cornerLeft], SidewalkHeight); //10 - street corner raised
addV(p[furth + 2, i], SidewalkHeight); //11 - p2 raised
addV(p[furth + 3, i], SidewalkHeight - SideCoverHeight); //12 - p3 cover
addV(sideCrns[cornerLeft], SidewalkHeight - SideCoverHeight); //13 - side corner cover
addV(sideCrns[cornerLeft], SidewalkHeight); //14 - side corner raised
addV(p[furth + 3, i], SidewalkHeight); //15 - p3 raised
// Right sidewalk
triangles.AddRange(new[] { vindex, vindex + 1, vindex + 2 }); // sidewalk side cover 0
triangles.AddRange(new[] { vindex, vindex + 2, vindex + 3 }); // sidewalk side cover 1
triangles.AddRange(new[] { vindex, vindex + 4, vindex + 1 }); // sidewalk surface 0
triangles.AddRange(new[] { vindex + 4, vindex + 5, vindex + 1 }); // sidewalk surface 1
triangles.AddRange(new[] { vindex + 4, vindex + 7, vindex + 6 }); // sidewalk road raise 0
triangles.AddRange(new[] { vindex + 4, vindex + 6, vindex + 5 }); // sidewalk road raise 1
vindex = vertices.Count;
// Right sidewalk surface
addV(p[furth, i], SidewalkHeight); // 0 - p0 raised
addV(sideCrns[cornerRight], SidewalkHeight); // 1 - side corner raised
addV(p[furth + 1, i], SidewalkHeight); // 2 - p1 raised
addV(strCrns[cornerRight], SidewalkHeight); // 3 - street corner raised
triangles.AddRange(new[] { vindex, vindex + 2, vindex + 1 }); // sidewalk surface 0
triangles.AddRange(new[] { vindex + 2, vindex + 3, vindex + 1 }); // sidewalk surface 1
vindex = vertices.Count;
// Right sidewalk raise from road
addV(p[furth + 1, i], SidewalkHeight); // 0 - p1 raised
addV(strCrns[cornerRight], SidewalkHeight); // 1 - street corner raised
addV(strCrns[cornerRight], 0); // 2 - street corner
addV(p[furth + 1, i], 0); // 3 - p1
triangles.AddRange(new[] { vindex, vindex + 3, vindex + 2 }); // sidewalk road raise 0
triangles.AddRange(new[] { vindex, vindex + 2, vindex + 1 }); // sidewalk road raise 1
vindex = vertices.Count;
// Road surface
triangles.AddRange(new[] { vindex + 8, vindex + 6, vindex + 7 }); // road surface 0
triangles.AddRange(new[] { vindex + 8, vindex + 9, vindex + 6 }); // road surface 0
triangles.AddRange(new[] { vindex + 6, vindex + 9, 0 }); // road surface 0
addV(strCrns[cornerRight], 0); // 0 - street corner
addV(p[furth + 1, i], 0); // 1 - p1
addV(p[furth + 2, i], 0); // 2 - p2
addV(strCrns[cornerLeft], 0); // 3 - street corner
triangles.AddRange(new[] { vindex + 2, vindex, vindex + 1 }); // road surface 0
triangles.AddRange(new[] { vindex + 2, vindex + 3, vindex }); // road surface 0
triangles.AddRange(new[] { vindex, vindex + 3, 0 }); // road surface 0
vindex = vertices.Count;
// Left sidewalk
triangles.AddRange(new[] { vindex + 11, vindex + 10, vindex + 9 }); // sidewalk road raise 0
triangles.AddRange(new[] { vindex + 11, vindex + 9, vindex + 8 }); // sidewalk road raise 1
triangles.AddRange(new[] { vindex + 11, vindex + 14, vindex + 10 });// sidewalk surface 0
triangles.AddRange(new[] { vindex + 11, vindex + 15, vindex + 14 });// sidewalk surface 1
triangles.AddRange(new[] { vindex + 15, vindex + 12, vindex + 13 });// sidewalk side cover 0
triangles.AddRange(new[] { vindex + 15, vindex + 13, vindex + 14 });// sidewalk side cover 1
// Left sidewalk raise from road
addV(p[furth + 2, i], 0); // 0 - p2
addV(strCrns[cornerLeft], 0); // 1 - street corner
addV(strCrns[cornerLeft], SidewalkHeight); // 2 - street corner raised
addV(p[furth + 2, i], SidewalkHeight); // 3 - p2 raised
triangles.AddRange(new[] { vindex + 3, vindex + 2, vindex + 1 }); // sidewalk road raise 0
triangles.AddRange(new[] { vindex + 3, vindex + 1, vindex }); // sidewalk road raise 1
vindex = vertices.Count;
// update vindex
// Left sidewalk surface
addV(strCrns[cornerLeft], SidewalkHeight); // 0 - street corner raised
addV(p[furth + 2, i], SidewalkHeight); // 1 - p2 raised
addV(sideCrns[cornerLeft], SidewalkHeight); // 2 - side corner raised
addV(p[furth + 3, i], SidewalkHeight); // 3 - p3 raised
triangles.AddRange(new[] { vindex + 1, vindex + 2, vindex });// sidewalk surface 0
triangles.AddRange(new[] { vindex + 1, vindex + 3, vindex + 2 });// sidewalk surface 1
vindex = vertices.Count;
// Left sidewalk cover
addV(p[furth + 3, i], SidewalkHeight - SideCoverHeight); // 0 - p3 cover
addV(sideCrns[cornerLeft], SidewalkHeight - SideCoverHeight); // 1 - side corner cover
addV(sideCrns[cornerLeft], SidewalkHeight); // 2 - side corner raised
addV(p[furth + 3, i], SidewalkHeight); // 3 - p3 raised
triangles.AddRange(new[] { vindex + 3, vindex, vindex + 1 });// sidewalk side cover 0
triangles.AddRange(new[] { vindex + 3, vindex + 1, vindex + 2 });// sidewalk side cover 1
vindex = vertices.Count;
}
@ -355,6 +375,7 @@ namespace TransportGame.Unity
List<Vector3> vertices = new List<Vector3>();
List<UnityEngine.Vector2> uv = new List<UnityEngine.Vector2>();
List<int> triangles = new List<int>();
int vIndex = 0;
// Helper function
Action<Vector2, float, int> addV = (v, h, term) =>
@ -374,42 +395,68 @@ namespace TransportGame.Unity
Vector2[] pT1 = segmentTerminal1Limit[s.Id];
Vector2[] pT2 = segmentTerminal2Limit[s.Id];
addV(pT1[0], SidewalkHeight - SideCoverHeight, 1);
addV(pT1[0], SidewalkHeight, 1);
addV(pT1[1], SidewalkHeight, 1);
addV(pT1[1], 0, 1);
addV(pT1[2], 0, 1);
addV(pT1[2], SidewalkHeight, 1);
addV(pT1[3], SidewalkHeight, 1);
addV(pT1[3], SidewalkHeight - SideCoverHeight, 1);
// Left sidewalk side cover
addV(pT1[0], SidewalkHeight - SideCoverHeight, 1); // 0
addV(pT1[0], SidewalkHeight, 1); // 1
addV(pT2[3], SidewalkHeight, 2); // 2
addV(pT2[3], SidewalkHeight - SideCoverHeight, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk side cover 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk side cover 1
vIndex = vertices.Count;
addV(pT2[0], SidewalkHeight - SideCoverHeight, 2);
addV(pT2[0], SidewalkHeight, 2);
addV(pT2[1], SidewalkHeight, 2);
addV(pT2[1], 0, 2);
addV(pT2[2], 0, 2);
addV(pT2[2], SidewalkHeight, 2);
addV(pT2[3], SidewalkHeight, 2);
addV(pT2[3], SidewalkHeight - SideCoverHeight, 2);
// Left sidewalk surface
addV(pT1[0], SidewalkHeight, 1); // 0
addV(pT1[1], SidewalkHeight, 1); // 1
addV(pT2[2], SidewalkHeight, 2); // 2
addV(pT2[3], SidewalkHeight, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk surface 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk surface 1
vIndex = vertices.Count;
triangles.AddRange(new[] { 0, 15, 14 }); // sidewalk side cover 0
triangles.AddRange(new[] { 0, 14, 1 }); // sidewalk side cover 1
triangles.AddRange(new[] { 1, 14, 13 }); // sidewalk surface 0
triangles.AddRange(new[] { 1, 13, 2 }); // sidewalk surface 1
triangles.AddRange(new[] { 2, 12, 3 }); // sidewalk road raise 0
triangles.AddRange(new[] { 2, 13, 12 }); // sidewalk road raise 1
// Left sidewalk raise from road
addV(pT1[1], SidewalkHeight, 1); // 0
addV(pT1[1], 0, 1); // 1
addV(pT2[2], 0, 2); // 2
addV(pT2[2], SidewalkHeight, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk road raise 0
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk road raise 1
vIndex = vertices.Count;
// Road surface
triangles.AddRange(new[] { 3, 12, 11 }); // road surface 0
triangles.AddRange(new[] { 3, 11, 4 }); // road surface 0
addV(pT1[1], 0, 1); // 0
addV(pT1[2], 0, 1); // 1
addV(pT2[1], 0, 2); // 2
addV(pT2[2], 0, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // road surface 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // road surface 0
vIndex = vertices.Count;
// Left sidewalk
triangles.AddRange(new[] { 4, 11, 10 }); // sidewalk road raise 0
triangles.AddRange(new[] { 4, 10, 5 }); // sidewalk road raise 1
triangles.AddRange(new[] { 5, 10, 9 }); // sidewalk surface 0
triangles.AddRange(new[] { 5, 9, 6 }); // sidewalk surface 1
triangles.AddRange(new[] { 6, 9, 8 }); // sidewalk side cover 0
triangles.AddRange(new[] { 6, 8, 7 }); // sidewalk side cover 1
// Right sidewalk raise from road
addV(pT1[2], 0, 1); // 0
addV(pT1[2], SidewalkHeight, 1); // 1
addV(pT2[1], SidewalkHeight, 2); // 2
addV(pT2[1], 0, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk road raise 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk road raise 1
vIndex = vertices.Count;
// Right sidewalk surface
addV(pT1[2], SidewalkHeight, 1); // 0
addV(pT1[3], SidewalkHeight, 1); // 1
addV(pT2[0], SidewalkHeight, 2); // 2
addV(pT2[1], SidewalkHeight, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk surface 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk surface 1
vIndex = vertices.Count;
// Right sidewalk cover
addV(pT1[3], SidewalkHeight, 1); // 0
addV(pT1[3], SidewalkHeight - SideCoverHeight, 1); // 1
addV(pT2[0], SidewalkHeight - SideCoverHeight, 2); // 2
addV(pT2[0], SidewalkHeight, 2); // 3
triangles.AddRange(new[] { vIndex, vIndex + 3, vIndex + 2 }); // sidewalk side cover 0
triangles.AddRange(new[] { vIndex, vIndex + 2, vIndex + 1 }); // sidewalk side cover 1
vIndex = vertices.Count;
// Construct mesh
Mesh mesh = new Mesh();

View File

@ -23,7 +23,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
Policies = $0
$0.TextStylePolicy = $1

View File

@ -29,7 +29,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
Policies = $0
$0.TextStylePolicy = $1