夜風のMixedReality

xRと出会って変わった人生と出会った技術を書き残すためのGeekなHoloRangerの居場所

UnityでテクスチャがスクロールするShaderを書く

本日はShader枠です。

Shaderでは様々な表現が行えますが、今回はその最も基本的な表現の一つとしてUVに沿ってテクスチャがスクロールするShaderを書いていきます。

〇コード

今回は次のShaderを基に編集していきます。

Shader "Custom/scroll"
{
    Properties
    {
            // Main maps.
            _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
           
            _MainTex("Albedo", 2D) = "white" {}
    }

    SubShader
    {
   Pass
            {
                Name "Main"
                Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
                LOD 100

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"
                #include "UnityStandardConfig.cginc"
                #include "UnityStandardUtils.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                //追加
                float2 uv : TEXCOORD0;

                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 position : SV_POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };
             fixed4 _Color;
             sampler2D _MainTex;
        
            v2f vert(appdata_t v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                float4 vertexPosition = v.vertex;

                o.uv = v.uv;
                o.position = UnityObjectToClipPos(vertexPosition);
                return o;
            }

            fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target
            {
                fixed4 albedo = tex2D(_MainTex, i.uv);
                albedo *= _Color;
                fixed4 output = albedo;
                return output;
            }
            ENDCG
        }
    }
    
    Fallback "Hidden/InternalErrorShader"
}

上記のShaderではテクスチャが貼られるだけのシンプルかつ基本形なShaderです。

f:id:Holomoto-Sumire:20201014212118j:plain

albedoに次のようにTime関数を加えました。

fixed4 frag(v2f i ,fixed facing : VFACE):SV_Target
{
  fixed4 albedo = tex2D(_MainTex, i.uv+_Time);
 ...
}

tex2D関数は何をどのようにというようにテクスチャを指定するため、時間によってどのようにに当たるuv座標が変わることでスクロールするようになりました。

f:id:Holomoto-Sumire:20201014213635g:plain

このままではスクロールが早すぎるので次のように変更しました。

    Properties
    {
        // Main maps.
        _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
        _scroll("Scroll" ,Range(0,1))=0.1
        _MainTex("Albedo", 2D) = "white" {}
    }
...
           float _scroll;
            fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target
            {
                fixed4 albedo = tex2D(_MainTex, i.uv+(_Time*_scroll));
           }

これでスクロールの速度を変えられるようになりました。

f:id:Holomoto-Sumire:20201014214446g:plain

〇本日描いたShader

Shader "Custom/scroll"
{
    Properties
    {
        // Main maps.
        _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
        _scroll("Scroll" ,Range(0,1))=0.1
        _MainTex("Albedo", 2D) = "white" {}
    }

        SubShader
    {
   Pass
            {
                Name "Main"
                Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
                LOD 100

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"
                #include "UnityStandardConfig.cginc"
                #include "UnityStandardUtils.cginc"
         

            struct appdata_t
            {
                float4 vertex : POSITION;
                //追加
                float2 uv : TEXCOORD0;

                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 position : SV_POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };
             fixed4 _Color;
             sampler2D _MainTex;

            v2f vert(appdata_t v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
                float4 vertexPosition = v.vertex;

                o.uv = v.uv;
                o.position = UnityObjectToClipPos(vertexPosition);
                return o;
            }
            float _scroll;
            fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target
            {
                fixed4 albedo = tex2D(_MainTex, i.uv+(_Time*_scroll));
                albedo *= _Color;
                fixed4 output = albedo;
                return output;
            }
            ENDCG
        }
    }

        Fallback "Hidden/InternalErrorShader"
}