Making multiple shader program variants 制作多个着色程序变体 - Unity Shader Reference 系列12

Making multiple shader program variants 制作多个着色程序变体

本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

Often it is convenient to keep most of a piece of shader code fixed but also allow slightly different shader “variants” to be produced. This is commonly called “mega shaders” or “uber shaders”, and is achieved by compiling the shader code multiple times with different preprocessor directives for each case.
通常,保持着色器代码的大部分不变,但也允许生成稍微不同的着色器“变体”是很方便的。这通常被称为“mega shaders”或“uber shaders”,通过多次编译shader代码,并为每种情况使用不同的预处理器指令来实现。

In Unity this can be achieved by adding a #pragma multi_compile or #pragma shader_feature directive to a shader snippet. This works in surface shaders too.
在Unity中,这可以通过添加一个#pragma multi_compile或#pragma shader_feature指令到shader代码片段实现。这也适用于表面着色器。

At runtime, the appropriate shader variant is picked up from the Material keywords (Material.EnableKeyword and DisableKeyword) or global shader keywords (Shader.EnableKeyword and DisableKeyword).
在运行时,从材质关键字(材质)中选取适当的着色器变体(Material.EnableKeyword和DisableKeyword)或全局着色器关键字(shader.EnableKeyword和DisableKeyword)。

How multi_compile works
A directive like:

#pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON

Will produce two shader variants, one with FANCY_STUFF_OFF defined, and another with FANCY_STUFF_ON. At runtime, one of them will be activated based on the Material or global shader keywords. If neither of these two keywords are enabled then the first one (“off”) will be used.
将生成两个着色器变体,一个定义了FANCY_STUFF_OFF,另一个定义了FANCY_STUFF_ON。在运行时,其中一个将根据材质或全局着色器关键字被激活。如果这两个关键字都不启用,那么将使用第一个关键字(“off”)。

There can be more than two keywords on a multi_compile line, for example this will produce four shader variants:
在multi_compile行上可能有两个以上的关键字,例如,这

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值