3
3

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] surface shaderでライティング

Posted at

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.

3
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?