bate's blog

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

FXComposer超入門8

ブリンフォン鏡面反射

ハーフベクトルを使うやつです。
ピクセルシェーダーで処理します。
ソースコードだけで済ませます。
ハイライトがでない場合はシーンにカメラをバインドしてください。


/*

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

float SpecularPower : Specular
<
	string UIName = "Specular Power";
	string UIWidget = "Slider";
	float UIMin = 0.0f;
	float UIMax = 50.0f;
	float UIStep = 0.1f;
> = 10.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 H = normalize(normalize(IN.L)+E);
	return IN.Color + pow(max(0,dot(N,H)),SpecularPower);
}

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