How to recalculate normals after a shader moves vertices

Posted By: jumpman

How to recalculate normals after a shader moves vertices - 09/13/18 17:45

Hello, how do I recalculate a model's normals after I move the vertices within the vertex shader? Is that possible?
Posted By: txesmi

Re: How to recalculate normals after a shader moves vertices - 09/28/18 09:03

No, it is not possible. There is no way of accesing vertex buffers.

Salud!
Posted By: Superku

Re: How to recalculate normals after a shader moves vertices - 09/28/18 11:35

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.
Posted By: jumpman

Re: How to recalculate normals after a shader moves vertices - 09/30/18 23:03

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));

Posted By: Superku

Re: How to recalculate normals after a shader moves vertices - 10/01/18 11:02

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.
Posted By: jumpman

Re: How to recalculate normals after a shader moves vertices - 10/02/18 16:21

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
© 2024 lite-C Forums