-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLeadChainMesh.shader
392 lines (332 loc) · 9.65 KB
/
LeadChainMesh.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//Copyright(c) <2019> <JackyGun twitter@konchannyan>
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Shader "JackyGun/LeadShader/chainMesh"
{
Properties
{
_Tess("Tess",Range(1, 32)) = 4
_TargetId("TargetId",Float) = 1.5
_DomainId("DomainId",Float) = 1.0
_ChainLen("ChainLen",Float) = 1.0
_MaxDis("MaxDis",Float) = 5.0
_CanDebug("CanDebug",Float) = 0.0
_Color0("Color0", Color) = (0, 0, 0, 1)
_Color1("Color1", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags{ "Queue" = "AlphaTest+2" "RenderType" = "Opaque" }
Pass
{
CGPROGRAM
#pragma target 5.0
#pragma vertex mainVS
#pragma hull mainHS
#pragma domain mainDS
#pragma geometry mainGS
#pragma fragment mainFS
#pragma multi_compile_instancing
#include "UnityCG.cginc"
int _Tess;
float _TargetId;
float _DomainId;
float _ChainLen;
float _MaxDis;
float _CanDebug;
float4 _Color0;
float4 _Color1;
// Structure
struct VS_IN
{
float4 pos : POSITION;
uint instanceID : SV_InstanceID;
};
struct VS_OUT
{
float4 pos : POSITION;
uint instanceID : INSTANCE_ID;
int targetID : TARGET_ID;
};
struct CONSTANT_HS_OUT
{
float Edges[4] : SV_TessFactor;
float Inside[2] : SV_InsideTessFactor;
};
struct HS_OUT
{
uint instanceID : INSTANCE_ID;
int targetID : TARGET_ID;
};
struct DS_OUT
{
uint pid : PID; // GeometryShaderに実行用の一意連続なIDを発行する
uint instanceID : INSTANCE_ID;
int targetID : TARGET_ID;
};
struct GS_OUT
{
int state : STATE;
float4 vertex : SV_POSITION;
float4 color : COLOR0;
};
// Main
VS_OUT mainVS(VS_IN In)
{
#if defined(UNITY_INSTANCING_ENABLED)
VS_IN dm;
uint iid = In.instanceID;
uint i;
int tid = -1;
for (i = 1; i < 8; i++)
{
if (i == iid) continue;
dm.instanceID = i;
UNITY_SETUP_INSTANCE_ID(dm);
float sX = sqrt(pow(unity_ObjectToWorld[0].x, 2) + pow(unity_ObjectToWorld[0].y, 2) + pow(unity_ObjectToWorld[0].z, 2));
if (distance(sX, _TargetId) < _DomainId) {
tid = i;
break;
}
}
VS_OUT Out;
Out.pos = In.pos;
Out.instanceID = In.instanceID;
Out.targetID = tid;
return Out;
#else
VS_OUT Out;
Out.pos = In.pos;
Out.instanceID = In.instanceID;
Out.targetID = -2;
return Out;
#endif
}
CONSTANT_HS_OUT mainCHS()
{
CONSTANT_HS_OUT Out;
int t = _Tess + 1;
Out.Edges[0] = t;
Out.Edges[1] = t;
Out.Edges[2] = t;
Out.Edges[3] = t;
Out.Inside[0] = t;
Out.Inside[1] = t;
return Out;
}
[domain("quad")]
[partitioning("pow2")]
[outputtopology("point")]
[outputcontrolpoints(4)]
[patchconstantfunc("mainCHS")]
HS_OUT mainHS(InputPatch<VS_OUT, 4> In, uint i : SV_OutputControlPointID)
{
HS_OUT Out;
Out.instanceID = In[0].instanceID;
Out.targetID = In[0].targetID;
return Out;
}
[domain("quad")]
DS_OUT mainDS(CONSTANT_HS_OUT In, const OutputPatch<HS_OUT, 4> patch, float2 uv : SV_DomainLocation)
{
DS_OUT Out;
Out.pid = (uint)(uv.x * _Tess) + ((uint)(uv.y * _Tess) * _Tess);
Out.instanceID = patch[0].instanceID;
Out.targetID = patch[0].targetID;
return Out;
}
[maxvertexcount(36)]
void mainGS(point DS_OUT input[1], inout TriangleStream<GS_OUT> outStream)
{
GS_OUT o;
uint pid = input[0].pid;
uint iid = input[0].instanceID;
int tid = input[0].targetID;
if (tid < 0) {
if (pid == 0 && _CanDebug > 0) {
o.state = tid;
o.color = 0;
o.vertex = UnityObjectToClipPos(float4(-0.01, -0.01, 0, 1));
outStream.Append(o);
o.vertex = UnityObjectToClipPos(float4(-0.01, +0.01, 0, 1));
outStream.Append(o);
o.vertex = UnityObjectToClipPos(float4(+0.01, -0.01, 0, 1));
outStream.Append(o);
outStream.RestartStrip();
o.vertex = UnityObjectToClipPos(float4(-0.01, +0.01, 0, 1));
outStream.Append(o);
o.vertex = UnityObjectToClipPos(float4(+0.01, +0.01, 0, 1));
outStream.Append(o);
o.vertex = UnityObjectToClipPos(float4(+0.01, -0.01, 0, 1));
outStream.Append(o);
outStream.RestartStrip();
}
return;
}
DS_OUT dm;
dm.instanceID = tid;
UNITY_SETUP_INSTANCE_ID(dm);
float4 ppos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
dm.instanceID = iid;
UNITY_SETUP_INSTANCE_ID(dm);
float4 mpos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
if (distance(ppos, mpos) > _MaxDis)
return;
float len = _ChainLen;
// float y0 = mpos.y;
// float y1 = ppos.y;
float y0 = min(mpos.y, ppos.y);
float y1 = max(mpos.y, ppos.y);
float w = distance(mpos.xz, ppos.xz);
float b = y0 + y1;
float c = 0.25f * (b * b + w * w - len * len);
float d = b * b - 4 * 1 * c;
float y = y0;
// float y = min(y0, y1);
if (d >= 0) {
y = 0.25f * ( - b - sqrt(d));
}
//float h0 = (y0 - y);
//float h1 = (y1 - y);
//float xa0 = -sqrt(h0);
//float xa1 = +sqrt(h1);
y = lerp(y1, y, 1);
float h = (y1 - y);
float w2 = w / 2;
w2 = w2 * w2;
w2 = max(w2, 0.000001f);
float aa = h / w2;
float t_size = (_Tess + 1) * _Tess + 1;
float e0 = (pid + 0) / t_size;
float e1 = (pid + 1) / t_size;
float x0 = e0 * 2 - 1;
float x1 = e1 * 2 - 1;
x0 *= w / 2;
x1 *= w / 2;
float ye0 = aa * x0 * x0 - h;
float ye1 = aa * x1 * x1 - h;
float3 fpos = lerp(mpos.xyz, ppos.xyz, e0) + float3(0, ye0, 0);
float3 tpos = lerp(mpos.xyz, ppos.xyz, e1) + float3(0, ye1, 0);
//float xi0 = lerp(xa0,xa1,e0);
//float xi1 = lerp(xa0,xa1,e1);
//float ye0 = xi0 * xi0 + y;
//float ye1 = xi1 * xi1 + y;
//mpos.y = 0;
//ppos.y = 0;
//float3 fpos = lerp(mpos.xyz, ppos.xyz, e0) + float3(0, ye0, 0);
//float3 tpos = lerp(mpos.xyz, ppos.xyz, e1) + float3(0, ye1, 0);
float3 dd = tpos - fpos;
fpos += dd * 0.25f;
tpos -= dd * 0.25f;
float3 cdir = 0.005;
float4 wpos0 = mul(UNITY_MATRIX_VP, float4(fpos + float3(-cdir.x, +cdir.y, +cdir.z), 1));
float4 wpos1 = mul(UNITY_MATRIX_VP, float4(fpos + float3(+cdir.x, +cdir.y, +cdir.z), 1));
float4 wpos2 = mul(UNITY_MATRIX_VP, float4(fpos + float3(-cdir.x, +cdir.y, -cdir.z), 1));
float4 wpos3 = mul(UNITY_MATRIX_VP, float4(fpos + float3(+cdir.x, +cdir.y, -cdir.z), 1));
float4 wpos4 = mul(UNITY_MATRIX_VP, float4(fpos + float3(-cdir.x, -cdir.y, +cdir.z), 1));
float4 wpos5 = mul(UNITY_MATRIX_VP, float4(fpos + float3(+cdir.x, -cdir.y, +cdir.z), 1));
float4 wpos6 = mul(UNITY_MATRIX_VP, float4(fpos + float3(-cdir.x, -cdir.y, -cdir.z), 1));
float4 wpos7 = mul(UNITY_MATRIX_VP, float4(fpos + float3(+cdir.x, -cdir.y, -cdir.z), 1));
o.state = tid;
o.color = pid % 2 ? _Color0 : _Color1;
o.vertex = wpos0;
outStream.Append(o);
o.vertex = wpos1;
outStream.Append(o);
o.vertex = wpos2;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos2;
outStream.Append(o);
o.vertex = wpos1;
outStream.Append(o);
o.vertex = wpos3;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos4;
outStream.Append(o);
o.vertex = wpos6;
outStream.Append(o);
o.vertex = wpos5;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos6;
outStream.Append(o);
o.vertex = wpos7;
outStream.Append(o);
o.vertex = wpos5;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos0;
outStream.Append(o);
o.vertex = wpos2;
outStream.Append(o);
o.vertex = wpos4;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos4;
outStream.Append(o);
o.vertex = wpos2;
outStream.Append(o);
o.vertex = wpos6;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos3;
outStream.Append(o);
o.vertex = wpos1;
outStream.Append(o);
o.vertex = wpos7;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos7;
outStream.Append(o);
o.vertex = wpos1;
outStream.Append(o);
o.vertex = wpos5;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos1;
outStream.Append(o);
o.vertex = wpos0;
outStream.Append(o);
o.vertex = wpos5;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos5;
outStream.Append(o);
o.vertex = wpos0;
outStream.Append(o);
o.vertex = wpos4;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos2;
outStream.Append(o);
o.vertex = wpos3;
outStream.Append(o);
o.vertex = wpos6;
outStream.Append(o);
outStream.RestartStrip();
o.vertex = wpos6;
outStream.Append(o);
o.vertex = wpos3;
outStream.Append(o);
o.vertex = wpos7;
outStream.Append(o);
outStream.RestartStrip();
}
float4 mainFS(GS_OUT i) : SV_Target
{
if (i.state < 0){
// black : error
// white : not hit
return float4((i.state + 2).rrr, 1);
}
return i.color;
}
ENDCG
}
}
}