2016-12-19 90 views
0

我有一个着色器生成不透明蒙版并旋转它。如何通过纹理遮罩更改生成的遮罩?

这是它的外观:

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)); 

... 
+0

你能否澄清这个问题?我不完全确定问题是什么,它看起来像你有什么需要在底部。添加一个Texture2D参数并对其进行采样? – Nonameghost

+0

如果你问“如何抽样texture2D”,有一些文档:https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html 只要看看漫反射纹理的例子。 – Nonameghost

+0

我想使用texture2D获得与GIF图片相同的效果,而不是通过生成蒙版。如果我添加了一个纹理2D,它不会旋转或使其错误 –

回答

0

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

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

我不知道你如何在纹理2D的末端得到精确的淡入淡出效果,但也许有一些聪明的时间用法,你可以找出动画。

+0

另外一个选项,材质上的工作表动画:http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture – Nonameghost