UNITY SHADER Properties
时间: 2023-10-20 16:34:24 浏览: 169
Unity Shader中的Properties块是用来定义shader的属性的。这个块为程序员提供了一种方便的方式来创建与代码相符的控件,而不需要程序员关心如何显示。在Properties块中,需要按照规定的格式来编写属性代码。这些属性可以在Unity中的Inspector面板上显示出相关的控件,从而实现对shader行为的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Unity shader教程-第二课:Shader的框架和Properties详细介绍](https://blog.csdn.net/TinyHum/article/details/43197165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关问题
unity shader Properties语义
### Unity Shader 中 Properties 的含义和用法
在 Unity 中,`Properties` 是着色器程序中的一个重要部分,用于定义可以在材质面板中调整的各种参数。这些属性允许开发者通过编辑器界面轻松修改着色器的行为而不必重新编译代码。
#### 属性的语法结构
一个典型的 `Properties` 块看起来像这样:
```csharp
Shader "Custom/MyShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color Tint", Color) = (1,1,1,1)
_Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
}
SubShader
{
// 子着色器内容...
}
FallBack "Diffuse"
}
```
每条属性声明遵循特定格式:`_PropertyName ("Display Name", Type) = DefaultValue {}`[^1]。
- `_PropertyName`: 实际使用的变量名,在 HLSL 或 CG 编程时调用。
- `"Display Name"`: 显示名称,即在 Inspector 面板上显示的文字说明。
- `Type`: 数据类型,决定了该属性可以接受什么类型的输入值。
- `DefaultValue`: 默认初始值设置。
#### 支持的数据类型
支持多种数据类型来满足不同需求,主要包括但不限于以下几种[^2]:
- **2D Texture**: 表示二维贴图资源,通常用来存储颜色信息或其他图像数据。
```csharp
_MainTex ("Base (RGB)", 2D) = "white" {}
```
- **Color**: RGB(A) 彩色值,可用于调节物体的颜色或光照效果。
```csharp
_TintColor ("Tint Color", Color) = (1,1,1,1)
```
- **Range(min,max)**: 数字范围滑动条控件,适用于控制透明度阈值、强度因子等连续变化的数值。
```csharp
_MetallicGlossMap ("Smoothness", Range(0,1)) = 0.5
```
- **Int / Float**: 整数或浮点数单个值,适合开关标志位或是简单的比例系数。
```csharp
_IsEnabled ("Enable Effect?", Int) = 1
```
- **Vector**: 四维向量,常被用来表示位置坐标、方向矢量或者是四个独立分量的一组相关联的小数值。
```csharp
_Direction ("Light Direction", Vector) = (0.5,0.866,-0.707,0)
```
当存在重复命名的属性时,Unity 只会采用第一次遇到的那个,并忽略之后相同名字的其他定义[^4]。
unity shader入门精要_【Unity Shader】在URP里写Shader(二):从一个Unlit Shader开始
在Unity中,Shader是用来渲染场景的关键部分。在URP中,Shader的编写方式与传统的Shader编写方式略有不同,但其核心思想仍然是相同的。
在本文中,我们将从一个简单的Unlit Shader开始,介绍在URP中编写Shader的基本步骤。
1. 创建Shader
首先,我们需要创建一个新的Shader。在Project视图中右键点击Assets文件夹,选择Create > Shader > Unlit Shader。
2. 编写Shader
打开新创建的Shader文件,我们可以看到如下代码:
```
Shader "Unlit/MyUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
这个Shader使用了Unity默认的Unlit Shader模板,并添加了一个_MainTex属性,该属性用于接收贴图,并在渲染过程中使用。
3. 添加SubShader
在URP中,我们需要为Shader添加一个SubShader。在SubShader中,我们可以定义一系列Pass,每个Pass都是对场景中不同物体的渲染。
修改代码如下:
```
Shader "Unlit/MyUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag () : SV_Target
{
fixed4 col = fixed4(1, 1, 1, 1);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
在这个代码中,我们添加了一个新的SubShader,并为其添加了一个Pass。这个Pass是一个简单的颜色填充Pass,用于在没有贴图的情况下渲染物体。
4. 测试Shader
最后,我们需要将Shader应用到一个物体上并测试它的效果。在Scene视图中创建一个新的Cube,并将新创建的Shader应用到该Cube上。
现在,你应该能够看到在没有贴图的情况下,该Cube被渲染为白色。如果你将贴图赋给_MainTex属性,你应该能够看到该Cube被渲染为该贴图。
这就是从一个简单的Unlit Shader开始,在URP中编写Shader的基本步骤。通过深入学习Shader编写的过程,你将能够创建更高级、更复杂的Shader,并为你的游戏带来更加出色的视觉效果。
阅读全文
相关推荐
















