写一个unity虚线shader
时间: 2023-10-24 19:07:35 浏览: 131
以下是一个简单的Unity虚线shader示例:
```
Shader "Custom/DashedLine" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_DashSize ("Dash Size", Range(0.01, 0.1)) = 0.05
_GapSize ("Gap Size", Range(0.01, 0.1)) = 0.05
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
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;
};
float _DashSize;
float _GapSize;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _Color;
fixed4 frag (v2f i) : SV_Target {
float2 repeat = float2(1.0, 1.0);
float2 pos = i.uv * repeat;
float2 d = fmod(pos / _DashSize, 2.0);
float2 g = fmod(pos / _GapSize, 2.0);
float2 col = fmod((pos - _GapSize / 2.0) / _DashSize, 2.0);
col = step(1.0, col) * step(d.x, 1.0 - d.y) * step(g.x, 1.0 - g.y);
return tex2D(_MainTex, i.uv) * _Color * col.xxxx;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
该shader可以在材质上应用,使用一张白色纹理可以实现虚线效果。其中Dash Size和Gap Size属性控制虚线和间隔的大小,Color属性控制虚线的颜色。
阅读全文