- Home /
Determine whether or not model is "inside out"
Pretty simple question... sometimes (and seemingly at random), my procedural meshes will be inside out. I've tried to figure out under what cases this is true, but it seems to be at random, and I have no idea why it's happening.
What will happen is... the faces will be facing the correct direction, but the stitching on the sides that I do by hand will sometimes be inverted (with the normals facing inward, not outward as they should be).
So my question is... is there some script that can automatically make all normals face outward? Or what would cause this behavior to happen at random? Remember it's not the entire model, just the sides.
I know that this can often be caused by writing the triangles in reverse order to the triangle, but I can't figure out why/when this occurs, so I can't just reverse the triangles.
Any help on this would be appreciated, thanks.
EDIT: So the problem is that my mesh faces always point in one direction, so if the user creates an object "in reverse" (for example, drawing counter-clockwise instead of clockwise), the mesh faces will be facing the wrong way. How can I tell if my object is drawn in reverse, or if my mesh faces are facing the wrong direction?
@SpikeX, you're welcome, glad it helped. And I'm curious, can you talk about your project? :) Given the user-drawn shapes and edge colliders, for some reason it re$$anonymous$$ds me of Crayon Physics :) http://www.crayonphysics.com/
Yeah, that's kind of what we're shooting for, although it will probably just be released on the web as a free game, as this is mostly a "for-fun"/portfolio-builder-type project. :)
Answer by Cyclops · Apr 26, 2010 at 10:15 PM
This might be useful: Determining whether a polygon is clockwise.... It includes simple C source code. It also assumes the polygon doesn't have holes and isn't self-intersecting - not sure what your drawing algorithm allows.
This is EXACTLY what I was looking for! I was able to incorporate this into my script and it works beautifully. I don't have any more objects inside-out anymore. THAN$$anonymous$$ YOU!
Either the link in @Cyclops post is broken, or my browser just doesn't like it.
Here's another link to :
How to deter$$anonymous$$e if a polygon is clockwise
...and just in case that link dies at some point also, here's what the highly-upvoted answer on that topic says :
Some of the other suggested methods will fail in the case of a non-convex polygon, such as a crescent. Here's a simple one that will work with non-convex polygons (it'll even work with a self-intersecting polygon like a figure-eight, telling you whether it's mostly clockwise).
Sum over the edges, (x2-x1)(y2+y1). If the result is positive the curve is clockwise, if it's negative the curve is counter-clockwise. (The result is twice the enclosed area, with a +/- convention.)
point[0] = (5,0) edge[0]: (6-5)(4+0) = 4 point[1] = (6,4) edge[1]: (4-6)(5+4) = -18 point[2] = (4,5)
edge[2]: (1-4)(5+5) = -30 point[3] = (1,5) edge[3]: (1-1)(0+5) = 0 point[4] = (1,0) edge[4]: (5-1)(0+0) = 0 --- -44 counter-clockwise--post by user : "Beta" at stackoverflow.com
...and here's a little function I just made a little bit ago for using what Beta at stackoverflow showed us (Thanks, Beta).
It's working on convex AND concave polygons so far in my tests building some oddball meshes, but it's not doing so well with squirrelly self-intersecting poly's (might work with fig-8's like Beta said, but I didn't try that because I'm only using it for con[cave/vex] poly's anyway and only did some weird intersectors just to see what happened).
C#
bool PolygonIsClockwise(params Vector2[] points)
{
int l = points.Length ;
float sum = 0f ;
for(int i = 0 ; i < l ; i++)
{
int n = i+1 >= l-1 ? 0 : i+1 ;
float x = points[n].x - points[i].x ;
float y = points[n].y + points[i].y ;
sum += (x*y) ;
}
return (sum < 0) ? false : true ;
}
UnityScript
function PolygonIsClockwise(points : Vector2[]) : boolean
{
var l : int = points.length ;
var sum : float = 0f ;
for(i : int = 0 ; i < l ; i++)
{
var n : int = i+1 >= l-1 ? 0 : i+1 ;
var x : float = points[n].x - points[i].x ;
var y : float = points[n].y + points[i].y ;
sum += (x*y) ;
}
return (sum < 0) ? false : true ;
}
Think I did the UnityScript conversion right anyway, getting rustier at that language after I moved over into C#.
Answer by duck · Apr 25, 2010 at 05:58 AM
I've done a fair amount of procedural mesh building in Unity, and I've never seen inconsistent results as to which side the triangles come out. It seems to me to be completely deterministic and predictable - so unfortunately I think the problem must come down to an error in your mesh building code!
As far as I'm aware, there are only two determining factors as to which side of a triangle is visible:
The vertex winding order. If the triangle is defined clockwise, one side is visible; anticlockwise, and the other side is visible.
The shader's "Cull setting". This can be set to "Back", "Front" or "Off. The default is "Back" (meaning any back-facing tris are not drawn). This would affect every tri drawn by the shader, not just a few.
It's a common misconception that the normals affect which side of a triangle is visible. This is not the case - the normals only affect how the material responds to light. If the normal is pointing in the opposite direction to the triangle's face direction, it will be lit as if it were facing away, but it will still be drawn front-facing. I think this misconception stems from 3D editing apps, where they often merge the concept of normals and face-winding order into a single feature.
Oh, okay... I think that makes sense. It would seem that when I create a shape in a clockwise direction, they're facing the right way, and counter-clockwise they aren't.
Is there some way to check for this? I don't know how to differentiate between a "clockwise-drawn" and "counter-clockwise-drawn" shape.
Or is there some way I could just detect if the triangles are facing the wrong way ins$$anonymous$$d? -- This seems to be the problem I'm having, but I don't know exactly how to fix it.
How are you generating the vertex positions in the first place? I'm not sure how else to describe it other than this - when you define a triangle, you specify 3 vertices. The triangle will be visible from the side which sees those vertices in a counter-clockwise direction (in the order you defined them). Duck 2 $$anonymous$$s ago
The user draws a shape with a certain amount of points, and I just loop through and add the triangles. Problem is... the user could draw the shape in a clockwise or counter-clockwise direction.
Ok, if the user is drawing the points, perhaps you could take the average of the 3 points, then use Atan2 measure the angle around that centre point - assu$$anonymous$$g they're on a sort of flat plane. You can then sort them by the angle returned to get them into CW or CCW order. (see http://answers.unity3d.com/questions/8543)
@SpikeX, is this related to your other question about colliders? $$anonymous$$ore specifically, is the user-generated shape a 2D drawing, a 3D drawing, or a 2D drawing extruded one unit (as per the previous question)? What dimension'ed object are you trying to triangulate? ($$anonymous$$ight want to update the Question).
Your answer
Follow this Question
Related Questions
Trying to create a hard edged procedural torus mesh 2 Answers
Seam on Procedural Mesh 1 Answer
Procedural Mesh, Strange Clipping 1 Answer
Meshes flipping on procedural generation 0 Answers