Unityのカスタムシェーダー

Unityのカスタムシェーダー
方向ライト計算つき
スクリプトからシェーダへMVP行列をわたすサンプル

参考）
https://matcha-choco010.net/2018/10/22/unity-shader-forward-lambert/
https://light11.hatenadiary.com/entry/2020/02/19/224324

座標系の変換
https://matcha-choco010.net/2018/08/30/mvp-matrix/

参考）
```
Shader "Custom/TestShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 100

        Pass
        {
            Tags { "LightMode" = "ForwardBase"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            uniform float4x4  _matView;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            void vert(in appdata v, out v2f o)
            {
                //o.vertex = UnityObjectToClipPos(v.vertex);
                o.vertex = mul(mul(UNITY_MATRIX_P, mul(_matView, unity_ObjectToWorld)), v.vertex);

                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            }


            void frag(in v2f i, out fixed4 col : SV_Target)
            {
                float3 lightDir = _WorldSpaceLightPos0.xyz;
                float3 normal = normalize(i.worldNormal);
                float NL = dot(normal, lightDir);

                float3 baseColor = tex2D(_MainTex, i.uv);
                float3 lightColor = _LightColor0;

                col = fixed4(baseColor * lightColor * max(NL, 0), 0);
            }
            ENDCG
        }
    }
}
```

# 平行移動

Unityシェーダの行列はfloat4x4 タイプで、列優先 (column major) です。

```
    private void SetMatrix(Camera camera)
    {
        Matrix4x4 viewMatrix = camera.worldToCameraMatrix;
        //var projectionMatrix = GL.GetGPUProjectionMatrix(camera.projectionMatrix, true);
        //Shader.SetGlobalMatrix("_MatrixVP", projectionMatrix * viewMatrix);

        Shader.SetGlobalMatrix(matName, viewMatrix);
        Shader.SetGlobalFloat("_MoveX", 3.0f);
        Shader.SetGlobalFloat("_MoveY", 1.0f);
        Shader.SetGlobalFloat("_MoveZ", 0.0f);
//        material.SetFloat(matName, viewMatrix);
    }
```

```
            void vert(in appdata v, out v2f o)
            {
                //o.vertex = UnityObjectToClipPos(v.vertex);
                float4x4 moveMatrix = float4x4(1, 0, 0, _MoveX,
                    0, 1, 0, _MoveY,
                    0, 0, 1, _MoveZ,
                    0, 0, 0, 1);

                float4 t_pos = mul(moveMatrix, v.vertex);

                o.vertex = mul(mul(UNITY_MATRIX_P, mul(_matView, unity_ObjectToWorld)), t_pos);

                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
            }

```