- Home /
Get "Winding" of an Edge
I've been trying for the last few days to get shadow volumes working for non-manifold objects. The one Aras provided doesn't work on some models like buildings. (his version is here)
I found an article online which apparently solves the problem, and works for all types of models. (the article can be found here)
I've been trying to get it implemented, but on turning the C++ code into Unity C# code, using Unity Mesh systems, some of the things I just can't find in the Unity Mesh system. The most important one being, the "winding" or "direction" of an edge. I tried to understand what this meant from the article, and it showed a picture. But it doesn't say what caused the edge "winding" to be different from the winding of the polygon its attached to. (and this is instrumental in determining how to draw shadow volumes using the technique it describes.)
I think I understand how the technique works, given there being separate windings, but first off, I don't know what it means for an edge to have its own winding/why it would have its own winding, and second, I don't know how to get this from the Mesh object in Unity.
Other than that, I've got most of the technique coded (I think). Does anyone know the answer to these two things? (1, why would an edge have its own winding when it seems that only a polygon would need one; and 2, how do I obtain this winding from the data in a Unity Mesh object.)
Here's the code so far:
private Mesh GetShadowVolume(MeshFilter filter, Mesh m, Vector3 lightDif)
{
//Transform vertices positions and such from local to global coordinates
Vector3[] m_vertices = m.vertices;
for(int i = 0;i < m_vertices.Length;i++)
m_vertices[i] = filter.transform.TransformPoint(m_vertices[i]);
Vector3[] m_normals = m.normals;
for(int i = 0;i < m_normals.Length;i++)
m_normals[i] = filter.transform.TransformDirection(m_normals[i]);
int[] m_triangles = m.triangles;
//Initialize edge counter values
int[] counterE = new int[m_triangles.Length];
for(int i = 0;i < counterE.Length;i++)
counterE[i] = 0;
Mesh result = new Mesh();
List<Vector3> n_vertices = new List<Vector3>();
List<Face> n_faces = new List<Face>();
for (int t = 0; t < m_triangles.Length; t += 3)
{
if(FacesLight(CalculatePolygonNormal(m_normals[m_triangles[t]], m_normals[m_triangles[t + 1]], m_normals[m_triangles[t + 2]]), lightDif))
{
//Add the front volume cap
for (int v = 0; v < 3; v++)
{
int triIndex = m_triangles[t + v];
n_vertices.Add(m_vertices[triIndex]);
}
n_faces.Add(new Face(n_vertices.Count - 3, n_vertices.Count - 2, n_vertices.Count - 1));
//Add the projected rear volume cap
for (int v = 2; v >= 0; v--)
n_vertices.Add(Project(m_vertices[m_triangles[t + v]], lightDif));
n_faces.Add(new Face(n_vertices.Count - 3, n_vertices.Count - 2, n_vertices.Count - 1));
for (int e = 0; e < 3; e++)
{
//THE LINE BELOW IS THE ONE I DON'T KNOW HOW TO IMPLEMENT
//Original C++ code: "if (triangles[t].edges[e]->vertices[0]==triangles[t].vertices[e])"
if (m_triangles[t + e] == m_triangles[t]) //Edge is directed the same as triangle winding (unfinished line)
counterE[t + e]++;
else
counterE[t + e]--;
}
}
}
//Add the projected silhouette
for (int t = 0; t < m_triangles.Length; t += 3)
{
for (int e = 0; e < 3; e++)
{
while (counterE[t + e] > 0)
{
n_vertices.Add(m_vertices[m_triangles[t + e]]);
n_vertices.Add(m_vertices[m_triangles[t]]);
n_vertices.Add(Project(m_vertices[m_triangles[t]], lightDif));
n_vertices.Add(Project(m_vertices[m_triangles[t + e]], lightDif));
Face quad = new Face(n_vertices.Count - 4, n_vertices.Count - 3, n_vertices.Count - 2, n_vertices.Count - 1);
n_faces.AddRange(quad.Tri(n_vertices));
counterE[t + e]--;
}
while (counterE[t + e] < 0)
{
n_vertices.Add(m_vertices[m_triangles[t]]);
n_vertices.Add(m_vertices[m_triangles[t + e]]);
n_vertices.Add(Project(m_vertices[m_triangles[t + e]], lightDif));
n_vertices.Add(Project(m_vertices[m_triangles[t]], lightDif));
Face quad = new Face(n_vertices.Count - 4, n_vertices.Count - 3, n_vertices.Count - 2, n_vertices.Count - 1);
n_faces.AddRange(quad.Tri(n_vertices));
counterE[t + e]++;
}
}
}
Vector3[] fVertices = new Vector3[n_vertices.Count];
for(int i = 0;i < n_vertices.Count;i++)
fVertices[i] = filter.transform.InverseTransformPoint(n_vertices[i]);
result.vertices = fVertices;
int[] fTriangles = new int[n_faces.Count * 3];
int i2 = 0;
foreach(Face face in n_faces)
{
fTriangles[i2] = face.n1;
i2++;
fTriangles[i2] = face.n2;
i2++;
fTriangles[i2] = face.n3;
i2++;
}
result.triangles = fTriangles;
result.RecalculateNormals();
result.RecalculateBounds();
return result;
}
Any help at all would be appreciated. I've been spending hours on it, and I don't know how to get any farther.
Thanks, Stephen
I've been working on it some more. The code is cleaned out, and now generates a shadow volume mesh rather than drawing it directly with the GL methods. This makes it more flexible, and fixes the problem of the sub-terrain parts of the shadow volume being drawn over the terrain.
The code structure is working now, I'm just having trouble understanding and implementing the details of their algorithm. (currently, the projection and such work fine, but not knowing how to implement checks of the polygon and edge 'winding' keeps me from getting it to look like a shadow rather than simply a projected, grey mesh)