LoginSignup
1
2

More than 3 years have passed since last update.

【Unity】自分なりのShaderLabメモ (随時更新

Posted at

初めに

これは自分なりにShaderLabをどこまで理解できているかの確認とメモ代わりに残すものです。
多分所々間違えがあると思いますが、間違えがあれば教えてほしいです。

参考にしたサイト

Unity ShaderLab ノート
【Unityシェーダ入門】Unityのシェーダで遊んでみよう

レンダリングパイプライン

Input AssemblyVertexSurfaceLightningRender Target Output
覚えておこうね!

目次

  • Standard Surface Shaderの中身
    • 名前
    • プロパティ
      • 型一覧
    • Tags
      • Tag一覧
    • LOD
    • Cg言語開始と終わり
    • 関数宣言

Standard Surface Shaderの中身

tandard Surface Shaderの中身はこんな感じになっています。

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

ここから自分なりに解説を入れていきます。

名前

     ↓どの名前に紐づけするか ↓Shaderの名前"
Shader "Custom / NewSurfaceShader"
Shader "Custom / NewSurfaceShader / test"

このように階層を増やすことも可能

プロパティ

//-------------------------プロパティ(パラメーターInspectorで触れる)----------------
    Properties
    {
        //プロパティの内部にはこんな感じで書く
        //名前("名前", 型)=(初期値)
        //名前("名前", 型)= 初期値
        //名前("名前", 型)= "初期値"{}

        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
//-------------------------プロパティ(パラメーターInspectorで触れる)----------------

型一覧

型名 説明
Color 色情報を取り扱う(RGBA)
2D テクスチャを取り扱います
Float Range 書き方はRange(最小値,最大値)でOK
Float 普通のfloat
Vector 4つのfloatの集合体(number,number,number,number)

Tags

Tagを設定することによって、このShader全体の設定をすることができます。
またSubShaderの中でしか使えないTagやPassの中でしか使えないTagが存在しますので間違えないようにしましょう。

//こんな感じの
Tags { "RenderType"="Opaque" }

Tag一覧

SubShaderで使える

Tag名 説明
Queue レンダリング順 Background, Geometry, AlphaTest, Transparent, Overlay, Geometry+3等
RenderType あまり詳しくない…不透明ならOpaque、透明ならTransparentだと思います?詳しい人教えて Opaque, Transparent
DisableBatching バッチするかしないか False, True, LODFading
ForceNoShadowCasting シャドウの投影するかしないか True, False
IgnoreProjector プロジェクターが投影するかどうかフラグ を使用せよとのこと True, False
CanUseSpriteAtlas シェーダがSpriteを使っていた場合に不具合ある場合はFalseを使用せよとのこと を使用せよとのこと True, False
PreviewType マテリアルインスペクタのPreview画面の3D形状(球だったり、SkyBoxだったり) Sphere, Plane, Skybox

Passで使える

Tag名 説明
LightMode ライトの設定 Always, ForwardBase, ForwardAdd, Deferred, ShadowCaster, PrepassBase, PrepassFinal, Vertex, VertexLMRGBM, VertexLM
PassFlags データ渡しのフラグ管理 OnlyDirectional
RequireOptions 条件をレンダリング SoftVegetation

LOD

//こんなやつ
LOD 100

rend.material.shader.maximumLODがここに設定したLOD以下の場合そのShaderを使用する。
こんな感じのShaderがある場合


    SubShader {
        LOD 150
        Pass{
            // 赤
            Color(1,0,0,1)
        }
    }
    SubShader {
        LOD 100
        Pass{
            // 緑
            Color(0,1,0,1)
        }
    }
    SubShader {
        LOD 50
        Pass{
            // 青
            Color(0,0,1,1)
        }
    }
    SubShader {
        Pass{
            // 白
            Color(1,1,1,1)
        }
    }

rend.material.shader.maximumLODの値次第で処理する内容を変えることができる。

Cg言語開始と終わり

SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

      //-----------ここから始まって-----------
        CGPROGRAM

        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    //-----------ここで終わり!-----------
    }

CG言語での処理や関数宣言はCGPROGRAMの後に書き
ENDCGの後には処理はかかない。

関数宣言

//ここでサーフェスシェーダを宣言して surfとして使えるようにします。
 #pragma surface surf Standard fullforwardshadows

こんな感じに宣言することによって

void surf ()

が使えるようになります。

1
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
1
2