• Remix
  • Share
  • New Entry

on 29 Nov 2023
  • 23
  • 27
  • 0
  • 1
  • 1181
drawframe(1);
Write your drawframe function below
function drawframe(f)
% Icy comet
persistent frms
if f == 1
% A problem with texturing a sphere is what to do about the poles. It is difficult
% to texture the [x, y, z] surface matrices created using the
% sphere command without getting significant anisotropy, pinching and/or
% discontinuities near the top and bottom.
%
% In this example a randomly sampled sphere will be generated, then uniform noise will be added,
% followed by smoothing using the the connectivity matrix relating adjacent points. The
% smoothing allows the noise distribution to be made more realistic and
% avoids the pole pinching artifacts & discontinuities.
% % % Creating the comet nodes
% Random sphere used here: replace with a fibonacci sphere for a different
% effect
% Predictable
rng(7,'twister');
% Number of vertices on Asteroid
n=5e4;
% Randomly sampled sphere
g=randn(3,n);
p=g./vecnorm(g);
% % % Determining connectivity between points
k=convhull(p');
c=@(x)sparse(k(:,x)*[1,1,1],k,1,n,n);
t=c(1)|c(2)|c(3);
% % % Create the roughness penalizer
w=spdiags(-sum(t,2)+1,0,t*1.); % t*1. is to convert t from logical to double
% % % Random radial distance function to be penalized:
r=rand(n,1);
% % % Solve for smoothed surface. Smoothing will be different for ice vs.
% rock components.
s=(speye(n)+1e3*w'*w)\r-.43;
s2=(speye(n)+3e3*w'*w)\r-.4; % Smoother for the ice component
s=s/mean(s);
s2=s2/mean(s2);
% % % Apply surface to vertices for rock & ice components
p1 = p.*s';
p2 = p.*s2';
% % % Plot.
T(1)=trisurf(k,p2(1,:),p2(2,:),p2(3,:),'FaceC', 'r', 'EdgeC', 'none','AmbientS',0,'DiffuseS',1,'SpecularS',0);
hold on;
T(2)=trisurf(k,p1(1,:),p1(2,:),p1(3,:), s,'EdgeC', 'none','AmbientS',0);
colormap(jet); % Use a colorful colormap
caxis([-1,1]*std(s)+1);
T(1).Parent.Parent.Position(3:4) = [1120, 840];
% % % For looping
H=hgtransform('Parent',gca);
H2=hgtransform('Parent',gca);
set(T,'parent',H);
% Add stars in the background
rd = rand(n,1);
C=scatter3(p(1,:)*50, p(2,:)*50, p(3,:)*50, rd*300, [1,1,1].*rd,'.');
set(C,'parent',H2);
% % % pretty
axis equal off % Pretty
set(gcf,'color','k');
camproj p
camva(6);
camtarget([0,0,0.3]);
light('position',[2,-3,10],'style','local');
light('position',[2,-3,10],'style','local','color',[1,1,1]*.3);
ags = linspace(0, pi/3, 48);
for n = 1:48
set(H, 'Matrix', makehgtform('xrotate',n/(16*pi)));
set(H2, 'Matrix', makehgtform('zrotate',n/(16*pi)));
camtarget([0,0,0.3]);
campos([21*sin(ags(n)), -21*cos(ags(n)), 0]);
frms{n}=getframe(gcf);
end
cnt = 1;
close
end
image(frms{f}.cdata);
axis equal off
camva(4.5);
% S.D.G.
end
Animation
Remix Tree