zoukankan      html  css  js  c++  java
  • [UnityShader基础]07.MaterialPropertyDrawer

    参考链接:

    https://blog.csdn.net/e295166319/article/details/60141677

    https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html

    1.Toggle

    a.[Toggle] _Invert ("Invert?", Float) = 0

    勾选时,表示使shader关键字_INVERT_ON生效,关键字格式为:name_ON(全部大写)

    取消勾选时,表示使对应的关键字失效

    要注意的是,这个关键字是要去定义的,否则Toggle无效

     1 Shader "Custom/TestMaterialProperty"
     2 {
     3     Properties
     4     {
     5         [Toggle] _Invert ("Invert?", Float) = 0
     6     }
     7     SubShader
     8     {
     9         Pass
    10         {
    11             CGPROGRAM
    12             #pragma vertex vert
    13             #pragma fragment frag
    14             #pragma multi_compile __ _INVERT_ON
    15 
    16             #include "UnityCG.cginc"
    17 
    18             struct appdata
    19             {
    20                 float4 vertex : POSITION;
    21             };
    22 
    23             struct v2f
    24             {
    25                 float4 vertex : SV_POSITION;
    26             };
    27 
    28             v2f vert (appdata v)
    29             {
    30                 v2f o;
    31                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    32                 return o;
    33             }
    34             
    35             fixed4 frag (v2f i) : SV_Target
    36             {
    37                 fixed4 col = fixed4(1, 0, 0, 0);
    38                 #ifdef _INVERT_ON
    39                 col = fixed4(0, 1, 0, 0);
    40                 #endif
    41                 return col;
    42             }
    43             ENDCG
    44         }
    45     }
    46 }

    效果如下,来回勾选就可以看到颜色在切换了

    b.[Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0

    勾选时,表示使shader关键字ENABLE_FANCY生效

    取消勾选时,表示使对应的关键字失效

     1 Shader "Custom/TestMaterialProperty"
     2 {
     3     Properties
     4     {
     5         [Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0
     6     }
     7     SubShader
     8     {
     9         Pass
    10         {
    11             CGPROGRAM
    12             #pragma vertex vert
    13             #pragma fragment frag
    14             #pragma multi_compile __ ENABLE_FANCY
    15 
    16             #include "UnityCG.cginc"
    17 
    18             struct appdata
    19             {
    20                 float4 vertex : POSITION;
    21             };
    22 
    23             struct v2f
    24             {
    25                 float4 vertex : SV_POSITION;
    26             };
    27 
    28             v2f vert (appdata v)
    29             {
    30                 v2f o;
    31                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    32                 return o;
    33             }
    34             
    35             fixed4 frag (v2f i) : SV_Target
    36             {
    37                 fixed4 col = fixed4(1, 0, 0, 0);
    38                 #ifdef ENABLE_FANCY
    39                 col = fixed4(0, 1, 0, 0);
    40                 #endif
    41                 return col;
    42             }
    43             ENDCG
    44         }
    45     }
    46 }

    2.KeywordEnum

    [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0

    当选择None时,表示使shader关键字_OVERLAY_NONE生效,关键字格式为:属性名_枚举名(全部大写)

     1 Shader "Custom/TestMaterialProperty"
     2 {
     3     Properties
     4     {
     5         [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
     6     }
     7     SubShader
     8     {
     9         Pass
    10         {
    11             CGPROGRAM
    12             #pragma vertex vert
    13             #pragma fragment frag
    14             #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
    15 
    16             #include "UnityCG.cginc"
    17 
    18             struct appdata
    19             {
    20                 float4 vertex : POSITION;
    21             };
    22 
    23             struct v2f
    24             {
    25                 float4 vertex : SV_POSITION;
    26             };
    27 
    28             v2f vert (appdata v)
    29             {
    30                 v2f o;
    31                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    32                 return o;
    33             }
    34             
    35             fixed4 frag (v2f i) : SV_Target
    36             {
    37                 fixed4 col = fixed4(1, 1, 1, 1);
    38                 #if _OVERLAY_NONE
    39                     col = fixed4(1, 0, 0, 1);
    40                 #elif _OVERLAY_ADD
    41                     col = fixed4(0, 1, 0, 1);
    42                 #elif _OVERLAY_MULTIPLY
    43                     col = fixed4(0, 0, 1, 1);
    44                 #endif
    45                 return col;
    46             }
    47             ENDCG
    48         }
    49     }
    50 }

    效果如下,选择不同的选项即显示不同的颜色

  • 相关阅读:
    Atitit attilax要工作研究的要素 纪要 方案 趋势 方向 概念 理论
    Atitit 常见每日流程日程日常工作.docx v7 r8f
    Atitit it 互联网 软件牛人的博客列表
    Atitit 信息链(Information Chain)的概念理解 attilax总结
    Atitit 知识点的体系化 框架与方法 如何了解 看待xxx
    Atitit 聚合搜索多个微博 attilax总结
    Atitit 企业知识管理PKM与PIM
    Atitit 项目沟通管理 Atitit 沟通之道 attilax著.docx
    Atitit 项目管理软件 在线服务 attilax总结 1. 项目管理协作的历史 1 1.1. Worktile 406k 1 1.2. Teambition  584k in baidu
    Atitit.每周末总结 于每周一计划日程表 流程表 v8 import 上周遗漏日志补充 检查话费 检查流量情况 Crm问候 Crm表total and 问候
  • 原文地址:https://www.cnblogs.com/lyh916/p/10702438.html
Copyright © 2011-2022 走看看