多版本Shader与multi_compile 多版本Shader与multi_compile

多版本Shader与multi_compile

 
 
#pragma multi_compile Type_1 Type_2 Type_3 ...

这个指令将会生成多个Shader变体(variants),运行时根据材质或是全局的Keyword决定哪个变体起效

所有关键字都没有起效的话, 会选择第一个起效,所以一般是写 XXX_OFF XXX_ON  默认关闭某选项

#pragma multi_compile __ FOO_ON

这种方式会少用一个关键字(总关键字数目有限制 256最大,内部已经用了大约60个)

#pragma shader_feature 和 #pragma multi_compile相似

不同的是shader_feature没有用到的不会被包含进去 multi_compile 全部版本都会被包含

所以 shader_feature 材质用 multi_compile 代码控制用

#pragma shader_feature FANCY_STUFF 是  #pragma shader_feature _ FANCY_STUFF 的缩写

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:

    • 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
 
#pragma multi_compile Type_1 Type_2 Type_3 ...

这个指令将会生成多个Shader变体(variants),运行时根据材质或是全局的Keyword决定哪个变体起效

所有关键字都没有起效的话, 会选择第一个起效,所以一般是写 XXX_OFF XXX_ON  默认关闭某选项

#pragma multi_compile __ FOO_ON

这种方式会少用一个关键字(总关键字数目有限制 256最大,内部已经用了大约60个)

#pragma shader_feature 和 #pragma multi_compile相似

不同的是shader_feature没有用到的不会被包含进去 multi_compile 全部版本都会被包含

所以 shader_feature 材质用 multi_compile 代码控制用

#pragma shader_feature FANCY_STUFF 是  #pragma shader_feature _ FANCY_STUFF 的缩写

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:

    • 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

猜你喜欢

转载自www.cnblogs.com/nafio/p/10832489.html