如何为两个图像之间的共享空间着色?

时间:2018-12-13 12:55:43

标签: unity3d

我的新游戏概念需要这个主意,但我自己甚至在统一论坛中都找不到解决方案。

请问有什么Unity开发人员可以帮助我吗?

EXAMPLE

谢谢

1 个答案:

答案 0 :(得分:3)

我用来实现此结果的解决方案是使用2遍模板着色器,并将相机背景修改为纯色-白色。

着色器中有遍(颜色为硬编码)。然后只需使用此着色器创建材质并将其放置到网格上即可。

是的,此版本仅适用于正确形状的网格。因此,当您想要一个圆形或三角形时,请从3D建模引擎中导出网格,然后在此着色器中将其用于Unity。

Shader "Custom/NewSurfaceShader"{
Properties {}
SubShader 
{
    Tags { "RenderType"="Geometry" }

    Pass
    {
        Stencil
        {
            Comp always
            Pass IncrSat
        }

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 vertex : SV_POSITION;
        };

        sampler2D _MainTex;
        float4 _MainTex_ST;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = v.uv;

            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 col = fixed4(0, 0, 0, 1);
            return col;
        }
        ENDCG
    }

    Pass
    {
        Stencil
        {
            Ref 1
            Comp Less
        }

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            float4 vertex : SV_POSITION;
        };

        sampler2D _MainTex;
        float4 _MainTex_ST;

        v2f vert (appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = v.uv;

            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 col = fixed4(1, 1, 1, 1);
            return col;
        }
        ENDCG
    }
}

}

This is the result


编辑:

仅升级了着色器,在此解决方案中,它也适用于精灵形状。您不再需要创建网格。唯一的缺点是现在它需要3次着色器遍历。

    Shader "Unlit/NewUnlitShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 uv  : TEXCOORD0;
            };

            sampler2D _MainTex;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col = col * i.color;

                if(col.a < .5f)
                {
                    discard;
                }

                return col;
            }

            ENDCG
        }

        Pass
        {
            Stencil
            {
                Comp always
                Pass IncrSat
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 uv  : TEXCOORD0;
            };

            sampler2D _MainTex;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = fixed4(0, 0, 0, 1);
                return col;
            }
            ENDCG
        }

        Pass
        {
            Stencil
            {
                Ref 1
                Comp Less
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 uv  : TEXCOORD0;
            };

            sampler2D _MainTex;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = fixed4(1, 1, 1, 1);
                return col;
            }
            ENDCG
        }
    }
}

enter image description here