unity发光shader
时间: 2023-08-26 09:04:53 浏览: 145
要在Unity中实现发光(glow)效果,需要使用Shader。以下是一个简单的发光Shader示例:
```C#
Shader "Custom/GlowShader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_GlowColor ("GlowColor", Color) = (1,1,1,1)
_GlowIntensity ("GlowIntensity", Range(0, 10.0)) = 1.0
}
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;
float4 _MainTex_ST;
float4 _Color;
float4 _GlowColor;
float _GlowIntensity;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.uv) * _Color;
float4 glow = tex2D(_MainTex, i.uv);
glow *= _GlowColor;
glow *= _GlowIntensity;
col += glow;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
这个Shader有几个属性:
- `_MainTex`:用于指定贴图
- `_Color`:用于指定基础颜色
- `_GlowColor`:用于指定发光颜色
- `_GlowIntensity`:用于指定发光强度
可以根据需要进行调整。在使用时,将这个Shader应用到需要发光的物体上即可。