読者です 読者をやめる 読者になる 読者になる

bate's blog

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

メモ

スペキュラマップだけ、光源の位置とカメラ位置の影響を受ける。

/*

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

keywords: material classic

date: YYMMDD

*/

// My Add
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";
>;

// Light
float4 PointLightPos : Position
<
	string UIName = "Point Light Pos";
	//string UIWidget = "none";
	string Object = "PointLight";
	string Space = "World";
>;

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

float4 FloorColor : Color
<
	string UIName = "Floor Color";
	string UIWidget = "Color";
> = { 0.1f, 0.8f, 0.1f, 1.f };

texture FloorTexture : Diffuse
<
	string ResourceName = "red.bmp";
	string ResourceType = "2D";
	string UIName = "Surface Texture";
>;

sampler2D FloorSampler = sampler_state
{
	Texture = <FloorTexture>;
	MinFilter = LinearMipMapLinear;
	MagFilter = Linear;
};

texture SpecularTexture : Diffuse
<
	string ResourceName = "specular.bmp";
	string ResourceType = "2D";
	string UIName = "Surface Texture";
>;

sampler2D SpecularSampler = sampler_state
{
	Texture = <SpecularTexture>;
	MinFilter = LinearMipMapLinear;
	MagFilter = Linear;
};

//-----------------------------------------------
// Struct
//-----------------------------------------------

// input data from vertex buffer
struct appdata
{
	float4 Position		: POSITION;
	float3 Normal		: NORMAL;
	float2 UV			: TEXCOORD0;
};

// pass from vertex program to pixel program
struct VertexOutput
{
	float4 Position		: POSITION;
	float3 Normal		: TEXCOORD0;
	float2 UV			: TEXCOORD1;
	float3 Eye			: TEXCOORD2;
	float3 L;
	float Dist;
};

// vertex shader entry point
VertexOutput mainVS( appdata IN )
{
	VertexOutput OUT = (VertexOutput)0;
	
	float3 localCameraPos = mul((float3x3)IW, PointLightPos.xyz);
	float3 localLightPos = mul((float3x3)IW, CameraPos.xyz);
	OUT.Position = mul( WVP, IN.Position );
	OUT.Normal = normalize( IN.Normal );
	OUT.UV = IN.UV;
	OUT.Eye = localCameraPos-IN.Position.xyz;
	OUT.L = normalize( localLightPos-IN.Position.xyz );
	OUT.Dist = distance( localLightPos.xyz, IN.Position.xyz );
	return OUT;
}

// pixel shader entry point
float4 mainPS( VertexOutput IN ) : COLOR
{
	// My Add
	float3 N = normalize(IN.Normal);
	float3 E = normalize(IN.Eye);
	float3 R = -E+2.0f*dot(N,E)*N;
	float dist = IN.Dist;
	float dist2 = dist*dist;
	float attenu = 1;//(1+1*dist+1*dist2);
	
	float4 color = tex2D( FloorSampler, IN.UV );
	float4 specular = tex2D( SpecularSampler, IN.UV );
	float coeff = max(0, dot(IN.L, R));
	return color + attenu*coeff*specular;// * FloorColor;
}

technique technique0 {
	pass p0 {
		CullFaceEnable = false;	
		VertexProgram = compile vp40 mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}
広告を非表示にする