Hallo
I am following the tutorial on ((tutorial effects normals)) for basic normals calculation but i see that normals need to go through the same math function that I am using to generate the mesh… any hint, tutorial?
tx
S.
- define pi 3.14
float2 Scale = 1;
float2 Offset = 0;
float3 Cone(float2 uv)
{
uv *= Scale;
uv += Offset;
float u = uv.x * 2*pi;
float v = uv.y;
float3 newPos;
newPos.x = 1.2*(1 -v/(2*pi))*cos(3*v)*(1 + cos(u)) + 3*cos(3*v);
newPos.y = 9*v/(2*pi) + 1.2*(1 - v/(2*pi))*sin(u);
newPos.z = 1.2*(1 -v/(2*pi))*sin(3*v)*(1 + cos(u)) + 3*sin(3*v);
return newPos;
}
float NeighbourOffset = 0.001;
vs2ps VS(
float4 PosO : POSITION,
float4 TexCd : TEXCOORD0)
{
//declare output struct
vs2ps Out;
//calc new position
float3 p = Cone(PosO.xy);
//calc neighbour pos in u direction
float3 n1 = Cone(PosO.xy + float2(NeighbourOffset, 0));
//calc neighbour pos in v direction
float3 n2 = Cone(PosO.xy + float2(0, NeighbourOffset));
//get tangent vector 1
float3 t1 = p - n1;
//get tangent vector 2
float3 t2 = p - n2;
//get normal
float3 NormO = cross(t1, t2);
//set new pos
PosO.xyz = p;
//put normal in view space and normalize it
Out.NormV = normalize(mul(NormO, tWV));
//inverse light direction in view space
Out.LightDirV = normalize(-mul(lDir, tV));
//inverse view dir in view space
Out.ViewDirV = -normalize(mul(PosO, tWV));
//transform position (projected)
Out.Pos = mul(PosO, tWVP);
//transform texturecoordinates
Out.TexCd = mul(TexCd, tTex);
return Out;
}
where is the problem exactly? you use the mesh function 3 times, that gives you a small triangle on the surface, like in the code. then you use the 3 points to get a normal vector with the cross product… it looks correct…
you would use the solution from vux if you know the derivative of your function and want to calculate the normal directly.
Yeah that’s a common problem when doing parametric surfaces, Some triangles can have their normals pointing to the opposite direction once transformed.
Using partial derivatives is more efficient indeed (saves a lot of sin/cos), but yeah quite more cumbersome (had some functions like that and some softwares like mathematica comes in handy), and you still end up with the same “reverse normals” problem.
ok that was dumb …really dumb … anyway, i am attaching the latest patch where I am trying to calculate the normals in CPU, how do I make the cross product in a patch ?
S.
@tonfilm: i am not used to work with shaders so I haven t noticed it before, just the Normals help patch … and yes I ve found out yesterday night the cross product node …
Hi,
if I have an mesh with computed normals (the normals node) what would be a good way to display or highlight or something like that only the parts of the mesh that create a plane surface (a desk, table, sheet of paper, …) that is the polygons with normals in the same direction.
how could that be done?
you could input a vector which is normal to your plane in the mesh and compare the normals to this vector (using the dot product). if the value of the dot product is above a certain threshold, you can consider this vector as part of the plane and give it another color…