Sepia Shader

I found this techrepublic page, which gives a conversion algorithm from a standard RGB image to a Sepia image tone as follows:

outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189)
outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168)
outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131)

You can obviously tweak these values if you like – there is no exact formula (Sepia itself being a natural material originally derived from the ink sac of a Cuttlefish, and now derived using a variety of chemicals).

You can apply this algorithm in the finalcolor modifier of a Unity surface shader as follows:

Shader "Custom/Sepia" {
	Properties {
	  _MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
	  Tags { "RenderType"="Opaque" }
	  LOD 200
		
	  CGPROGRAM
	    #pragma surface surf Lambert finalcolor:Sepia

            sampler2D _MainTex;

	    struct Input {
		float2 uv_MainTex;
	    };

    	    void surf (Input IN, inout SurfaceOutput o) {
		half4 c = tex2D (_MainTex, IN.uv_MainTex);
		o.Albedo = c.rgb;
		o.Alpha = c.a;
	    }
		
	    void Sepia (Input IN, SurfaceOutput o, inout fixed4 color) {
         
            fixed3 sepia;
            sepia.r = dot(color.rgb, half3(0.393, 0.769, 0.189));
            sepia.g = dot(color.rgb, half3(0.349, 0.686, 0.168));   
            sepia.b = dot(color.rgb, half3(0.272, 0.534, 0.131));
         
            color.rgb = sepia;
        }
		
	ENDCG
    } 
    FallBack "Diffuse"
}

Which gives:

image

About these ads
This entry was posted in Game Dev and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s