Unity 3D uses heightmap to create terrain || Unity 3D uses brush to draw terrain

Unity 3D uses heightmap to create terrain

There are two ways to edit terrain in Unity 3D:

  • Edit terrain with the terrain editor.

  • Quickly model terrains by importing a pre-rendered grayscale image.

The height of each point on the terrain is represented as a column of values ​​in a matrix. heightmapThis matrix can be represented by a grayscale image called a heightmap ( ).

A grayscale image is a picture that uses two-dimensional graphics to represent three-dimensional height changes. Darker colors, closer to black, represent lower points, and lighter colors, closer to white, represent higher points.

Usually, Photoshop or other 3D software can be used to export the grayscale image, and the format of the grayscale image is a  grayscale image that RAW 格式Unity 3D can support  16 位.

Unity provides options to import and export heightmaps for terrains.

  • Click  Settings tool the button and find the button labeled  Import RAWand  Export RAW .

  • These two buttons allow  RAW 格式reading or writing heightmaps from standard and are compatible with most image and surface editors.

case idea

RAW 格式Heightmap import supported in Unity 3D  .

RAW 格式It is equivalent to the "source file" of various picture formats, and its conversion is irreversible.

Note:

  • In Photoshop software, the filter function can be used to make height maps.

In this case, the terrain is automatically generated by importing the height map made in Photoshop into the Unity 3D system.

case design

In this case, create a simple terrain in Unity 3D through the height map made in Photoshop, set the information of the imported height map in the terrain parameter list, and the terrain effect of the imported height map.

Case implementation

  1. Create terrain. Execute  GameObject the →  3D Object →  Terrain menu command.

  1. Click Add Terrain in the Inspector panel  Import Raw 按钮, as shown in the image below.

  1. Set terrain parameters.

  1. After testing, you can observe the terrain effect created based on the height map.

parameter list

parameter meaning describe
Depth depth Depending on the file format setting, it can be 8-bit or 16-bit.
Width width Sets the width of the heightmap.
Height high Set the height of the heightmap.
Byte Order byte order Can be Macor , depending on the file format setting Windows.
Terrain Size terrain size Defines the size of the terrain.

Unity 3D uses brushes to draw terrain 

In Unity 3D, in addition to using the height map to create terrain, you can also use brushes to draw terrain.

The initial surface just created is just one huge flat surface. Unity 3D provides some tools summarized in the terrain menu bar, which can be used by game developers to create many surface elements on this plane.

There are 7 buttons in the terrain menu bar, the meanings are

  • Edit terrain height.

  • Edit terrain specific heights.

  • Smooth transition terrain.

  • terrain map.

  • Add a tree model.

  • Added grass and mesh models.

  • other settings.

As shown in the figure below, each button can activate the corresponding submenu to operate and edit the terrain.

Terrain Height Rendering

In the Terrain Editor, the first 3 tools are used to plot terrain changes in height.

The first button on the left is active  Raise/Lower Height 工具, as shown in the figure below.

When using this tool, the height will increase as the mouse is swept over the terrain. If the mouse is fixed in one place, the height will gradually increase. This is similar to the sprayer tool in image editors.

If pressed during mouse operation  Shift 键, the height will be lowered. Different brushes can be used to create different effects.

For example:

  • When creating hilly terrain,  soft-edged height elevation can be done with the brush.

  • And for steep peaks and valleys, use  hard-edged a brush for height cuts.

  1. Paint Height, similar to  Raise/Lower 工具, but with one more attribute  Height, which is used to set the target height, as shown in the figure below (the second one on the left).

  • When painting on a terrain object, areas above this height will drop and areas below will rise.

  • Shift + 单击 Game developers can use the height property to set the height manually, or they can use the height of the mouse position to be sampled on the terrain  .

  • Next to the height property is one  Flatten 按钮that simply levels the entire terrain to the selected height, useful for setting a raised horizon.

  • Paint HeightThe painted terrain contains parts above and below the horizon, such as creating plateaus in the scene and adding artificial elements - roads, platforms and steps, etc. are convenient.

  1. Smooth Height Does not significantly raise or lower terrain height, but averages out nearby areas. This softens the surface and reduces steep changes, similar to the blur tool in image processing.

Smooth Height Can be used to moderate sharp, rough rocks on the surface. The parameter list is as follows:

parameter meaning describe
Brushes the brush Sets the style of the brush.
Setting set up
Brushes Size brush size Sets the size of the brush.
Opacity Opacity Sets the height at which the brush draws.
Height high Sets the numerical value of the drawing height.

Terrain texture painting

Texture images can be added to the surface of the terrain to create color and fine detail.

Since terrain is such a huge object, it is standard practice in practice to use a texture that repeats without gaps (i.e. continuously) and overlay it in patches on the surface. Different regions of the texture can be painted to simulate different ground , such as grassland, desert and snow.

The drawn textures can be used with different opacities, so that gradients can be formed between different terrain textures, and the effect is more natural.

The fourth button on the left of the terrain editor is a texture drawing tool, click this button and execute it in the menu  Add Texture 命令, you can see a window in which you can set a texture and its properties.

