HLSL directional lighting: How to update lighting after rotating model ?

Started by
0 comments, last by gomidas 5 years, 8 months ago

Hello, I made a directional light with diffuse. Now If I rotate my camera I see changes. Some parts naturally becomes darker. I did not pass parameters from variables I directly put them to see if that works. My problem, if I rotate or move cam I see changes but if I rotate or move mesh I cant see changes. What should I add to see the differences when mesh rotated ?

 


Texture2D ShaderTexture : register(t0);
Texture2D ShaderTextureNormal : register(t1);

SamplerState Sampler : register(s0);

cbuffer CBufferPerFrame{
float4 AmbientColor = float4(1.0f,1.0f,1.0f,0.0f);
float4 LightColor = float4(1.0f,1.0f,1.0f,1.0f);
float3 lightDirection = float3(0.0f,0.0f,-1.0f);
}

cbuffer CBufferPerObject{
float4x4 worldViewProj;
float4x4 world;
}

struct VS_IN
{
	float4 pos : POSITION;
	float3 normal : NORMAL; // Normal - for lighting
	float4 col : COLOR;
	float2 TextureUV: TEXCOORD0; // Texture UV coordinate

};


struct PS_IN
{
	float4 pos : SV_POSITION;
	float4 col : COLOR;
	float2 TextureUV: TEXCOORD;
	float3 normal : NORMAL;
    float3 tangent : TANGENT;
    float3 binormal : BINORMAL;
	float3 lightDirection:LIGHT;
	float3 WorldPos:POSITION1;

};


PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;

	output.pos = mul(input.pos, worldViewProj);
	output.col = 1.0f-((input.pos.w /* * input.col*/) / (input.pos.z /* *input.col*/));

	output.TextureUV = input.TextureUV;
	output.normal = normalize(mul(float4(input.normal,0), world).xyz);
	output.lightDirection=float3(0.0f,0.0f,-1.0f);

	output.tangent = CalculateTangent(input.normal);
	output.col = input.col;
	output.WorldPos = mul( input.pos, world );
	return output;
}

float4 PS( PS_IN input ) : SV_Target
{
	float3 sampledNormal = (2*ShaderTextureNormal.Sample(Sampler,input.TextureUV).xyz) - 1.0f;
	float3x3 tbn = float3x3(input.tangent, input.binormal, input.normal);
	sampledNormal = mul(sampledNormal, tbn);

	float4 N = ShaderTextureNormal.Sample(Sampler, input.TextureUV)*2.0f-1.0f;
	float4 D = ShaderTexture.Sample(Sampler, input.TextureUV);


	float3 lightDirection = normalize(-1*input.lightDirection);

	float n_dot_1 = dot (lightDirection, input.normal);
	float3 ambient =  D.rgb * float3(1.0f,1.0f,1.0f) * 0;
	float3 diffuse=(float3)0;

if( N.x == -1 && N.y == -1  && N.z == -1  && N.w == -1)
{
	//HERE
	if(n_dot_1>0){
	 diffuse = D.rgb* float3(1,1,1) * 1 * n_dot_1;
	}
	input.col.rgb=(ambient+ diffuse);
	input.col.a = D.a;
}
else
{
	//Not used for now.
	//input.col = saturate(dot(N,input.lightDirection));
}

	return input.col;
}

 

This topic is closed to new replies.

Advertisement