bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

FXComposer超入門5

FXComposer超入門と言いつつもCgFX入門になってしまっているけど気にしない。
超入門3,4ではワールドでライティング計算をしたが、
今回はTeapotのローカル座標でライティング計算を行う。


手順は前回、前々回を参考にすれば大丈夫なのでコードだけを示す。
Teapotのライティングが前回と同じならOK。

/*

% Description of my shader.
% Second line of description for my shader.

keywords: material classic

date: YYMMDD

*/

float4x4 WIT : WorldInverseTranspose <string UIWidget = "none";>;
float4x4 WVP : WorldViewProjection <string UIWidget = "none";>;
float4x4 W : World <string UIWidget = "none";>;
float4x4 VI : ViewInverse <string UIWidget = "none";>;
float4x4 IW : WorldInverse <string UIWidget = "none";>;


// Light
float4 PointLightPos : Position
<
	string UIName = "Point Light Pos";
	//string UIWidget = "none";
	string Object = "PointLight";
	string Space = "World";
> = { 5.0f, 0.0f, 0.0f, 1.0f };

float4 PointLightColor : Diffuse
<
	string UIName = "Point Light Color";
	string UIWidget = "Color";
	string Object = "PointLight";
> = { 1.0f, 0.5f, 0.4f, 1.0f };

float4 AmbientLight : Ambient
<
	string UIName = "Ambient Light";
	string UIWidget = "Color";
> = { 0.3f, 0.3f, 0.78f, 1.0f };

// Sphere
float4 Kd : Diffuse
<
	string UIName = "Sphere Diffuse";
	string UIWidget = "Color";
> = { 0.1f, 0.7f, 0.2f, 1.0f };

float4 Ka
<
	string UIName = "Sphere Ambient";
	string UIWidget = "Color";
> = { 1.0f, 1.0f, 1.0f, 1.0f };


// struct
struct appdata
{
	float4 Pos		: POSITION;
	float3 Normal	: NORMAL0;
};

struct V2Pdata
{
	float4 HPos		: POSITION;
	float4 Color	: COLOR0;
};

V2Pdata mainVS(appdata IN)
{
	V2Pdata OUT = (V2Pdata)0;
	
	OUT.HPos = mul(WVP, IN.Pos);
	
	float3 normal = normalize(IN.Normal);
	float3 localLightPos = mul((float3x3)IW, PointLightPos.xyz);
	
	float3 L = normalize(localLightPos-IN.Pos.xyz);
	float3 N = normal;
	float4 amb = AmbientLight * Ka;
	float4 dif = PointLightColor * Kd;
	OUT.Color = amb + dif * max(0, dot(L,N));
	
	return OUT;
}

float4 mainPS(V2Pdata IN) :COLOR
{
	return IN.Color;
}

technique technique0 {
	pass p0 {
		CullFaceEnable = false;
		DepthTestEnable = true;
		VertexProgram = compile vp40 mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}