⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lighting.fx

📁 这也是一个游戏编程基础入门的事例!它是通过鼠标的移动改变照相机的仰角
💻 FX
字号:


//光照计算

struct Mtrl
{
	float4 ambient;
	float4 diffuse;
	float4 specular;
	float  specPower;
};

struct DirLight
{
	float4 ambient;
	float4 diffuse;
	float4 specular;
	float3 direction;  
};


float4x4	g_world;
float4x4	g_worldInvTrans;
float4x4	g_WVP;
Mtrl		g_mtrl;
DirLight	g_light;
float3		g_eyePosW;
texture		g_tex;

sampler TexS = sampler_state
{
	Texture = <g_tex>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = WRAP;
    AddressV  = WRAP;
};

struct	VSOut
{
	float4 posH: POSITION0;
	float2 tex0: TEXCOORD0;
	float4 diffuse: COLOR0;
	float4 specular: COLOR1;
};

VSOut VS(float3 posL: POSITION0,float3 normalL: NORMAL0,float2 tex0: TEXCOORD0)
{
	
	VSOut V = (VSOut)0;
	
	//transform to homogenous space
	V.posH = mul(float4(posL,1.0f),g_WVP);
	
	float3 normalW	= mul(float4(normalL,0.0f),g_worldInvTrans).xyz;
	float3 posW		= mul(float4(posL,1.0f),g_world).xyz;
	
	//光照计算
	//漫射光
	float d = max(dot(normalW,-g_light.direction),0);
	float diffuse = d * (g_mtrl.diffuse*g_light.diffuse).rgb;
	
	//镜面光
	float3 r = reflect(g_light.direction,normalW);
	float3 toEyeW = normalize(g_eyePosW - posW);
	float p = max(dot(r,toEyeW),0);
	float s = pow(abs(p),g_mtrl.specPower);
	float3 specular = s * (g_mtrl.specular,g_light.specular).rgb;
	
	//环境光
	float3 ambient = (g_mtrl.ambient * g_light.ambient).rgb;
	
	V.diffuse = float4(ambient + diffuse,g_mtrl.diffuse.a);
	V.specular = float4(specular,1.0f);
	
	V.tex0 = tex0;
	
	return V;
	
}


float4 PS(float2 tex0: TEXCOORD0,float4 d: COLOR0,float4 s: COLOR1): COLOR
{
	float4 texColor = tex2D(TexS,tex0);
	float4 diffuse = d * texColor;

	return diffuse;
	
}


technique Tech
{
	pass P0
	{
		vertexShader = compile vs_2_0 VS();
		pixelShader = compile ps_2_0 PS();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -