bate's blog

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

FXComposer超入門6

フォン鏡面反射をやります。
前回のローカル計算のランバートを使います。
基本的には、鏡面反射分を足すだけです。


1.新しいプロジェクトを作成します。
2.ジオメトリを作ります。ここではトーラスを選びます。
3.ポイントライトを作成します。
4.新しいエフェクトを作成します。
5.前回のランバートのソースコードをコピペする。
6.トーラスにマテリアルを適応する。
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;
	float3 Normal	: TEXCOORD0;
	float3 Eye		: TEXCOORD1;
	float3 L;
};

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 localCameraPos = mul((float3x3)IW, CameraPos.xyz);
	
	float4 amb = AmbientLight * Ka;
	float4 dif = PointLightColor * Kd;
	
	OUT.L = normalize(localLightPos-IN.Pos.xyz);
	OUT.Eye = localCameraPos-IN.Pos.xyz;	// dir from facet to eye
	OUT.Normal = normal;
	OUT.Color = amb + dif * max(0, dot(OUT.L, normal));
	
	return OUT;
}

float4 mainPS(V2Pdata IN) :COLOR
{
	float3 N = normalize(IN.Normal);
	float3 E = normalize(IN.Eye);
	float3 R = -E+2.0f*dot(N,E)*N;
	return IN.Color + pow(max(0,dot(IN.L,R)),10);
}

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

8.下の絵1のようになっていることを確認する。

9.ハイライトの部分だけピクセル単位で処理しています。
10.左側の[Geometries]→[Torus]を右クリックして[Properties]を選ぶ。
11.[Properties]ウィンドウの[Torus]の[NumSegments]を16に、[NumSides]を8にする。
12.ポリゴン数を減らしてローポリにします。
13.ハイライトの部分が形状を滑らかにしていることを確認する。
14.おしまい。