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

「テクスチャ2Dのサンプル方法」をお探しの場合は、いくつかのドキュメントがあります:https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html 拡散テクスチャの例を見てください。 – Nonameghost

+0

私は、maskを生成するのではなく、texture2Dを使ってGIFピクチャと同じ効果を得たいと思います。 texture2Dを追加した場合、回転していないか間違っています –

答えて

0

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

[OK]を私が正しくあなたの質問を理解していれば、あなたがしたいですテクスチャ2Dパラメータを追加して回転させます。時間の経過とともにUV座標を回転させる必要があります。これは、おそらく上のリンクのコードを使用して行うことができます。

テクスチャ2Dの最後に正確なフェードをどのように取得するのかは分かりませんが、アニメーションを理解する時間を賢明に使うことはできますか。

+0

また、素材のシートアニメーションもオプションです:http://wiki.unity3d.com/index.php?title=Animating_Tiled_texture – Nonameghost

関連する問題