当前位置:网站首页>[unity shader custom material panel part I]
[unity shader custom material panel part I]
2022-07-01 06:24:00 【The king of scrolls is coming】
Customizing the material panel is done by UI Simple adjustments to the style can make it written Shader More convenient and easy to use .
Unity Provides the base class :
1. Different types of DrawerClass
The following table shows how to MaterialPropertyDrawer Built in different types of DrawerClass A summary is made .
| type | describe |
|---|---|
| ToggleDrawer | take float The type data is displayed as a switch , The value can only be 0 perhaps 1,0 To close ,1 For opening . |
| EnumDrawer | Enumeration will float The type data is displayed as a drop-down list , It can be used to select the mixing coefficient , Comparison methods, etc , You can also customize it . |
| KeywordEnumDrawer | and EnumDrawer similar , Will be float The type data is displayed as a drop-down list , But you need to define shader keyword Can be used . |
| PowerSliderDrawer | Sliding bar of exponential correspondence , The values on the slider no longer correspond to the linear relationship . |
| IntRangeDrawer | Display the range data as a slider that can only set integers . |
Writing Shader When ,DrawerClass It needs to be written before the corresponding attribute "[ ]" in , The suffix of the category is “Drawer” No need to add , because Unity It will be added automatically when editing .
1.1.Toggle
take float The data of type is displayed on the material property panel in the form of a switch , The value can only be set to 0 or 1“0 Bit off /1 Bit on ”.
When the switch is on ,Shader key word (keyword) Will be Unity The default setting is “Property name” + “ON”, It should be noted that , All letters of keywords must be capitalized .
for example : [Toggle] _Invert("Invert color?" , Float) = 0
Shader The keyword of will be set to _INVERT_ON
Besides using Unity Default keyword , You can also customize a special keyword ,
for example :[Toggle(ENABLE_FANCY) ] _Fancy("Fancy ?" , Float) = 0
Names in parentheses ENABLE_FANCY It's custom Shader key word .
1.2.Enum
enumeration (Enum) take float The data of type is displayed on the material property panel in the form of pull-down list ,Unity Some built-in enumeration classes are provided for users , for example BlendMode、CillMode、ComparaFunction
for example :[Enum(UnityEngine.Rendering.BlendMode) ] _Blend("Blend mode" , Float) = 1
This is a Unity Built in enumeration class of all mixing coefficients , The default value is 0 Means to select the first mixing coefficient , The default value is 1 Indicates that the second mixing coefficient is selected , And so on . The final display effect on the material panel is shown in the figure :
These options are Shader All mixing factors that can be used in .
You can also customize the name of the enumeration / Numerical pair , But an enumeration can only be customized at most 7 Names / Numerical pair .
for example :[Enum(Off, 0, On, 1) ] _ZWrite("ZWrite" , Float) = 0
The enumeration defined is “ Whether to write deeply ”, In parentheses is the name of the definition / Numerical pair , Serial number 0 Corresponding Off, Serial number 1 Corresponding On, Intermediate symbol “ , ” Interval on , The default is serial number 0, That is to say Off.
1.3.KeywordEnum
Keyword enumeration (KeywordEnum) It is similar to ordinary enumeration , Also is to float Type of data to The form of drop-down list is displayed on the material property panel , But keyword enumeration will have corresponding Shader key word , stay Shader Pass through “ #pragma shader_feature” or “ #pragma multi_compile” Command can open or close a part Shader Code .
Shader The keyword format is :property name_enum name, The attribute name +“ Draw the line down ”+ Enumeration name , All English must be capitalized , And support at most 9 Key words . for instance :[KeywordEnum(None, Add, Multiply) ] _Overlay("Overlay mode" , Float) = 0
In brackets None, Add, Multiply Are the three enumeration names defined , Intermediate use “ , ” Symbols separate . The default value is 0, Indicates the default use of None. The three options correspond to Shader The key words are :_OVERLAY_NONE、_OVERLAY_ADD and _OVERLAY_MULTIPLY.
1.4. Define keywords in the compilation instructions
Defined ToggleDrawer perhaps KeywordEnumDrawer after , If you want to use it normally , You also need to declare in the compilation directive Shader key word . for example , As defined above None、Add、Multiply Keyword enumeration , The code in the compile instruction is as follows :#pragma shader_frature _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
Different keywords need to be separated by spaces .
in addition , You can also use another compilation instruction to define keywords , The code is as follows :#pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY
Although it seems to pass through a Shader File implements different situations , however Unity It will automatically compile different versions of Shader file , These are different versions Shader The document is called Shader variant (Variants), The above compilation instructions contain three Shader variant .
Suppose you add another instruction :#pragma shader_feature _INVERT_ON
This directive contains Toggle There are two cases of closing and opening , therefore Unity It will eventually compile 2 * 3 = 6 individual Shader variant . So a lot of shader feature or multi compile At the time of instruction , There will be a lot of Shader Variation file .
The differences between the two different compilation instructions are as follows :
- shader_feature: Only variations will be generated for the keywords used by the material , Keywords that are not used will not generate variants , Therefore, it is impossible to switch effects through scripts at runtime .
- multi_compile: Variants will be generated for all keywords , Therefore, you can switch the effects through scripts at run time .
stay Shader You can view this... In the attribute setting panel of the file Shader Number of variants generated , Pictured
Through the open "Skip unused shader_Features" Option to view only the number of variants that use keywords , You can also turn off "Skip unused shader_Features" Option to view the number of variants of all keywords . To determine what specific keywords are , You can click "Show" see .
1.5.PowerSlider
Index slider (PowerSlider) The attributes of range type values will be displayed as sliding bars corresponding to nonlinearity . The values on the slider no longer correspond to the linear relationship , But in an exponential way . for instance :[PowerSlider(3.0) ] _Brightness("Brightness" , Range(0.01, 1)) = 0
This is one with 3 Is the sliding bar of exponential correspondence , among , The values in brackets are exponents , The final effect on the material property panel is shown in the figure :
It is not difficult to find , When the value is 0.5 When , The slider is not in the middle of the slider , This is the nonlinear correspondence .
Let's take a look at y=x3 The function curve of , As shown in the figure , Variables in functions x Is the position of the sliding block ,y Is the value of the attribute .
From the curve 1 You can see from the number , When the slider is in 0.5 In position , The value of the attribute is only 0.1 A little more , From the curve 2 You can see from the number , When the attribute value is 0.5 Some time , The slider has already arrived 0.8 Left and right , This is the corresponding relationship of exponential slider .
1.6.IntRange
It also displays the value in the form of a slider on the material property panel , But the number is no longer float type , Only integer values .
for example :[IntRange] _Alpha("Alpha" , Range(0, 255)) = 100
Can use the interval on the slider [0,255] Integer values between .
边栏推荐
- 69 cesium code datasource loading geojson
- 【KV260】利用XADC生成芯片温度曲线图
- [ManageEngine Zhuohao] the role of LAN monitoring
- kubeadm搭建kubenetes 集群(个人学习版)
- Differences between in and exists in MySQL
- ABP 学习解决方案中的项目以及依赖关系
- Kubedm builds kubenetes cluster (Personal Learning version)
- 连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
- FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview
- HDU - 1501 Zipper(记忆化深搜)
猜你喜欢
随机推荐
Freeswitch dial the extension number
HDU - 1501 zipper (memory deep search)
Forkjoin and stream flow test
To sort out the anomaly detection methods, just read this article!
What is a port scanning tool? What is the use of port scanning tools
C语言课设工资管理系统(大作业)
[ManageEngine Zhuohao] the role of LAN monitoring
Save data in browser to local file
C# ManualResetEvent 类的理解
[network security tool] what is the use of USB control software
C语言课设学生考勤系统(大作业)
子类调用父类的同名方法和属性
Application of IT service management (ITSM) in Higher Education
记磁盘扇区损坏导致的Mysql故障排查
68 cesium code datasource loading czml
three. JS summary
SystemVerilog learning-08-random constraints and thread control
异常检测方法梳理,看这篇就够了!
Factorial divisor (unique decomposition theorem)
【企业数据安全】升级备份策略 保障企业数据安全





![[file system] how to run squashfs on UBI](/img/d7/a4769420c510c47f3c2a615b514a8e.png)



