   hlsl splatting shader with lighting
  I want to add a directional light to this splatting shader. I have looked at a diffuse lighting shader and tried to merge it into the splatting shader but am having some difficulties.Quote://matricesfloat4x4 matViewProjection; float4x4 matWorld; //ambient variablesfloat4 AmbientColor;float4 AmbientIntensity; //diffuse variablesfloat4 DiffuseColor;float DiffuseIntensity;float3 DiffuseLightDirection; sampler AlphaMap = sampler_state {    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; };  sampler TextureOne = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; };  sampler TextureTwo = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; };  sampler TextureThree = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; };  struct VS_INPUT {    float4 Position : POSITION0;    float2 alphamap : TEXCOORD0;    float2 tex : TEXCOORD1;    float3 Normal : NORMAL0;};  struct VS_OUTPUT {    float4 Position : POSITION0;    float2 alphamap : TEXCOORD0;    float2 tex : TEXCOORD1;    float3 Normal : TEXCOORD2;};  struct PS_OUTPUT {    float4 diffuse : COLOR0; };  VS_OUTPUT vs_main( VS_INPUT Input ) {    VS_OUTPUT Output;    Output.Position = mul( Input.Position, matViewProjection ); //transform the position    Output.alphamap = Input.alphamap;    Output.tex = Input.tex;Output.Normal = normalize(mul(Input.Normal, matWorld)); //transform normal and normalize    return( Output ); }  PS_OUTPUT ps_main(in VS_OUTPUT input) {    float texScale = 1.0;    PS_OUTPUT output = (PS_OUTPUT)0;vector a = tex2D(AlphaMap, input.alphamap);    vector i = tex2D(TextureOne, mul(input.tex, texScale));   vector j = tex2D(TextureTwo, mul(input.tex, texScale));    vector k = tex2D(TextureThree, mul(input.tex, texScale));float4 oneminusx = 1.0 - a.x;    float4 oneminusy = 1.0 - a.y;    float4 oneminusz = 1.0 - a.z;vector l = a.x * i + oneminusx * i;    vector m = a.y * j + oneminusy * l;    vector n = a.z * k + oneminusz * m;float4 diffuse = dot(DiffuseLightDirection, input.Normal) * DiffuseIntensity * DiffuseColor;    float4 ambient = AmbientIntensity * AmbientColor;   // output.diffuse = n;    output.diffuse.rgb = output.diffuse.rgb + saturate(diffuse + ambient);    return output; }  technique Default_DirectX_Effect {    pass Pass_0    {  VertexShader = compile vs_2_0 vs_main();  PixelShader = compile ps_2_0 ps_main();    } }can somebody help me get lighting into this splatting shader? Because the fixed function lighting wont work if I use the shader, so i have to roll my own.   
  Thought this might do it but am having some trouble with this too.Quote://matricesfloat4x4 matViewProjection; float4x4 matWorld;//ambient variablesfloat4 AmbientColor = float4(1.0f, 1.0f, 1.0f, 1.0f);float AmbientIntensity = 0.1f;//diffuse variablesfloat4 DiffuseColor = float4(0.0f, 0.0f, 1.0f, 1.0f);float DiffuseIntensity = 1.0f;sampler AlphaMap = sampler_state {    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; }; sampler TextureOne = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; }; sampler TextureTwo = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; }; sampler TextureThree = sampler_state {    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;    ADDRESSU = WRAP;    ADDRESSV = WRAP;    ADDRESSW = WRAP; }; struct VS_INPUT {    float4 Position : POSITION0;    float2 alphamap : TEXCOORD0;    float2 tex : TEXCOORD1;    float3 Normal : NORMAL0;   float4 Color : COLOR0;}; struct VS_OUTPUT {    float4 Position : POSITION0;    float2 alphamap : TEXCOORD0;    float2 tex : TEXCOORD1;    float3 Normal : TEXCOORD2;   float4 Color : COLOR0;   float3 L : TEXCOORD3;}; struct PS_OUTPUT {    float4 Color : COLOR0; }; VS_OUTPUT vs_main( VS_INPUT Input ) {    VS_OUTPUT Output;    Output.Position = mul( Input.Position, matViewProjection ); //transform the position    Output.alphamap = Input.alphamap;    Output.tex = Input.tex;    Output.Color = Input.Color;   float3 lightdirection = float3(0.0f, 1.0f, 0.0f);   Output.L = normalize(lightdirection);   Output.Normal = normalize(mul(Input.Normal, matWorld));   return( Output ); } sampler2D tex0 : register(s4); PS_OUTPUT ps_main(in VS_OUTPUT input) {    float texScale = 1.0;    PS_OUTPUT output = (PS_OUTPUT)0;    vector a = tex2D(AlphaMap, input.alphamap);    vector i = tex2D(TextureOne, mul(input.tex, texScale));   vector j = tex2D(TextureTwo, mul(input.tex, texScale));    vector k = tex2D(TextureThree, mul(input.tex, texScale));    float4 oneminusx = 1.0 - a.x;    float4 oneminusy = 1.0 - a.y;    float4 oneminusz = 1.0 - a.z;    vector l = a.x * i + oneminusx * i;    vector m = a.y * j + oneminusy * l;    vector n = a.z * k + oneminusz * m;    float4 Ambient_Light = AmbientIntensity * AmbientColor;   float NdotL = dot(input.Normal, input.L);   float4 Diffuse_Light = DiffuseIntensity * DiffuseColor * saturate(NdotL);   output.Color = n;    //output.Color = (Ambient_Light + Diffuse_Light) * tex2D(tex0, input.tex);   return output; } technique Default_DirectX_Effect {    pass Pass_0    {  VertexShader = compile vs_2_0 vs_main();  PixelShader = compile ps_2_0 ps_main();    } };  What trouble are you having?   ;  Well the terrain is showing up all black. If I take out the lighting code in the shader it works fine.   ;Quote:Original post by Mhartwig123Well the terrain is showing up all black. If I take out the lighting code in the shader it works fine.The shader works differently if you take out the lighting code.However, have you tried to check factor by factor what is the reason of the black output?//output.Color = (Ambient_Light + Diffuse_Light) * tex2D(tex0, input.tex);If Ambient_Light + Diffuse_Light is zero, you'll get black terrain.If tex2D(tex0, input.tex) is zero, you'll get black terrain.Simply : check those values by outputting them to screen.One thing I noticed : diffuse = dot(DiffuseLightDirection, input.Normal) may result negative values. You should clamp the result between [0..1]Good luck![Edited by - kauna on May 12, 2010 5:47:22 AM]