Mesh created with FaceHelper doesn’t light properly

Software: Away3D 4.x

Dragoola, Newbie
Posted: 13 September 2013 03:22 AM   Total Posts: 16

Hi,

I want to create a more complex object(a house) straight out of triangles. I’ve been using facehelper and everything seemed al right until I tried to add lighting. Lighting sometimes works for some triangles and doesn’t work for others. I suspect it has something to do with either vertex normals or vertex tangents. I know they have to get auto generated and I checked the subgeometry and they are there.

here is the code I use to create the mesh:

var geometry:Geometry = new Geometry();
   var 
material:MaterialBase WallTypeLib.types[typeID].material;
   
material.repeat true;
   
mesh = new Mesh(geometrymaterial);
   
   
//triangle 1:
   
   
var v0:Vertex = new Vertex(startPoint.xstartPoint.ystartPoint.z);
   var 
v1:Vertex = new Vertex(startPoint.xstartPoint.heightstartPoint.z);
   var 
v2:Vertex = new Vertex(endPoint.xendPoint.yendPoint.z);
   
   var 
uv0:UV = new UV(0height);
   var 
uv1:UV = new UV(00);
   var 
uv2:UV = new UV(length(), height);
   
FaceHelper.addFace(meshv0v1v2uv0uv1uv20); 

On a related note, I noticed when looking in the subgeometry of the plane primitive that it has way more entries for the vertex data, vertex normals and vertex tangents. I am expecting 3 entries per vertex in the vertex data but the plane has about 52 for 4 vertices.

Thank you, any suggestion helps, I am quite stuck.

   

Avatar
Fabrice Closier, Administrator
Posted: 13 September 2013 10:21 AM   Total Posts: 1265   [ # 1 ]

Seeing a snapshot would certainly help, but it boils down to this:
As the light uses the vertex normals, depending on how you define the faces sharing or not the indices, these vertex normals are averaged between faces.
If an angle between 2 walls is too steep, the shader will attempt to smoothen corners, which will give a totally wrong rendering on a cube for instance.

That’s why some primitives may hold what it seams “more” information than a minimum geometric description. They do hold in fact the minimum requirements for a propper gpu rendering.

To solve your issue, simply add a Weld pass after your mesh is constructed and pass an angle threshold that meets your requirements. Unless your house has lots of variations in angle between ceilings/walls/roof etc… Something like 30 deg should cover most cases. Otherwize, you will have to build each items one at a time, apply the weld and finally merge instead.

You may also be interrested in MeshDebugger to inspect your normals if you still not get the propper rendering at some locations.

   

Dragoola, Newbie
Posted: 13 September 2013 04:03 PM   Total Posts: 16   [ # 2 ]

Thank you for your quick reply,

I fiddled around with it some more and realised what my specific problem is so I’m just going to post it here, in case anyone encounters the same.

When creating a square plane(for example), I would use the Face helper to add two triangles with appropriate uv coordinates and let the engine calculate the vertex normals and tangents.

Two things that I learned are:

1. The engine defines the vertex normals(and possibly tangents) based on the order in which you enter the vertices. Clockwise gives normals pointing to one side and counter clockwise to the other. My problem here is that I would define one triangle clockwise and the other counter clockwise.

2. The second thing that I didn’t know is that although triangles are by default rendered double-sided, light is calculated properly only for the side that the normals point out of, the back side is just a copy of the other. For example, if there is a light in front of the side that has correct normals then that side will be illuminated and the backside will also be illuminated also light does not hit it. If, however there is a light in front of the side that does not have normals pointing out of it, neither of the faces will be illuminated.

So the best solution I could come up with would be to make the triangles single-sided and for each visible triangle create two triangles back to back, each of them having the normals pointing out of the visible side.

If I got something wrong, please let me know.

   

Avatar
Fabrice Closier, Administrator
Posted: 13 September 2013 04:32 PM   Total Posts: 1265   [ # 3 ]

1: the face winding is indeed of importance to generate the normals.

2: tho that would work, it is not a very good practice (read a bad one).
You are better off simply loop after your are done to compare the normals between 2 faces by using their shared edges.

In practice, the last is also something that you do not need to do as you can do this during the build. Simply calculate the normal with your 3 points and compare with the normals of the faces sharing their edges with the new one
or define a rule per plane xz, yz, xy if your generator/app works AA.

   

Dragoola, Newbie
Posted: 13 September 2013 04:50 PM   Total Posts: 16   [ # 4 ]

I don’t really understand this last part:

Fabrice Closier - 13 September 2013 04:32 PM

Simply calculate the normal with your 3 points and compare with the normals of the faces sharing their edges with the new one
or define a rule per plane xz, yz, xy if your generator/app works AA.

Do you mean there is a way to define a triangle that has two sides, and both are lighted properly without having to double it up? It would somehow need two sets of normals, one for each side, wouldn’t it?

   

Avatar
Fabrice Closier, Administrator
Posted: 13 September 2013 08:16 PM   Total Posts: 1265   [ # 5 ]

“Do you mean there is a way to define a triangle that has two sides”
No, I said you can by comparing the face already existing with a shared edge to the to be defined triangle that you can check if the direction of the normal is correct or not. If the direction is correct you will have the normal pointing in similar direction, and will be lighted correctly. You can then set the bothSides to true if your walls have no thickness.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X