写一个 unity shader 实例代码
时间: 2023-10-24 19:14:17 浏览: 181
以下是一个简单的Unity Shader示例代码,它将一个场景中的物体渲染成红色:
```
Shader "Custom/RedShader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target {
return fixed4(1, 0, 0, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
```
这个Shader包含一个纹理属性 `_MainTex`,在渲染时使用了这个属性。在SubShader中,我们定义了一个Pass,并使用了CGPROGRAM指令来定义着色器的顶点和片段函数。在顶点函数中,我们将顶点的位置从对象坐标系转换到裁剪空间坐标系,并计算了纹理坐标uv。在片段函数中,我们返回了红色的颜色。FallBack指令表示如果当前渲染器不支持我们的着色器,将会回退到Diffuse着色器。
阅读全文
相关推荐















