5
\$\begingroup\$

I am using NGUI. I have an "NGUI Unity 2D Sprite". This sprite has a material with this shader:

Shader "DM/Ripple Shader" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}
        _Scale ("Scale", Range(0.5,500.0)) = 3.0
        _Speed ("Speed", Range(-50,50.0)) = 1.0
    }
    SubShader {
        Tags { "RenderType"="Opaque"}
        LOD 200
        Cull Off
        CGPROGRAM
        #pragma surface surf Lambert
        #include "UnityCG.cginc"

        half4 _Color;
        half _Scale;
        half _Speed;
        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half2 uv = (IN.uv_MainTex - 0.5) * _Scale;
            half r = sqrt (uv.x*uv.x + uv.y*uv.y);
            half z = sin (r+_Time[1]*_Speed) / r;
            o.Albedo = _Color.rgb * tex2D (_MainTex, IN.uv_MainTex+z).rgb;
            o.Alpha = _Color.a;
            o.Normal = (z, z, z);
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

It is basically a ripple effect.

Anyway, this sprite is a child of another Game Object, which an NGUI UI Panel one.

I want the Panel to use Soft Clipping. I want to clip the sprite.

However, when you activate Soft Clipping (or any sort of clipping really), the sprite's shader will not work anymore. The sprite will be drawn normally, but it won't have the desired ripple effect.

How can I clip my sprite without affecting its custom shader?


I am aware that NGUI has a shader that does the clipping, and I am supposed to "grab its logic and combine it with my shader". However, I do not understand how am I supposed to that. This is the NGUI shader that supposedly does the clipping:

Shader "Hidden/Unlit/Transparent Colored 1"
{
    Properties
    {
        _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    }

    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Offset -1, -1
            Fog { Mode Off }
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);
            float2 _ClipArgs0 = float2(1000.0, 1000.0);

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

            struct v2f
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float2 worldPos : TEXCOORD1;
            };

            v2f o;

            v2f vert (appdata_t v)
            {
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.color = v.color;
                o.texcoord = v.texcoord;
                o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                // Softness factor
                float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;

                // Sample the texture
                half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
                col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
                return col;
            }
            ENDCG
        }
    }

    SubShader
    {
        LOD 100

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }

        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMaterial AmbientAndDiffuse

            SetTexture [_MainTex]
            {
                Combine Texture * Primary
            }
        }
    }
}

How am I supposed to use both?

\$\endgroup\$
0
\$\begingroup\$

I did a quick google search and found this question on the NGUI forum: http://www.tasharen.com/forum/index.php?topic=9004.0

In the first reply, Nikki says that NGUI does clipping in the shaders. This means that you'll have to modify your ripple shader to take clipping into account.

I did a quick browse through the shaders packaged with NGUI and found they all contain logic for clipping. Take a look at them, the clipping areas are well commented so you should be able to include them in your shader.

\$\endgroup\$
  • \$\begingroup\$ Unfortunately I don't know much about shaders. I found the NGUI shader you were talking about, but I don't know how to use it along with my shader above. \$\endgroup\$ – Oxide Jul 30 '15 at 19:40
0
\$\begingroup\$

Check out this link http://answers.unity3d.com/questions/365806/how-to-make-custom-clipping-in-ngui.html this link shows another question asked on unity forums and shows an answer which also contains a link to a showcase video.

\$\endgroup\$
  • \$\begingroup\$ This answer would be better if it included a summary of the technique to solve this issue. In the event that the links on this page ever change location, someone finding these answers wouldn't be any closer to a solution. It's fine to link to an outside source for credit and further details, as long as the answer is also able to stand on its own. \$\endgroup\$ – DMGregory Aug 16 '17 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.