Search Unity

Mask Shader over Multiple Sprites in World Space

Discussion in 'Shaders' started by F4bs, Aug 16, 2019.

  1. F4bs

    F4bs

    Joined:
    Apr 23, 2019
    Posts:
    19
    hey, i'm trying to write a shader over multiple sprites, because the characters in my game are made of multiple sprites. i 'm new to shaders so pls don't expect a code that makes sense ^^:

    Shader "Custom/ShaderMaskLit"{
    Properties {
    _MainTex ("_MainTex", 2D) = "white" { }
    _AlphaMask ("_AlphaMask", 2D) = "white" { }
    }
    SubShader {
    Tags {
    "Queue"="Transparent"
    "RenderType"="Transparent"
    "IgnoreProjector"="True"
    "PreviewType"="Plane"
    "CanUseSpriteAtlas"="True"
    }
    Cull Off
    Lighting Off
    ZWrite Off
    Blend One OneMinusSrcAlpha

    CGPROGRAM
    #pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha
    #include "UnityCG.cginc"
    float4 _MainTex_ST;
    sampler2D _MainTex;
    sampler2D _AlphaMask;
    struct Input {
    fixed4 color;
    float2 _uv_MainTex;
    float2 _uv_STD;
    float4 rect_Sprite;
    };

    void vert (inout appdata_full i, out Input o)
    {
    UNITY_INITIALIZE_OUTPUT(Input,o);
    o.rect_Sprite = float4(0,0,1,1);
    o._uv_MainTex = TRANSFORM_TEX(i.texcoord,_MainTex);
    o._uv_STD = float2((i.texcoord.x - o.rect_Sprite.x)/o.rect_Sprite.z,(i.texcoord.y - o.rect_Sprite.y)/o.rect_Sprite.w);
    //o_uv_STD = TRANSFORM_TEX(o._uv_STD,_MainTex);
    }

    void surf (Input i, inout SurfaceOutput o)
    {
    float4 result = float4(0,0,0,0);

    float2 uv_alpha1 = i._uv_STD;
    float2 center_alpha1 = float2(0.5,0.5);
    //float3 worldPos = mul(unity_ObjectToWorld, i._uv_STD); //WorldPos
    //uv_alpha1 = worldPos;
    float4 color_alpha1 = tex2D(_AlphaMask, uv_alpha1);
    float2 uv_ROOT = i._uv_STD;
    float4 color_ROOT = tex2D(_MainTex,uv_ROOT);
    color_ROOT = float4(color_ROOT.rgb, color_ROOT.a * color_alpha1.a);
    result = float4(color_ROOT.rgb,color_ROOT.a*1);
    o.Albedo = result.rgb*result.a;
    o.Alpha = result.a;
    }
    ENDCG
    }
    fallback "Standard"
    }

    when i now add i a texture to the alpha mask i get this result:


    so i know i have to multiply unity_ObjectToWorld with the uv at some point, so if i uncomment these lines
    float3 worldPos = mul(unity_ObjectToWorld, i._uv_STD); //WorldPos
    uv_alpha1 = worldPos
    i get this result:


    but i cant manage to get a feathered mask that starts at the feet of the character and ends at the middle. is this even possible? thanks for any help in advance :)
     

    Attached Files:

    Last edited: Aug 16, 2019