#Calculate Normals in Frag shader using lightposition

1 messages · Page 1 of 1 (latest)

somber portal
#
            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

boreal imp
# somber portal 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.

somber portal
#

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

boreal imp
#

This:
float normal_face = saturate(mul(normal_tex, normalize(light_vector))); is NOT a dot product.

somber portal
#

ops

boreal imp
#

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.

somber portal
#

the result doesn't change

boreal imp
#

IDK, try reversing the result then. Is it all "flipped" compared to what you want?

somber portal
#

no. I want all on light

boreal imp
#

OK, that's your normal map, or the result of the surfact normal output

somber portal
#

and add shadow on vertices that are not facing the light

#

this is my starting normal map

#

the surface is a plane

boreal imp
#

OK, so
return float4(normal_face.xxx, 1);

#

To see what it gives.

somber portal
somber portal
#

like expected

boreal imp
#

Really?
Nah.
OK, try
return float4((saturate(normal_face)+.1).xxx, 1);

somber portal
boreal imp
#

The light vector looks reversed to me.

#

Try it the other way around.

somber portal
#

you mean of the normal tex? right and left side of wall tile? yes they are wrong

#

but it's not relevant

boreal imp
#

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

somber portal
#

like this?

boreal imp
#

Yes, that's closer

#

Now the light is about where it should be.

somber portal
#

is the light_vector inversed

#

but it's not relevant

boreal imp
#

I have to go now.
Of course it is relevant, I mean, it just changed the whole pic.

#

Someone else will be around

somber portal
#

yes, but the problem of shadow persist

#

ok, thanks for the support

boreal imp
#

Shadow casting and self-shading are different things. We're discussing self-shading.

#

just to be clear

somber portal
#

yes it is

boreal imp
#

Shadow casting is another step.

somber portal
#

i wont shadow cast

boreal imp
#

OK.
Good luck, later.

heady acorn
#

Make sure the normal map texture is of type normal map, or try to get your lighting to work first without it altogether

somber portal
#

my light wont work even without tex

heady acorn
#

Is it the exact same type of distortion?

somber portal
#

using a null texture

#

or i have to change the code too?

heady acorn
somber portal
#

[NoScaleOffset]_NormalTexture("NormalTexture", 2D) = "bump" {}

#

it should be neutral

#

as far as i remember

heady acorn
#

In that case I recommend you keep the normal map off until the lighting function works as expected

somber portal
#

ok, what i should use as facing vecotr to calculate with light pos?

heady acorn
#

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

somber portal
#

mmm..
the question was to calculate normals using lightposition.

#

I know:

  1. pixel position
  2. light position
  3. texture normal that say where it is facing (correct me if i'm wrong)
heady acorn
#

Why are you trying to calculate normals? Don't your meshes have any?

somber portal
#

my mesh is lowpoly

#

no tiles on the wall as vertexes

heady acorn
#

So?

#

I think you still have vertex normals

somber portal
#

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

heady acorn
#

They're combined with interpolated vertex normals to add bumps to a surface

somber portal
#

how to add the normal tex to this?