Hello, while I was attempting to write a simple shader, I encountered issues with my float3 parameter named DiffuseLightDirection
: For some reason it gets optimised out (propably replaced by a constant) even if it is actually used in one line of code.
Here’s the shader’s HLSL code:
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
matrix World;
matrix View;
matrix Projection;
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;
float4x4 WorldInverseTranspose;
float3 DiffuseLightDirection;
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Normal : NORMAL0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
float4 normal = mul(input.Normal, WorldInverseTranspose);
float lightIntensity = dot(normal, DiffuseLightDirection);
output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return saturate(input.Color + AmbientColor * AmbientIntensity);
}
technique Ambient
{
pass Pass1
{
VertexShader = compile VS_SHADERMODEL VertexShaderFunction();
PixelShader = compile PS_SHADERMODEL PixelShaderFunction();
}
}
And here’s my drawing C# function:
void DrawModelEffect(Model Model,ModelMatrices matrices,Effect effect)
{
foreach(ModelMesh mesh in Model.Meshes)
{
foreach(ModelMeshPart part in mesh.MeshParts)
{
part.Effect = effect;
effect.Parameters["View"].SetValue(matrices.view);
effect.Parameters["Projection"].SetValue(matrices.projection);
effect.Parameters["World"].SetValue(matrices.translation * mesh.ParentBone.Transform);
AmbientShader.Parameters["AmbientColor"].SetValue(AmbientColor.ToVector4());
AmbientShader.Parameters["AmbientIntensity"].SetValue(AmbientIntensity);
AmbientShader.Parameters["DiffuseLightDirection"].SetValue(new Vector3(1,1,0));
Matrix worldInverseTransposeMatrix = Matrix.Transpose(Matrix.Invert(mesh.ParentBone.Transform * matrices.translation));
effect.Parameters["WorldInverseTranspose"].SetValue(worldInverseTransposeMatrix);
}
mesh.Draw();
}
}
For some reason the AmbientShader.Parameters dictionary doesn’t contain the parameter I wish to change (as seen in the picture below)
Thanks for your attention.