多版本Shader與multi_compile

https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.htmlhtml

 

#pragma multi_compile Type_1 Type_2 Type_3 ...ui

這個指令將會生成多個Shader變體(variants),運行時根據材質或是全局的Keyword決定哪一個變體起效this

全部關鍵字都沒有起效的話, 會選擇第一個起效,因此通常是寫 XXX_OFF XXX_ON  默認關閉某選項3d

 

#pragma multi_compile __ FOO_ONhtm

這種方式會少用一個關鍵字(總關鍵字數目有限制 256最大,內部已經用了大約60個)ip

 

#pragma shader_feature 和 #pragma multi_compile類似get

不一樣的是shader_feature沒有用到的不會被包含進去 multi_compile 所有版本都會被包含it

因此 shader_feature 材質用 multi_compile 代碼控制用基礎

 

#pragma shader_feature FANCY_STUFF 是  #pragma shader_feature _ FANCY_STUFF 的縮寫unity3d

 

shader中判斷

#ifdef Type_n

... ..

#endif

 

 

 

在腳本里用

Material.EnableKeyword 和 DisableKeyword

Shader.EnableKeyword 和 DisableKeyword

控制keyword起效

 

注意: 5.4中用Shader.EnableKeyword設置了全局使用默認的key, 用Material.EnableKeywor設置單個不使用默認值無效

          用Shader.EnableKeyword設置了全局不使用默認的key, 用Material.EnableKeywor設置單個使用默認值起效

          EnableKeyword 和 DisableKeyword 最好組合使用 好比一組有三個,必須寫1個enable2個disable

 

 

#pragma multi_compile A B C

#pragma multi_compile D E

會生成6(3*2)個變體

 

用上面的方式並不能根據不一樣的狀況減小Pass,

能夠用LOD屬性控制SubShader的起效來手動的管理起效的Pass

把須要的基礎Pass放在基礎Shader中,用UsePass來組織須要起效的Pass

默認的ShaderLOD值爲 200

 

 

Built-in shaders in Unity have their LODs set up this way:

來源: https://docs.unity3d.com/Manual/SL-ShaderLOD.html

  • VertexLit kind of shaders = 100
  • Decal, Reflective VertexLit = 150
  • Diffuse = 200
  • Diffuse Detail, Reflective Bumped Unlit, Reflective Bumped VertexLit = 250
  • Bumped, Specular = 300
  • Bumped Specular = 400
  • Parallax = 500
  • Parallax Specular = 600
相關文章
相關標籤/搜索