• /
• # Bifrost Rings

on 3 Dec 2023
• 20
• 48
• 0
• 2
• 895
function drawframe(f)
% This is Xor's AMAZING shader
% Here is my translation to MATLAB
iRes = [800,450];
iTime = f/4
persistent fragCoord
if isempty(fragCoord)
[x,y]=meshgrid(1:iRes(1),iRes(2):-1:1);
fragCoord = cat(3,x',y');
end
im4 = mainImage(fragCoord);
imr = imresize(im4, flip(iRes).*2, 'method', 'bilinear');
imshow(imr);
function finalColor=mainImage(fragCoord)
% Initialize hue and clear finalColor
finalColor = zeros(iRes(1),iRes(2),3);
r = cat(3,iRes(1),iRes(2));
I = fragCoord;
A = [1 -1; 2 2];
p = (I - r.*0.6);
p = permute(p, [3 1 2]);
for i = 1:iRes(1)
for j = 1:iRes(2)
p(:,i,j) = A*p(:,i,j);
end
end
p = permute(p, [2 3 1]);
for i = 0:32
denom = 2.*iRes(2) -p(:,:,2);
I = p./denom;
c = [0,1,2];
a = atan2(I(:,:,2),I(:,:,1))*ceil(0.1*i) + iTime*sin(i^2) + i^2;
for j = 1:3
d1 = 0.2./(abs(vecnorm(I,2,3).*80 - i) + 40./iRes(2));
d2 = clamp(cos(a),0,0.6);
d3 = cos(a - i + c(j)) +1;
finalColor(:,:,j) = finalColor(:,:,j) + d1.*d2.*d3;
end
end
finalColor = permute(finalColor,[2 1 3]);
end
end
function result = clamp(x, lower, upper)
result = max(min(x, upper), lower);
end