forked from mathworks/awesome-matlab-students
-
Notifications
You must be signed in to change notification settings - Fork 1
/
COLOR_PULSE.m
81 lines (64 loc) · 1.86 KB
/
COLOR_PULSE.m
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
animateFrames();
function animateFrames()
animFilename = 'COLOR_PULSE.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame);
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, 'LoopCount', Inf, 'DelayTime', delayTime);
else
imwrite(A,map,animFilename, 'WriteMode', 'append', 'DelayTime', delayTime);
end
end
end
function drawframe(f)
% @"kishimisu" I don't know who you are but you are GLSL god.
% this is my transposed version in MATLAB.
% https://www.shadertoy.com/view/mtyGWy
% What I've learned transposing GLSL to MATLAB:
% vec.length = vecnorm( vec,2,3)
% vec.fract = mod( vec, 1)
% - all the rest is nearly pure math and talent.
iRes = cat(3, 800, 800);
iTime = f/47*3;
persistent fragCoord
if isempty(fragCoord)
[x,y]=meshgrid(1:iRes(1), 1:iRes(2));
fragCoord = cat(3,x,y);
end
img = mainImage(fragCoord);
imagesc(img)
axis equal
axis off
function out=palette( t )
cc = ones(1,1,3);
aa = cc/2;
dd = reshape([0.263,0.416,0.557],1,1,3);
out = aa + aa.*cos( 6.28318*( cc .* t + dd) );
end
function finalColor=mainImage( fragCoord )
uv = (fragCoord * 2 - iRes) / iRes(2);
uv0 = uv;
finalColor = 0;
i=0;
while i < 4
uv = mod(uv * 1.5, 1) - 0.5;
d = vecnorm(uv,2,3) .* exp(-vecnorm(uv0,2,3));
col = palette( vecnorm(uv0,2,3) + i *0.4 + iTime*0.4);
d = sin( d*10 + iTime )/8;
d = abs(d);
d = (0.01 ./ d).^ 1.2;
finalColor = finalColor + col .* d;
i=i+1;
end
end
end