嘿真的又是很久没见了,最近确实有点非常很特别小忙,今天带来一下MATHWORKS官方举办的迷你黑客大赛第三期(MATLAB Flipbook Mini Hack)的最新进展!!目前比赛已经刚好进行了一周,前两届都要求提交280个字符内的代码来生成图片,但是这一届,要求提交2000个字符内的代码来生成动画,字符限制一放开大家就开始有种各显神通的意思,仅仅一周就贡献了接近200个优质作品
本次比赛要求提交名为drawframe(f)
的函数代码,生成长达2s,48帧的动态图,动图生成是依靠如下代码来做到的:
function contestAnimator()
animFilename = 'animation.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
闲话少叙,一起看作品!!
看过前两期比赛的人应该对Tim
大佬有着深刻的印象这期比赛第一周他也贡献了三个优质的作品:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13177
function drawframe(nn)
% Moon over rolling fog.
% Comments below,
% Persistent variables -> keep you from having to recompute these
% variables at each iteration / save computation time
persistent enz ags x d c mx my mn cmp
if nn == 1
% Everything is precomputed so we don't really need this -> undo to
% get different clouds each time
rng default
% Base vector for pixels
x = linspace(-1, 1, 240);
% Make complex 3D noise spectrum -> spectrally shaping using
% distance. This is expensive so only do once.
b=length(x);
d=x(1:2:end)/2;
c=length(d);
r = ifftshift(sqrt((x*2).^2 + d'.^2 + permute(x.^2, [1, 3, 2])));
ags = linspace(0, 2*pi, 49);
enz = exp(6i*randn(c, b, b))./max(r.^2.4,2e-5);
% Make moon
[mx, my] = meshgrid(x);
rd = (mx-0.6).^2 + (my+0.2).^2;
mn = 1 - erf(max(rd - 0.3, 0)*3);
% Custom colormap for clouds
cmp = linspace(0, 1, 256)'.*[1, 1, 1];
cmp(:, 1) = cmp(:, 1).^1.6;
cmp(:, 2) = cmp(:, 2).^1.6*0.97;
cmp(:, 3) = tanh(cmp(:, 3))*1.1;
end
% Compute current noise realization. Linear phase causes a looping
% shift and offset creates the cloud morphing.
dt = abs(ifftn(exp(1i*(x.*ags(nn)/2 - ags(nn)/2)).*enz));
% Tapering the noise
dt = 5*dt.*(d-d(1))'.*permute(erf((x - 0.1)*2)+1, [1, 3, 2]);
dt = dt(:, :, 1:end);
% Thresholding for transparency
ap = dt>0.08;
% Making it look like clouds / adding glint
c_alp = cumsum(ap, 3);
CLS = (1.1 - erf(c_alp/5)).*ap.*dt;
ALP = ap.*rescale(dt);
% Alpha blend across volume
[AI, AS] = P(ALP, CLS);
% Loop spatially / keeps formations in roughly same spot. Turn off to
% see a different cloud movement effect.
AI = circshift(squeeze(AI)', [0, -5*nn]);
AS = circshift(squeeze(AS)', [0, -5*nn]);
% Web Matlab's 3D rendering is so slow that this fails to execute if
% you use surf to blend the images, so let's use the alpha-blender used
% to flatten the clouds for the RGB background, moon and cloud layers
% Convert clouds to RGB image using custom colormap
C = round(min(AI*40, 1)*255+1);
C = double(ind2rgb(C, cmp));
% Permuter to work with alpha blending function convention
PP = @(x)permute(x, [3, 2, 1]);
% This is a mess, but, it computes the alpha then applies the alpha
% blender to each channel and combines to form an image.
A = PP(cat(3, AS, mn, ones(size(mn))));
IG = PP([P(A, PP(cat(3, C(:, :, 1), mn, 0*mn))); P(A, PP(cat(3, C(:, :, 2), mn, 0*mn))); P(A, PP(cat(3, C(:, :, 3), mn*0.9, 0*mn)))]);
% Now, show.
image(IG);
axis equal;
camva(5.8);
%S.D.G.
% Alphablend function
function [AIf, ASf] = P(ATf, CLSf)
ASf = ATf(1, :, :);
AIf = CLSf(1, :, :);
% Have alpha, have intensity, compress onto single plane using alpha
% blending formula
for nf = 2:size(ATf, 1)
APf = ASf;
ASf = APf + ATf(nf, :, :).*(1-APf);
AIf = (AIf.*APf + CLSf(nf, :, :).*ATf(nf, :, :).*(1-APf))./(eps + ASf);
end
end
end
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12822
% % % Endless Tunnel
%
% How this works:
% - A torus is generated with texture that is periodic at pi/4 intervals
% - The camera is moved around a quarter of the torus so that looping has
% no discontinuities.
%
% We are swimming in characters this year so this could be significantly
% compressed and more features added!
function drawframe(nn)
% This enables the same "random" texture to be created every time
rng default;
r=6; % Torus radius
N=80; % Texture resolution. Set to higher on your own computer for improvements.
% (Has longer rendering times!)
R=zeros(N, N*8); % Initial torus radius
R(:,N/2:N:end)=-.02; % Notches every bit
R=conv2(R,[1:4,3:-1:1],'same'); % Fare the notches
R=R+conv2(randn(size(R))/500,ones(3)/9,'same')+1; % Add some random noise. Probably doesn't do a whole
% lot for N = 80.
R(end, :)=R(1, :); % Match endpoints to prevent a glitch
% Make angles for torus (radial and axial)
l=@(x)linspace(0,2*pi,x);
ag1=l(N)';
ag2=l(N*8);
% This will be the tunnel
b=R.*cos(ag1);
z=R.*sin(ag1);
x=b.*cos(ag2)+r*cos(ag2);
y=b.*sin(ag2)+r*sin(ag2);;
% This will be the algae growing on the pipe
R2=R*.99;
b=R2.*cos(ag1);
z2=R2.*sin(ag1);
x2=b.*cos(ag2)+r*cos(ag2);
y2=b.*sin(ag2)+r*sin(ag2);
% This will eventually be the water at the bottom
rd=linspace(4, 8, N)';
xd=rd.*cos(ag2);
yd=rd.*sin(ag2);
% Make camera positions
ag=linspace(0,2*pi,48*4);
cpx=r*cos(ag);
cpy=r*sin(ag);
cpz=zeros(size(cpx))-0.2;
% Texture: this will be the water. Making a complex random
% function. Noise generated spectrally like this has looping boundaries /
% can be tiled, so it is tiled for start/end consistency
[xx,yy]=meshgrid(linspace(-1,1,N*8),linspace(-1,1,N));
rd=sqrt(xx.^2+yy.^2);
sgnO=((randn(N, N*8)+1i*randn(N, N*8))./((ifftshift(rd.^2)+1e-5)));
sgnO=ifft2(sgnO(:,[1:N,end-N+1:end]));
sgnO=[sgnO,sgnO,sgnO,sgnO];
% This will be the algae on the side.
sg=(((randn(N,N*8)+1i*randn(N, N*8))./((ifftshift(rd.^2)+1e-6))));
sg=real(ifft2(sg(:, 1:N*2)));
sg=[sg,sg,sg,sg];
sg=erf(sg*2)+1;
sg=-sg.*(1-erf(sin(ag1)+1));
% This will be our phase looper. Since the water phase noise is complex, we
% can loop over 2*pi to get a splashing effect
ags=linspace(0, pi/4, 49);
ags=ags(1:48);
clf % Lights seem to accumulate without this...
% Algae color
clrF = permute([0.3, 0.5, 0.3]*0.4, [1, 3, 2]);
% Faster if we limit the number of faces generated each frame... this
% truncates the rendered portion of the torus to what is visible
[~, ci] = min(abs(ag2-ag(nn)));
rgv = (1:N*2) + ci;
% Update water noise using a phase rotation
sgn = real(sgnO.*exp(1i*ags(nn)*16));
sgn = sgn - mean(sgn(:));
sgn = erf(1*sgn/std(sgn(:)));
% Truncate
sgn = sgn(:, rgv);
sg = sg(:, rgv);
idz = N*8;
% Begin plotting
clr = (conv2(rand(N, N*8), ones(5)/25, 'same')*0.3+0.7).*ones(N, N*8, 3)*0.5;
surf(x(:, rgv), y(:, rgv), z(:, rgv), clr(:, rgv, :), 'SpecularStrength', 1, 'AmbientStrength', 0);
hold on;
surf(x2(:, rgv), y2(:, rgv), z2(:, rgv), (rand(size(sgn))*0.2+0.8).*clrF, 'FaceAlpha', 'flat', 'AlphaData', erf(rescale(1-sg)*2+0.5)*1, 'SpecularStrength', 0, 'AmbientStrength', 0);
surf(xd(:, rgv), yd(:, rgv), rand(size(sgn))*0.002 + sgn/20-0.8, 'FaceAlpha', 0.9, 'AmbientStrength', 0, 'DiffuseStrength', 0.35, 'SpecularStrength', 1);
% Appearance and camera settings
shading interp
axis equal
camproj('Perspective');
camva(50);
campos([cpx(nn), cpy(nn), cpz(1)+sin(ags(nn)*16)/6 - 0.2]);
camtarget([cpx(nn+18), cpy(nn+18), cpz(nn+18)-0.4]);
% Headlight: one doesn't seem strong enough
camlight;
camlight
D = camlight;
D.Color = [0.6, 0.8, 1];
% This is inefficient, should just directly set water color in the call to
% surf but you can pick a different colormap to alter the water color here
cmp = copper + summer;
colormap(cmp/2);
caxis([-1, 1]);
% S.D.G.
end
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13660
function drawframe(f)
% Ring world...
persistent H CS S3 U
if f==1
rng(2,'v4')
% % % % Make the ring % % % %
r=6; % Radius
N=60; % Number of edge-pixels on base unit
U=8; % Rotating by 1/8 of a circle
% Add texture to ring. Make it tech-y
R=zeros(N,N);
R(randi(N^2, 3))=1;
k1=ones(N/2)/10;
k2=k1;
k2(2:end-1,2:end-1)=0; % Kernel for glowing lights
RL=conv2(R,k2,'same'); % Glowing lights
R=conv2(R,k1,'same'); % Surface
% Tiling
R=repmat(R,[1,U])+1;
RL=repmat(RL,[1,U]);
% Fix seams. Expendable code.
R=s(R,N);
RL=s(RL,N);
RB=(R-1)/2;
RL=RL./RL;
% Notch to fit the land in...
NH=N/2;
NW=floor(N/6);
R(NH-NW+1:NH+NW, :)=0;
% Make our angles
l=@(x)linspace(0,2*pi,x);
ag1=l(N)';
a=l(N*U);
c=cos(a);
v=sin(a);
% Ring torus
b=erf(R.*cos(ag1)*5)+RB; % Make it square-ish
z=R.*sin(ag1);
x=b.*c+r*c;
y=b.*v+r*v;
% % % % Make the land inside.
% Spectral noise shaping... land mass will be a noisy cylinder
q=linspace(-1,1,N);
rand(8);
k=@()exp(6i*randn(N))./(q.^2+q'.^2+2e-4);
oo=@(x)ifft2(x);
B=rescale(abs(oo(k())))-.3;
CS=k();
C=rescale(abs(real(oo(CS.*exp(1i*.10)))))-.5;
X=@(x)max(repmat(x,[1,U])',0);
B=X(B)/2; % Max makes the water
C=X(C); % Clouds
% Assemble the cylinder s
[xl,yl,zl]=cy(a',q,r-B/3-.6,N); % Land
[xc,yc,zc]=cy(a',q,r+0*B-1.1,N); % Cloud
[xa,ya,za]=cy(a',q,r+0*B-1,N); % Atmosphere
za=za*1.1;
% Custom colormap: landscapey with low value = blue = water
cmp=max(copper,summer);
cmp(1,:)=[.1,.3,1]/2;
% Now plot
H=hgtransform();
surf(x,z,y,'pa',H); % Ring
material([0,.1,.6,3]);
hold on;
surf(x,z,y,RL.*cat(3,0,1,1),'AmbientS',1,'pa',H); % Light
hold on;
% Land, clouds, atmosph
S=surf(xl,zl,yl,B,'pa',H);
wn=sin(ag1/2);
alph =(1-wn).^3'.*ones(N*U, 1);
cp = rescale(alph, 0.7, 1);
cp = cat(3, cp.^8, cp.^2.5, cp.^0.9);
mm=@(x,y)material(x,y);
S3=surf(xc,zc,yc,0*xc+cat(3,1,1,1),'FaceAlpha','flat','AlphaData',erf(C*4),'pa',H);
S2=surf(xa,za,ya,cp,'FaceAlpha','flat','AlphaData',alph.^.7);
mm(S3,[1,0,0,5]);
mm(S2,[.6,1,0,5]);
mm(S,[0,1,.3,20]);
colormap(cmp);
caxis([0,.3]);
axis equal
shading flat
% Add stars
x=randn(3,900);
x=x./vecnorm(x)*20;
scatter3(x(1,:),x(2,:),x(3,:),rand(900,1)*100,rand(900,1)*[1,1,1],'.');
hold off;
% Axis / camera etc.
shading flat
axis equal off
light('position',[0,-20,0]);
light('position',[10,10,5]);
lighting('gouraud');
shading flat
set(gcf,'color','k');
camproj('perspective')
camva(30);
campos([78, -40, -40]/6);
camtarget([-1, 0, -1]);
end
ags=linspace(0,2*pi,49);
oo=@(x)ifft2(x);
C=rescale(abs(real(oo(CS.*exp(1i*ags(f))))))-.5;
X=@(x)max(repmat(x,[1,U])',0);
C=X(C); % Clouds
campos([78,-40+sin(ags(f))*2,-40]/6)
S3.AlphaData=erf(C*4);
set(H,'Ma', makehgtform('yrotate',(f-1)*pi/4/48));
end
% Extra functions:
% Make xyz cylinder coordinates
function [x,y,z]=cy(a,q,R,N)
x=cos(a).*R;
y=sin(a).*R;
z=q.*ones(N*8,1)*.9;
end
% Seam joiner
function g=s(g,N)
d=g(N,:)/2+g(1,:)/2;
g(1:4, :) = d.*ones(4,1);
g(end-3:end,:) = d.*ones(4,1);
end
Augusto Mazzei
这位大佬在第一周最后关头交上的非常炫酷且优质的作品:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13775
1
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
hiahiahia他甚至还弄了个自画像:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13475
function drawframe(f)
if any(f==1:4:48)
hold off
yy = [198 189];
xx = [388 554];
% I took these coordinates using 'drawpolygon' eheh :)
bocca =[411 370;438 383;466 386;497 382;
504 382;525 366;482 362;469 366;448 363];
nose=[420 313;443 296;513 306;460 316];
eyeL = [345 198;368 193;372 207;390 207;402 202;
400 193;404 188;420 187;389 176;346 187];
neck=[383 437;383 586;633 558;609 408];
eyebL=[325 163;357 153;401 148;427 143;
417 126;379 121;348 134];
eyebR=[503 149;519 114;574 118;594 126;
627 158;601 148;572 140;533 144];
nero=[109 832;99 730;152 592;318 525;363 515;
553 486;674 500;733 539;770 595;805 605;
850 840;820 837;784 837];
caps=[228 72;255 79;280 29;291 13;680 4;
695 44;699 128;677 174;656 249;653 112;
587 121;619 97;517 109;582 66;487 92;
541 35;436 118;400 118;468 69;360 109;
302 144;302 246;272 157;264 170;248 143;252 112];
rosso=[377 474;336 658;404 595;449 699;
654 574;672 535;627 437;621 486;416 592];
rosa=[295 222;280 204;278 253;297 333;323 357;
331 392;368 458;442 495;522 484;599 443;
636 370;649 316;679 280;690 202;685 183;
657 200;661 97;628 18;317 20;290 108];
marr = [.3 .2 .14];
dra( nero,'k');
dra( neck,[1 .90 .90]);
dra( rosso,[.63 .07 .18]);
dra( rosa,[1 .94 .94]);
dra( bocca,[.81 .67 .61]);
dra( nose,[.95 .9 .9]);
dra( caps,marr);
dra( eyebL,marr);
dra( eyebR,marr);
for i=1:2
for j=1:100
drawc([xx(i)+rand*5 -(yy(i)+rand*5)])
end
end
axis equal
axis off
set(gcf,'color',[255,255,224]/255)
end
end
function dra(a,c)
fill(a(:,1),-a(:,2),c,'EdgeCol','none'); hold on
end
function drawc(c)
r = 10+rand*6;
aa = 2*pi*linspace(0,1,randi([4 8]));
fill( sin(aa)*r+c(1)+randn*5, randn*5+cos(aa)*r+c(2), hsv2rgb([rand .5 1]),...
'FaceAlpha',.5,'EdgeColor','none');
end
以下是一个在比赛最开始几天非常惊艳的作品,由Jenny Bosten
大佬绘制:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12937
function drawframe(f)
g1=[45 129 191
64 148 195
135 178 210
158 196 215
170 207 216
210 219 218];
g2=[120 110 119
142 119 113
167 123 88
194 134 72
206 142 68
219 142 28];
z=linspace(1,6,100);
d=[linspace(10,30,24) linspace(30,10,24)];
x(:,:,1)=interp1(1:6,g1,z);
x(:,:,2)=interp1(1:6,g2,z);
x=permute(x,[3 1 2]);
x=permute(interp1([1,48],x,1:2:48),[2,3,1])./255;
x=cat(3,x,x(:,:,end:-1:1));
a=1000;
q=linspace(-235,235,48);
m=linspace(pi,-pi,48);
X=linspace(-255,255,a);
[t,r]=cart2pol(X-q(f),100+X'-100*cos(m(f)));
[x_i,y_i]=find(r<21);
[x_i2,y_i2]=find(r<3);
b=x(:,:,f);
j=[1 1 .9];
colormap([j;flipud(b)]);
r(r<20)=1;
for k=1:300
y(k,:)=circshift(r(min(x_i),:),randi(100)-50);
y(k,y_i2+round(randi(round(d(f)))-d(f)/2))=1;
end
imagesc([r(100:600,:);y])
axis equal
axis off
xlim([100 900]);
end
甚至还有人绘制了本场比赛的logo:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13022
% Author:Eric Ludlam
function drawframe(f)
% Constants
blue=validatecolor("#0076a8");
darkblue=validatecolor("#004b87");
S.vertices = [ 0 2 0; 1 2 0; % top
1 0 0; 2 0 0; % middle
0 -2 0; 1 -2 0; % bottom
];
S.faces = [ 1 2 4 3;
3 4 6 5;
];
S.facevertexcdata = [ 1 ;
1 ];
% Convert our number into different animation stages.
framemax = floor(48/5); % 5 stages
stage = ceil(f/framemax);
f = mod(f-1,framemax)+1; % approx 9 frames per stage
if stage > 5
stage = 5;
f = f + framemax;
framemax = framemax + 4;
end
% Initialize
if f==1 && stage==1
ax = newplot;
set(gcf, 'color','white');
axis(ax, [ -2 6 -5 3 0 1 ])
set(ax, 'position', [ 0 0 1 1], 'visible','off','tag','mh_ax')
daspect(ax,[1 1.5 1]);
clim([1 2]);
colormap(ax,[ blue; darkblue]);
p1 = patch('vertices',[], 'faces', [], 'FaceColor', 'flat', 'EdgeColor', 'none', 'tag', 'chevron1');
p2 = patch('vertices',[], 'faces', [], 'FaceColor', 'flat', 'EdgeColor', 'none', 'tag', 'chevron2');
txt = text(0,0,'', ...
'FontUnits', 'norm', ...
'FontSize', .17, ... % normalized units lets you resize this fig to any old size.
'Color', blue, ...
'VerticalAlignment', 'top',...
'HorizontalAlignment', 'center',...
'tag', 'mh_txt');
else
p1 = findobj('tag','chevron1');
p2 = findobj('tag','chevron2');
txt = findobj('tag','mh_txt');
end
% Animate
switch stage
case 1 % First chevron appears
xrng = linspace(-4,0,framemax);
x = xrng(f);
S.vertices = S.vertices + [ x 0 0 ];
set(p1, S);
set([txt p2], 'visible','off');
case 2 % Flip down
tmp = S.vertices([1 2],:);
yrng = linspace(2,-2,framemax);
tmp(:,2) = yrng(f);
S.vertices = [ S.vertices
tmp ];
S.faces = [ S.faces
3 4 8 7 ];
S.facevertexcdata = [ 1;1;2 ];
set(p1, S);
case 3 % Second chevron appears
xrng = linspace(-4,2,framemax);
x = xrng(f);
S.vertices = S.vertices + [ x 0 0 ];
S.facevertexcdata(2) = 2;
set(p2, S, 'visible','on');
case 4 % Minihack Txt appears
xrng = linspace(-5,2,framemax);
x = xrng(f);
clr = sprintf('\\color[rgb]{%.2f,%.2f,%.2f}', darkblue);
set(txt, 'visible','on', 'Position', [x -2.5], 'String', "Mini \bf" + clr + "Hack");
case 5 % Txt color change
rrng = linspace(darkblue(1),blue(1),framemax);
grng = linspace(darkblue(2),blue(2),framemax);
brng = linspace(darkblue(3),blue(3),framemax);
clr = sprintf('\\color[rgb]{%.2f,%.2f,%.2f}', rrng(f),grng(f),brng(f));
set(txt,'String', "Mini \bf" + clr + "Hack");
end
end
剩下一些不详细讲解了,大家自己看叭~
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13372
function drawframe(xx)
% Inspiration: https://www.gabrielgambetta.com/computer-graphics-from-scratch/reflection.html
%% Scene Setup
vs = [1.5 1]; % viewport size
p = 1; % projection plane
O = [0 0 0];
c = eye(3); % Camera Rotation
Cw = 120; Ch = 80;
cnvs = zeros(Cw+1,Ch+1,3); % Canvas of pixels
b = Sphere([8/47*xx-196/47 -1 3],1,[0 0 255],500,0.2);
r = Sphere([2 0 4],1,[255 0 0],500,0.3);
g = Sphere([-2 0 4],1,[0 255 0],10,0.4);
y = Sphere([0 -5001 0],5000,[0 255 255],1000,0.5);
sp = [r g b y];
l1 = Light('amb',0.2,[],[]);
l2 = Light('pt',0.6,[2 1 0],[]);
l3 = Light('dir',0.2,[],[1 4 4]);
ls = [l1 l2 l3];
%%
for i = 1:Cw+1 % For each column of pixels
x = i-1-Cw/2;
slc = zeros(1,Ch,3);
for j = 1:Ch+1 % For each row of pixels
y = j-1-Ch/2;
D = C2V(x,y,Cw,Ch,vs,p);
D = (c*D')';
color = TR(O,D,1,inf,sp,ls,3);
slc(1,j,:) = rot90(color);
end
cnvs(i,:,:) = slc;
end
% Show the canvas, upsample by a factor of 2
imshow(imresize(rot90(uint8(cnvs)),2));
axis equal;
end
%%
function D = C2V(x,y,Cw,Ch,V,d)
% Canvas to Viewport
D = [x*V(1)/Cw, y*V(2)./Ch, d];
end
function c = TR(O,D,ti,ta,sp,ls,rd)
% Trace Ray
% rd: recursion depth
[cs,ct] = CI(O,D,ti,ta,sp);
if isempty(cs)
c = [0 0 0];
return
else
P = O + ct*D; % Compute intersection
N = P-cs.m; % Compute sphere normal at intersection
N = N/norm(N);
c = cs.c*CL(P,N,ls,-D,cs.s,ta,sp);
end
r = cs.rf;
if ~(rd<=0 || r<=0)
R = RR(-D,N);
rc = TR(P,R,0.001,Inf,sp,ls,rd-1); % reflected color
c = c*(1-r)+rc*r;
end
end
function [s,t] = CI(O,D,t_min,t_max,sp)
% CI: ClosestIntersection
t = inf; % Distance to shere
s = []; % Sphere
for i = 1:length(sp)
[t1,t2] = IRS(O, D, sp(i));
if t1>t_min && t1<t_max && t1 < t
t = t1;
s = sp(i);
end
if t2>t_min && t2<t_max && t2 < t
t = t2;
s = sp(i);
end
end
end
function i = CL(P,N,ls,V,s,ta,sp)
% Compute Lighting
i = 0;
for l = 1:length(ls)
if strcmp(ls(l).t,'amb') % Ambient light
i = i+ls(l).i;
else
if strcmp(ls(l).t,'pt') % Point light
L = ls(l).p - P;
else
L = ls(l).d; % Diffuse light
end
% Shadow Check
ss = CI(P,L,0.001,ta,sp);
if ~isempty(ss)
continue
end
% Diffuse
n_dot_l = dot(N,L);
if n_dot_l>0
i = i + ls(l).i*n_dot_l/(norm(N)*norm(L));
end
% Specular
if s ~= -1
R = RR(L,N);
r_dot_v = dot(R,V);
if r_dot_v > 0
i = i + ls(l).i*(r_dot_v/(norm(R)*norm(V)))^s;
end
end
end
end
end
function r = RR(R,N)
% Reflect Ray
r = 2*N*dot(N,R)-R;
end
function [t1,t2] = IRS(O,D,s)
% IntersectRaySphere
C = s.m;
r = s.r;
OC = O - C;
k1 = dot(D, D);
k2 = 2*dot(OC, D);
k3 = dot(OC, OC) - r*r;
d = k2*k2 - 4*k1*k3; % discriminant
if d < 0
t1 = inf;
t2 = inf;
else
t1 = (-k2 + sqrt(d)) / (2*k1);
t2 = (-k2 - sqrt(d)) / (2*k1);
end
end
function l = Light(t,i,p,d)
% t: type (amb: ambient, pt: point, dir: directional)
% i: intensity
% p: position
% d: direction
l.t = t;
l.i = i;
l.p = p;
l.d = d;
end
function s = Sphere(cntr,r,col,spec,ref)
% cntr: center
% r: radius
% col: color
% spec: spectral
% ref: reflective
s.m = cntr;
s.r = r;
s.c = col;
s.s = spec;
s.rf = ref;
end
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13213
function drawframe(f)
persistent handles
if f == 1 || isempty(handles)
rng('default')
axes(colormap=gray,View=[172,22],Color='k')
hold on
camva(3)
h=randg(1,25); % building heights
b=bar3(h);
axis equal
g=.17; % height and width of windows
[m,k]=ndgrid(1:25); % (x,y) building indices
handles.w = gobjects(0);
for i=1:625 % loop through each building
% Interpolate building color according to building height
% Ideally this should be outside the loop but that would require more characters
q=b(m(i));
set(q,'CData',q.ZData,'FaceColor','interp')
for j=1:2 % loop through each column of windows
L=k(i)-.27+(j-1)*.37; % in k-a+(j-1)*b, a is the signed distance from center of building in row k(i) to left edge of window in column j and b is the interval between left edges of windows.
x=L+[0,g,g,0]'; % [L;R;R;L] edges of windows in building i, window column j
z=ones(4,1)*(.2:.4:h(i)-.2)+[0;0;g;g]; % 4xn height of windows for n levels of windows; in a:b:c a is height of first row of windows, b is the interval between window-bottoms, c is height of tallest window.
y=z*0+m(i)+.45; % 4xn location of front face of building
% Plot windows for building i, column j
for w = 1:width(z)
handles.w(end+1)=patch(z(:,w)*0+x,y(:,w),z(:,w),'y');
if rand(1) < 0.25
handles.w(end).FaceColor = 'none';
end
end
end
end
set(gcf,'color','k')
axis off
camva(2.5)
campos([58.256 185.12 25.621])
view([165.27, 7.0091])
clim([0 8])
% lightning light
handles.light = light('style','infinite',...
'Position',[-0.21759 1.3841 0.1921], ...
'Visible', 'off');
% lightning background
ax = gca;
handles.Z = patch(ax.XLim([2,2,1,1]),...
ax.YLim([1 1 1 1]),...
ax.ZLim([1 2 2 1]),...
[6 7.5 -3 -6],...
'Visible','off');
handles.rain = gobjects(1);
xlim(xlim) % lazy way of freezing axis limits
ylim(ylim)
zlim(zlim)
end
% randomly toggle on/off some window lights
p = 0.001; % portion (0:1) of lights to toggle
n = numel(handles.w);
idx = randperm(n,ceil(n*p));
colors = {handles.w(idx).FaceColor};
isnone = cellfun(@ischar, colors);
set(handles.w(idx(isnone)), 'FaceColor', 'y')
set(handles.w(idx(~isnone)), 'FaceColor', 'none')
% Show lightning on select frames
lightningFrames = [24 26 28 29 31:34];
onoff = ismember(f,lightningFrames);
handles.light.Visible = onoff;
handles.Z.Visible = onoff;
% Rain
if mod(f,2)>0 % update every odd frame
handles.rain.delete
xyRain0 = rand(3,300).*[26;26;10]-[0;0;2];
xyRain1 = xyRain0 + [-1;0;-2]; % orientation and length of rain lines
handles.rain = plot3([xyRain0(1,:);xyRain1(1,:)], ...
[xyRain0(2,:);xyRain1(2,:)], ...
[xyRain0(3,:);xyRain1(3,:)], ...
'color',[.7 .7 .7 .4]);
end
end
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13760
function drawframe(f)
persistent u n b M2 a2 a2_a r2
if f==1
u=@linspace;
n=@rescale;
b=@circshift;
q=400;
a=1.65;
m=0;
%sets up the colormap
g=[0.5373 0.1529 0.0824
0.6745 0.2353 0.1176
0.8706 0.3412 0.1647
0.9176 0.6353 0.2471
0.9843 0.9922 0.9725];
g=interp1(u(0,100,5),g,u(0,100,255));
g2=g-.15;
g2(g2<0)=0;
colormap([g;g2]);
% for the sunset have 2 different colourmaps for the background and the
% clouds then blend within each channel
%declare some functions and variables to save characters
X=u(1,255,200); %vector ranging between 1 and 255 200 long
X2=u(-1,1,400); %vector ranging between -1 and 1 400 long
[t2,r2]=cart2pol(X2,X2'+.1);%make radial gradient for the sun
M=n(-r2(1:200,:),1,280);%or for sun
X=u(-1,1,q);
[t,r]=cart2pol(X,X'); %create r matrix for filter in fourier transform
l1=d(r,a); %generate 3 cloud layers using filtered noise function
l2=-d(r,a);
l3=d(r,a);
i1=round(u(1,q,48)); %create 2 sets of indices for slow and fast moving cloud layers
i2=round(u(1,800,48));
[x,y] = meshgrid(0:399);
%smaller first constant creates wider cloud band (line below).
M3 = exp(-.007*(y-290).^2/4^2); % gaussian envelope in matrix to keep clouds in a specified band.
%pregenerate frames so all frames can be rescaled together
for k=1:48
%pairs of matrices define the pixelmap and the alpha
a1(:,:,1)=n(l1,-6,8); %first cloud layer doesn't move
a1_a(:,:,1)=n(l1,-6,8);
a1(:,:,2)=n(b(l2,i2(k),2),-6,8); %second cloud layer traverses the image once
a1_a(:,:,2)=n(b(l2,i2(k),2),-6,8);
a1(:,:,3)=n(b(-l3,i1(k),2),-6,8); %third cloud layer traverses the image twice
a1_a(:,:,3)=n(b(l3,i1(k),2),-6,8);
a2(:,:,k)=(max(a1,[],3)).*M3; %blend 3 cloud layers using a max function
a2_a(:,:,k)=(max(a1_a,[],3)).*M3; %blend 3 cloud layers using a max function
end
a2=n(abs(a2),-80,255);
a2_a=n(a2_a,0,1);
for k=1:48
M2(:,:,k)=M.*(1-(a2_a(201:400,:,k)))+(a2_a(201:400,:,k)).*a2(201:400,:,k); %blend alpha cloud layer with sky layer
end
%flip matrix and create sea currents using circshift
M2=n(M2,0,255);
end
D=flip(M2(:,:,f))+255;
% reflection from water
M4=M2(:,:,f);
M4(r2(1:200,:)<0.045)=255;%set sun colour
for k1=30:200
D(k1,197:203)=255+255-randi(80);
M4=[M4;b(D(k1,:),round(4*randn),2)];
end
image(M4)
axis equal
axis off
camva(4)
function c=d(r,a)
c=rescale(abs(ifft2(fftshift(fft2(randn(400))).*(r.^-a))),0,1).^1; %function to create clouds
end
end
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13187
function drawframe(f)
persistent h
if f==1 || isempty(h)
%% plot the city
h = struct(); % store graphics handles.
rng(0) % the scene is customized for seed=0 (twister)
set(gcf,'color','k')
h.ax = axes(Position=[0 0 1 1],colormap=gray,Color='k',Projection='perspective');
hold on
axis equal
h.ax.XTickLabel = '';
h.ax.YTickLabel = '';
h.ax.ZTickLabel = '';
xlim([-27.76, 88.76])
ylim([-4.5, 65.5])
zlim([0,10.7])
h.light = light(po=[-.7 .7 -1]);
campos([1.3,33,7]) % z=buildingHeights(33) % plot3(1.3, 33,7,'r*')
camtarget([40 33 3])
camva(70)
buildingHeights = randg(2,60); % building heights
h.bar = bar3(h.ax,buildingHeights);
set(h.bar,'EdgeColor','k')
% Loop through buildings
h.windows = gobjects([2,size(buildingHeights)]);
floorCount = nan(size(buildingHeights));
for i = 1:height(buildingHeights)
for j = 1:width(buildingHeights)
c = 0.1:0.2:buildingHeights(i,j)-.2;
floorCount(i,j) = numel(c);
d = [c;c];
z = d(:)'+[0;0;.1;.1];
y = i+[-.2,.2]+[-1;1;1;-1]/12;
y = repmat(y,size(c));
ison = logical(rand(1,numel(d))>0.3); % some window lights will be off
yellow = [1,1,.05];
p1 = patch(h.ax,z(:,ison)*0+j-.4,y(:,ison),z(:,ison),yellow);
p2 = patch(h.ax,z(:,~ison)*0+j-.4,y(:,~ison),z(:,~ison),yellow);
if ~isempty(p1)
h.windows(1,i,j) = p1;
end
if ~isempty(p2)
p2.FaceColor = 'none';
h.windows(2,i,j) = p2;
end
end
end
h.ax.XAxis.Visible = 'off';
h.ax.ZTick = 0:5:10;
h.ax.TickLength = [0 0];
grid(h.ax,'on')
h.ax.GridColor = 'm';
h.ax.GridAlpha = 1;
end
%% Animate construction
h.ax.GridAlpha = 1-f/48; % Fade grid
h.ax.CameraPosition(3) = f/48*7+2;
h.ax.CameraPosition(1) = max(40-f/40*38.7,1.3);
h.ax.CameraTarget(3) = 2.2 - f/48*3.9;
camva(50*f/48+30)
end
我提交的一个作品:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/13382
function drawframe(n)
global bl1 bl2 bl3 ml X1 X2 M
L=1600;H=1400;S=1000;R=400;F=550;
x1=@(x)x.*80+F+40.*(rand(1)-.5);
x2=@(x)x.*60+F+200+40.*(rand(1)-.5);
l=@linspace;
f=@fill;
if n==1
rng(12)
ax=gca;
ax.XColor='none';
ax.YColor='none';
ax.XLim=[0,L];
ax.YLim=[0,H];
ax.DataAspectRatio=[1 1 1];
hold on;
rgb=@(C)hsv2rgb(C./[360,100,100]);
cF=[225 35 70]; % 远山的颜色
cM=[210 70 10]; % 近山的颜色
cC=[250 26 43]; % 云的颜色
cS=[215 100 18]; % 天空的颜色
cN=[253,252,222]./255;
% 背景为远山的颜色 ==========================================================
ax.Color=rgb(cM);
% 画出天空颜色渐变效果 ======================================================
bp=rgb(cF)+l(0,1,S)'.*(rgb(cS)-rgb(cF));
ip=(1:S)'.*ones(1,L);
C=ind2rgb(ip,bp);
[X,Y]=meshgrid(1:L,401:H);
surf(X,Y,X.*0,'CData',C,'EdgeColor','none');
% 画出山脉效果 =============================================================
[X,Y]=meshgrid(l(0,1,S));
CLX=(-cos(X.*2.*pi)+1).^.2;
CLY=(-cos(Y.*2.*pi)+1).^.2;
r=(X-.5).^2+(Y-.5).^2;
% 8层山
for i=1:8
% 每次都生成一次二维随机噪声,并取其中一行的数据
h=abs(ifftn(exp(5i*rand(S))./r.^1.05)).*(CLX.*CLY).*24;
nh=(8-i)*30+h(500,:);
if i==1,nh=nh.*.8;end
hm=ceil(max(nh));
C=ones(hm,S);
% 颜色矩阵构造
tcol=cF+(cM-cF)./8.*(i);
tcol=rgb(tcol);
C=ind2rgb(C,tcol);
% 用nan数值框出山的轮廓
a=ones(hm,S);
a((1:hm)'>nh)=nan;
% 绘制山峰
image([-50,L+50],[R,R+hm],C,'AlphaData',a.*0.98);
end
% 绘制月亮和云朵 ===========================================================
t1=l(-pi/2,pi/2,100);
t2=l(pi/2,3*pi/2,100);
X1=cos(t1).*80;Y1=sin(t1).*80;
X2=cos(t2).*80;Y2=sin(t2).*80;
ml=f([X1,X2]+1350,[Y1,Y2]+1200,cN,'EdgeColor','none');
a=abs(ifftn(exp(3i*rand(S))./r.^.8)).*(CLX.*CLY);
a=a./max(a,[],'all');
C=ones(size(a));
C=ind2rgb(C,rgb(cC));
% 越向下、云越透明
dy=(1:S)./S.*0.8+0.2;
image([0,L],[800,H],C,'AlphaData',a.*(dy'));
bp=rgb(cF)+l(0,1,R)'.*(rgb(cM)-rgb(cF));
ip=(1:R)'.*ones(1,L);
C=ind2rgb(ip,bp);
[X,Y]=meshgrid(1:L,0:R);
surf(X,Y,X.*0,'CData',C,'EdgeColor','none');
% 绘制石头桥 ===============================================================
X=l(100,S,100);
Y=sin(l(0,pi,100));
f(X,Y.*150+R,[.1,.1,.1],'EdgeColor','none')
plot(X,Y.*150+R,'Color',[cN,.3],'LineWidth',1)
T=l(0,pi,100);
f(cos(T).*80+F,sin(T).*80+R,cN,'EdgeColor','none');
f(cos(T).*60+F+200,sin(T).*60+R,cN,'EdgeColor','none');
f(cos(T).*60+F-200,sin(T).*60+R,cN,'EdgeColor','none');
for i=1:7
y=(i-1)/6;
x=[-1,1].*sqrt(1-y.^2);
cp={'Color',cN,'LineWidth',1};
bl1(i)=plot(x1(x),R-[y,y].*90,cp{:});
bl2(i)=plot(x2(x),R-[y,y].*60,cp{:});
bl3(i)=plot(x2(x)-R,R-[y,y].*60,cp{:});
end
else
k=l(-1,1,48);
tX2=X2.*k(n);
ml.XData=[X1,tX2]+1350;
for i=1:7
y=(i-1)/6;
x=[-1,1].*sqrt(1-y.^2);
set(bl1(i),'XData',x1(x));
set(bl2(i),'XData',x2(x));
set(bl3(i),'XData',x2(x)-R);
end
end
end
由于这一周属实太忙,我交的很多作品都是原本代码的修改版本,在公众号或者知乎都找得到,因此就不都放代码了,大家感兴趣可以去投个票玩hiahiahia:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12797
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12777
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12757
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12742
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries/12787
比赛还有20天才结束,感兴趣的快去参加:
https://ww2.mathworks.cn/matlabcentral/communitycontests/contests/6/entries