- /
-
MATropolis (moving light, chaging sky, stars & snow)
on 23 Nov 2023
- 11
- 23
- 2
- 1
- 1677
drawframe(1);
Write your drawframe function below
function drawframe(f)
% Adjusted version of the MATropolis with lighting
% Set seed for random number generator (needed to prevent bars changing
% height between steps)
rng(20210214,'twister')
% Generate random bar heights and plot as bar3
h = randg(1,15);
b = bar3(h);
% Define building colormap and display dependent on bar height
cmap = pink();
customCmap = cmap(:,[2,3,1]);
for i=1:numel(b)
set(b(i),'CData',b(i).ZData,'FaceColor','interp')
end
% Define sky colours
skymap=[[linspace(.1,.3,18)' linspace(0,.7,18)' linspace(.3,.9,18)'];[linspace(.3,.1,30)' linspace(.7,0,30)' linspace(.9,.3,30)']];
% Add moving light source
theta = interp1([0 48],[0 2*pi],f);
ht = 4*sin(theta) + 2;
pos = [17*cos(theta) 10*sin(theta) ht];
light(Position = pos)
% Loop through buildings
for i=1:15 % 1:size(h,1)
for j=1:15 % 1:size(h,2)
c=.1:.2:h(i,j)-.2;
d=[c;c];
z=d(:)'+[0;0;.1;.1];
y=i+[-.2,.2]+[-1;1;1;-1]/12;
y=repmat(y,size(c));
if(f>=20 && rand<=(f-20)/28)
patch(z*0+j-.4,y,z,'w')
else
patch(z*0+j-.4,y,z,'k')
end
end
end
% Assign colormap and remove unnecessary tick marks/labels
set(gca,'Colormap',customCmap,'Color',skymap(f,:),'XTick',[],'YTick',[],'ZTick',[])
axis([0 15 0 15 0 5]);
camva(4)
% Add stars in the sky
if(f>=28)
n = 80; % Number of stars
maxX = 15; % Max Coordinate
X = 15*rand(1,n); % Position
Y = zeros(1,n);
Z = 4*rand(1,n);
S = randi(10,1,n)+2; % Spokes
C = rand(1,n)*0.7*((f-27)/21) + 0.3; % Brightness Variation
B(1,:) = (rand(1,n)/10+0.9).*C; % Hue Variation
B(2,:) = (rand(1,n)/10+0.9).*C;
B(3,:) = (rand(1,n)/10+0.9).*C;
if(mod(f,4)==0) % Alters the rng to allow a twinkling effect
rand;
end
end
hold on
if(f>=28)
% % Stars for back left wall
for k=1:n
ms = 8*((f-27)/21)*rand; % Choose star (marker) size
if(ms>=8*((f-27)/21)*0.75)
plot3(X(k),Y(k),Z(k),'*','MarkerSize',ms,'Color',[B(1,k) B(2,k) B(3,k)],'LineWidth',2); % emphasise largest
end
plot3(X(k),Y(k),Z(k),'*','MarkerSize',ms,'Color',[B(1,k) B(2,k) B(3,k)]); % add star (marker)
% stars for back right wall
ms = 8*((f-27)/21)*rand;% Choose star (marker) size
if(ms>=8*((f-27)/21)*0.75)
plot3(Y(k)+15,X(k),Z(k),'*','MarkerSize',ms,'Color',[B(1,k) B(2,k) B(3,k)],'LineWidth',2); % emphasise largest
end
plot3(Y(k)+15,X(k),Z(k),'*','MarkerSize',ms,'Color',[B(1,k) B(2,k) B(3,k)]); % add star (marker)
end
end
% Add patch to remove gaps between buildings
patch([0 15 15 0 0],[0 0 15 15 0],[0.1 0.1 0.1 0.1 0.1],'k');
% Add snow
rng(0)
scatter3(15*rand(1,500)+(rand(1,500)-0.5)*sin(f/6),15*rand(1,500)+(rand(1,500)-0.5)*sin(f/6),3+10*rand(1,500)-3*(f/48),75,[.9 .9 .9],'filled','MarkerFaceAlpha',0.5,'MarkerEdgeAlpha',0)
view([-63 17]);
hold off
end