Home Unreal Engine

Custom Color Grading Shader: Help

polycounter lvl 16
Offline / Send Message
Indecom polycounter lvl 16
Hey guys, i've been struggling over the past couple of hours to create a custom color grading shader that I can inject into my post processing chain as a material effect. For reference, this is what i'm trying to accomplish: http://udn.epicgames.com/Three/ColorGrading.html

Now many of you are going to ask me why i dont use the color grading effect that's built into the udk, the answer is because it doesnt do what i want it to. I plan on using a custom LUT image that has been limited to a specific palette of 256 colors. Using the built in color grading system blends all of the 256 colors together creating a massive amount of colors that drastically exceeds my targeted 256 colors.

This is where my shader is at currently, at this point in time i'm merely trying to make my scene look... normal, when this shader is active:
LutMatFail.png

and this is the result:
LutTryFail.png

Right now i'm not focusing on the 256 color limiting, right now i just want to replicate the udk's built in color grading effect using this material.

Also i have a snippet of code from an old .fx file i programmed for dark basic back in the day that does exactly what i'm trying to do here, however injecting this into a custom node returns a bunch of errors:
float2 screenRes = { 800,600 };

texture Texture0
<string resourceName = "";>;
sampler2D diffuseSampler = sampler_state
{
	texture = <Texture0>;
	mipFilter = linear; magFilter = linear; minFilter = linear;
	addressU = clamp; addressV = clamp;

};

texture Texture1
<string resourceName = "";>;
sampler2D lookupTableSampler= sampler_state
{
	texture = <Texture1>;
	mipFilter = none; magFilter = none; minFilter = none;
	addressU = clamp; addressV = clamp;
};

//---------------------------------------------------------
// Quad - ( Shader 0 )
// 
// Quad.
//---------------------------------------------------------
void S0_VertexShader( float4 iPos			: POSITION,
					  float2 iUV0			: TEXCOORD0,
					  out float4 oPos		: POSITION,
					  out float2 oUV0		: TEXCOORD0 )
{
	oUV0	= iUV0;
	iUV0.y	= 1.0f - iUV0.y;
	iUV0	= iUV0 * 2.0f - 1.0f;
	oPos	= float4( iUV0.x - ( 1.0f / screenRes.x ),
					  iUV0.y + ( 1.0f / screenRes.y ), 1.0f, 1.0f );
}

float4 S0_PixelShader( float2 iUV0 : TEXCOORD0 ) : COLOR
{
	float4 diffuseMap = tex2D( diffuseSampler, iUV0 );
	
	float2 lutUV;
	lutUV.u = diffuseMap.g/16+(1/16)*floor(diffuseMap.b*16);
	lutUV.v = diffuseMapr+(1/16)*floor(diffuseMap.b*16);
	
	diffuseMap.rgb = tex2D( lookupTableSampler, lutUV );
	
	return diffuseMap;
}


technique tech_Refraction
{
	pass p0
	{
		vertexShader		= compile vs_2_0 S0_VertexShader();
		pixelShader			= compile ps_2_0 S0_PixelShader();
	}
}

The part of that code that i care about is this section:

float4 S0_PixelShader( float2 iUV0 : TEXCOORD0 ) : COLOR
{
float4 diffuseMap = tex2D( diffuseSampler, iUV0 );

float2 lutUV;
lutUV.u = diffuseMap.g/16+(1/16)*floor(diffuseMap.b*16);
lutUV.v = diffuseMapr+(1/16)*floor(diffuseMap.b*16);

diffuseMap.rgb = tex2D( lookupTableSampler, lutUV );

return diffuseMap;
}


It displays the math required to retrieve a location on a 256x16 texture lookup table based on the current color of a pixel on the screen. However my math up above is close, but nowhere near correct.
Sign In or Register to comment.