#Calculate Normals in Frag shader using lightposition
1 messages · Page 1 of 1 (latest)
float4 frag(v2f i) : SV_Target
{
float4 normal_tex = tex2D(_NormalTexture, i.uv);
normal_tex.rgb = UnpackNormalmapRGorAG(normal_tex);
const float3 light_vector = i.world_vertex - _PointLightPosition;
float normal_face = saturate(mul(normal_tex, normalize(light_vector)));
return normal_face;
I can do it in shadergraph too
this part shouldn't be shadowed
Usually, if you want to calculate shading, you'd do an "NdotL" operation.
So the dot product of the surface Normal vector (N) and the Light direction vector (L). That gives you a float ranging from -1 to 1, where 1 is full light, 0 is the transition to shadow/black and anything < 0 is in shadow. You'd then multiply the color of the surface by that self-shadowing float.
That's the simplest self-shading function. Simple lighting.
There's other factors like attenuation, and the entire function itself...for example blinn-phong adds specular highlights. And PBR is physically correct.
yes, the dot product is giving me the problem. when the pixel is perpendicular to light the dot gives 0 and that's bad because something perpendicular to light would be lighten
This:
float normal_face = saturate(mul(normal_tex, normalize(light_vector))); is NOT a dot product.
ops
Try float normal_face = dot(normal_tex, normalize(light_vector));
But you then have to multiply it by the color value and THAT result is what you'd return.
saturated.
And you can always add an ambient light amount to it, say .08 or something you can set. And saturate it too if you want.
the result doesn't change
IDK, try reversing the result then. Is it all "flipped" compared to what you want?
OK, that's your normal map, or the result of the surfact normal output
and add shadow on vertices that are not facing the light
this is my starting normal map
the surface is a plane
same result
like expected
Really?
Nah.
OK, try
return float4((saturate(normal_face)+.1).xxx, 1);
you mean of the normal tex? right and left side of wall tile? yes they are wrong
but it's not relevant
I have to go to work.
I mean the light direction vector. You have shadows on the top edges of the brick, and light on the bottoms. Should be the other way around
for a light on the ceiling
like this?
I have to go now.
Of course it is relevant, I mean, it just changed the whole pic.
Someone else will be around
Shadow casting and self-shading are different things. We're discussing self-shading.
just to be clear
yes it is
Shadow casting is another step.
i wont shadow cast
OK.
Good luck, later.
These 45° angles in shading occur when a normal map is sampled as sRGB rather than linear
Make sure the normal map texture is of type normal map, or try to get your lighting to work first without it altogether
my light wont work even without tex
Is it the exact same type of distortion?
Is "null" white, black or a neutral normal? The difference is significant
[NoScaleOffset]_NormalTexture("NormalTexture", 2D) = "bump" {}
it should be neutral
as far as i remember
In that case I recommend you keep the normal map off until the lighting function works as expected
ok, what i should use as facing vecotr to calculate with light pos?
Originally the question was how to calculate normals, wasn't it?
Not much point trying to calculate lighting before you have something to use as normals, assuming right now you don't
mmm..
the question was to calculate normals using lightposition.
I know:
- pixel position
- light position
- texture normal that say where it is facing (correct me if i'm wrong)
Why are you trying to calculate normals? Don't your meshes have any?
ah, yes on the wall
ok, my bad. I have to use this normals instead of the textures?
this looks good
I start from here to add the normal tex
Normal maps are in tangent space, they don't alone contain any information about the geometry normals
They're combined with interpolated vertex normals to add bumps to a surface
how to add the normal tex to this?