bate's blog

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

FXComposer超入門9

ポストエフェクトをやってみます。
画面全体に指定した色を加算します。
超簡単なポストエフェクトです。


これまでとの違いは、
・レンダーターゲットの指定にScriptという要素が出てくること
・テクニックが2パスになること
です。


1.前回のBlinn-Phongのプロジェクトを使います。
2.空のCgFXエフェクトを追加します。
3.下のコード1を空のCgFXにコピペします。

/*

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

keywords: material classic

date: YYMMDD

*/

float Script : STANDARDSGLOBAL
<
	string UIWidget = "none";
	string ScriptClass = "scene";
	string ScriptOrder = "postprocess";
	string ScriptOutput = "color";
	string Script = "Technique=Main;";
> = 0.8;

float4x4 WorldViewProj : WorldViewProjection;

// full-screen
float4 ClearColor
<
	string UIWidget = "color";
	string UIName = "Clear Color";
> = { 0.0, 0.0, 0.0, 1.0 };

float ClearDepth
<
	string UIWidget = "none";
> = 1.0;

float4 MulColor : COLOR
<
	string UIName = "Full Screen Mul Color";
	string UIWidget = "color";
> = { 0.0, 0.0, 0.0, 1.0 };

float2 ViewportSize : VIEWPORTPIXELSIZE
<
	string UIName = "Screen Size";
	string UIWidget = "none";
>;

// render to texture
texture ScreenMap : RENDERCOLORTARGET
<
	float2 ViewPortRatio = { 1.0, 1.0 };
	int MipLevels = 1;
	string Format = "X8R8G8B8";
	string UIWidget = "none";
>;
sampler2D ScreenSampler = sampler_state
{
		texture = <ScreenMap>;
		WrapS = ClampToEdge;
		WrapT = ClampToEdge;
		MinFilter = Linear;
		MagFilter = Linear;
};

texture ObjMap : RENDERCOLORTARGET
<
	float2 ViewPortRatio = { 1.0, 1.0f };
	int MipLevels = 1;
	string Format = "X8R8G8B8";
	string UIWidget = "none";
>;
sampler2D objSampler = sampler_state
{
	texture = <ObjMap>;
	WrapS = ClampToEdge;
	WrapT = ClampToEdge;
	MinFilter = Linear;
	MagFilter = Linear;
};

texture DepthBuffer : RENDERDEPTHSTENCILTARGET
<
	float2 ViewPortRatio = { 1.0, 1.0 };
	string Format = "D24S8";
	string UIWidget = "none";
>;

// struct
struct V2Pdata
{
	float4 Position		: POSITION;
	float2 UV			: TEXCOORD0;
};

// shader entry point
V2Pdata objVS(float3 pos : POSITION, float2 UV : TEXCOORD0)
{
	V2Pdata OUT = (V2Pdata)0;
	OUT.Position = float4(pos, 1);
	OUT.UV = float2(UV.xy);
	return OUT;
}

float4 objPS(V2Pdata IN) : COLOR
{
	return tex2D(ScreenSampler, IN.UV);
}

V2Pdata mainVS(float3 pos : POSITION, float2 UV : TEXCOORD0)
{
	V2Pdata OUT = (V2Pdata)0;
	OUT.Position = float4(pos, 1);
	OUT.UV = float2(UV.xy);
	return OUT;
}

float4 mainPS(V2Pdata IN) : COLOR
{
	float4 full_screen_color = MulColor;
	float4 obj_color = tex2D(objSampler, IN.UV);
	return float4(full_screen_color+obj_color);
}



// tech
technique Main
<
	string Script =
		"RenderColorTarget0=ScreenMap;"
		"RenderDepthStencilTarget=DepthBuffer;"
			"ClearSetColor=ClearColor;"
			"ClearSetDepth=ClearDepth;"
			"Clear=Color;"
			"Clear=Depth;"
			"ScriptExternal=color;"
		"Pass=obj;"
		"Pass=main;";
>
{
	pass obj
	<
		string Script =
			"RenderColorTarget=ObjMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 objPS();
	}
	
	pass main
	<
		string Script =
			"RenderColorTarget0=;"
			"RenderDepthStencilTarget=;"
			"Draw=Buffer;";
	>
	{
		CullFaceEnable = false;
		DepthTestEnable = false;
		DepthMask = false;
		BlendEnable = false;
		VertexProgram = compile vp40 mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}

4.左側にある[Materials]に追加されたポストエフェクトのマテリアルを[Render]ウィンドウにドラッグ&ドロップします。
5.下の絵1のようになることを確認します。

絵1

6.ポストエフェクトのマテリアルのプロパティにある「Full Screen Mul Color」で色を変更して画面全体が変化することを確認します。
7.下の絵2のようになることを確認します。

絵2

8.おしまい。



画面全体にかかる色を乗算にしたり減算にしたりして色の付き方見てください。