OSDN Git Service

new project
[mindgames/Mindgames_main.git] / Mindgames / Library / PackageCache / com.unity.visualeffectgraph@6.9.0-preview / Editor / Resources / GradientDashedBorder.shader
1 Shader "Hidden/VFX/GradientDashedBorder"
2 {
3     Properties
4     {
5         _Border("Border",float) = 1
6         _Radius("Radius",float) = 1
7         _PixelScale("PixelScale",float) = 1
8         _Size("Size",Vector) = (100,100,0,0)
9         _ColorStart("ColorStart",Color) = (1,1,0,1)
10         _ColorEnd("ColorEnd", Color) = (0,1,1,1)
11         _ColorMiddle("ColorMiddle", Color) = (0,1,1,1)
12     }
13     SubShader
14     {
15         Tags { "RenderType"="Transparent" "Queue"="Transparent"}
16         LOD 100
17         Cull Off
18         ZWrite Off
19         ZTest Always
20         Blend SrcAlpha OneMinusSrcAlpha
21
22         Pass
23         {
24             CGPROGRAM
25             #pragma vertex vert
26             #pragma fragment frag
27
28             #include "UnityCG.cginc"
29
30             struct appdata
31             {
32                 float4 vertex : POSITION;
33                 float2 uv : TEXCOORD0;
34                 float2 uv2 : TEXCOORD1;
35             };
36
37             struct v2f
38             {
39                 float2 uv : TEXCOORD0;
40                 float4 vertex : SV_POSITION;
41                 float4 pos : TEXCOORD2;
42                 float2 clipUV : TEXCOORD1;
43                 float height : TEXCOORD3;
44                 float distance : TEXCOORD4;
45             };
46
47             float _Border;
48             float _Radius;
49             float _PixelScale;
50             float2 _Size;
51             fixed4 _ColorStart;
52             fixed4 _ColorEnd;
53             fixed4 _ColorMiddle;
54
55             uniform float4x4 unity_GUIClipTextureMatrix;
56             sampler2D _GUIClipTexture;
57
58             v2f vert (appdata v)
59             {
60                 v2f o;
61
62                 float2 size = _Size - float2(_Radius,_Radius);
63
64                 float margingScale = 2 + (_Border/_Radius /_PixelScale);
65
66                 o.pos = float4(v.vertex.xy * size + v.uv* margingScale * v.vertex.xy* _Radius, 0, 0);
67                 o.height = (v.vertex.y + 1)* 0.5;
68                 o.vertex = UnityObjectToClipPos(o.pos);
69                 o.uv = v.uv*margingScale;
70                 float3 eyePos = UnityObjectToViewPos(o.pos );
71                 o.clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
72
73
74                 float2 dist = v.uv2 * _Size;
75                 o.distance = dist.y + dist.x;
76                 return o;
77             }
78
79             fixed4 frag(v2f i) : SV_Target
80             {
81                 if (fmod(i.distance + 10,20) > 10)
82                     discard;
83
84                 float pixelScale = 1.0f/abs(ddx(i.pos.x));
85
86                 float realRadius = (_Radius - _Border * 0.5 - 0.5); // radius at the center of the line. -0.5 to keep space for AA
87                 float2 uvCenter = i.uv * _Radius / realRadius; // uv expressed in realRadius instead of _Radius
88                 //float uvDist = 1-abs(1-length(uvCenter)); //
89                 float uvDist = length(uvCenter); // distance to center expressed in realRadius
90                 float uvBorder = _Border*0.5f / realRadius; // half border width expressed in realdRadius
91                 float borderDist = abs((uvDist-1) / uvBorder); // distance from center of line expressed in half border
92                 /*
93                 if( borderDist > 1) // possible optim : is the early discard is more profitable than the branch ?
94                     discard;
95                 */
96                 float clipA = tex2D(_GUIClipTexture, i.clipUV).a;
97                 float pixelBorderSize = _Border*0.5 * pixelScale; // half border expressed on transformed pixel
98                 borderDist = pixelBorderSize * (1 - borderDist) + 0.5; // signed distance from edge of line in transformed pixel
99
100                 //float height = 0.5 + i.pos.y / i.height * 0.5; // height expressed in size.y
101
102                 fixed4 color;
103                 if (i.height > 0.5f)
104                     color = lerp(_ColorMiddle,_ColorEnd , (i.height - 0.5f) * 2);
105                 else
106                     color = lerp(_ColorStart, _ColorMiddle, i.height * 2);
107                 
108                 return float4(color.rgb,color.a*saturate(borderDist)*clipA);
109             }
110             ENDCG
111         }
112     }
113 }