HLSL Shading for 3dsMax 02 - Diffuse Lighting

Purpose:
Create a Shader to Implement the Lambert's Cosine Law.

Solution:
1.Study of the Lambert's Cosine Law from <<Introduction to 3D Game Programming with DirectX 10>>

HLSL Shading for 3dsMax 02 - Diffuse Lighting_第1张图片

2.Shader Source

float4x4 wvp : WorldViewProjection <string UIWidget="None" ;>;
float4x4 w : World <string UIWidget="None";>;

float4 LightPosition : POSITION
<
	string UIName="Light Position";
	string Object="PointLight";
	string Space="World";
	int refID=0;
	
> =float4(100,100,100,0);

float4 LightColor: LIGHTCOLOR
<
	int LightRef=0;
> =float4(1,1,1,0);

struct VS_INPUT
{
	float4 Pos: POSITION;
	float4 Normal: NORMAL;
};

struct VS_OUTPUT
{
	float4 Pos : POSITION0;
	float3 L: TEXCOORD0;
	float3 N: TEXCOORD1;
};

VS_OUTPUT vs_main(VS_INPUT Input)
{
	VS_OUTPUT Result;
	Result.Pos = mul(Input.Pos,wvp);
	
	float4 worldPos=mul(Input.Pos,w);
	Result.L=LightPosition-worldPos;
	Result.N=mul(Input.Normal,w);
	
	return Result;
}

float4 ps_main(VS_OUTPUT Input):COLOR
{
	float3 N=normalize(Input.N);
	float3 L=normalize(Input.L);
	
	float NdotL=max(dot(N,L),0);
	float4 Result=NdotL*LightColor;
	return float4(Result.xyz,1);
}

technique DiffuseLighting
{
	pass Lambert
	{
		VertexShader = compile vs_2_0 vs_main();
		PixelShader  = compile ps_2_0 ps_main();
	}
};


3.Load the shader and add a Omi(Point) light to the scene.

HLSL Shading for 3dsMax 02 - Diffuse Lighting_第2张图片

4.try move the light around to see the effect

HLSL Shading for 3dsMax 02 - Diffuse Lighting_第3张图片

HLSL Shading for 3dsMax 02 - Diffuse Lighting_第4张图片

HLSL Shading for 3dsMax 02 - Diffuse Lighting_第5张图片


你可能感兴趣的:(shader,FX,HLSL,3DSMAX)