-
Notifications
You must be signed in to change notification settings - Fork 5
/
PopCommon.cginc
586 lines (464 loc) · 14.2 KB
/
PopCommon.cginc
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// nicer matrix space names/funcs that are missing
#define unity_WorldToClip UNITY_MATRIX_VP
float3 UnityObjectToWorldPos(float3 LocalPos)
{
return mul( unity_ObjectToWorld, float4( LocalPos, 1 ) ).xyz;
}
float3 GetCameraWorldForward()
{
// float4x4 _CameraToWorld; // <--- now unity_CameraToWorld
float4 Forward = float4(0,0,1,0);
Forward = mul( unity_CameraToWorld, Forward );
return Forward.xyz;
}
#define hypotenuse(o,a) sqrt( (a*a)+(o*o) )
#define lengthsq(x) ( dot(x,x) )
float max3(float a,float b,float c)
{
return max( a, max( b,c ) );
}
float min3(float a,float b,float c)
{
return min( a, min( b,c ) );
}
float min4(float a,float b,float c,float d)
{
return min( a, min( b, min(c,d) ) );
}
float Clamp01(float x)
{
return clamp( x, 0.0, 1.0 );
}
float Range01(float Min,float Max,float Time)
{
return Clamp01( (Time-Min) / (Max-Min) );
}
float Range(float Min,float Max,float Time)
{
return (Time-Min) / (Max-Min);
}
float2 Range(float2 Min,float2 Max,float2 Time)
{
return float2( Range(Min.x,Max.x,Time.x), Range(Min.y,Max.y,Time.y) );
}
float3 Range(float3 Min,float3 Max,float3 Time)
{
return float3( Range(Min.x,Max.x,Time.x), Range(Min.y,Max.y,Time.y), Range(Min.z,Max.z,Time.z) );
}
bool Inside(float Min,float Max,float Value)
{
return (Value>=Min) && (Value<=Max);
}
bool Inside(float2 Min,float2 Max,float2 Value)
{
return Inside(Min.x,Max.x,Value.x) && Inside(Min.y,Max.y,Value.y);
}
bool Inside01(float a)
{
return Inside(0,1,a);
}
bool Inside01(float2 ab)
{
return Inside01(ab.x) && Inside01(ab.y);
}
// 0 = red, 1=green
float3 NormalToRedGreen(float Value)
{
Value = Clamp01( Value );
if ( Value < 0.5 )
{
float Yellow = Range( 0.0, 0.5, Value );
return float3( 1.0, Yellow, 0.0 );
}
float Yellow = Range( 1.0, 0.5, Value );
return float3( Yellow, 1.0, 0.0 );
}
float3 RgbToHsl(float3 rgb)
{
float r = rgb.x;
float g = rgb.y;
float b = rgb.z;
float Max = max3( r, g, b );
float Min = min3( r, g, b );
float h = 0;
float s = 0;
float l = ( Max + Min ) / 2.f;
if ( Max == Min )
{
// achromatic/grey
h = s = 0;
}
else
{
float d = Max - Min;
s = l > 0.5f ? d / (2 - Max - Min) : d / (Max + Min);
if ( Max == r )
{
h = (g - b) / d + (g < b ? 6 : 0);
}
else if ( Max == g )
{
h = (b - r) / d + 2;
}
else //if ( Max == b )
{
h = (r - g) / d + 4;
}
h /= 6;
}
return float3( h, s, l );
}
float hue2rgb(float p,float q,float t)
{
if(t < 0) t += 1.f;
if(t > 1) t -= 1.f;
if(t < 1.f/6.f) return p + (q - p) * 6.f * t;
if(t < 1.f/2.f) return q;
if(t < 2.f/3.f) return p + (q - p) * (2.f/3.f - t) * 6.f;
return p;
}
float3 HslToRgb(float3 Hsl)
{
/*
// https://github.com/hughsk/glsl-hsv2rgb/blob/master/index.glsl
// better version!
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(fract(Hsl.xxx + K.xyz) * 6.0 - K.www);
return Hsl.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), Hsl.y);
*/
float h = Hsl.x;
float s = Hsl.y;
float l = Hsl.z;
if(s == 0){
return float3( l, l, l );
}else{
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
float p = 2.f * l - q;
float3 Rgb;
Rgb.x = hue2rgb(p, q, h + 1.f/3.f);
Rgb.y = hue2rgb(p, q, h);
Rgb.z = hue2rgb(p, q, h - 1.f/3.f);
return Rgb;
}
}
float4 LerpRgba(float4 a,float4 b,float Time)
{
float3 hsla = RgbToHsl( a.xyz );
float3 hslb = RgbToHsl( b.xyz );
float3 hsl_lerp = lerp( hsla, hslb, Time );
float3 rgb_lerped = HslToRgb( hsl_lerp );
return float4( rgb_lerped.x, rgb_lerped.y, rgb_lerped.z, lerp( a.w, b.w, Time ) );
}
float3 LerpRgb(float3 a,float3 b,float Time)
{
float3 hsla = RgbToHsl( a.xyz );
float3 hslb = RgbToHsl( b.xyz );
float3 hsl_lerp = lerp( hsla, hslb, Time );
float3 rgb_lerped = HslToRgb( hsl_lerp );
return rgb_lerped;
}
float sign (float2 p1, float2 p2, float2 p3)
{
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
bool PointInTriangle_Direct(float2 pt, float2 v1, float2 v2, float2 v3)
{
bool b1 = sign(pt, v1, v2) < 0.0f;
bool b2 = sign(pt, v2, v3) < 0.0f;
bool b3 = sign(pt, v3, v1) < 0.0f;
return ((b1 == b2) && (b2 == b3));
}
// glcore no longer allows intrinsic cross(float2)
// rewrite GetTriangleBarycentric to not use cross at all to simplify code
float2 Cross2(float2 a2,float2 b2)
{
float3 a3 = float3( a2.x, a2.y, 0 );
float3 b3 = float3( b2.x, b2.y, 0 );
float3 c3 = cross( a3, b3 );
return c3.xy;
}
// http://gamedev.stackexchange.com/a/23745
float3 GetTriangleBarycentric(float2 Point,float2 p1,float2 p2,float2 p3)
{
float2 a = p1;
float2 b = p2;
float2 c = p3;
float2 p = Point;
float2 v0 = b - a;
float2 v1 = c - a;
float2 v2 = p - a;
float d00 = dot(v0, v0);
float d01 = dot(v0, v1);
float d11 = dot(v1, v1);
float d20 = dot(v2, v0);
float d21 = dot(v2, v1);
float denom = d00 * d11 - d01 * d01;
float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0 - v - w;
return float3(u,v,w);
}
float2 GetTriangleBarycentric2(float2 Point,float2 p1,float2 p2,float2 p3,float2 a,float2 b,float2 c)
{
float3 Bary = GetTriangleBarycentric( Point, p1, p2, p3 );
return (a * Bary.x) + (b * Bary.y) + (c * Bary.z);
}
float3 GetTriangleBarycentric3(float2 Point,float2 p1,float2 p2,float2 p3,float3 a,float3 b,float3 c)
{
float3 Bary = GetTriangleBarycentric( Point, p1, p2, p3 );
return (a * Bary.x) + (b * Bary.y) + (c * Bary.z);
}
float4 GetTriangleBarycentric3(float2 Point,float2 p1,float2 p2,float2 p3,float4 a,float4 b,float4 c)
{
float3 Bary = GetTriangleBarycentric( Point, p1, p2, p3 );
return (a * Bary.x) + (b * Bary.y) + (c * Bary.z);
}
bool PointInsideTriangle(float2 p,float2 t0,float2 t1,float2 t2)
{
return PointInTriangle_Direct(p,t0,t1,t2);
float2 uv = GetTriangleBarycentric2( p, t0, t1, t2, float2(0,0), float2(1,0), float2(1,1) );
// Just evaluate s, t and 1-s-t. The point p is inside the triangle if and only if they are all positive.
if ( uv.x >= 0 && uv.y >= 0 && 1-uv.x-uv.y >=0 )
return true;
//return uv.x >= 0 && uv.y >= 0 && uv.x <= 1 && uv.y <= 1;
return false;
}
bool Approximately(float a,float b)
{
return abs(a-b)<= 0.001f;
}
float3 LatLonToView(float2 LatLon)
{
// http://en.wikipedia.org/wiki/N-vector#Converting_latitude.2Flongitude_to_n-vector
float latitude = LatLon.x;
float longitude = LatLon.y;
float las = sin(latitude);
float lac = cos(latitude);
float los = sin(longitude);
float loc = cos(longitude);
return float3( los * lac, las, loc * lac );
}
float2 ViewToEquirect(float3 View3)
{
View3 = normalize(View3);
float2 longlat = float2(atan2(View3.x, View3.z) + UNITY_PI, acos(-View3.y));
//longlat.x += lerp( 0, UNITY_PI*2, Range( 0, 360, LatitudeOffset ) );
//longlat.y += lerp( 0, UNITY_PI*2, Range( 0, 360, LongitudeOffset ) );
float2 uv = longlat / float2(2.0 * UNITY_PI, UNITY_PI);
return uv;
}
float2 NormalizeUv(float2 uv)
{
// 0..1 -> -1..1
uv *= 2;
uv -= 1;
return uv;
}
#define CUBEMAP_UP 0
#define CUBEMAP_FORWARD 1
#define CUBEMAP_LEFT 2
#define CUBEMAP_BACKWARD 3
#define CUBEMAP_RIGHT 4
#define CUBEMAP_DOWN 5
#define CUBEMAP_FACECOUNT 6
// https://github.com/SoylentGraham/panopo.ly/blob/master/site_upload/cubemap.php#L286
#define CUBEMAP_UP_UVTRANSFORM float3x3( -1,0,0, 0,0,1, 0,1,0 ) // float3( -uv.x, 1, uv.y );
#define CUBEMAP_DOWN_UVTRANSFORM float3x3( -1,0,0, 0,0,-1, 0,-1,0 ) // float3( -uv.x, -1, -uv.y );
#define CUBEMAP_FORWARD_UVTRANSFORM float3x3( 1,0,0, 0,1,0, 0,0,1 ) // float3( uv.x, uv.y, 1 );
#define CUBEMAP_BACKWARD_UVTRANSFORM float3x3( -1,0,0, 0,1,0, 0,0,-1 ) // float3( -uv.x, uv.y, -1 );
#define CUBEMAP_LEFT_UVTRANSFORM float3x3( 0,0,-1, 0,1,0, 1,0,0 ) // float3( -1, uv.y, -uv.x );
#define CUBEMAP_RIGHT_UVTRANSFORM float3x3( 0,0,1, 0,1,0, -1,0,0 ) // float3( 1, uv.y, -uv.x );
// must match CUBEMAP_XX order
static const float3x3 CubemapUvTransform[CUBEMAP_FACECOUNT] =
{
CUBEMAP_UP_UVTRANSFORM,
CUBEMAP_FORWARD_UVTRANSFORM,
CUBEMAP_LEFT_UVTRANSFORM,
CUBEMAP_BACKWARD_UVTRANSFORM,
CUBEMAP_RIGHT_UVTRANSFORM,
CUBEMAP_DOWN_UVTRANSFORM
};
// gr: be careful, current use of this I believe is 0..1 and not -1...1 but ViewToEquirect corrects for lat/lon
float3 CubeUvToView(float2 uv,float3x3 Transform)
{
uv = NormalizeUv(uv);
return mul( Transform, float3(uv,1) );
}
float3 CubeLeftToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_LEFT_UVTRANSFORM );
}
float3 CubeRightToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_RIGHT_UVTRANSFORM );
}
float3 CubeUpToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_UP_UVTRANSFORM );
}
float3 CubeDownToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_DOWN_UVTRANSFORM );
}
float3 CubeForwardToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_FORWARD_UVTRANSFORM );
}
float3 CubeBackwardToView(float2 uv)
{
return CubeUvToView( uv, CUBEMAP_BACKWARD_UVTRANSFORM );
}
float TimeAlongLine2(float2 Position,float2 Start,float2 End)
{
float2 Direction = End - Start;
float DirectionLength = length(Direction);
float Projection = dot( Position - Start, Direction) / (DirectionLength*DirectionLength);
return Projection;
}
float2 NearestToLine2(float2 Position,float2 Start,float2 End)
{
float Projection = TimeAlongLine2( Position, Start, End );
// past start
Projection = max( 0, Projection );
// past end
Projection = min( 1, Projection );
// is using lerp faster than
// Near = Start + (Direction * Projection);
float2 Near = lerp( Start, End, Projection );
return Near;
}
float DistanceToRay2(float2 Position,float2 Start,float2 End)
{
// get length of cross product
float2 LineDir = End - Start;
float2 PerpDir = float2( LineDir.y, -LineDir.x);
float2 dirToPt1 = Start - Position;
return abs( dot( normalize(PerpDir), dirToPt1 ) );
}
float DistanceToLine2(float2 Position,float2 Start,float2 End)
{
float2 Near = NearestToLine2( Position, Start, End );
return length( Near - Position );
}
// gr: from the tootle engine :) https://github.com/TootleGames/Tootle/blob/master/Code/TootleMaths/TLine.cpp
float3 NearestToRay3(float3 Position,float3 Start,float3 Direction)
{
float3 LineDir = Direction;
float LineDirDotProduct = dot( LineDir, LineDir );
// avoid div by zero
// gr: this means the line has no length.... for shaders maybe we can fudge/allow this
if ( LineDirDotProduct == 0 )
return Start;
float3 Dist = Position - Start;
float LineDirDotProductDist = dot( LineDir, Dist );
float TimeAlongLine = LineDirDotProductDist / LineDirDotProduct;
// gr: for line segment
/*
if ( TimeAlongLine <= 0.f )
return Start;
if ( TimeAlongLine >= 1.f )
return GetEnd();
*/
// gr: lerp this for gpu speedup
return Start + LineDir * TimeAlongLine;
}
float DistanceToRay3(float3 Position,float3 Start,float3 Direction)
{
float3 Nearest = NearestToRay3( Position, Start, Direction );
return length( Position - Nearest );
}
// possibly faster than above if whatever shader compiler replaces length() with a sqrt formula
float DistanceSqToRay3(float3 Position,float3 Start,float3 Direction)
{
float3 Nearest = NearestToRay3( Position, Start, Direction );
float3 Delta = Position - Nearest;
// Use pythaguras to work out distance between the two points
// gr: err isn't this dot prod/magnitude?
//float DistSq = (Delta.x * Delta.x) + (Delta.y * Delta.y) + (Delta.z * Delta.z);
float DistSq = dot( Delta, Delta );
return DistSq;
}
// vector from Pos to nearest point on ray
float DeltaToRay3(float3 Position,float3 Start,float3 Direction)
{
float3 Nearest = NearestToRay3( Position, Start, Direction );
return Position - Nearest;
}
/* // z is intersection=1 no_intersection=0
float3 GetLineLineIntersection2(float2 StartA,float2 EndA,float2 StartB,float2 EndB)
{
let CornerScore = (ScoreA+ScoreB)/2;
//return [0.5,0.5,1];
// https://stackoverflow.com/a/1968345
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
// intersect the intersection point may be stored in the floats i_x and i_y.
let p0_x = LineA[0];
let p0_y = LineA[1];
let p1_x = LineA[2];
let p1_y = LineA[3];
let p2_x = LineB[0];
let p2_y = LineB[1];
let p3_x = LineB[2];
let p3_y = LineB[3];
let s1_x = p1_x - p0_x;
let s1_y = p1_y - p0_y;
let s2_x = p3_x - p2_x;
let s2_y = p3_y - p2_y;
let s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
let t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
let ix = p0_x + (t * s1_x);
let iy = p0_y + (t * s1_y);
if ( ScoreA===undefined )
return [ix,iy];
return [ix,iy,CornerScore];
}
return null;
}
*/
void GetRayRayIntersection3(float3 StartA,float3 DirA,float3 StartB,float3 DirB,out float IntersectionTimeA,out float IntersectionTimeB)
{
// must be normalised to match c# version
float3 da = normalize(DirA);
float3 db = normalize(DirB);
float3 dc = StartB - StartA;
float3 dadb_cross = cross(da, db);
float3 dcdb_cross = cross(dc, db);
float3 dcda_cross = cross(dc, da);
float sa = dot(dcdb_cross, dadb_cross) / lengthsq(dadb_cross);
float sb = dot(dcda_cross, dadb_cross) / lengthsq(dadb_cross);
IntersectionTimeA = sa;
IntersectionTimeB = sb;
}
void GetLineLineIntersection3(float3 StartA,float3 EndA,float3 StartB,float3 EndB,out float IntersectionTimeA,out float IntersectionTimeB)
{
float LengthA = length(EndA - StartA);
float LengthB = length(EndB - StartB);
float3 DirA = (EndA - StartA);
float3 DirB = (EndB - StartB);
GetRayRayIntersection3( StartA, DirA, StartB, DirB, IntersectionTimeA, IntersectionTimeB );
// Intersection time is along ray in world units, even though we normalised the dir.
// if they cross at ita=2 then thats still 2 in world space
// so divide to get it relative to the line
IntersectionTimeA /= LengthA;
IntersectionTimeB /= LengthB;
}
void GetLineLineIntersection3Clamped(float3 StartA,float3 EndA,float3 StartB,float3 EndB,out float IntersectionTimeA,out float IntersectionTimeB)
{
GetLineLineIntersection3( StartA, EndA, StartB, EndB, IntersectionTimeA, IntersectionTimeB);
IntersectionTimeA = Clamp01(IntersectionTimeA);
IntersectionTimeB = Clamp01(IntersectionTimeB);
}
// same as PopMath
float2 AngleRadianToVector2(float radian)
{
float x = sin(radian);
float y = cos(radian);
return float2(x,y);
}
// same as PopMath
float2 AngleDegreeToVector2(float degree)
{
return AngleRadianToVector2( radians(degree) );
}