summaryrefslogtreecommitdiffstats
path: root/spa/plugins/vulkan/shaders/disk-intersection.comp
blob: 7b92fdfb37c71f09d2a78fd26674828633dbf36d (plain)
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
// The MIT License
// Copyright © 2013 Inigo Quilez
// 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.


// Other intersectors:
//
// Box:             https://www.shadertoy.com/view/ld23DV
// Triangle:        https://www.shadertoy.com/view/MlGcDz
// Capsule:         https://www.shadertoy.com/view/Xt3SzX
// Ellipsoid:       https://www.shadertoy.com/view/MlsSzn
// Sphere:          https://www.shadertoy.com/view/4d2XWV
// Capped Cylinder: https://www.shadertoy.com/view/4lcSRn
// Disk:            https://www.shadertoy.com/view/lsfGDB
// Capped Cone:     https://www.shadertoy.com/view/llcfRf
// Rounded Box:     https://www.shadertoy.com/view/WlSXRW
// Rounded Cone:    https://www.shadertoy.com/view/MlKfzm
// Torus:           https://www.shadertoy.com/view/4sBGDy
// Sphere4:         https://www.shadertoy.com/view/3tj3DW
// Goursat:         https://www.shadertoy.com/view/3lj3DW


#define SC 3.0

#if 1
//
// Elegant way to intersect a planar coordinate system (3x3 linear system)
//
vec3 intersectCoordSys(in vec3 o, in vec3 d, vec3 c, vec3 u, vec3 v)
{
	vec3 q = o - c;
	return vec3(dot(cross(u, v), q),
		    dot(cross(q, u), d),
		    dot(cross(v, q), d)) / dot(cross(v, u), d);
}

#else
//
// Ugly (but faster) way to intersect a planar coordinate system: plane + projection
//
vec3 intersectCoordSys(in vec3 o, in vec3 d, vec3 c, vec3 u, vec3 v)
{
	vec3 q = o - c;
	vec3 n = cross(u, v);
	float t = -dot(n, q) / dot(d, n);
	float r = dot(u, q + d * t);
	float s = dot(v, q + d * t);
	return vec3(t, s, r);
}

#endif

vec3 hash3(float n)
{
	return fract(sin(vec3(n, n + 1.0, n + 2.0)) *
		     vec3(43758.5453123, 12578.1459123, 19642.3490423));
}

vec3 shade(in vec4 res)
{
	float ra = length(res.yz);
	float an = atan(res.y, res.z) + 8.0 * iTime;
	float pa = sin(3.0 * an);

	vec3 cola =
	    0.5 + 0.5 * sin((res.w / 64.0) * 3.5 + vec3(0.0, 1.0, 2.0));

	vec3 col = vec3(0.0);
	col += cola * 0.4 * (1.0 - smoothstep(0.90, 1.00, ra));
	col +=
	    cola * 1.0 * (1.0 -
			  smoothstep(0.00, 0.03,
				     abs(ra - 0.8))) * (0.5 + 0.5 * pa);
	col +=
	    cola * 1.0 * (1.0 -
			  smoothstep(0.00, 0.20,
				     abs(ra - 0.8))) * (0.5 + 0.5 * pa);
	col +=
	    cola * 0.5 * (1.0 -
			  smoothstep(0.05, 0.10,
				     abs(ra - 0.5))) * (0.5 + 0.5 * pa);
	col +=
	    cola * 0.7 * (1.0 -
			  smoothstep(0.00, 0.30,
				     abs(ra - 0.5))) * (0.5 + 0.5 * pa);

	return col * 0.3;
}

vec3 render(in vec3 ro, in vec3 rd)
{
	// raytrace
	vec3 col = vec3(0.0);
	for (int i = 0; i < 64; i++) {
		// position disk
		vec3 r = 2.5 * (-1.0 + 2.0 * hash3(float (i)));
		r *= SC;
		// orientate disk
		vec3 u = normalize(r.zxy);
		vec3 v = normalize(cross(u, vec3(0.0, 1.0, 0.0)));

		// intersect coord sys
		vec3 tmp = intersectCoordSys(ro, rd, r, u, v);
		tmp /= SC;
		if (dot(tmp.yz, tmp.yz) < 1.0 && tmp.x > 0.0) {
			// shade
			col += shade(vec4(tmp, float (i)));
		}
	}

	return col;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
	vec2 q = fragCoord.xy / iResolution.xy;
	vec2 p = -1.0 + 2.0 * q;
	p.x *= iResolution.x / iResolution.y;

	// camera
	vec3 ro =
	    2.0 * vec3(cos(0.5 * iTime * 1.1), 0.0,
		       sin(0.5 * iTime * 1.1));
	vec3 ta = vec3(0.0, 0.0, 0.0);
	// camera matrix
	vec3 ww = normalize(ta - ro);
	vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
	vec3 vv = normalize(cross(uu, ww));
	// create view ray
	vec3 rd = normalize(p.x * uu + p.y * vv + 1.0 * ww);

	vec3 col = render(ro * SC, rd);

	fragColor = vec4(col, 1.0);
}

void mainVR(out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri,
	    in vec3 fragRayDir)
{
	vec3 col = render(fragRayOri + vec3(0.0, 0.0, 0.0), fragRayDir);

	fragColor = vec4(col, 1.0);
}