Debian Codegraph @

screensaver.shadertoy/resources/shaders/plasmatriangle.frag.glsl

raw
1 // Taken from https://www.shadertoy.com/view/XdX3WN
2
3 void mainImage( out vec4 fragColor, in vec2 fragCoord )
4 {
5 float pointRadius = 0.06;
6 float linkSize = 0.04;
7 float noiseStrength = 0.08; // range: 0-1
8
9 float minDimension = min(iResolution.x, iResolution.y);
10 vec2 bounds = vec2(iResolution.x / minDimension, iResolution.y / minDimension);
11 vec2 uv = fragCoord.xy / minDimension;
12
13 vec3 pointR = vec3(0.0, 0.0, 1.0);
14 vec3 pointG = vec3(0.0, 0.0, 1.0);
15 vec3 pointB = vec3(0.0, 0.0, 1.0);
16
17 // Make the points orbit round the origin in 3 dimensions.
18 // Coefficients are arbitrary to give different behaviours.
19 // The Z coordinate should always be >0.0, as it's used directly to
20 // multiply the radius to give the impression of depth.
21 pointR.x += 0.32 * sin(1.32 * iTime);
22 pointR.y += 0.3 * sin(1.03 * iTime);
23 pointR.z += 0.4 * sin(1.32 * iTime);
24
25 pointG.x += 0.31 * sin(0.92 * iTime);
26 pointG.y += 0.29 * sin(0.99 * iTime);
27 pointG.z += 0.38 * sin(1.24 * iTime);
28
29 pointB.x += 0.33 * sin(1.245 * iTime);
30 pointB.y += 0.3 * sin(1.41 * iTime);
31 pointB.z += 0.41 * sin(1.11 * iTime);
32
33 // Centre the points in the display
34 vec2 midUV = vec2(bounds.x * 0.5, bounds.y * 0.5);
35 pointR.xy += midUV;
36 pointG.xy += midUV;
37 pointB.xy += midUV;
38
39 // Calculate the vectors from the current fragment to the coloured points
40 vec2 vecToR = pointR.xy - uv;
41 vec2 vecToG = pointG.xy - uv;
42 vec2 vecToB = pointB.xy - uv;
43
44 vec2 dirToR = normalize(vecToR.xy);
45 vec2 dirToG = normalize(vecToG.xy);
46 vec2 dirToB = normalize(vecToB.xy);
47
48 float distToR = length(vecToR);
49 float distToG = length(vecToG);
50 float distToB = length(vecToB);
51
52 // Calculate the dot product between vectors from the current fragment to each pair
53 // of adjacent coloured points. This helps us determine how close the current fragment
54 // is to a link between points.
55 float dotRG = dot(dirToR, dirToG);
56 float dotGB = dot(dirToG, dirToB);
57 float dotBR = dot(dirToB, dirToR);
58
59 // Start with a bright coloured dot around each point
60 fragColor.x = 1.0 - smoothstep(distToR, 0.0, pointRadius * pointR.z);
61 fragColor.y = 1.0 - smoothstep(distToG, 0.0, pointRadius * pointG.z);
62 fragColor.z = 1.0 - smoothstep(distToB, 0.0, pointRadius * pointB.z);
63 fragColor.w = 1.0;
64
65 // We want to show a coloured link between adjacent points.
66 // Determine the strength of each link at the current fragment.
67 // This tends towards 1.0 as the vectors to each point tend towards opposite directions.
68 float linkStrengthRG = 1.0 - smoothstep(dotRG, -1.01, -1.0 + (linkSize * pointR.z * pointG.z));
69 float linkStrengthGB = 1.0 - smoothstep(dotGB, -1.01, -1.0 + (linkSize * pointG.z * pointB.z));
70 float linkStrengthBR = 1.0 - smoothstep(dotBR, -1.01, -1.0 + (linkSize * pointB.z * pointR.z));
71
72 // If the current fragment is in a link, we need to know how much the
73 // linked points contribute of their colour.
74 float sumDistRG = distToR + distToG;
75 float sumDistGB = distToG + distToB;
76 float sumDistBR = distToB + distToR;
77
78 float contribRonRG = 1.0 - (distToR / sumDistRG);
79 float contribRonBR = 1.0 - (distToR / sumDistBR);
80
81 float contribGonRG = 1.0 - (distToG / sumDistRG);
82 float contribGonGB = 1.0 - (distToG / sumDistGB);
83
84 float contribBonGB = 1.0 - (distToB / sumDistGB);
85 float contribBonBR = 1.0 - (distToB / sumDistBR);
86
87 // Additively blend the link colours into the fragment.
88 fragColor.x += (linkStrengthRG * contribRonRG) + (linkStrengthBR * contribRonBR);
89 fragColor.y += (linkStrengthGB * contribGonGB) + (linkStrengthRG * contribGonRG);
90 fragColor.z += (linkStrengthBR * contribBonBR) + (linkStrengthGB * contribBonGB);
91
92 // Use an underlying texture to provide some noise
93 float noiseMin = 1.0 - noiseStrength;
94 fragColor.xyz *= (1.0 - noiseStrength) + (noiseStrength * texture(iChannel0, uv * 2.0).xyz);
95 }