• Remix
  • Share
  • New Entry

on 8 Nov 2023
  • 10
  • 37
  • 3
  • 1
  • 1199
drawframe(1);
Write your drawframe function below
function drawframe(f)
fogcolor = [ 1 1 1];
if f == 1
% First frame, initialize our little city
n = 25;
sz = .3;
v1 = randi(10,n,3) - [ 5 0 0 ];
% Place a street in here
mask = v1(:,1) <= 0;
v1(mask,1) = v1(mask,1)-1;
% Duplicate so we are in a repeating donut
vertxyz = [ v1; v1+[0 10 0] ];
% Building shapes
verts = zeros(0,3);
faces = zeros(0,4);
for idx=1:(n*2)
vs = (idx-1)*8;
v = vertxyz(idx,:);
r = v + [ -sz -sz 0
sz -sz 0
sz sz 0
-sz sz 0];
verts(vs+1:vs+8,:) = [ r; r.*[1 1 0] ];
fs = (idx-1)*5;
faces(fs+1:fs+5,:) = [ 1 2 3 4; % roof
1 2 6 5; % side 1, et c
2 3 7 6;
3 4 8 7;
4 1 5 8 ] + vs;
end
newplot
% Buildings
patch('Faces', faces, 'vertices', verts, ...
'EdgeColor', 'w', 'EdgeAlpha', 1, ...
'FaceColor', [ .1 .1 .9 ], 'tag', 'building' );
% Floor
S = surf2patch(-10:10, 0:30, zeros(31,21));
patch(S, 'FaceColor',[.2 .6 .2], 'EdgeColor', 'none', 'tag', 'ground');
% Decorate
daspect([ 1 1 1 ]);
view(2)
axis([-10 10 0 20 0 15]);
box on
set(gca,'clipping','off', 'visible','off', 'proj', 'p')
set(gcf,'color', 'w');
end
B = findobj('tag','building');
G = findobj('tag','ground');
yp = f/4.8;
cp = [ 0 yp 1 ];
campos(cp);
camtarget([0 yp+10 2]);
camva(90);
% Now compute the FOG!
addFog(cp, B, [ .2 .1 .7 ]);
addFog(cp, G, [.2 .5 .2 ]);
function addFog(cp, p, clr)
v1 = p.Vertices-cp; % Center around camera position.
% Compute depth from camera, and rescale as 0-1
depthA = hypot(hypot(v1(:,1),v1(:,2)), v1(:,3));
% 10 is the size of our building set.
depthB = rescale(depthA,'InputMin', 0, 'InputMax', 10);
% Treat fog as a semi-transparent white on top of the patch.
% The depth implies the volume of fog you need to see through to get to the vertex.
fog_cdata = fogcolor.*depthB + clr.*(1-depthB);
% Update patch
set(p, 'FaceVertexCData', fog_cdata, 'FaceColor', 'interp');
end
end
Animation
Remix Tree
Load full remix tree