Custom shaders
Shader Graph
Sub shader graph /Artistic/Adjustment/ColorMatrix
is included.
Amplify Shader
Amplify shader ColorMatrix
function is included.
Basics
ColorMatrix requires a minimum target to 3. Just do not define it if it is not necessary. But if it is defined to 2.0, replace it by 3.0. It will works.
But if you have a really old hardware, the solution is:
- replace _ColorMatrices[5] to a single float4x4 (but ranges will not work)
- include ColorMatrix.cginc and use directly
ApplyColorMatrix(fixed3 colorRGB, float4x4 mat)
- create your own plugin to override the rendering
Properties
ColorMatrix has 3 properties :
float4x4 _ColorMatrices[5]; // list of matrices
float _MatrixCount = 0.0; // the number of matrices (max 5)
float _DebugCM = 0.0; // 1.0 => ranges debug
ColorMatrix.cginc
Contains the low level functions
// the main function
// apply a matrix for a color
// debugFilter is the color to apply for debug range (1.0 = white, 2.0 = red, 3.0 = green, 4.0 = blue, 5.0+ = yellow)
fixed3 ApplyColorMatrixWithRange(fixed3 colorRGB, float4x4 mat, float debugFilter=0.0);
SimpleCM.cginc
Contains a simple function and properties to add ColorMatrix in your shaders.
ApplySimpleColorMatrix(fixed3 c);
Example 1 : Volume shader
Standard volume shader
Shader "Wonderland/Standard ColorMatrix" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#include "SimpleCM.cginc"
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = ApplySimpleColorMatrix(c.rgb);
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Example 2 : Unity Sprites-Default
UnitySpritesCM.cginc
Simple implementation of unity sprites fragment.
#ifndef WONDERLAND_UNITY_SPRITES_COLOR_MATRIX_INCLUDED
// Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers d3d11 gles
#define WONDERLAND_UNITY_SPRITES_COLOR_MATRIX_INCLUDED
#include "UnitySprites.cginc"
#include "SimpleCM.cginc"
fixed4 SpriteFragCM(v2f IN) : SV_Target
{
fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
c.rgb = ApplySimpleColorMatrix(c.rgb);
UNITY_APPLY_FOG(IN.fogCoord, c);
c.rgb *= c.a;
return c;
}
#endif
Sprites-Default implementation
UnitySpritesCM is included and fragment pragma is now "SpriteFragCM".
Shader "Wonderland/Sprite-Default ColorMatrix"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
[HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex SpriteVert
#pragma fragment SpriteFragCM
#pragma target 3.0
#pragma multi_compile_instancing
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#pragma multi_compile_fog
#include "UnitySpritesCM.cginc"
ENDCG
}
}
}
Example 3 : with Ferr2D
Ferr2DTCommon.cginc to Ferr2DTCommonCM.cginc
Create a new file Ferr2DTCommonCM.cginc
#ifndef FERR2D_COMMON_COLOR_MATRIX_INCLUDED
#define FERR2D_COMMON_COLOR_MATRIX_INCLUDED
#include "/Assets/Modules/Ferr/2DTerrain/Shaders/Ferr2DTCommon.cginc"
#include "/Assets/Wonderland/ColorMatrix/Shaders/SimpleCM.cginc"
fixed4 fragCM(VS_OUT inp) : COLOR {
fixed4 color = tex2D(_MainTex, inp.uv);
#ifdef FERR2DT_LIGHTMAP
fixed3 light = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, inp.lightuv));
#elif MAX_LIGHTS > 0
fixed3 light = UNITY_LIGHTMODEL_AMBIENT;
#endif
color = color * inp.color;
#if MAX_LIGHTS > 0
for (int i = 0; i < MAX_LIGHTS; i++) {
light += GetLight(i, inp.viewpos);
}
#endif
#if defined(FERR2DT_LIGHMAP) || MAX_LIGHTS > 0
color.rgb *= light;
#endif
#if defined(FERR2DT_VERTEXLIT)
color.rgb *= inp.light;
#endif
color.rgb = ApplySimpleColorMatrix(color.rgb);
UNITY_APPLY_FOG(inp.fogCoord, color);
return color;
}
#endif
VertlitTransparent.shader to VertlitTransparentCM.shader
Create a new file VertlitTransparentCM.shader
Shader "MyProject/Ferr/2D Terrain/Vertex Lit/Textured Transparent" {
Properties {
_MainTex("Texture (RGB)", 2D ) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Cull Off
ZWrite Off
Fog {Mode Off}
Pass {
Tags { LightMode = Vertex }
CGPROGRAM
#pragma vertex vert
#pragma fragment fragCM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_fog
#define FERR2DT_TINT
#define FERR2DT_VERTEXLIT
#include "UnityCG.cginc"
#include "Ferr2DTCommonCM.cginc"
ENDCG
}
}
Fallback "Unlit/Texture"
}