3D rough terrain generation with Matlab

2 views (last 30 days)
ARIFF HAIKAL
ARIFF HAIKAL on 9 Oct 2019
Edited: Abhishek on 19 Jun 2025
Dear friends,
how can i create a 3D rough terrain generation with Matlab ? (x,y,z coordinates with height)
or any files/tutorials/source code which may help me .
Thank you. :)
  3 Comments
ARIFF HAIKAL
ARIFF HAIKAL on 9 Oct 2019
Edited: ARIFF HAIKAL on 9 Oct 2019
yes sir . with x,y and z coordinates .
D.Nivedh Kumar
D.Nivedh Kumar on 13 Jun 2025
Edited: Mathieu NOE on 17 Jun 2025
% generateUnevenTerrainSolid.m
% Author: Nivedh verified 🛠
% Final version – No face-index error
% -------------------------
% PARAMETERS
% -------------------------
Lx = 2.0; % Length (m)
Ly = 2.0; % Width (m)
H = 0.1; % Max bump height (m)
T = 0.05; % Thickness below zero
N = 60; % Grid points (>= 2)
% -------------------------
% CREATE MESH GRIDS
% -------------------------
[x, y] = meshgrid(linspace(-Lx/2, Lx/2, N), linspace(-Ly/2, Ly/2, N));
zTop = H * sin(2*pi*x/Lx) .* cos(2*pi*y/Ly);
zBot = -T * ones(size(zTop));
% -------------------------
% TOP + BOTTOM PATCHES
% -------------------------
s1 = surf(x, y, zTop, 'EdgeColor','none'); fvTop = surf2patch(s1, 'triangles'); delete(s1);
s2 = surf(x, y, zBot, 'EdgeColor','none'); fvBot = surf2patch(s2, 'triangles'); delete(s2);
V_top = fvTop.vertices;
F_top = fvTop.faces;
V_bot = fvBot.vertices;
F_bot = fvBot.faces + size(V_top,1); % offset indices for bottom
% -------------------------
% GENERATE SIDE WALLS
% -------------------------
rows = N; cols = N;
V_all = [V_top; V_bot];
F_sides = [];
idx = @(r,c) (c-1)*rows + r;
nTop = size(V_top,1);
% FRONT wall (Y = min)
for i = 1:rows-1
v1 = idx(i,1);
v2 = idx(i+1,1);
v3 = v2 + nTop;
v4 = v1 + nTop;
F_sides = [F_sides; v1 v2 v3; v1 v3 v4];
end
% BACK wall (Y = max)
for i = 1:rows-1
v1 = idx(i,cols);
v2 = idx(i+1,cols);
v3 = v2 + nTop;
v4 = v1 + nTop;
F_sides = [F_sides; v1 v2 v3; v1 v3 v4];
end
% LEFT wall (X = min)
for j = 1:cols-1
v1 = idx(1,j);
v2 = idx(1,j+1);
v3 = v2 + nTop;
v4 = v1 + nTop;
F_sides = [F_sides; v1 v2 v3; v1 v3 v4];
end
% RIGHT wall (X = max)
for j = 1:cols-1
v1 = idx(rows,j);
v2 = idx(rows,j+1);
v3 = v2 + nTop;
v4 = v1 + nTop;
F_sides = [F_sides; v1 v2 v3; v1 v3 v4];
end
% -------------------------
% COMBINE AND EXPORT STL
% -------------------------
V_all_mm = V_all * 1000; % Convert to mm for STL
F_all = [F_top; F_bot; F_sides];
TR = triangulation(F_all, V_all_mm);
stlwrite(TR, 'unevenTerrainSolid.stl');
disp("✅ STL exported successfully.");

Sign in to comment.

Answers (1)

Abhishek
Abhishek on 18 Jun 2025
Edited: Abhishek on 19 Jun 2025
To create a 3D rough terrain in MATLAB, you can start by generating a grid using ‘meshgrid’ and then apply random noise (like ‘randn’) to simulate height variation.
You can also try smoothing it with a Gaussian filter (using ‘imgaussfilt’). This helps produce a more natural terrain appearance.
For visualization, ‘surf’ works well, and you can use light colormaps like ‘summer’ or ‘pink’ for a softer look. Enhancing the rendering with lighting ‘gouraud’ , ‘material dull’ , and ‘camlight’ can also improve the visual effect.
To help illustrate the idea, here's a minimal example I tried on my end.
n = 100;
[x, y] = meshgrid(linspace(-10, 10, n));
z = imgaussfilt(randn(n), 2);
surf(x, y, z, 'EdgeColor', 'none');
colormap(summer);
material dull;
camlight headlight;
lighting gouraud;
  • I started off by defining the resolution of the terrain, in this case, it is defined using ‘n’. So ‘nxn’ points would be used.
  • Created a 2D grid of ‘X’ and ‘Y’ coordinates over the range -10 to 10, using ‘meshgrid’.
  • Used ‘randn(n)’ to generate a matrix of random height values by normal distribution, followed by smoothing the height values using a Gaussian FIlter with a standard deviation of 2.
  • Finally plotted using the ‘surf’ function.
I ran this approach on MATLAB R2024b, and below is the result:
You can adjust the height of the terrain by tweaking the standard deviation value passed in the ‘imgaussfilt()’ method. For this approach, it was set to 2. If you need to decrease the height followed by more smooth terrain, you can try increasing the standard deviation value.
You can find additional information about ‘meshgrid’, ‘imgaussfilt’ and ‘surf’ in the official documentation of MATLAB:
Hope it helps you.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!