2017-02-19 9 views
0

Heyasを回転させるグラデーションカラー、ユニティ頂点シェーダ - テクスチャの中央付近にあり

私はちょうどshadertoy上、このように、中心付近の色を回転させるグラデーションシェーダを達成しようとしていますが、この1つはありますフラグメントシェーダ

これは私の最初のシェイダー体験ですが、今は2日間勉強していますが、翻訳に問題があり、線形代数の多くの用語に慣れてしまいました。ネイティブスピーチスクールレッスン。

だから私は、私は、スクリプトを通過する回転行列があります。このような頂点を回転させるシェーダで

public class RotationMatrix : MonoBehaviour 
{ 
    public float rotationSpeed = 10f; 
    public Vector2 texPivot = new Vector2(0.5f, 0.5f); 
    Renderer _renderer; 

    void Start() 
    { 
     _renderer = GetComponent<Renderer>(); 
    } 

    protected void Update() 
    { 
     Matrix4x4 t = Matrix4x4.TRS(-texPivot, Quaternion.identity, Vector3.one); 

     Quaternion rotation = Quaternion.Euler(0, 0, Time.time * rotationSpeed); 
     Matrix4x4 r = Matrix4x4.TRS(Vector3.zero, rotation, Vector3.one); 

     Matrix4x4 tInv = Matrix4x4.TRS(texPivot, Quaternion.identity, Vector3.one); 
     _renderer.material.SetMatrix("_Rotation", tInv * r * t); 
    } 
} 

:これは本当に私フランケンシュタインのコードであることを

#pragma vertex vert 
#pragma fragment frag 
#include "UnityCG.cginc" 

    sampler2D _MainTex; 
    float4 _MainTex_ST; 
    fixed4 _Color; 
    fixed4 _Color2; 
    fixed _Scale; 
    float4x4 _Rotation; 

    struct v2f { 
     float4 pos : SV_POSITION; 
     fixed4 color : COLOR; 
     float2 uv : TEXCOORD0; 
    }; 

    v2f vert(appdata_full v) 
    { 
     v2f o; 
     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); 
     v.texcoord.xy = mul(_Rotation, v.texcoord.xy); 
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 
     o.color = lerp(_Color, _Color2, v.texcoord.x * _Scale); 

     return o; 
    } 

    fixed4 frag(v2f i) : SV_Target 
    { 
     float4 color; 
     color.rgb = i.color.rgb; 
     color.a = tex2D(_MainTex, i.uv).a * i.color.a; 
     return color; 
    } 


     ENDCG 
    } 
    } 

注意をチュートリアルやフォーラムの投稿から作成されました.Cg/HLSLではすべてが新しくなっています。お時間をSorry for the eye cancer

ありがとう:ここ

は私が得るものです。

答えて

1

コーナーにあるクォード(0,0)のUV座標の原点を中心に回転が起こることを除いて、コードは期待どおりに機能するようです。あなたのUVが正規化されていると考えると、変形の前後でオフセットがあるのはです。

サイドノートon optim:グローバルな_Timeパラメータにアクセスできるので、フレームごとにコードから行列を設定する必要はありません。シェーダですぐに作成できます。

half2 Rotate2D(half2 _in, half _angle) { 
    half s, c; 
    sincos(_angle, s, c); 
    float2x2 rot = { c, s, -s, c }; 
    return mul(rot, _in); 
} 

ため、あなたrotationSpeedパラメータは材料定数

することができ
関連する問題