-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridFloorShader.shader
135 lines (111 loc) · 4.06 KB
/
GridFloorShader.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Shader "Ogxd/Grid"
{
Properties
{
_MainColor("Main Color", Color) = (0.5, 1.0, 1.0)
_SecondaryColor("Secondary Color", Color) = (0.0, 0.0, 0.0)
_BackgroundColor("Background Color", Color) = (0.0, 0.0, 0.0, 0.0)
_MaskTexture("Texture", 2D) = "white" {}
[Header(Grid)]
_Scale("Scale", Float) = 1.0
_GraduationScale("Graduation Scale", Float) = 1.0
_Thickness("Lines Thickness", Range(0.0001, 0.01)) = 0.005
_SecondaryFadeInSpeed("Secondary Fade In Speed", Range(0.1, 4)) = 0.5
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
ZWrite On // We need to write in depth to avoid tearing issues
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _GridTexture;
float4 _GridTexture_ST;
sampler2D _MaskTexture;
float4 _MaskTexture_ST;
float _Scale;
float _GraduationScale;
float _Thickness;
float _SecondaryFadeInSpeed;
fixed4 _MainColor;
fixed4 _SecondaryColor;
fixed4 _BackgroundColor;
v2f vert(appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
// Remap UVs from [0:1] to [-0.5:0.5] to make scaling effect start from the center
o.uv = v.uv - 0.5f;
// Scale the whole thing if necessary
o.uv *= _GraduationScale;
// UVs for mask texture
o.uv1 = TRANSFORM_TEX(v.uv1, _MaskTexture);
return o;
}
// Remap value from a range to another
float remap(float value, float from1, float to1, float from2, float to2) {
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = _MainColor;
fixed4 maskCol = tex2D(_MaskTexture, i.uv1);
// With the ceil value of the log10 of the scale, we obtain the closest measure unit above (eg : 165 -> 3, 0.146 -> 0, 0.001 -> -3)
// Then, we do 10^(this value) to get the actual value of that unit in meters
// Finally, we divide the scale by this unit in meters to have our log mapped scale
// This way, we are sure our logMappedScale is between 0.1 and 1
float logMappedScale = _Scale / pow(10, ceil(log10(_Scale)));
// We want a zoom in effect when the users is scaling up his model.
// Scaling up in 3D space means a lower scale value, so we have to use the invert the scale for our zoom effect.
float localScale = 1 / logMappedScale;
// Fade is used to make secondary grid appear slowly instead of popping
// Here we remap the value from logMappedScale from [0.1:1] to [0:1]
// The power can be used to make the fade effect faster or slower
float fade = pow(1 - remap(logMappedScale, 0.1, 1, 0.00001, 0.99999), _SecondaryFadeInSpeed);
float2 pos;
pos.x = floor(frac((i.uv.x - 0.5 * _Thickness) * localScale) + _Thickness * localScale);
pos.y = floor(frac((i.uv.y - 0.5 * _Thickness) * localScale) + _Thickness * localScale);
if (pos.x == 1 || pos.y == 1) {
col.a = max((1 - fade), fade);
}
else {
pos.x = floor(frac((i.uv.x - 0.5 * _Thickness) * 10.0 * localScale) + _Thickness * 10.0 * localScale);
pos.y = floor(frac((i.uv.y - 0.5 * _Thickness) * 10.0 * localScale) + _Thickness * 10.0 * localScale);
if (pos.x == 1 || pos.y == 1) {
col = _SecondaryColor;
col.a = (1 - fade);
}
else {
col = _BackgroundColor;
}
}
// Apply mask multiplying by its alpha
col.a *= maskCol.a;
return col;
}
ENDCG
}
}
}