Jump to content
  • Advertisement
Sign in to follow this  
M_Johnson

Multiple Shaders

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

So I have 3 shaders in seperate files:

Shader 1: Responsible for skinning, transforms vertices based current bone transforms.
Shader 2: Performs normal mapping based on a single light source
Shader 3: Handles shadows that are being cast on the character.

What would be the best way of combining these 3 shaders so I can have a skinned model with normal mapping and able to interact with shadows? One option I thought of was to make it so the shaders contained nothing but the actual functions themselves (removal of global variables) then create an uber technique which #includes the vertex/pixel shaders from these 3 files and implements each one as a pass, then stick all the required globals in the uber technique and blend between the outputs to the render target, I'm not even sure that this would produce the desired results though or if I'd get lots of weird lighting/shadowing artifacts due to the skinning stage.

I'm trying to avoid implementing the whole lot in one vertex/pixel shader. Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
One way to handle the light is to have a hidden HLSL header in the engine that include subroutines for handling the light so that you can add new light types from the engine without changing the shaders.

Example:

void Engine_GetDiffuseAndSpecularLight ( in float3 Pos_WorldSpace, in float3 Normal_WorldSpace, in float SpecularSharpness, out float3 SumOfDiffuseLight, out float3 SumOfSpecularLight ) {
int i;
float3 Direction;
float Dist;
float Intensity;
float SpecularLevel;
float3 ProjectedPos;
float3 UndirectedLight;
float ProjectionIntensity;
float2 ConeProjection;
float ConeDist;
float3 ReflectedViewDirection;

ReflectedViewDirection = reflect(normalize(Pos_WorldSpace - CameraPos_WorldSpace), Normal_WorldSpace);

// Start with ambient light and no specular light
SumOfDiffuseLight = AmbientLight;
SumOfSpecularLight = float3(0.0f, 0.0f, 0.0f);

// Get the sum of diffuse light from each light source
for(i = 0; i < NumberOfLights; i++) {
switch(Lights.Type) {
case 0: // Point light
Dist = length(Pos_WorldSpace - Lights.Pos);
if (Dist < Lights.Radius) {
Direction = normalize(Pos_WorldSpace - Lights.Pos);
Intensity = pow(((Lights.Radius - Dist) / Lights.Radius), 2.0f);
UndirectedLight = Lights.Color * Intensity;
SumOfDiffuseLight += saturate(-dot(Direction,Normal_WorldSpace)) * UndirectedLight;
SpecularLevel = saturate(-dot(Direction,ReflectedViewDirection));
SumOfSpecularLight += pow(SpecularLevel * SpecularLevel, SpecularSharpness) * UndirectedLight;
}
break;
case 1: // Sun light
UndirectedLight = Lights.Color;
SumOfDiffuseLight += saturate(-dot(Lights.ZAxis,Normal_WorldSpace)) * UndirectedLight;
SpecularLevel = saturate(-dot(Lights.ZAxis,ReflectedViewDirection));
SumOfSpecularLight += pow(SpecularLevel * SpecularLevel, SpecularSharpness) * UndirectedLight;
break;
case 2: // Spot light
Dist = length(Pos_WorldSpace - Lights.Pos);
if (Dist < Lights.Radius) {
ProjectedPos = mul(Pos_WorldSpace - Lights.Pos, transpose(float3x3(Lights.XAxis,Lights.YAxis,Lights.ZAxis)));
ConeProjection = float2(ProjectedPos.x / (ProjectedPos.z * Lights.WidthSlope),ProjectedPos.y / (ProjectedPos.z * Lights.HeightSlope));
ConeDist = length(ConeProjection);
if (ConeDist < 1.0f && ProjectedPos.z > 0.0f) {
Direction = normalize(Pos_WorldSpace - Lights.Pos);
Intensity = pow(((Lights.Radius - Dist) / Lights.Radius), 2.0f);
ProjectionIntensity = 1.0f - pow(ConeDist,2.0f);
UndirectedLight = Lights.Color * Intensity * ProjectionIntensity;
SumOfDiffuseLight += saturate(-dot(Direction,Normal_WorldSpace)) * UndirectedLight;
SpecularLevel = saturate(-dot(Direction,ReflectedViewDirection));
SumOfSpecularLight += pow(SpecularLevel * SpecularLevel, SpecularSharpness) * UndirectedLight;
}
}
break;
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!