Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Venryx · Feb 05, 2013 at 08:21 AM · meshdirectionsurfacepolygonedge

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Venryx · Feb 06, 2013 at 08:47 AM 0
Share

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)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I acces 4-gons (quads) from script 0 Answers

Large triangular polygon with a large number of points in the mesh 0 Answers

Selecting a single polygon / face at runtime 1 Answer

Mesh texture not properly rendered 0 Answers

Detect a flat surface on a complex mesh? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges