如何通过纹理掩码更改生成的蒙版?

时间:2016-12-19 20:04:35

标签: unity3d shader

我有一个生成不透明蒙版并旋转它的着色器。

它的外观如下:

enter image description here

生成的掩码如下所示:

enter image description here

我通过代码生成一个掩码,但我想从texture2D中获取掩码。

我该怎么做? 如何仅通过texture2D更改掩码生成?

我的着色器代码:

Shader "Custom/RadialOpacity" {
    Properties {
        [PerRendererData]_MainTex ("MainTex", 2D) = "white" {}
        _Color ("Color", Color) = (1,1,1,1)
        _OpacityRotator ("Opacity Rotator", Range(-360, 360)) = -360 //  2 full circles
        [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0            
    }

    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
            "CanUseSpriteAtlas"="True"
            "PreviewType"="Plane"
        }

        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }

            Blend One OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag            
            #pragma multi_compile _ PIXELSNAP_ON

            #include "UnityCG.cginc"                       
            #pragma target 3.0
            uniform sampler2D _MainTex; 
            uniform float4 _MainTex_ST;
            uniform float4 _Color;
            uniform float _OpacityRotator;            

            static const float TAU = float(6.283185); // это 2 * PI

            struct VertexInput {
                float4 vertex : POSITION;                               
                float2 texcoord0 : TEXCOORD0;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;                
                float3 normalDir : TEXCOORD2;                                
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.uv0 = v.texcoord0;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                #ifdef PIXELSNAP_ON
                    o.pos = UnityPixelSnap(o.pos);
                #endif

                return o;
            }

            float4 frag(VertexOutput i) : COLOR {
                i.normalDir = normalize(i.normalDir);                
                float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));                

                float2 oStart = (i.uv0 - 0.5);
                float2 oVector = float2(-1, -1);
                float oRotatorNormalized = _OpacityRotator / 360.0;

                float oRotator_ang = oRotatorNormalized * -TAU;
                float oRotator_cos = cos(oRotator_ang);
                float oRotator_sin = sin(oRotator_ang);                
                float2x2 oRotationMatrix = float2x2(oRotator_cos, -oRotator_sin, oRotator_sin, oRotator_cos);               

                float2 oRotatorComponent = mul(oVector * oStart, oRotationMatrix);

                /* generating opacity mask BEGIN_SECTION */
                float2 oMaskHorizOrVert = atan2(oRotatorComponent.g, oRotatorComponent.r);          
                float oAtan2MaskNormalized = (oMaskHorizOrVert / TAU) + 0.5;                
                float oAtan2MaskRotatable = oRotatorNormalized - oAtan2MaskNormalized;
                float oWhiteToBlackMask = ceil(oAtan2MaskRotatable);                
                /* generating opacity mask END_SECTION */                   

                float oFinalMultiply = _MainTex_var.a * max(oAtan2MaskNormalized, ceil(oWhiteToBlackMask)); 

                /*** (Emissive) ***/                
                float3 finalColor = _MainTex_var.rgb * _Color.rgb * oFinalMultiply;             
                return fixed4(finalColor, oFinalMultiply);
            }

            ENDCG
        }       
    }

    FallBack "Diffuse"    
}

我希望得到类似的东西:

Properties {
    ...
    _OpacityMask ("OpacityMask", 2D) = "white" {}
    ...
}

...

float oWhiteToBlackMask = ceil(OpacityMask);
float oFinalMultiply = _MainTex_var.a * max(oAtan2MaskNormalized, ceil(oWhiteToBlackMask)); 

...

1 个答案:

答案 0 :(得分:0)

https://forum.unity3d.com/threads/rotation-of-texture-uvs-directly-from-a-shader.150482/

好的,如果我正确理解你的问题,你想要添加纹理2D参数并让它旋转。您需要随时间旋转UV坐标,您可以使用上面链接中的代码完成此操作。

我不确定你是如何在纹理2D的最后得到那种精确的淡入淡出,但也许你可以巧妙地利用时间来弄清楚动画。