当前位置:   article > 正文

【Unity】Shader编辑器扩展_vs中unity shader编程扩展

vs中unity shader编程扩展

目录

一、Shader编辑器扩展——简单介绍

1.1、利用ShaderGUI派生类来重新绘制Shader的Inspector窗口

1.2、利用MaterialPropertyDrawers,即加标签形式扩展

二、介绍Shader编辑器扩展的标签形式——方便好用


一、Shader编辑器扩展——简单介绍

1.1、利用ShaderGUI派生类来重新绘制Shader的Inspector窗口

  1. Shader "Unlit/BaseUnityShader"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. //[Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
  7. }
  8. SubShader
  9. {
  10. Tags { "RenderType" = "Opaque" }
  11. LOD 200
  12. CGPROGRAM
  13. #pragma surface surf Lambert addshadow
  14. #pragma shader_feature REDIFY_ON
  15. sampler2D _MainTex;
  16. struct Input {
  17. float2 uv_MainTex;
  18. };
  19. void surf(Input IN, inout SurfaceOutput o)
  20. {
  21. half4 c = tex2D(_MainTex, IN.uv_MainTex);
  22. o.Albedo = c.rgb;
  23. o.Alpha = c.a;
  24. #if REDIFY_ON
  25. o.Albedo.gb *= 0.5;
  26. #endif
  27. }
  28. ENDCG
  29. }
  30. CustomEditor "UnityShaderEditorUI" //指向继承于ShaderGUI的类,进行编辑Shader的Inspector窗口
  31. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. public class UnityShaderEditorUI : ShaderGUI
  7. {
  8. static string KEY = "REDIFY_ON";
  9. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  10. {
  11. base.OnGUI(materialEditor, properties);
  12. Material targetMat = materialEditor.target as Material;
  13. bool redify = Array.IndexOf(targetMat.shaderKeywords, KEY) != -1;
  14. EditorGUI.BeginChangeCheck();
  15. redify = EditorGUILayout.Toggle("Redify material", redify);
  16. if (EditorGUI.EndChangeCheck())
  17. {
  18. if (redify)
  19. {
  20. targetMat.EnableKeyword(KEY);
  21. }
  22. else
  23. {
  24. targetMat.DisableKeyword(KEY);
  25. }
  26. }
  27. }
  28. }

1.2、利用MaterialPropertyDrawers,即加标签形式扩展

  1. Shader "Unlit/BaseUnityShader"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. //Toggle标签,REDIFY_ON是Shader变体,设置该属性会直接赋值到变体上(控制开关)
  7. [Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
  8. }
  9. SubShader
  10. {
  11. Tags { "RenderType" = "Opaque" }
  12. LOD 200
  13. CGPROGRAM
  14. #pragma surface surf Lambert addshadow
  15. //Shader变体 REDIFY_ON
  16. #pragma shader_feature REDIFY_ON
  17. sampler2D _MainTex;
  18. struct Input {
  19. float2 uv_MainTex;
  20. };
  21. void surf(Input IN, inout SurfaceOutput o)
  22. {
  23. half4 c = tex2D(_MainTex, IN.uv_MainTex);
  24. o.Albedo = c.rgb;
  25. o.Alpha = c.a;
  26. #if REDIFY_ON
  27. o.Albedo.gb *= 0.5;
  28. #endif
  29. }
  30. ENDCG
  31. }
  32. //CustomEditor "UnityShaderEditorUI" //指向继承于ShaderGUI的类,进行编辑Shader的Inspector窗口
  33. }

二、介绍Shader编辑器扩展的标签形式——方便好用

  1. Shader "Unlit/DrawerUnityShader"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. //Toggle
  7. [Toggle] _Toggle("Tog?", Float) = 0
  8. [Toggle(ENABLE_FANCY)] _Fancy("Fancy", Float) = 0
  9. [Enum(UnityEngine.ShaderMilk.ShaderEnum)] _ShaderEnum("ShaderEnum", Float) = 1
  10. [Enum(Apple, 1, Banana, 2)] _Flood("Flood", Float) = 1
  11. //三个变种key名称为:属性名+_+枚举名(全大写) 如:_OVERLAY_NONE
  12. [KeywordEnum(None, Add, Multiply)] _Overlay("Overlay", Float) = 0//疑问:如何不选择任意一个变体 为空
  13. //非线性滑动条
  14. [PowerSlider(3.0)] _Shininess("Shininess", Range(0.01, 1)) = 0.08
  15. [IntRange] _Alpha ("Alpha", Range(0, 255)) = 100
  16. [Space] _Prop1 ("Prop1", Float) = 0
  17. [Space(50)] _Prop2 ("Prop2", Float) = 0
  18. [Header(Test)] _Header1("Header1" , Float) = 0
  19. }
  20. SubShader
  21. {
  22. Tags { "RenderType"="Opaque" }
  23. LOD 100
  24. Pass
  25. {
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29. // make fog work
  30. #pragma multi_compile_fog
  31. #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
  32. #pragma multi_compile _ ENABLE_FANCY
  33. //#pragma shader_feature ENABLE_FANCY
  34. #include "UnityCG.cginc"
  35. struct appdata
  36. {
  37. float4 vertex : POSITION;
  38. float2 uv : TEXCOORD0;
  39. };
  40. struct v2f
  41. {
  42. float2 uv : TEXCOORD0;
  43. UNITY_FOG_COORDS(1)
  44. float4 vertex : SV_POSITION;
  45. };
  46. sampler2D _MainTex;
  47. float4 _MainTex_ST;
  48. float _ShaderEnum;
  49. v2f vert (appdata v)
  50. {
  51. v2f o;
  52. o.vertex = UnityObjectToClipPos(v.vertex);
  53. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  54. UNITY_TRANSFER_FOG(o,o.vertex);
  55. return o;
  56. }
  57. fixed4 frag (v2f i) : SV_Target
  58. {
  59. // sample the texture
  60. fixed4 col = tex2D(_MainTex, i.uv);
  61. // apply fog
  62. UNITY_APPLY_FOG(i.fogCoord, col);
  63. if (_ShaderEnum == 1) {
  64. col = fixed4(1, 0, 0, 1);
  65. }else if (_ShaderEnum == 4) {
  66. col = fixed4(0, 1, 0, 1);
  67. }
  68. #if _OVERLAY_NONE
  69. col = fixed4(1, 1, 0, 1);
  70. #elif _OVERLAY_ADD
  71. col = fixed4(1, 0, 1, 1);
  72. #elif _OVERLAY_MULTIPLY
  73. col = fixed4(1, 0.5, 1, 1);
  74. #else
  75. col = fixed4(0, 0, 0, 1);
  76. #endif
  77. return col;
  78. }
  79. ENDCG
  80. }
  81. }
  82. }

 更多知识可参考Unity官方手册Custom Shader GUI、MaterialPropertyDrawers等

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

闽ICP备14008679号