2016-12-19 114 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