-->



Shader "UI/InvertColor"
{
//Propertiesに_BlendRateを指定してInspectorからアクセスできるようにする
Properties
{
_BlendRate ("BlendRate", Range(0.0, 1.0)) = 1
}
SubShader
{
Tags
{
"RenderPipeline" = "UniversalPipeline"
"Queue" = "Transparent"
}
Cull Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
//後述のLinearToSRGB、SRGBToLinearで必要なライブラリ
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
struct Attributes
{
half2 uv : TEXCOORD0;
float4 positionOS : POSITION;
half4 color : COLOR;
};
struct Varyings
{
half2 uv : TEXCOORD0;
float4 positionCS : SV_POSITION;
half4 color : COLOR;
};
//uGUI Image > Source Imageにアタッチされた画像を参照する
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
//SRP Batcherを適用
CBUFFER_START(UnityPerMaterial)
//今回追加したプロパティに対応する変数
half _BlendRate;
CBUFFER_END
Varyings vert (Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.uv = input.uv;
output.color = input.color;
return output;
}
half4 frag (Varyings input) : SV_Target
{
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
col *= input.color;
//ピクセル色をリニア空間ルールから一旦ガンマ空間ルールへ変換
half3 sRGB = LinearToSRGB(col.rgb);
//ガンマ空間ルール上で色を反転
sRGB = abs(_BlendRate - sRGB);
//色をガンマ空間ルールからリニア空間ルールに戻す
col.rgb = SRGBToLinear(sRGB);
return col;
}
ENDHLSL
}
}
}

col.rgb = 1 - col.rgb;
col.rgb = abs(_BlendRate - col.rgb);
half4 frag (Varyings input) : SV_Target
{
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
col *= input.color;
//ピクセル色をリニア空間ルールから一旦ガンマ空間ルールへ変換
half3 sRGB = LinearToSRGB(col.rgb);
//ガンマ空間ルール上で色を反転
sRGB = abs(_BlendRate - sRGB);
//色をガンマ空間ルールからリニア空間ルールに戻す
col.rgb = SRGBToLinear(sRGB);
return col;
}
//ピクセル色をリニア空間ルールから一旦ガンマ空間ルールへ変換
half3 sRGB = LinearToSRGB(col.rgb);
//ガンマ空間ルール上で色を反転
sRGB = abs(_BlendRate - sRGB);
//色をガンマ空間ルールからリニア空間ルールに戻す
col.rgb = SRGBToLinear(sRGB);
//後述のLinearToSRGB、SRGBToLinearで必要なライブラリ
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"