3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

[Unity] surface shaderでライティング

surface shaderでライティングを自前で行うには、#pragmaで必要な関数名を指定するようです。

custom-lighting
#pragma surface surf BasicDiffuse

// 中略

// インライン関数でライティングを実装
inline float4 LightingBasicDiffuse(SurfaceOutput s, fixed3 lightDir, fixed atten) {
    float difLight = max(0, dot(s.Normal, lightDir));
    float4 col;
    col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);
    col.a   = s.Alpha;
    return col;
}

コードを見てもらうと分かりますが、#pragma surface surf BasicDiffuseBasicDiffuseLightingを付与したインライン関数を実装することでライティングが行われます。

inline float4 LightingBasicDiffuse(SurfaceOutput s, fixed3 lightDir, fixed atten) {...}の部分ですね。

また、この引数を変えることでいくつかのライティングを行うことができるようです。

参考にした書籍(Unity Shaders and Effects Cookbook [eBook])から引用させてもらうと、

Forward rendering

forward-rendering
half4 LightingName (SurfaceOutput s, half3 lightDir, half atten){}

This function is used for forward rendering when the view direction is not needed.

ViewDirectionあり

with-view-direction
half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten){}

This function is used in forward rendering when a view direction is needed.

Deferred rendering

deferred-rendering
half4 LightingName_PrePass (SurfaceOutput s, half4 light){}

This function is used when you are using deferred rendering for your project.

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What are the problem?