Shader "Unlit/PaintBrush" { Properties { _MainTex ("Texture", 2D) = "white" {} _BrushTex("Brush Texture",2D)= "white" {} _Color("Color",Color)=(1,1,1,1) _UV("UV",Vector)=(0,0,0,0) _Size("Size",Range(1,1000))=1 _Cutoff("Cutoff",Range(1,1000))=1 _SizeY("SizeY",Range(1,1000))=1 } SubShader { Tags { "RenderType"="Transparent" } LOD 100 ZTest Always Cull Off ZWrite Off Fog{ Mode Off } Blend SrcAlpha OneMinusSrcAlpha //Blend One DstColor 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; sampler2D _BrushTex; fixed4 _UV; float _Size; float _SizeY; fixed4 _Color; float _Cutoff; 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 { // sample the texture float size = _Size; float2 uv = i.uv + (0.5f/size); uv = uv - _UV.xy; uv *= size; float sizeY = _SizeY; uv.y=uv.y*sizeY/size; fixed4 col = tex2D(_BrushTex,uv); col.rgb = 1; col *= _Color; clip(col.a - _Cutoff); return col; } ENDCG } } } 请帮我实现线的头尾效果
时间: 2024-03-14 12:47:19 浏览: 77
UnityShader实现百叶窗效果
要实现线的头尾效果,可以引入一个贴图来代表线头和线尾的样式,并在代码中根据线的方向和长度将其正确地贴在线的两端。以下是一个简单的示例:
在 Properties 中添加两个贴图属性:
```
_HeadTex("Head Texture", 2D) = "white" {}
_TailTex("Tail Texture", 2D) = "white" {}
```
在 SubShader 中添加以下代码:
```
struct v2f {
...
float4 headTailUV : TEXCOORD1;
};
v2f vert(appdata v) {
v2f o;
...
float4 headTailUV = float4(0, 0, 1, 1);
float lineLength = length(v.vertex);
if (lineLength > 0) {
float2 lineDir = normalize(v.vertex.xy);
float2 headTailSize = float2(_HeadTex_ST.z - _HeadTex_ST.x, _HeadTex_ST.w - _HeadTex_ST.y) * _Size;
float2 headUV = lineDir * headTailSize + float2(0.5, 0.5);
float2 tailUV = lineDir * headTailSize + float2(0.5, 0.5);
headTailUV = float4(headUV, tailUV);
}
o.headTailUV = headTailUV;
...
}
fixed4 frag(v2f i) : SV_Target {
...
float4 headTailUV = i.headTailUV;
float2 uv = i.uv + (0.5f / size);
uv = uv - _UV.xy;
uv *= size;
float2 headUV = headTailUV.xy;
float2 tailUV = headTailUV.zw;
float4 headTexel = tex2D(_HeadTex, headUV);
float4 tailTexel = tex2D(_TailTex, tailUV);
float4 brushTexel = tex2D(_BrushTex, uv);
float4 col = brushTexel * _Color;
col *= step(0.5, col.a);
col.a = 1;
if (uv.x < headUV.x) {
col *= headTexel;
}
if (uv.x > tailUV.x) {
col *= tailTexel;
}
clip(col.a - _Cutoff);
return col;
}
```
这里的实现假设线的起点在原点,线的方向在 xy 平面上。如果线的起点和方向有变化,需要根据实际情况进行调整。
阅读全文