当前位置:网站首页>[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 .
边栏推荐
- Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
- Solve the problem of garbled files uploaded by Kirin v10
- [network security tool] what is the use of USB control software
- HCM Beginner (II) - information type
- Projects and dependencies in ABP learning solutions
- IT服务管理(ITSM)在高等教育领域的应用
- 阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
- B-树系列
- 高阶-二叉搜索树详解
- 让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
猜你喜欢
随机推荐
10 golang operator
JMM details
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
sql中TCL语句(事务控制语句)
FPGA - clocking -02- clock wiring resources of internal structure of 7 Series FPGA
自开发软件NoiseCreater1.1版本免费试用
HDU - 1501 Zipper(记忆化深搜)
高阶-二叉搜索树详解
[ManageEngine Zhuohao] use unified terminal management to help "Eurex group" digital transformation
讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
webapck打包原理--启动过程分析
局域网监控软件有哪些功能
C# ManualResetEvent 类的理解
Uniapp tree level selector
SystemVerilog learning-07-class inheritance and package use
Distributed lock implementation
make: g++:命令未找到
Save data in browser to local file
Top 10 Free 3D modeling software for beginners in 2022
Dongle data collection