Gamestudio Links
Zorro Links
Newest Posts
TradeVars not working with pending trades
by jcl
2 seconds ago
Creating a Directory
by txesmi
Yesterday at 10:15
Multi asset correlation
by jcl
Yesterday at 09:57
Help in analysis...
by MINER
Yesterday at 09:13
Strange problem with a global int
by jcl
Yesterday at 08:48
New Zorro version 2.03
by jcl
Yesterday at 08:28
not using profit targets during strategy development
by laz
Yesterday at 00:23
AUM Magazine
Latest Screens
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Evonet Beta v1.0 : Online Project Development Template
Who's Online
6 registered (kvm, 20BN, Dalla, AndrewAMD, 2 invisible), 581 Guests and 7 Spiders online.
Key: Admin, Global Mod, Mod
Newest Members
Pelin27, Aziz, lucianopneto, j45p41, Max_Ahn
18173 Registered Users
Topic Options
Rate This Topic
#474049 - 09/13/18 19:45 How to recalculate normals after a shader moves vertices
jumpman Offline
Serious User

Registered: 04/17/02
Posts: 1222
Loc: ny
Hello, how do I recalculate a model's normals after I move the vertices within the vertex shader? Is that possible?
_________________________
A8 Pro 8.47

Top
#474195 - 09/28/18 11:03 Re: How to recalculate normals after a shader moves vertices [Re: jumpman]
txesmi Online
Serious User

Registered: 06/13/07
Posts: 1211
Loc: Hiporope and its pain
No, it is not possible. There is no way of accesing vertex buffers.

Salud!

Top
#474196 - 09/28/18 13:35 Re: How to recalculate normals after a shader moves vertices [Re: txesmi]
Superku Offline
Senior Expert

Registered: 09/13/03
Posts: 6813
Loc: Kiel (Germany)
I try to approximate correct normals manually after moving vertices in the vertex shader. For example when I rotate vertices I rotate the normal as well, or when the movement is sine based I add cosine values with the same argument. It's not exact but with the normalization in the pixel shader it oftentimes improves the result over leaving the normal unchanged.
_________________________
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Superku - stuck in development hell: http://store.steampowered.com/app/407570

Check out my new game: Pogostuck: Rage With Your Friends

Top
#474219 - 10/01/18 01:03 Re: How to recalculate normals after a shader moves vertices [Re: Superku]
jumpman Offline
Serious User

Registered: 04/17/02
Posts: 1222
Loc: ny
How do you approximate normals after moving them in the XYZ directions?

(btw how are you rotating vertices?! laugh )

Here is a small version of what Im doing now:

Code:
-----VERTEX SHADER-----
InPos += mul(offset14,matWorldInv );  // move vertex

// Transform the vertex from object space to clip space: 
OutPos = mul(InPos, matWorldViewProj);  //  OutPos = mul(InPos, matWorldViewProj); 
  

// Transform the normal from object space to world space: 
  OutNormal = normalize(mul(InNormal, matWorld));



Edited by jumpman (10/01/18 01:08)
_________________________
A8 Pro 8.47

Top
#474223 - 10/01/18 13:02 Re: How to recalculate normals after a shader moves vertices [Re: jumpman]
Superku Offline
Senior Expert

Registered: 09/13/03
Posts: 6813
Loc: Kiel (Germany)
Well in that shader code you quoted all vertices are moved by the same offset, no? Then the normal should stay the same. You'd have to show other code.
The approximation always depends on how you move the vertices, there's no general solution.

My rotation code usually looks something like this:
float effect = InPos.y/32.0; // depends on the model and type of "effect" you want to achieve, this would be for a vertical twist motion
float angle = sin(vecTime.w*0.1)*effect;
float sina = sin(angle);
float cosa = cos(angle);
float oldValue = InPos.x;
InPos.x = cosa*InPos.x - sina*InPos.z;
InPos.z = sina*oldValue + cosa*InPos.z;

If you want to rotate around any other position then the origin/ nullvector, subtract some shift vector or value first, do the rotation and then add the same amount again.
_________________________
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Superku - stuck in development hell: http://store.steampowered.com/app/407570

Check out my new game: Pogostuck: Rage With Your Friends

Top
#474264 - 10/02/18 18:21 Re: How to recalculate normals after a shader moves vertices [Re: Superku]
jumpman Offline
Serious User

Registered: 04/17/02
Posts: 1222
Loc: ny
Hey superku, I forgot to add the vertex movement part based on texture. This uses tex2dlod, which is a texture fetch within the vertex shader. Wherever there is color information, these areas vertices will be moved:

Code:
////---- TOP LEFT -- CLOTH 0 RED
float4 cloth1 = tex2Dlod(clothSampler5, float4(InTex.x*0.5f,InTex.y*0.5f,0,0) ); 
float3 offset1 = float3(cloth1.r*vertMove0.r,cloth1.r*vertMove0.g,cloth1.r*vertMove0.b);

InPos += mul(offset1,matWorldInv );

// Transform the vertex from object space to clip space: 
OutPos = mul(InPos, matWorldViewProj);  //  OutPos = mul(InPos, matWorldViewProj); 
  

// Transform the normal from object space to world space: 
  OutNormal = normalize(mul(InNormal, matWorld));



As you can see, vertices are moved in the cardinal directions
_________________________
A8 Pro 8.47

Top


Moderator:  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de