Skip to content

Instantly share code, notes, and snippets.

@OrganicIrradiation
Created March 23, 2015 10:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save OrganicIrradiation/2a927ab7f9cfeb1bff78 to your computer and use it in GitHub Desktop.
Save OrganicIrradiation/2a927ab7f9cfeb1bff78 to your computer and use it in GitHub Desktop.
2D and 3D Perlin Noise in MATLAB
s = (cos(s * pi - pi) + 1) / 2;
function s = noise3D (m,k)
s = zeros([m,m,m]);
w = m;
i = 0;
while w > 3
i = i + 1;
d = smooth3(randn([m,m,m]), 'gaussian',k,i);
s = s + i * d(1:m, 1:m, 1:m);
w = w - ceil(w/2 - 1);
end
s = (s - min(min(s(:,:)))) ./ (max(max(s(:,:))) - min(min(s(:,:))));
end
s = (s - min(min(s(:,:)))) ./ (max(max(s(:,:))) - min(min(s(:,:))));
function s = perlin (m)
s = zeros(m); # output image
w = m; # width of current layer
i = 0; # iterations
while w > 3
i = i + 1;
d = interp2(randn(w), i-1, "spline");
s = s + i * d(1:m, 1:m);
w -= ceil(w/2 - 1);
end
end
function s = perlin2D (m)
s = zeros([m,m]); % Prepare output image (size: m x m)
w = m;
i = 0;
while w > 3
i = i + 1;
d = interp2(randn([m,m]), i-1, 'spline');
s = s + i * d(1:m, 1:m);
w = w - ceil(w/2 - 1);
end
s = (s - min(min(s(:,:)))) ./ (max(max(s(:,:))) - min(min(s(:,:))));
end
function s = perlin3D (m)
s = zeros([m,m,m]); % Prepare output image (size: m x m x m)
w = m;
i = 0;
while w > 3
i = i + 1;
d = interp3(randn([m,m,m]), i-1, 'spline');
s = s + i * d(1:m, 1:m, 1:m);
w = w - ceil(w/2 - 1);
end
s = (s - min(min(s(:,:)))) ./ (max(max(s(:,:))) - min(min(s(:,:))));
end
@SCLeoX
Copy link

SCLeoX commented Jun 8, 2018

I am afraid that this is probably value noise instead of Perlin noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment