bate's blog

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

FXComposer超入門10

前回画面エフェクトをやったので、
それを利用して画面をボカします。


注意点は、読み込み中のレンダーターゲットに書き込みができないことです。


1.前回のプロジェクトを開きます。
2.前回の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 UV0			: TEXCOORD0;
	
	float2 UV1			: TEXCOORD1;
	float2 UV2			: TEXCOORD2;
	float2 UV3			: TEXCOORD3;
	float2 UV4			: TEXCOORD4;
};

// shader entry point
V2Pdata objVS(float3 pos : POSITION, float2 UV : TEXCOORD0)
{
	V2Pdata OUT = (V2Pdata)0;
	OUT.Position = float4(pos, 1);
	OUT.UV0 = float2(UV.xy);
	OUT.UV1 = float2(UV.x, UV.y+1.0/ViewportSize.y);
	OUT.UV2 = float2(UV.x, UV.y-1.0/ViewportSize.y);
	OUT.UV3 = float2(UV.x-1.0/ViewportSize.x, UV.y);
	OUT.UV4 = float2(UV.x+1.0/ViewportSize.x, UV.y);
	return OUT;
}

float4 objPS(V2Pdata IN) : COLOR
{
	float4 color = 0.4f * tex2D(ScreenSampler, IN.UV0);
	color += 0.15f * tex2D(ScreenSampler, IN.UV1);
	color += 0.15f * tex2D(ScreenSampler, IN.UV2);
	color += 0.15f * tex2D(ScreenSampler, IN.UV3);
	color += 0.15f * tex2D(ScreenSampler, IN.UV4);
	
	return color;
}

float4 filter0PS(V2Pdata IN) : COLOR
{
	float4 color = 0.4f * tex2D(objSampler, IN.UV0);
	color += 0.15f * tex2D(objSampler, IN.UV1);
	color += 0.15f * tex2D(objSampler, IN.UV2);
	color += 0.15f * tex2D(objSampler, IN.UV3);
	color += 0.15f * tex2D(objSampler, IN.UV4);
	
	return color;
}


float4 filter1PS(V2Pdata IN) : COLOR
{
	float4 color = 0.4f * tex2D(ScreenSampler, IN.UV0);
	color += 0.15f * tex2D(ScreenSampler, IN.UV1);
	color += 0.15f * tex2D(ScreenSampler, IN.UV2);
	color += 0.15f * tex2D(ScreenSampler, IN.UV3);
	color += 0.15f * tex2D(ScreenSampler, IN.UV4);
	
	return color;
}

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

float4 mainPS(V2Pdata IN) : COLOR
{
	float4 full_screen_color = MulColor;
	float4 obj_color = tex2D(ScreenSampler, IN.UV0);
	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=filter0;"
		"Pass=filter1;"
		"Pass=filter2;"
		"Pass=filter3;"
		"Pass=filter4;"
		"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 filter0
	<
		string Script =
			"RenderColorTarget=ScreenMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 filter0PS();
	}
	
	pass filter1
	<
		string Script =
			"RenderColorTarget=ObjMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 filter1PS();
	}
	
	pass filter2
	<
		string Script =
			"RenderColorTarget=ScreenMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 filter0PS();
	}
	
	pass filter3
	<
		string Script =
			"RenderColorTarget=ObjMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 filter1PS();
	}
	
	pass filter4
	<
		string Script =
			"RenderColorTarget=ScreenMap;"
			"RenderDepthStencilTarget=DepthBuffer;"
			"Draw=Buffer;";
	>
	{
		VertexProgram = compile vp40 objVS();
		CullFaceEnable = false;
		DepthTestEnable = false;
		FragmentProgram = compile fp40 filter0PS();
	}
	
	pass main
	<
		string Script =
			"RenderColorTarget0=;"
			"RenderDepthStencilTarget=;"
			"Draw=Buffer;";
	>
	{
		CullFaceEnable = false;
		DepthTestEnable = false;
		DepthMask = false;
		BlendEnable = false;
		VertexProgram = compile vp40 mainVS();
		FragmentProgram = compile fp40 mainPS();
	}
}

3.下の絵1が下の絵2のようにボケることを確認します。

絵1

絵2

4.おしまい。