沙纹代码分享!程序生成波浪沙丘,简洁优雅适配沙漠场景

作者分享了一段简单的沙纹代码,能通过程序生成波浪形的沙丘,代码本身既简洁又优雅,很适合应用在沙漠场景中。
Here’s the commented code that makes the ripples in the animation.
N=400; % Tile edge size (pixels)
H=randn(N); % Initial conditions (random)
y=1:N; % Helper vector
c=@(x,y)ifft2(fft2(x).*fft2(y)); % Circular convolution function
k=c(H>3.8,y<29&y'<29)<1; % No flow mask
for n=1:3e3 % Ripple evolution loop
d=gradient(H); % Gradient for grain size & hop
g=(d+1)/6; % Sand lost
m=mod(round(y+randn(N).k4),N)+1; % Hop destination (x)
o=mod(round(y’+(9-9*d).*k),N)+1; % Hop destination (y)
H=c(H-g+accumarray([o(:),m(:)],g(:),[N,N]),(y<4&y'<4)/9); % Migrate
H=circshift(H,[-1,-1]); % Offset correction for convolution
%% << insert your visualize of dune array H here >>
end

(reddit:Timuu5)

请登录后发表评论

    没有回复内容