lonestar

0x10c-inspired space game based on the DCPU-16
git clone git://squid-tech.com/lonestar.git
Log | Files | Refs | README

commit c7a800c3c469f469b8f25d5f8cc505abb3a8a5e7
parent b44b2ff6d790745198176c0ec2ed8655763cb525
Author: Josh Moore <jxm5210@rit.edu>
Date:   Mon, 25 Nov 2019 19:29:16 -0500

Fix missing triangles in floor mesh

Diffstat:
Msrc/LoneStar/Assets/Scripts/ShipLoader.cs | 98++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 47 insertions(+), 51 deletions(-)

diff --git a/src/LoneStar/Assets/Scripts/ShipLoader.cs b/src/LoneStar/Assets/Scripts/ShipLoader.cs @@ -8,7 +8,7 @@ using Mirror; public class ShipLoader { // how much to scale loaded walls, etc by - const float SHIP_SCALE = 3; + const float SHIP_SCALE = 4; string shipname; @@ -24,73 +24,63 @@ public class ShipLoader this.shipname = name; } - // get all 8 point neighbors - Vector3[] getNeighbors(Vector3 point) - { - Vector3[] result = new Vector3[] - { - point + new Vector3(-SHIP_SCALE, 0, -SHIP_SCALE), - point + new Vector3(0, 0, -SHIP_SCALE), - point + new Vector3(SHIP_SCALE, 0, -SHIP_SCALE), - point + new Vector3(SHIP_SCALE, 0, 0), - point + new Vector3(SHIP_SCALE, 0, SHIP_SCALE), - point + new Vector3(0, 0, SHIP_SCALE), - point + new Vector3(-SHIP_SCALE, 0, SHIP_SCALE), - point + new Vector3(-SHIP_SCALE, 0, 0) - }; - - return result; - } - // calculate all of the triangles for a mesh - //\TODO figure out missing triangles bug //\TODO draw triangles for the underside as well int[] computeTriangles(Vector3[] points) { int[] triangles = new int[points.Length * 6]; int ti = 0; - bool skipY = false; - bool skipX = false; - for (int y = 0; y < 24; y++) { for (int x = 0; x < 32; x++) { Vector3 pos = new Vector3(x * SHIP_SCALE, 0, y * SHIP_SCALE); - if (Array.IndexOf(points, pos) > -1) - { - Vector3[] posNeighbors = getNeighbors(pos); + int a = Array.IndexOf(points, pos); + int b = Array.IndexOf(points, pos + new Vector3(SHIP_SCALE, 0, 0)); + int c = Array.IndexOf(points, pos + new Vector3(SHIP_SCALE, 0, SHIP_SCALE)); + int d = Array.IndexOf(points, pos + new Vector3(0, 0, SHIP_SCALE)); - for (int i = 0; i < posNeighbors.Length; i++) + if (a > -1) + { + if (b > -1 && d > -1) + { + // top + triangles[ti] = a; + triangles[ti + 1] = d; + triangles[ti + 2] = b; + ti += 3; + } + else if (c > -1 && d > -1) { - int a = Array.IndexOf(points, posNeighbors[i]); - int b = Array.IndexOf(points, posNeighbors[(i + 1) % posNeighbors.Length]); - int c = Array.IndexOf(points, pos); - - if (a > -1 && b > -1) - { - triangles[ti] = c; - triangles[ti + 1] = b; - triangles[ti + 2] = a; - - ti += 3; - skipX = true; - skipY = true; - } + // top + triangles[ti] = a; + triangles[ti + 1] = d; + triangles[ti + 2] = c; + ti += 3; + } + else if (b > -1 && c > -1) + { + // top + triangles[ti] = a; + triangles[ti + 1] = c; + triangles[ti + 2] = b; + ti += 3; } } - if (skipX) x++; - skipX = false; + if (b > -1 && c > -1 && d > -1) + { + // top + triangles[ti] = b; + triangles[ti + 1] = d; + triangles[ti + 2] = c; + ti += 3; + } } - - if (skipY) y++; - skipY = false; } - UnityEngine.Debug.Log((ti / 6).ToString()); return triangles; } @@ -207,6 +197,15 @@ public class ShipLoader for (int i = 0; i < gridPoints.Length; i++) { gridPoints[i] = generatedVerts[i % generatedVerts.Count]; + + GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); + sphere.transform.parent = shipObject.transform; + sphere.transform.localScale = new Vector3(0.5f, 0.25f, 0.25f); + sphere.transform.position = gridPoints[i] + new Vector3(0, -SHIP_SCALE / 2, 0.1f); + + Light light = sphere.AddComponent<Light>(); + light.type = LightType.Point; + light.color = Color.red; } // calculate mesh triangles @@ -216,8 +215,7 @@ public class ShipLoader floorObject.name = "Floor"; Mesh floorMesh = new Mesh(); - UnityEngine.Debug.Log(String.Format("{0} {1}", gridPoints.Length, triangles.Length / 6)); - + //\TODO calculate mesh bounding box more smarter like float minX = 50000; float minY = 50000; float maxX = 0; @@ -241,8 +239,6 @@ public class ShipLoader (gridPoints[i].z - minY) / (maxY - minY)); } - - floorMesh.vertices = gridPoints; floorMesh.triangles = triangles; floorMesh.uv = uv;