bate's blog

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

FXComposer超入門7

頂点単位のフォン鏡面反射をやります。

コードだけ載せます。
凸形状ではないので色々と変なところもあります。

/*

% 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";>;

// Camera
float4 CameraPos : Position
<
	string UIName = "Camera Pos";
	string Object = "PerspectiveCamera";
	string Space = "World";
> = { 10.0f, 50.f, 10.f, 1.0f };

// 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 localLightPos = mul((float3x3)IW, PointLightPos.xyz);
	float3 localCameraPos = mul((float3x3)IW, CameraPos.xyz);
	
	float4 amb = AmbientLight * Ka;
	float4 dif = PointLightColor * Kd;
	
	float3 L = normalize(localLightPos-IN.Pos.xyz);
	float3 N = normalize(IN.Normal);
	
	float3 E = normalize(localCameraPos-IN.Pos.xyz);	// dir from facet to eye
	float3 R = -E+2.0f*dot(N,E)*N;
	float sign = max(0, dot(L,N));
	float coe = sign>0.0f?1.0f:0.0f;
	
	OUT.Color = amb + dif * sign + coe*pow(max(0,dot(-L,R)),16);
	
	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();
	}
}