Sign in to follow this  
Spa8nky

[C#] Clipping multiple triangles and vertex ordering

Recommended Posts

I'm currently clipping triangles from a mesh to 6 planes in order create a new mesh for a decal. Unfortunately I cannot figure out how to combine the clipped vertices correctly in order create the decal's mesh. The following code: - Detects the triangles that intersect the decal's OBB - Each triangle that the OBB intersects is clipped to the 6 planes of the OBB - The clipped vertices are then added to a list that makes the final decal's mesh vertices
            // Test each triangle that make up the intersecting sufraces
            for (int i = 0; i < triangles.Count; ++i)
            {
                CD_Triangle tri = triangles[i];

                if (Vector3.Dot(normal, tri.Normal) < 0.001f)
                {
                    // Triangle is facing away from decal's normal direction and can be ignored
                    continue;
                }

                // Triangle points must be wound CCW
                Vector3[] v_ToClip = new Vector3[3];

                if (tri.IsWoundCCW())
                {
                    v_ToClip[0] = tri.GetVertex(0);
                    v_ToClip[1] = tri.GetVertex(1);
                    v_ToClip[2] = tri.GetVertex(2);
                }
                else
                {
                    v_ToClip[0] = tri.GetVertex(1);
                    v_ToClip[1] = tri.GetVertex(0);
                    v_ToClip[2] = tri.GetVertex(2);
                }

                // Following array holds all vertices that have been clipped behind each plane tested
                Vector3[] v_Clipped = new Vector3[0];

                // Clip current triangle vertices to each of the 6 OBB planes
                for (int ii = 0; ii < 6; ++ii)
                {
                    v_Clipped = ClipVerticesToPlane(planes[ii], v_ToClip);

                    // If all vertices lie on the positive side of the plane
                    if (v_ToClip.Length == 0)
                    {            
                        // Discard this polygon
                        break;
                    }
                    else
                    {
                        // Otherwise set the new vertices to clip using the clipped vertices
                        v_ToClip = v_Clipped;
                    }
                }

                // Add clipped vertices for this triangle to the final decal vertices
                decalVertices.AddRange(v_Clipped);
            }


The list of clipped vertices should represent the final mesh however, some vertices might be duplicated. Also, because each triangle is clipped separately I have no idea how to index the vertices so that the mesh decal is drawn correctly. Can anyone please help? Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this