当前位置:   article > 正文

Unity Shader初涉之 -- 布尔值控制Shader使用哪种计算_unityshader 申明bool值

unityshader 申明bool值

最近开始学习Shader,深深被它的功能所折服,而在使用时会产生一个需求,能不能用一个布尔值去控制Shader的运算逻辑,可以让他展示两套不同的表现形式,在翻看有关Shader的书籍时看到了这一部分,特此记录:

using UnityEngine;
using UnityEditor;
using System;


public class CustomShaderGUI : ShaderGUI
{
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
// render the default gui
base.OnGUI(materialEditor, properties);

Material targetMat = materialEditor.target as Material;

// see if redify is set, and show a checkbox
bool redify = Array.IndexOf(targetMat.shaderKeywords, "REDIFY_ON") != -1;
EditorGUI.BeginChangeCheck();
redify = EditorGUILayout.Toggle("Redify material", redify);
if (EditorGUI.EndChangeCheck())
{
// enable or disable the keyword based on checkbox
if (redify)
targetMat.EnableKeyword("REDIFY_ON");
else
targetMat.DisableKeyword("REDIFY_ON");
}
}
}

原理也很简单,利用编辑器扩展类去实现添加选项,将这个结果运用于shader中,在shader中添加 CustomEditor "CustomShaderGUI" 直接可以利用了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/107699
推荐阅读
相关标签
  

闽ICP备14008679号