The first texture added will be used as a background to cover the terrain. If you want to add more textures, you can use the brush tool to achieve different texture textures by setting the brush size, transparency and Target Strength options, as shown in the figure below.

parameter list:

parameter meaning describe
Brushes the brush Sets the style of the brush.
Setting set up
Opacity Opacity Sets the height at which the brush draws.
Textures texture Sets the image style for drawing terrain textures.
Brush Size brush size Sets the size of the brush used to paint the texture.
Target Strength target strength Sets the influence of painted map textures.

trees drawing

Unity 3D terrains can be placed with trees. Trees can be painted onto terrain like heightmaps and textures, but trees are fixed, 3D objects that grow from surfaces.

Unity 3D uses optimizations (for example, applying a billboard effect to distant trees) to ensure good rendering, so a terrain can have a dense forest of thousands of trees while maintaining an acceptable frame rate.

Click  Edit Trees 按钮and select and  Add Tree 命令a window will pop up from which to choose a tree resource.

  • When a tree is selected, the tree can be drawn on the surface by drawing a texture or a height map. Press and hold  Shift 键to remove the tree from the area, and hold to  Ctrl 键only draw or remove the currently selected tree. The tree drawing panel is shown in the figure below.

  • parameter list:
parameter meaning describe
Tree Density tree density Sets the spacing of the trees.
Setting set up
Tree Height tree height Set the height of the tree, check it Random选项, and the effect that the height of the tree changes randomly within the specified range can appear.
Lock Width to Height Lock the aspect ratio of trees Lock the tree aspect ratio.
Brush Size brush size Sets the size of the brush when planting trees.
Tree Width tree width Set the width of the tree, check it Random选项, and the effect that the width of the tree changes randomly within the specified range can appear.
Random Tree Rotation random rotation of trees Sets the tree to randomly rotate by a certain angle.

grass and other details

A terrain surface can have grass and other small objects such as stones covering the surface. Grass is rendered using a 2D image to represent tussocks, while other details are generated from a standard mesh.

Click in the terrain editor  Edit Details 按钮, and in the menu that appears, you will see  Add Grass Texture and  Add Detail Mesh 选项, select  Add Grass Texture, and select the appropriate grass resource in the window that appears, as shown in the figure below.

parameter list:

parameter meaning describe
Detail Texture detail texture Specifies the image as the grass texture.
Min Width minimum width Sets the minimum width value for grass.
Max Width maximum width Sets the maximum width value for the grass.
Min Height minimum height Sets the minimum height value for grass.
Max Height maximum height Sets the maximum height value for grass.
Noise Spread Noise range Controls the size of the grass spawn clumps.
Healthy Color healthy color Sets the healthy color of the grass, which is more noticeable in the center of the noise.
Dry Color dry color Sets the dry color of the grass, which is more noticeable in the center of the noise.
Billboard billboard The grass will rotate in sync with the camera, always facing the camera.

terrain settings

Click the button on the far right of the terrain editor to open the terrain settings panel, as shown in the figure below. This panel is used to set terrain parameters as shown in the table below.

Basic terrain parameters:

parameter meaning describe
Draw draw Draw the terrain.
Pixel Error pixel tolerance The pixel tolerance allowed when displaying terrain meshes.
Base Map Dist Base map distance Sets the resolution for terrain heights.
Cast Shadows projection Sets whether the terrain has a projection.
Material material Add materials to the terrain.

Tree and detail parameters:

parameter meaning describe
Draw draw Sets whether to render objects other than terrain.
Detail Distance detail distance 设置摄像机停止对细节渲染的距离。
Detail Density 细节密度 设置细节密度。
Tree Distance 树木距离 设置摄像机停止对树进行渲染的距离。
Billboard Start 开始广告牌 设置摄像机将树渲染为广告牌的距离。
Fade Length 渐变距离 控制所有树的总量上限。
Max Mesh Trees 网格渲染树木最大数量 设置使用网格形式进行渲染的树木最大数量。

风参数:

参数 含义 描述
Speed 速度 风吹过草地的速度。
Size 大小 同一时间受到风影响的草的数量。
Bending 弯曲 设置草跟随风弯曲的强度。
Grass Tint 草的色调 设置地形上的所有草和细节网格的总体渲染颜色。

风域

地形中的草丛在运行测试时可以随风摆动,如果要实现树木的枝叶如同现实中一样随风摇摆的效果,就需要加入风域。

执行 GameObject → 3D Object → Wind Zone 菜单命令,创建一个风域,风域的参数如下图所示。

参数列表:

参数 含义 描述
Mode 风域模式 设置风域模式:Directional模式下整个场景中的树木都受影 响,Spherical模式下只影响球体包裹范围内的树木。
Main 主风 设置主要风力,产生风压柔和变化。
Turbulence 湍流 设置湍流风,产生一个瞬息万变的风压。
Pulse Magnitude 波动幅度 定义风力随时间的变化。
Pulse Frequency 波动频率 定义风向改变的频率。

风域不仅能实现风吹树木的效果,还能模拟爆炸时树木受到波及的效果。

需要注意的是,风域只能作用于树木,对其他游戏对象没有效果。场景中不同模式下的风域参数设置如下表所示。

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/128620541