3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity] Shader ForgeをつかってMatCapシェーダーを作る

Last updated at Posted at 2017-08-25

#概要
UnityのShader Forgeを使用してMatCapを作る。

#環境
Unity 5.5.2p4
Shader Forge 1.36

#実装内容

Free MatCap Shaders
https://www.assetstore.unity3d.com/jp/#!/content/8221

このアセットのシェーダー「MatCap/Vertex/Plain」と同じ見た目になるように実装する。

ノードの接続内容
image.png

#実行結果

左が「MatCap/Vertex/Plain」のもの。
右がShader Forgeで作ったもの。
image.png

Shader Forgeによって自動生成されたコード

TestMatCap.shader
Shader "Shader Forge/TestMatCap" {
    Properties {
        _MatCap ("MatCap", 2D) = "white" {}
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal 
            #pragma target 2.0
            uniform sampler2D _MatCap; uniform float4 _MatCap_ST;
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float3 normalDir : TEXCOORD0;
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                i.normalDir = normalize(i.normalDir);
                float3 normalDirection = i.normalDir;
////// Lighting:
////// Emissive:
                float node_4798 = 0.5;
                float2 node_4174 = (node_4798+(node_4798*mul( UNITY_MATRIX_V, float4(i.normalDir,0) ).xyz.rgb.rg));
                float4 _MatCap_var = tex2D(_MatCap,TRANSFORM_TEX(node_4174, _MatCap));
                float3 emissive = _MatCap_var.rgb;
                float3 finalColor = emissive;
                return fixed4(finalColor,1);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}

ピクセルシェーダーの計算が重いような、、、。
Shader Forgeのコードを基にピクセルシェーダーの処理を
頂点シェーダーに移したものを手書きで直したもの。

TestMatCap.shader
Shader "Shader Forge/TestMatCap2" {
    Properties {
        _MatCap ("MatCap", 2D) = "white" {}
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal 
            #pragma target 2.0
            uniform sampler2D _MatCap; uniform float4 _MatCap_ST;
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float3 normalDir : TEXCOORD0;
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.normalDir = mul((float3x3)UNITY_MATRIX_V, v.normal);
                o.normalDir = o.normalDir * 0.5 + 0.5;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                float4 _MatCap_var = tex2D(_MatCap, i.normalDir);
                return _MatCap_var;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}

手直ししたものでも同じ結果になった。
最適化した状態で出力できないものだろうか、、、

#感想
Shader Forgeって処理重いのかな、、、。
Amplify Shader Editor(https://www.assetstore.unity3d.com/jp/#!/content/68570)で
同じノード実装した結果、だれかやってくれないかなー。

あと、なぜかShader Forgeがアセットストアからアップデートできなくなってしまった:sweat:

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?