Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Help calculating normals

Author
Message
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 5th Jul 2019 15:21
Hoping someone can help here, as this has been a sticking point for months for me.

Can you calc mesh normals in a vertex shader alone? For example, I have water that moves in the vertex shader, but that objviously doesn't fix the normals. Everything I found about calculating normals states you have to reference all three vertices, which needs a geometry shader. Has anyone found a way around this?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 5th Jul 2019 18:33
not sure if this helps but I found this about getting the vertex position


I think that should allow you to normalize
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 5th Jul 2019 22:04
I'm not sure that's it......you need all three sets of cords I think......that make up the triangle.
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 5th Jul 2019 22:19 Edited at: 5th Jul 2019 22:23
Yes you can calculate normals in the vertex shader.

im currently intoxicated and replying on my phone so will try post an example tomorrow.

Using water as an example you are probably calculating a height using a maths equation, some hash function or a combination of noise images.... In any case you calculate the height of your vertex position and you calculate the height north of your position and east of it... So you get vectors parrallel to your surface in the x direction and in the z direction... or dy/dx and dy/dz values.

You then cross product these to get your normal. It works every time.

So each vertex calc actually needs 3 heights...one for the current position one for a position slightly +x and one for the position further into the screen +z. With those heights you have a normal.



you dont even have to cross product ...theres a real quick cheat where you just modify your existing straight up normal slightly but I iwll try clarify that tomorrow when im sober and not dying of a hangover...

You wouldnt want to try and use triangle normals as there could be 8 or more tringles sharing that one vertex anyway so this method is great and easy.
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 6th Jul 2019 00:00
As always, I bow to your genius.

If you can expand slightly more that would be awesome, but I'm away for the weekend myself so theres really no rush. Studio isn't working properly in 3d for my code / shaders yet either, so at the moment I'm "pausing" development a little, so this will give me something to work on.
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 6th Jul 2019 01:33
This can't be done via a Vertex Shader (although you could generate it via the Pixel / Fragment Shader as a World-Object Normal Transformed to a World-View-Projected Normal Map.
Now you can do Normal Calculation one of two ways.

Either:
a) within a Mesh Memblock when you're creating the initial Mesh
b) within a Geometry / Hull Shader (but AFAIK only AppGameKit Studio Supports that without a 3rd Party Plug-In)

Calculating Surface Normals is fairly simple though


janbo
16
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 7th Jul 2019 22:23
Simple vertex shaders don't have the knowledge about neighboring vertices except you pass the informations. But if you calculate the movement of the vertices in the shader itself via an equation, you should be able to get the neighboring vertex position and also calculate the normals for the vertices with the method Bengismo mentioned.
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 29th Jul 2019 12:34
Thanks everyone.

What I'm trying to do should be quite simple......I do the same thing with my grass to make sure it works with shadows......but for some reason setting the normal on the water doesn't work within the shader. It's very likely just something daft I'm missing, I'll keep plodding on once I finish the part I'm working on.
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 30th Jul 2019 12:39
I am curious, how do you make grass work with shadows? I assume you mean the shadows not swaying with the grass.
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 30th Jul 2019 16:25
No, I mean so that the grass receives shadows correctly. Because I have a day / night cycle, the grass only got the shadow from a point correctly, and didnt light correctly.

I manually fix this in the shader, but a similar technique wasnt working for the water.
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 30th Jul 2019 22:41
Ah, gotcha.

Login to post a reply

Server time is: 2024-11-22 16:17:05
Your offset time is: 2024-11-22 16:17:05