• Remix
  • Share
  • New Entry

on 3 Dec 2023
  • 17
  • 297
  • 0
  • 3
  • 1352
drawframe(20);
Write your drawframe function below
function drawframe(f)
% Inspired by: https://blogs.mathworks.com/matlab/2023/11/29/creating-natural-textures-with-power-law-noise-clouds-terrains-and-more/
% Create 2D power-law noise
rng(0,'twister')
k = 400;
m = randn(k); % k-by-k matrix
mf = fftshift(fft2(m));
% Filter1 (terrain & local storm conditions)
a = 2; % 1/fᵅ exponent - Brownian noise
d = ((1:k)-(k/2)-1).^2;
dd = sqrt(d(:) + d(:)');
filt = dd .^ -a; % frequency filter
filt(isinf(filt))=1; % replace +/-inf at DC or zero-frequency component
ff1 = mf .* filt;
% Filter2 (swell)
a = 3;
filt = dd .^ -a;
filt(isinf(filt))=1;
ff2 = mf .* filt;
% Construct the island
island = rescale(circshift(circshift(ifft2(ifftshift(ff1)),-k/8),-k/8,2),-1,1);
% Construct the wavefield (propagate waves, blend swell & local storm)
w2i_ratio = 0.2;
waves = rescale(circshift(circshift(ifft2(ifftshift(ff1)),-round(k*f/48)),round(k*f/48),2)*1+circshift(circshift(ifft2(ifftshift(ff2)),round(k*f/48)),round(k*f/48),2)*0.75,-w2i_ratio,w2i_ratio);
% Construct the cloud shade on the island (the wind blows towards the prevalent wave direction)
cloudshade = rescale(circshift(circshift(ifft2(ifftshift(ff1)),-round(k*f/48)-k/8),round(k*f/48)-k/8,2),-0.35,0.35);
x = linspace(-5, 5, width(island));
y = linspace(-5, 5, height(island));
island(island<waves) = NaN;
surf(x*0.8,y*0.8,island,'EdgeColor','none')
hold on
S_waves = surf(x,y,waves,'FaceColor','b','EdgeColor','none');
% Add wave breaking effect
whitefoam = waves;
whitefoam(whitefoam<=0.8*w2i_ratio) = NaN;
surf(x,y,whitefoam,'FaceColor','w','FaceAlpha',0.5,'EdgeColor','none')
whitefoam = waves;
whitefoam(whitefoam<=0.7*w2i_ratio|whitefoam>=0.9*w2i_ratio) = NaN;
surf(x,y,whitefoam,'FaceColor','c','FaceAlpha',0.4,'EdgeColor','none')
% Cast cloud shading on the island & apply edge effect
surf(x*0.875,y*0.875,island,'EdgeColor','none','FaceColor',[1 1 1]*0.25,'AlphaData',cloudshade,'FaceAlpha','flat');
hold off
demcmap(0)
view(0,35)
camva(3);
% Add lighting
light(Position = [-0.5 0.5 0.75])
material dull
S_waves.FaceLighting='gouraud';
axis('tight','equal','off')
end
Animation
Remix Tree