Skip to content
Snippets Groups Projects
Commit ec6eb465 authored by Maurice Manshanden's avatar Maurice Manshanden
Browse files

Attempt at triangle splitting

parent 1b68d08b
No related branches found
No related tags found
No related merge requests found
......@@ -310,6 +310,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c08c3492f2af1354c9a8faf4ca0c6381, type: 3}
m_Name:
m_EditorClassIdentifier:
seed: 82
--- !u!135 &1965633082
SphereCollider:
m_ObjectHideFlags: 0
......
......@@ -23,10 +23,12 @@ public class MeshBuilder
public Mesh GetMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = this.vertices.ToArray();
mesh.normals = this.normals.ToArray();
mesh.uv = this.uv.ToArray();
Mesh mesh = new Mesh
{
vertices = this.vertices.ToArray(),
normals = this.normals.ToArray(),
uv = this.uv.ToArray()
};
return mesh;
}
......@@ -41,15 +43,15 @@ public class MeshBuilder
float t = Vector3.Dot(p - origin, n) / Vector3.Dot(direction, n);
// Interpolate vertex, normal and uv
Vector3 vertex = t * this.vertices[index1] + (1 - t) * this.vertices[index2];
Vector3 vertex = origin + direction * t;
Vector3 normal = t * this.normals[index1] + (1 - t) * this.normals[index2];
Vector2 uv = t * this.uv[index1] + (1 - t) * this.uv[index2];
this.vertices.Add(vertex);
this.normals.Add(normal);
this.normals.Add(normal.normalized);
this.uv.Add(uv);
return this.vertices.Count;
return this.vertices.Count - 1;
}
public Vector3 GetCenter(IEnumerable<int> triangles)
......@@ -74,6 +76,11 @@ public class Splitter : MonoBehaviour
private MeshBuilder mesh;
private MeshTreeNode root;
float total = 0.3f;
bool swtch;
public int seed;
void Start()
{
Mesh mesh = this.GetComponent<MeshFilter>().mesh;
......@@ -82,7 +89,29 @@ public class Splitter : MonoBehaviour
this.BuildMeshTree();
this.GetComponent<MeshFilter>().mesh = this.mesh.GetMesh();
this.GetComponent<MeshFilter>().mesh.triangles = this.root.left.triangles;
this.GetComponent<MeshFilter>().mesh.triangles = this.root.triangles;
Random.InitState(this.seed);
}
private void Update()
{
this.total += Time.deltaTime;
if (this.total > 0.4)
{
this.total = 0;
this.swtch = !this.swtch;
if (this.swtch)
{
this.GetComponent<MeshFilter>().mesh.triangles = this.root.right.triangles;
}
else
{
this.GetComponent<MeshFilter>().mesh.triangles = this.root.left.triangles;
}
}
}
void BuildMeshTree()
......@@ -128,9 +157,9 @@ public class Splitter : MonoBehaviour
Vector3 v3 = this.mesh.vertices[i3];
// Test for each vertex whether they lie on the same side of the plane
bool s1 = v1.x * n.x + v1.y * n.y + v1.z * n.z + d < 0;
bool s2 = v2.x * n.x + v2.y * n.y + v2.z * n.z + d < 0;
bool s3 = v3.x * n.x + v3.y * n.y + v3.z * n.z + d < 0;
bool s1 = v1.x * n.x + v1.y * n.y + v1.z * n.z + d > 0;
bool s2 = v2.x * n.x + v2.y * n.y + v2.z * n.z + d > 0;
bool s3 = v3.x * n.x + v3.y * n.y + v3.z * n.z + d > 0;
if (s1 && s2 && s3)
{
......@@ -142,8 +171,32 @@ public class Splitter : MonoBehaviour
}
else
{
// The difficult case where one vertex is on one side, and the other
// The difficult cases where one vertex is on one side, and the other
// two vertices are on the other side of the plane.
int s = -1, p1 = -1, p2 = -1, l = -1;
if (s1 && !s2 && !s3) { s = i1; p1 = i2; p2 = i3; l = 0; }
if (!s1 && s2 && !s3) { s = i2; p1 = i1; p2 = i3; l = 0; }
if (!s1 && !s2 && s3) { s = i3; p1 = i1; p2 = i2; l = 0; }
if (!s1 && s2 && s3) { s = i1; p1 = i2; p2 = i3; l = 1; }
if (s1 && !s2 && s3) { s = i2; p1 = i1; p2 = i3; l = 1; }
if (s1 && s2 && !s3) { s = i3; p1 = i1; p2 = i2; l = 1; }
int x1 = mesh.SplitEdge(p1, s, p, n);
int x2 = mesh.SplitEdge(p2, s, p, n);
if (l == 0)
{
left.AddRange(new[] { x1, x2, s });
right.AddRange(new[] { x1, p1, p2, p2, x2, x1 });
}
else
{
right.AddRange(new[] { x1, x2, s });
left.AddRange(new[] { x1, p1, p2, p2, x2, x1 });
}
}
}
......@@ -159,7 +212,7 @@ public class Splitter : MonoBehaviour
};
// Split children
SplitNode(node.left);
SplitNode(node.right);
//SplitNode(node.left);
//SplitNode(node.right);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment