Синтаксис:
Используется glsl
Shader "Custom/Sprite Colored mask"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_ColorMaskTex ("Color mask texture", 2D) = "white" {}
_MainColor ("Main color (red)", Color) = (1,0,0,0)
_AddColor ("Additional 1st color (green)", Color) = (0,1,0,0)
_ExtColor ("Additional 2nd color (blue)", Color) = (0,0,1,0)
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _ColorMaskTex;
float4 _MainColor;
float4 _AddColor;
float4 _ExtColor;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
fixed4 SkinTex = tex2D(_ColorMaskTex, IN.texcoord);
float4 res = float4(MainTex.rgb * (1 - SkinTex.r - SkinTex.g - SkinTex.b) +
lerp (MainTex.rgb, _MainColor.rgb, _MainColor.a * SkinTex.a) * SkinTex.r +
lerp (MainTex.rgb, _AddColor.rgb, _AddColor.a * SkinTex.a) * SkinTex.g +
lerp (MainTex.rgb, _ExtColor.rgb, _ExtColor.a * SkinTex.a) * SkinTex.b,
MainTex.a);
res.a = MainTex.a;
return res;
}
ENDCG
}
}
}
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_ColorMaskTex ("Color mask texture", 2D) = "white" {}
_MainColor ("Main color (red)", Color) = (1,0,0,0)
_AddColor ("Additional 1st color (green)", Color) = (0,1,0,0)
_ExtColor ("Additional 2nd color (blue)", Color) = (0,0,1,0)
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _ColorMaskTex;
float4 _MainColor;
float4 _AddColor;
float4 _ExtColor;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
fixed4 SkinTex = tex2D(_ColorMaskTex, IN.texcoord);
float4 res = float4(MainTex.rgb * (1 - SkinTex.r - SkinTex.g - SkinTex.b) +
lerp (MainTex.rgb, _MainColor.rgb, _MainColor.a * SkinTex.a) * SkinTex.r +
lerp (MainTex.rgb, _AddColor.rgb, _AddColor.a * SkinTex.a) * SkinTex.g +
lerp (MainTex.rgb, _ExtColor.rgb, _ExtColor.a * SkinTex.a) * SkinTex.b,
MainTex.a);
res.a = MainTex.a;
return res;
}
ENDCG
}
}
}
С тремя масками раскраски и тремя аурами в подложке
Синтаксис:
Используется glsl
Shader "Custom/Sprite Colored mask + Aura"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_ColorMaskTex ("Color mask texture", 2D) = "white" {}
_MainColor ("Main color (red)", Color) = (1,0,0,0)
_AddColor ("Additional 1st color (green)", Color) = (0,1,0,0)
_ExtColor ("Additional 2nd color (blue)", Color) = (0,0,1,0)
_AuraMaskTex ("Aura mask texture", 2D) = "white" {}
_Aura1Color ("Aura 1 color (red)", Color) = (1,0,0,0)
_Aura1Cutoff ("Cutoff Aura 1", Range(0.0, 1.0)) = 1
_Aura2Color ("Aura 2 color (green)", Color) = (0,1,0,0)
_Aura2Cutoff ("Cutoff Aura 2", Range(0.0, 1.0)) = 1
_Aura3Color ("Aura 3 color (blue)", Color) = (0,0,1,0)
_Aura3Cutoff ("Cutoff Aura 3", Range(0.0, 1.0)) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="False"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
// float4 _MainTex_ST;
sampler2D _ColorMaskTex;
float4 _MainColor;
float4 _AddColor;
float4 _ExtColor;
sampler2D _AuraMaskTex;
float4 _Aura1Color;
float _Aura1Cutoff;
float4 _Aura2Color;
float _Aura2Cutoff;
float4 _Aura3Color;
float _Aura3Cutoff;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
float4 SkinTex = tex2D(_ColorMaskTex, IN.texcoord);
float4 AuraTex = tex2D(_AuraMaskTex, IN.texcoord);
float4 a1 = _Aura1Color * AuraTex.r * _Aura1Color.a;
a1.a = clamp(AuraTex.r * _Aura1Color.a * AuraTex.a + _Aura1Cutoff - 1, 0, 1);
float4 a2 = _Aura2Color * AuraTex.g * _Aura2Color.a;
a2.a = clamp(AuraTex.g * _Aura2Color.a * AuraTex.a + _Aura2Cutoff - 1, 0, 1);
float4 a3 = _Aura3Color * AuraTex.b * _Aura3Color.a;
a3.a = clamp(AuraTex.b * _Aura3Color.a * AuraTex.a + _Aura3Cutoff - 1, 0, 1);
float4 aura = a1 + a2 + a3;
aura.a = a1.a + a2.a + a3.a;
float4 res = float4(MainTex.rgb * (1 - SkinTex.r - SkinTex.g - SkinTex.b) +
lerp (MainTex.rgb, _MainColor.rgb, _MainColor.a * SkinTex.a) * SkinTex.r +
lerp (MainTex.rgb, _AddColor.rgb, _AddColor.a * SkinTex.a) * SkinTex.g +
lerp (MainTex.rgb, _ExtColor.rgb, _ExtColor.a * SkinTex.a) * SkinTex.b,
MainTex.a);
res.rgb = res.rgb * MainTex.a + aura.rgb * (1 - MainTex.a);
res.a = clamp(MainTex.a + aura.a, 0, 1);
return res;
}
ENDCG
}
}
}
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_ColorMaskTex ("Color mask texture", 2D) = "white" {}
_MainColor ("Main color (red)", Color) = (1,0,0,0)
_AddColor ("Additional 1st color (green)", Color) = (0,1,0,0)
_ExtColor ("Additional 2nd color (blue)", Color) = (0,0,1,0)
_AuraMaskTex ("Aura mask texture", 2D) = "white" {}
_Aura1Color ("Aura 1 color (red)", Color) = (1,0,0,0)
_Aura1Cutoff ("Cutoff Aura 1", Range(0.0, 1.0)) = 1
_Aura2Color ("Aura 2 color (green)", Color) = (0,1,0,0)
_Aura2Cutoff ("Cutoff Aura 2", Range(0.0, 1.0)) = 1
_Aura3Color ("Aura 3 color (blue)", Color) = (0,0,1,0)
_Aura3Cutoff ("Cutoff Aura 3", Range(0.0, 1.0)) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="False"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
// float4 _MainTex_ST;
sampler2D _ColorMaskTex;
float4 _MainColor;
float4 _AddColor;
float4 _ExtColor;
sampler2D _AuraMaskTex;
float4 _Aura1Color;
float _Aura1Cutoff;
float4 _Aura2Color;
float _Aura2Cutoff;
float4 _Aura3Color;
float _Aura3Cutoff;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
float4 SkinTex = tex2D(_ColorMaskTex, IN.texcoord);
float4 AuraTex = tex2D(_AuraMaskTex, IN.texcoord);
float4 a1 = _Aura1Color * AuraTex.r * _Aura1Color.a;
a1.a = clamp(AuraTex.r * _Aura1Color.a * AuraTex.a + _Aura1Cutoff - 1, 0, 1);
float4 a2 = _Aura2Color * AuraTex.g * _Aura2Color.a;
a2.a = clamp(AuraTex.g * _Aura2Color.a * AuraTex.a + _Aura2Cutoff - 1, 0, 1);
float4 a3 = _Aura3Color * AuraTex.b * _Aura3Color.a;
a3.a = clamp(AuraTex.b * _Aura3Color.a * AuraTex.a + _Aura3Cutoff - 1, 0, 1);
float4 aura = a1 + a2 + a3;
aura.a = a1.a + a2.a + a3.a;
float4 res = float4(MainTex.rgb * (1 - SkinTex.r - SkinTex.g - SkinTex.b) +
lerp (MainTex.rgb, _MainColor.rgb, _MainColor.a * SkinTex.a) * SkinTex.r +
lerp (MainTex.rgb, _AddColor.rgb, _AddColor.a * SkinTex.a) * SkinTex.g +
lerp (MainTex.rgb, _ExtColor.rgb, _ExtColor.a * SkinTex.a) * SkinTex.b,
MainTex.a);
res.rgb = res.rgb * MainTex.a + aura.rgb * (1 - MainTex.a);
res.a = clamp(MainTex.a + aura.a, 0, 1);
return res;
}
ENDCG
}
}
}
C накладываемыми масками перекрытия (4 маски)
Синтаксис:
Используется glsl
Shader "Custom/Sprite with mask"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_HeadTex ("Head texture", 2D) = "white" {}
_HeadColor ("Head coloring", Color) = (1,1,1,1)
_HeadVisible ("Head visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _HeadShow ("Head show", Float) = 0
_BodyTex ("Body texture", 2D) = "white" {}
_BodyColor ("Head coloring", Color) = (1,1,1,1)
_BodyVisible ("Body visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _BodyShow ("Body show", Float) = 0
_LegsTex ("Legs texture", 2D) = "white" {}
_LegsColor ("Head coloring", Color) = (1,1,1,1)
_LegsVisible ("Legs visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _LegsShow ("Legs show", Float) = 0
_FootTex ("Foots texture", 2D) = "white" {}
_FootColor ("Head coloring", Color) = (1,1,1,1)
_FootVisible ("Foot visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _FootShow ("Foots show", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="False"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
float4 AdditiveMix(float4 in_b, float4 in_t)
{
float4 result = float4(in_b.rgb * (1.0 - in_t.a) + in_t.rgb * in_t.a, 0.0);
result.a = in_t.a + in_b.a * (1.0 - in_t.a);
return result;
}
sampler2D _MainTex;
// float4 _MainTex_ST;
sampler2D _HeadTex;
float4 _HeadColor;
float _HeadVisible;
float _HeadShow;
sampler2D _BodyTex;
float4 _BodyColor;
float _BodyVisible;
float _BodyShow;
sampler2D _LegsTex;
float4 _LegsColor;
float _LegsVisible;
float _LegsShow;
sampler2D _FootTex;
float4 _FootColor;
float _FootVisible;
float _FootShow;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
float4 HeadTex = tex2D(_HeadTex, IN.texcoord) * float4(lerp(1.0, _HeadColor.rgb, _HeadColor.a), 1.0);
float4 BodyTex = tex2D(_BodyTex, IN.texcoord) * float4(lerp(1.0, _BodyColor.rgb, _BodyColor.a), 1.0);
float4 LegsTex = tex2D(_LegsTex, IN.texcoord) * float4(lerp(1.0, _LegsColor.rgb, _LegsColor.a), 1.0);
float4 FootTex = tex2D(_FootTex, IN.texcoord) * float4(lerp(1.0, _FootColor.rgb, _FootColor.a), 1.0);
float4 a1;
a1 = FootTex.rgba * _FootShow * _FootVisible;
a1 = AdditiveMix(a1, LegsTex.rgba * _LegsShow * _LegsVisible);
a1 = AdditiveMix(a1, BodyTex.rgba * _BodyShow * _BodyVisible);
a1 = AdditiveMix(a1, HeadTex.rgba * _HeadShow * _HeadVisible);
float4 res = float4(MainTex.rgb, MainTex.a);
res = AdditiveMix(res, a1);
return res;
}
ENDCG
}
}
}
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[PerRendererData] _MainTexCut ("CutOff", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_HeadTex ("Head texture", 2D) = "white" {}
_HeadColor ("Head coloring", Color) = (1,1,1,1)
_HeadVisible ("Head visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _HeadShow ("Head show", Float) = 0
_BodyTex ("Body texture", 2D) = "white" {}
_BodyColor ("Head coloring", Color) = (1,1,1,1)
_BodyVisible ("Body visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _BodyShow ("Body show", Float) = 0
_LegsTex ("Legs texture", 2D) = "white" {}
_LegsColor ("Head coloring", Color) = (1,1,1,1)
_LegsVisible ("Legs visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _LegsShow ("Legs show", Float) = 0
_FootTex ("Foots texture", 2D) = "white" {}
_FootColor ("Head coloring", Color) = (1,1,1,1)
_FootVisible ("Foot visibility", Range(0.0, 1.0)) = 1.0
[MaterialToggle] _FootShow ("Foots show", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="False"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
float4 AdditiveMix(float4 in_b, float4 in_t)
{
float4 result = float4(in_b.rgb * (1.0 - in_t.a) + in_t.rgb * in_t.a, 0.0);
result.a = in_t.a + in_b.a * (1.0 - in_t.a);
return result;
}
sampler2D _MainTex;
// float4 _MainTex_ST;
sampler2D _HeadTex;
float4 _HeadColor;
float _HeadVisible;
float _HeadShow;
sampler2D _BodyTex;
float4 _BodyColor;
float _BodyVisible;
float _BodyShow;
sampler2D _LegsTex;
float4 _LegsColor;
float _LegsVisible;
float _LegsShow;
sampler2D _FootTex;
float4 _FootColor;
float _FootVisible;
float _FootShow;
fixed4 frag(v2f IN) : COLOR
{
float4 MainTex = tex2D(_MainTex, IN.texcoord) * IN.color;
float4 HeadTex = tex2D(_HeadTex, IN.texcoord) * float4(lerp(1.0, _HeadColor.rgb, _HeadColor.a), 1.0);
float4 BodyTex = tex2D(_BodyTex, IN.texcoord) * float4(lerp(1.0, _BodyColor.rgb, _BodyColor.a), 1.0);
float4 LegsTex = tex2D(_LegsTex, IN.texcoord) * float4(lerp(1.0, _LegsColor.rgb, _LegsColor.a), 1.0);
float4 FootTex = tex2D(_FootTex, IN.texcoord) * float4(lerp(1.0, _FootColor.rgb, _FootColor.a), 1.0);
float4 a1;
a1 = FootTex.rgba * _FootShow * _FootVisible;
a1 = AdditiveMix(a1, LegsTex.rgba * _LegsShow * _LegsVisible);
a1 = AdditiveMix(a1, BodyTex.rgba * _BodyShow * _BodyVisible);
a1 = AdditiveMix(a1, HeadTex.rgba * _HeadShow * _HeadVisible);
float4 res = float4(MainTex.rgb, MainTex.a);
res = AdditiveMix(res, a1);
return res;
}
ENDCG
}
}
}
и интерфейсом для упрощения и кеширования
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ShaderInterface {
public enum ShaderMask {
Head = 0,
Body = 1,
Legs = 2,
Foot = 3
}
private static string[] maskName =
{
"_HeadTex",
"_BodyTex",
"_LegsTex",
"_FootTex"
};
private static string[] maskToggleName =
{
"_HeadShow",
"_BodyShow",
"_LegsShow",
"_FootShow"
};
private static string[] maskAlphaName =
{
"_HeadVisible",
"_BodyVisible",
"_LegsVisible",
"_FootVisible"
};
private static Dictionary<GameObject, Material> gos = new Dictionary<GameObject, Material>();
private static Dictionary<SpriteRenderer, Material> spr = new Dictionary<SpriteRenderer, Material>();
public static void SetMask(Material material, Texture2D texture, int smask)
{
smask = Mathf.Clamp(smask, 0, 3);
string mask = maskName[smask];
material.SetTexture(mask, texture);
}
public static void SetMask(Material material, Texture2D texture, ShaderMask smask)
{
if (material != null)
{
SetMask(material, texture, (int) smask);
SetToggleMask(material, true, (int) smask);
}
else
{
SetToggleMask(material, false, (int) smask);
}
}
public static void SetMask(SpriteRenderer renderer, Texture2D texture, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMask(material, texture, (int) smask);
}
public static void SetTexture(GameObject go, Texture2D texture, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMask(material, texture, (int) smask);
}
public static void SetToggleMask(Material material, bool state, int smask)
{
smask = Mathf.Clamp(smask, 0, 3);
string mask = maskToggleName[smask];
material.SetFloat(mask, state ? 1f : 0f);
}
public static void SetToggleMask(Material material, bool state, ShaderMask smask)
{
SetToggleMask(material, state, (int) smask);
}
public static void SetToggleMask(SpriteRenderer renderer, bool state, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetToggleMask(material, state, (int) smask);
}
public static void SetToggleMask(GameObject go, bool state, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetToggleMask(material, state, (int) smask);
}
public static void SetMaskAlpha(Material material, float value, int smask)
{
value = Mathf.Clamp(value, 0f, 1f);
smask = Mathf.Clamp(smask, 0, 3);
material.SetFloat(maskAlphaName[smask], value);
}
public static void SetMaskAlpha(Material material, float value, ShaderMask smask)
{
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskAlpha(SpriteRenderer renderer, float value, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskAlpha(GameObject go, float value, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskColor(Material material, Color value, int smask)
{
material.SetColor(maskName[smask], value);
}
public static void SetMaskColor(Material material, Color value, ShaderMask smask)
{
SetMaskColor(material, value, (int)smask);
}
public static void SetMaskColor(SpriteRenderer renderer, Color value, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMaskColor(material, value, (int)smask);
}
public static void SetMaskColor(GameObject go, Color value, int smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMaskColor(material, value, (int)smask);
}
public static void ClearCache()
{
gos.Clear();
spr.Clear();
}
}
using System.Collections.Generic;
using UnityEngine;
public static class ShaderInterface {
public enum ShaderMask {
Head = 0,
Body = 1,
Legs = 2,
Foot = 3
}
private static string[] maskName =
{
"_HeadTex",
"_BodyTex",
"_LegsTex",
"_FootTex"
};
private static string[] maskToggleName =
{
"_HeadShow",
"_BodyShow",
"_LegsShow",
"_FootShow"
};
private static string[] maskAlphaName =
{
"_HeadVisible",
"_BodyVisible",
"_LegsVisible",
"_FootVisible"
};
private static Dictionary<GameObject, Material> gos = new Dictionary<GameObject, Material>();
private static Dictionary<SpriteRenderer, Material> spr = new Dictionary<SpriteRenderer, Material>();
public static void SetMask(Material material, Texture2D texture, int smask)
{
smask = Mathf.Clamp(smask, 0, 3);
string mask = maskName[smask];
material.SetTexture(mask, texture);
}
public static void SetMask(Material material, Texture2D texture, ShaderMask smask)
{
if (material != null)
{
SetMask(material, texture, (int) smask);
SetToggleMask(material, true, (int) smask);
}
else
{
SetToggleMask(material, false, (int) smask);
}
}
public static void SetMask(SpriteRenderer renderer, Texture2D texture, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMask(material, texture, (int) smask);
}
public static void SetTexture(GameObject go, Texture2D texture, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMask(material, texture, (int) smask);
}
public static void SetToggleMask(Material material, bool state, int smask)
{
smask = Mathf.Clamp(smask, 0, 3);
string mask = maskToggleName[smask];
material.SetFloat(mask, state ? 1f : 0f);
}
public static void SetToggleMask(Material material, bool state, ShaderMask smask)
{
SetToggleMask(material, state, (int) smask);
}
public static void SetToggleMask(SpriteRenderer renderer, bool state, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetToggleMask(material, state, (int) smask);
}
public static void SetToggleMask(GameObject go, bool state, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetToggleMask(material, state, (int) smask);
}
public static void SetMaskAlpha(Material material, float value, int smask)
{
value = Mathf.Clamp(value, 0f, 1f);
smask = Mathf.Clamp(smask, 0, 3);
material.SetFloat(maskAlphaName[smask], value);
}
public static void SetMaskAlpha(Material material, float value, ShaderMask smask)
{
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskAlpha(SpriteRenderer renderer, float value, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskAlpha(GameObject go, float value, ShaderMask smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMaskAlpha(material, value, (int) smask);
}
public static void SetMaskColor(Material material, Color value, int smask)
{
material.SetColor(maskName[smask], value);
}
public static void SetMaskColor(Material material, Color value, ShaderMask smask)
{
SetMaskColor(material, value, (int)smask);
}
public static void SetMaskColor(SpriteRenderer renderer, Color value, ShaderMask smask)
{
Material material;
if (spr.ContainsKey(renderer))
{
material = spr[renderer];
}
else
{
material = renderer.material;
spr.Add(renderer, material);
}
SetMaskColor(material, value, (int)smask);
}
public static void SetMaskColor(GameObject go, Color value, int smask)
{
Material material;
if (gos.ContainsKey(go))
{
material = gos[go];
}
else
{
material = go.GetComponent<SpriteRenderer>().material;
gos.Add(go, material);
}
SetMaskColor(material, value, (int)smask);
}
public static void ClearCache()
{
gos.Clear();
spr.Clear();
}
}
Ссылки на загрузки:
4 маски с подкраской
Скрытый текст:
Скрытый текст: