您当前的位置: 首页 >  unity

Jave.Lin

暂无认证

  • 2浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity Shader - multi_compile 变体测试

Jave.Lin 发布时间:2019-06-24 20:55:21 ,浏览量:2

Code

脚本的注释可以按看,有些说明,与技巧

Main Testing script - 主要测试的脚本
// jave.lin 2019.06.24
using UnityEngine;

public enum AMBIENT_TOGGLE
{
    On,
    Off
}

public enum AMBIENT_COLOR_TYPE
{
    LightModel,
    Sky,
    Equator,
    Ground
}

public enum AMBIENT_APPLY_ONLY_R
{
    On,
    Off
}

public class TestMultiCompile : MonoBehaviour
{
    public AMBIENT_TOGGLE ambientToggle = AMBIENT_TOGGLE.On;
    public AMBIENT_COLOR_TYPE ambientColorType = AMBIENT_COLOR_TYPE.LightModel;
    public AMBIENT_APPLY_ONLY_R ambientApplyOnlyR = AMBIENT_APPLY_ONLY_R.Off;
    public string[] enabledLocalKeywords;

    private MeshRenderer[] mrs;
    // Start is called before the first frame update
    void Start()
    {
        mrs = GetComponentsInChildren();
        //避免有时你在退出unity运行时,有些开关的状态还是退出钱的设置,所以这里需要初始化一下
        //string[] allGlobalKeywords = { ... }; // 所有你想控制的全局关键字数组
        //for (int i = 0; i < allGlobalKeywords.Length; i++)
        //{
        //  Shader.DisableKeyword(allGlobalKeywords[i]);
        //}
        //然后有些需要Enable的,可以在下面这里再Enable
        //对局部的关键字就不需要上面的处理,因为你可以从Material.shaderKeywords里很容易看出来
        //对关键字的维护,可以参考TestMultiCompileEditor.cs的写法(目前是简单的写法)
    }

    // Update is called once per frame
    void Update()
    {
        // 应用优先级会被Shader.material.Enable/DisableKeyword覆盖
        if (ambientToggle == AMBIENT_TOGGLE.On)
            Shader.EnableKeyword("AMBIENT_ON");
        else
            Shader.DisableKeyword("AMBIENT_ON");

        // 下面的Enable/DisableKeyword都可以封装一下,用分组的方式,让同一组的互斥就好了
        switch (ambientColorType)
        {
            case AMBIENT_COLOR_TYPE.LightModel:
                Shader.EnableKeyword("LIGHTMODEL_AMBIENT");
                Shader.DisableKeyword("SKY_COLOR");
                Shader.DisableKeyword("EQUATOR_COLOR");
                Shader.DisableKeyword("GROUND_COLOR");
                break;
            case AMBIENT_COLOR_TYPE.Sky:
                Shader.DisableKeyword("LIGHTMODEL_AMBIENT");
                Shader.EnableKeyword("SKY_COLOR");
                Shader.DisableKeyword("EQUATOR_COLOR");
                Shader.DisableKeyword("GROUND_COLOR");
                break;
            case AMBIENT_COLOR_TYPE.Equator:
                Shader.DisableKeyword("LIGHTMODEL_AMBIENT");
                Shader.DisableKeyword("SKY_COLOR");
                Shader.EnableKeyword("EQUATOR_COLOR");
                Shader.DisableKeyword("GROUND_COLOR");
                break;
            case AMBIENT_COLOR_TYPE.Ground:
                Shader.DisableKeyword("LIGHTMODEL_AMBIENT");
                Shader.DisableKeyword("SKY_COLOR");
                Shader.DisableKeyword("EQUATOR_COLOR");
                Shader.EnableKeyword("GROUND_COLOR");
                break;
            default:
                break;
        }

        SetApplyOnlyR(ambientApplyOnlyR);

        // material.shaderKeywrods只会返回局部的关键字,所以Shader.EnableKeyword设置的是不会在这增加的
        enabledLocalKeywords = mrs[0].material.shaderKeywords;
    }

    private void SetApplyOnlyR(AMBIENT_APPLY_ONLY_R v)
    {

        for (int i = 0; i Rendering->Lighting Settings->Scene Tab

Scene Tab->Environment->Environment Lighting 在这里插入图片描述 将场景的环境光的Source设置为:Gradient,分别设置Sky,Equator,GroundColor颜色为:红,绿,蓝,因为我们的shader用到

Inspector Informations

在这里插入图片描述 在Global Keyword Summary与Local Keyword Summary都可以很方便看到关键字的的开关情况

  • Ambient Toggle 是控制Shader里头使用是否使用AMBIENT_ON 变体代码。
  • Ambient Color Type 对应就是环境光的颜色
  • Ambient Apply Only R 是否对环境观值应用R通道,On:是,那么环境光R通道将影响整体亮度;Off:否,那么环境光RGB通道将影响整体色调

其中 Ambient Toggle、Amibent Color Type都是全局关键字,Amibent Apply Only R是局部关键字

某个shader有多少个全局、局部关键字,都可以通过在Shader Inspector->…按钮,然后弹出的面板中可以看到

如下图: Global Keywords: 在这里插入图片描述 Local Keywords: 在这里插入图片描述

Scene View

在这里插入图片描述 最后开启 AmbientApplyOnlyR就花屏了,然后用Ground Color的R通道就可以设置亮度了

Project

MultiCompileTest 提取码: gdck

References

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

关注
打赏
1664331872
查看更多评论
0.0399s