Шейдеры для аниматора

Шейдеры и все-все-все.

Шейдеры для аниматора

Сообщение Cr0c 11 июн 2018, 00:02

C тремя масками раскраски
Синтаксис:
Используется 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
                }
        }
}
 

С тремя масками раскраски и тремя аурами в подложке
Синтаксис:
Используется 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
                }
        }
}
 

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
                }
        }
}
 

и интерфейсом для упрощения и кеширования
Синтаксис:
Используется 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();
    }
}
 

Ссылки на загрузки:
4 маски с подкраской
пример текстур анимации (3 маски подкраски + 3 ауры)
Скрытый текст:

Скрытый текст:
Последний раз редактировалось Cr0c 02 окт 2019, 11:18, всего редактировалось 1 раз.
Аватара пользователя
Cr0c
Адепт
 
Сообщения: 3035
Зарегистрирован: 19 июн 2015, 13:50
Skype: cr0c81

Re: Шейдеры для аниматора

Сообщение Cr0c 18 июн 2018, 01:01

Шейдер с 4 масками + подкраска масок + прозрачность масок + текстура для растворения спрайта
Синтаксис:
Используется glsl
Shader "Custom/Sprite with mask + alphamask"
{
    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
       
        _TexData ("Texture offset * tile", Vector) = (0,0,1,1)
        _AlphaMaskTex ("Alpha mask texture", 2D) = "white" {}
        _Cutoff ("Cutoff value", Range(0.0, 1.01)) = 0.0
        [MaterialToggle] _InverseMask ("Inverse mask", 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;

                        float4 _TexData;
                        sampler2D _AlphaMaskTex;
                        float _Cutoff;
                        float _InverseMask;


                        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 = float4(0.0, 0.0, 0.0, 0.0);
                                a1 = AdditiveMix(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);

                                float2 s = float2((IN.texcoord.x - _TexData.x) / _TexData.z, (IN.texcoord.y - _TexData.y) / _TexData.w);
                                float4 AlphaMask = tex2D(_AlphaMaskTex, s);
                                res.a = res.a * step(AlphaMask.a + _InverseMask * (1.0 - AlphaMask.a * 2.0), _Cutoff);

                                return res;
                        }
                ENDCG
                }
        }
}

Скрипт для объекта с анимацией, чтобы маска растворения легла на весь спрайт:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shader4Mask : MonoBehaviour {

    [System.Serializable]
    public class SpriteData
    {
        public SpriteRenderer spriteRenderer;
        public Sprite sprite;
        public Material material;
        public Vector4 data;
        public SpriteData(SpriteRenderer spriteRenderer)
        {
            this.spriteRenderer = spriteRenderer;
            this.material = spriteRenderer.materials[0];
            UpdateData();
        }
        public void UpdateData()
        {
            this.sprite = spriteRenderer.sprite;
            float w = sprite.texture.width;
            float h = sprite.texture.height;
            Rect r = sprite.rect;
            data = new Vector4(r.x / w, r.y / h, r.width / w, r.height / h);
        }
    }

    private SpriteRenderer _sr;
    public SpriteData sd;

        // Use this for initialization
        void Start () {
        _sr = this.GetComponent<SpriteRenderer>();
        sd = new SpriteData(_sr);
        sd.material.SetVector("_TexData", sd.data);
        Camera.onPreRender += OnPreRender2;
        }

    private void OnPreRender2(Camera cam)
    {
        sd.UpdateData();
        sd.material.SetVector("_TexData", sd.data);      
    }
    private void OnDisable()
    {
        Camera.onPreRender -= OnPreRender2;
    }
    private void OnEnable()
    {
        Camera.onPreRender += OnPreRender2;
    }
    private void OnDestroy()
    {
        Camera.onPreRender -= OnPreRender2;
    }
}
 

архив с шейдером, скриптом и масками для растворения
Скрытый текст:


P.S.: на шейдеры есть ограничения - все анимации должны быть из одной текстуры
Последний раз редактировалось Cr0c 06 июл 2018, 00:46, всего редактировалось 1 раз.
Аватара пользователя
Cr0c
Адепт
 
Сообщения: 3035
Зарегистрирован: 19 июн 2015, 13:50
Skype: cr0c81

Re: Шейдеры для аниматора

Сообщение Norman 05 июл 2018, 20:34

Спасибо тебе, добрый человек! (или крокодил?)
Классный шейдер!
А не проверяли - на АМД-видюхах или на встроенной в проц графике корректно работает? У меня была проблема с двумя шейдерами - один самодельный в Амплифай, другой шёл с ассетом из стора. У меня всё отображалось отлично (видюха НВидиа), а на компах с видюхами АМД - не отображалось вообще. Меши с этими шейдерами просто не отрисовывались.
Norman
UNIт
 
Сообщения: 111
Зарегистрирован: 31 мар 2017, 18:38

Re: Шейдеры для аниматора

Сообщение Cr0c 06 июл 2018, 00:49

Norman писал(а):Спасибо тебе, добрый человек! (или крокодил?)
Классный шейдер!
А не проверяли - на АМД-видюхах или на встроенной в проц графике корректно работает? У меня была проблема с двумя шейдерами - один самодельный в Амплифай, другой шёл с ассетом из стора. У меня всё отображалось отлично (видюха НВидиа), а на компах с видюхами АМД - не отображалось вообще. Меши с этими шейдерами просто не отрисовывались.

Нет, на AMD не проверял, их просто нет у знакомых.
Аватара пользователя
Cr0c
Адепт
 
Сообщения: 3035
Зарегистрирован: 19 июн 2015, 13:50
Skype: cr0c81


Вернуться в Shader Lab

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 2