3D rough terrain generation with Matlab

조회 수: 8 (최근 30일)
ARIFF HAIKAL
ARIFF HAIKAL 2019년 10월 9일
편집: Abhishek 2025년 6월 19일
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
ARIFF HAIKAL
ARIFF HAIKAL 2019년 10월 9일
편집: ARIFF HAIKAL 2019년 10월 9일
yes sir . with x,y and z coordinates .
D.Nivedh Kumar
D.Nivedh Kumar 2025년 6월 13일
편집: Mathieu NOE 2025년 6월 17일
% 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.");

댓글을 달려면 로그인하십시오.

답변 (1개)

Abhishek
Abhishek 2025년 6월 18일
편집: Abhishek 2025년 6월 19일
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,imgaussfiltand surf in the official documentation of MATLAB:
Hope it helps you.

카테고리

Help CenterFile Exchange에서 Data Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by