This page has been translated automatically.
Видеоуроки
Интерфейс
Основы
Продвинутый уровень
Подсказки и советы
Основы
Программирование на C#
Рендеринг
Профессиональный уровень (SIM)
Принципы работы
Свойства (properties)
Компонентная Система
Рендер
Физика
Редактор UnigineEditor
Обзор интерфейса
Работа с ассетами
Контроль версий
Настройки и предпочтения
Работа с проектами
Настройка параметров ноды
Setting Up Materials
Настройка свойств
Освещение
Sandworm
Использование инструментов редактора для конкретных задач
Расширение функционала редактора
Встроенные объекты
Ноды (Nodes)
Объекты (Objects)
Эффекты
Декали
Источники света
Geodetics
World-ноды
Звуковые объекты
Объекты поиска пути
Player-ноды
Программирование
Основы
Настройка среды разработки
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Плагины
Форматы файлов
Материалы и шейдеры
Rebuilding the Engine Tools
Интерфейс пользователя (GUI)
Двойная точность координат
API
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
Работа с контентом
Оптимизация контента
Материалы
Визуальный редактор материалов
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Учебные материалы

Работа с Landscape Terrain из кода

This article describes how to create and modify the Landscape Terrain object via code. But before we get down to coding, let's start with a bit of theory.В этой статье описывается, как создать и изменить объект Landscape Terrain с помощью кода. Но прежде чем мы перейдем к программированию, немного теории.

The surface of Landscape Terrain (ObjectLandscapeTerrain class) is represented by a single or multiple rectangular layers called Landscape Layer Maps (LandscapeLayerMap class). By creating and arranging layer maps you define the look and functionality of the terrain.Поверхность Landscape Terrain (класс ObjectLandscapeTerrain) представлена одним или несколькими прямоугольными слоями, называемыми Картами слоев ландшафта (класс LandscapeLayerMap). Создавая и упорядочивая карты слоев, вы определяете внешний вид и функциональность ландшафта.

To see a terrain, at least one Landscape Layer Map is required. Data of each Landscape Layer Map (heights, albedo, and masks) is stored in an .lmap file. To create such a file the LandscapeMapFileCreator class is used. If you want to load, modify, and apply settings stored in this file later, use the LandscapeMapFileSettings class.Чтобы увидеть ландшафт, требуется по крайней мере один Landscape Layer Map. Данные каждой карты слоя ландшафта (высоты, альбедо и маски) хранятся в файле .lmap. Для создания такого файла используется класс LandscapeMapFileCreator. Если вы хотите загрузить, изменить и применить настройки, сохраненные в этом файле позже, используйте класс LandscapeMapFileSettings.

Landscape Terrain rendering and modification is managed via the Landscape class.Управление рендерингом и модификацией Landscape Terrain осуществляется с помощью класса Landscape.

There is a set of API classes used to manage the Landscape Terrain object:Существует набор классов API, используемых для управления объектом Landscape Terrain:

  • ObjectLandscapeTerrain — managing general Landscape Terrain object parameters.ObjectLandscapeTerrain — управление общими параметрами объекта Landscape Terrain.
  • TerrainDetail — managing terrain details that define its appearance. Details are organized into a hierarchy, each of them can have an unlimited number of children. Details are attached to detail masks and are drawn in accordance with their rendering order (the one with the highest order shall be rendered above all others).TerrainDetail — управление деталями ландшафта, которые определяют его внешний вид. Детали организованы в иерархию, каждая из них может иметь неограниченное количество дочерних элементов. Детали привязаны к маскам деталей и рисуются в соответствии с порядком их отображения (деталь с самым высоким порядком отображается поверх всех остальных).
  • TerrainDetailMask — managing terrain detail masks. Each detail mask can have an unlimited number of details.TerrainDetailMask — управление масками деталей ландшафта. Каждая маска деталей может содержать неограниченное количество деталей.
  • LandscapeFetch — getting terrain data at a certain point (e.g. a height request) or check for an intersection with a traced line.LandscapeFetch — получение данных о ландшафте в определенной точке (например, запрос высоты) или проверка пересечения с линией трассировки.
  • LandscapeImages — to edit landscape terrain via API.LandscapeImages — для редактирования ландшафта через API.
  • LandscapeTextures — to edit landscape terrain via API.LandscapeTextures — для редактирования ландшафта через API.

See Also
Смотрите также
#

  • C++ Sample set demonstrating various Landscape Terrain features and use casesНабор примеров C++ Samples, демонстрирующий функционал и способы примения Landscape Terrain

Creating a Terrain
Создание ландшафта
#

To create a Landscape Terrain based on arbitrary height and albedo maps, the following workflow is used:Для создания ландшафта на основе произвольных карт высоты и альбедо нужно выполнить следующие операции:

  1. Create a LandscapeMapFileCreator instance, set necessary parameters (grid size, resolution, etc.) and generate a new .lmap file based on specified albedo and height images (tiles) via the Run() method. Here you can subscribe for events at different stages of creation.Создайте экземпляр LandscapeMapFileCreator, задайте необходимые параметры (размер сетки, разрешение и т. д.) и сгенерируйте новый файл .lmap на основе заданных изображений альбедо и высоты (тайлов) с помощью метода Run(). Здесь вы можете добавить обратные вызовы для разных этапов создания.
  2. Create a LandscapeMapFileSettings instance, load target .lmap file for settings, set necessary parameters (opacity and blending for height and albedo data) and apply them.Создайте экземпляр LandscapeMapFileSettings, загрузите целевой файл .lmap для настроек, установите необходимые параметры (непрозрачность и смешивание для данных высоты и альбедо) и примените их.
  3. Create a new ObjectLandscapeTerrain object.Создайте новый объект ObjectLandscapeTerrain.
  4. Create a LandscapeLayerMap based on the previously created .lmap file.Создайте LandscapeLayerMap на основе ранее созданного файла .lmap.

Preparing a Project
Подготовка проекта
#

Before we get to code, perform the following:Прежде чем мы перейдем к программированию, выполните следующее:

  1. Open SDK Browser and create a new C# (.NET) or C++ project depending on the programming language selected.Откройте SDK Browser и создайте новый проект C# (.NET) или C++ в зависимости от выбранного языка программирования.
  2. Open your project in UnigineEditor via the Open Editor button in SDK Browser.Откройте свой проект в UnigineEditor с помощью кнопки Open Editor в SDK Browser.
  3. Save the following images to your computer:Сохраните следующие изображения на свой компьютер:

    Карта альбедо Карта высот
    Карта альбедо Карта высот
  4. Drag the height.png file directly to the Asset Browser window to add it to your project. In the Import Dialog for your height map, set Image Format to R32F and click Yes.Перетащите файл height.png непосредственно в окно Asset Browser, чтобы добавить его в свой проект. В диалоговом окне импорта для вашей карты высот установите для Image Format значение R32F и нажмите Yes.
  5. Drag the albedo.png file directly to the Asset Browser window too. In the Import Dialog for your height map, set Texture Preset to Albedo (RGB — color, A — opacity) and click Yes.Перетащите файл albedo.png также непосредственно в окно Asset Browser. В диалоговом окне импорта для вашей карты высот установите для Texture Preset значение Albedo (RGB — color, A — opacity) и нажмите Yes.

Code
Код
#

Copy the source code below implemented as a C# component, save it to the LandscapeGenerator.cs file, create a new Dummy Node and assign the component to it.Скопируйте приведенный ниже исходный код, реализованный как компонент C#, сохраните его в файле LandscapeGenerator.cs, создайте новый Dummy Node и назначьте на него этот компонент.

LandscapeGenerator.cs LandscapeGenerator.cs

Исходный код (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec2 = Unigine.dvec2;
#else
	using Vec3 = Unigine.vec3;
	using Vec2 = Unigine.vec2;
#endif
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class LandscapeGenerator : Component
{
	[ShowInEditor]
	private Vec3 lmapPosition = Vec3.ZERO;

	[ShowInEditor]
	private float lmapRotation = 0.0f;

	[ShowInEditor]
	public Vec2 lmapSize = new Vec2(20.0f, 20.0f);

	[ShowInEditor]
	[ParameterSlider(Min = 0.0f)]
	private float lmapHeightScale = 1.0f;

	[ShowInEditor]
	[ParameterSlider(Min = 1)]
	private int lmapGridSizeX = 2;

	[ShowInEditor]
	[ParameterSlider(Min = 1)]
	private int lmapGridSizeY = 2;

	[ShowInEditor]
	[ParameterSlider(Min = 1)]
	private int lmapTileResolutionX = 512;

	[ShowInEditor]
	[ParameterSlider(Min = 1)]
	private int lmapTileResolutionY = 512;

	[ShowInEditor]
	private string lmapName = "map";

	public struct TileImages
	{
		[ParameterFile]
		public string albedoImagePath;

		[ParameterFile]
		public string heightImagePath;
	}

	[ShowInEditor]
	private List<TileImages> tiles = null;

	private ObjectLandscapeTerrain terrain = null;
	private LandscapeLayerMap lmap = null;

	private void Init()
	{
		Unigine.Console.Run("console_onscreen 1");
		if (tiles == null)
		{
			Log.Error("Fill the array with tiles.\n");
			return;
		}

		if (tiles.Count != lmapGridSizeX * lmapGridSizeY)
		{
			Log.Error("The count of tiles does not match the current grid size.\n");
			return;
		}

		if (string.IsNullOrEmpty(lmapName))
			lmapName = "map";

		// create .lmap file based on tiles with albedo and height images
		var lmapCreator = new LandscapeMapFileCreator();
		lmapCreator.Grid = new ivec2(lmapGridSizeX, lmapGridSizeY);
		lmapCreator.Resolution = new ivec2(lmapTileResolutionX * lmapGridSizeX, lmapTileResolutionY * lmapGridSizeY);
		lmapCreator.Path = lmapName + ".lmap";

		// subscribe for different stages of creation
		lmapCreator.EventCreate.Connect(OnCreatorCreate);
		lmapCreator.EventBegin.Connect(OnCreatorBegin);
		lmapCreator.EventProgress.Connect(OnCreatorProgress);
		lmapCreator.EventEnd.Connect(OnCreatorEnd);

		// start the creation process
		lmapCreator.Run();
	}

	private void OnCreatorCreate(LandscapeMapFileCreator creator, LandscapeImages images, int x, int y)
	{
		// get number of the current tile
		int tileNumber = x * lmapGridSizeY + y;
		Log.Message($"Create tile {tileNumber}\n");

		// set albedo for current tile
		if (FileSystem.IsFileExist(tiles[tileNumber].albedoImagePath))
		{
			Image albedoImage = new Image(tiles[tileNumber].albedoImagePath);
			if (albedoImage && albedoImage.Width == lmapTileResolutionX && albedoImage.Height == lmapTileResolutionY)
			{
				Image albedo = images.GetAlbedo();
				albedo.Create2D(albedoImage.Width, albedoImage.Height, albedoImage.Format, albedoImage.NumMipmaps);
				albedo.Copy(albedoImage, 0, 0, 0, 0, albedoImage.Width, albedo.Height);
			}
			else
				Log.Error("The albedo image cannot be loaded, or its resolution does not match the resolution of tile.\n");
		}
		else
			Log.Error("Albedo file does not exist.\n");

		// set height for current tile
		if (FileSystem.IsFileExist(tiles[tileNumber].heightImagePath))
		{
			Image heightImage = new Image(tiles[tileNumber].heightImagePath);
			if (heightImage && heightImage.Width == lmapTileResolutionX && heightImage.Height == lmapTileResolutionY)
			{
				Image height = images.GetHeight();
				height.Create2D(heightImage.Width, heightImage.Height, heightImage.Format, heightImage.NumMipmaps);
				height.Copy(heightImage, 0, 0, 0, 0, heightImage.Width, height.Height);
			}
			else
				Log.Error("The height image cannot be loaded, or its resolution does not match the resolution of tile.\n");
		}
		else
			Log.Error("Height file does not exist.\n");
	}

	private void OnCreatorBegin(LandscapeMapFileCreator creator)
	{
		Log.Message("--------------------\n");
		Log.Message($"--- {creator.Path} creation started ---\n");
		Log.Message("lmap creator begin\n");
	}

	private void OnCreatorProgress(LandscapeMapFileCreator creator)
	{
		Log.Message($"lmap creator progress: {creator.Progress}\n");
	}

	private void OnCreatorEnd(LandscapeMapFileCreator creator)
	{
		Log.Message("lmap creator end\n");
		Log.Message($"--- {creator.Path} created ---\n");
		Log.Message("--------------------\n");

		// after creating .lmap file apply settings
		ApplySettings();

		// and create terrain
		CreateTerrain();
	}

	private void ApplySettings()
	{
		// load target .lmap file for settings
		LandscapeMapFileSettings settings = new LandscapeMapFileSettings();
		settings.Load(FileSystem.GetGUID(lmapName + ".lmap"));

		// set parameters and apply them
		if (settings.IsLoaded)
		{
			// set alpha blend for height and albedo
			settings.HeightBlending = Landscape.BLENDING_MODE.ALPHA_BLEND;
			settings.AlbedoBlending = Landscape.BLENDING_MODE.ALPHA_BLEND;

			settings.EnabledHeight = true;
			settings.EnabledAlbedo = true;

			// disable opacity for height and albedo
			settings.EnabledOpacityAlbedo = false;
			settings.EnabledOpacityHeight = false;

			settings.Apply();
		}
	}

	private void CreateTerrain()
	{
		// create new terrain
		terrain = new ObjectLandscapeTerrain();
		terrain.ActiveTerrain = true;
		terrain.SetCollision(true, 0);

		// create layer map based on created .lmap file
		lmap = new LandscapeLayerMap();
		lmap.Parent = Landscape.GetActiveTerrain();
		lmap.Path = lmapName + ".lmap";
		lmap.Name = lmapName;
		lmap.Size = lmapSize;
		lmap.HeightScale = lmapHeightScale;
		lmap.WorldPosition = lmapPosition;
		lmap.SetWorldRotation(new quat(vec3.UP, lmapRotation));
	}

}

As the component is assigned, configure its settings in the Parameters window:После назначения компонента настройте его параметры в окне Parameters:

LandscapeGenerator component settings

Set the number of tiles in the corresponding field and drag albedo.png and height.png files to the Albedo Image Path and Height Image Path fields of the component.Установите количество тайлов в соответствующем поле и перетащите файлы albedo.png и height.png в поля Albedo Image Path и Height Image Path компонента.

Now you can launch your application via the Run button right in UnigineEditor.Теперь вы можете запустить свое приложение с помощью кнопки Run прямо в UnigineEditor.

Modifying Terrain By Adding New Layer Maps
Изменение ландшафта путем добавления новых карт слоев
#

Spawn new Landscape Layer Maps (LandscapeLayerMap) to modify terrain surface, these layer maps can represent vehicle tracks, chunks of trenches, or pits. This way is similar to using Decals: each layer is a separate node, so you can control each modification separately. Moreover, using Landscape Layer Map implies no data density limits, enabling you to achieve realistic results with high-quality insets.Создайте новые Landscape Layer Map (LandscapeLayerMap) для изменения поверхности ландшафта, эти карты слоев могут представлять следы транспортных средств, участки траншей или ям. Этот способ аналогичен использованию декалей: каждый слой является отдельной нодой, поэтому вы можете управлять каждой модификацией отдельно. Более того, использование Landscape Layer Map не подразумевает ограничений по плотности данных, что позволяет достигать реалистичных результатов с высококачественными вставками.

Adding Assets
Добавление ассетов
#

Let's create a new layer map for a crater, to do so, perform the following actions:Давайте создадим новую карту слоя для кратера. Для этого выполните следующие действия:

  1. Save the following images to be used for the crater to your computer:Сохраните на свой компьютер следующие изображения, которые будут использоваться для кратера:

    Карта альбедо для кратера Карта высот для кратера
    Карта альбедо для кратера Карта высот для кратера
  2. Switch to UnigineEditor and Drag the crater_height.png file directly to the Asset Browser window to add it to your project (just like you did before for the terrain's Height map). In the Import Dialog for your height map, set Image Format to R32F and click Yes.В UnigineEditor перетащите файл crater_height.png непосредственно в окно Asset Browser, чтобы добавить его в свой проект (точно так же, как вы делали раньше с картой высот ландшафта). В диалоговом окне импорта для вашей карты высот установите для Image Format значение R32F и нажмите Yes.
  3. Drag the crater_albedo.png file directly to the Asset Browser window (just like you did before for the terrain's Albedo map). In the Import Dialog for your albedo map set Texture Preset to Albedo (RGB — color, A — opacity) and click Yes.Перетащите файл crater_albedo.png непосредственно в окно Asset Browser (точно так же, как вы делали раньше с картой альбедо ландшафта). В диалоговом окне импорта для карты альбедо установите для Texture Preset значение Albedo (RGB — color, A — opacity) и нажмите Yes.
  4. Select Create -> Create Landscape Layer Map in the Asset Browser.В Asset Browser выберите Create -> Create Landscape Layer Map.

  5. Enter a name for the layer map: crater.Введите имя для карты слоя: crater.
  6. Select your new crater.lmap asset and adjust its settings as shown below (assign our images, select blending mode and adjust heights range for the crater).Выберите свой новый ассет crater.lmap и измените его настройки, как показано ниже (назначьте наши изображения, выберите режим наложения и отрегулируйте диапазон высот для кратера).

  7. Click Reimport and process to the Code section below.Нажмите Reimport и переходите к разделу Код далее.

Code
Код
#

So, we have a layer map file representing a crater (crater.lmap). To add a crater to the landscape surface at the desired location we simply create a new LandscapeLayerMap using our file and set its size and transformation.Итак, у нас есть файл карты слоя, представляющий кратер (crater.lmap). Чтобы добавить кратер на поверхность ландшафта в нужном месте, мы просто создаем новый LandscapeLayerMap с помощью нашего файла и задаем его размер и трансформации.

Исходный код (C#)
// create a new layer map
LandscapeLayerMap crater_lmap = new LandscapeLayerMap();

// add the layer map as a child to the active terrain
crater_lmap.Parent = Landscape.GetActiveTerrain();

// set a path to the crater.lmap file representing a crater
crater_lmap.Path = "crater.lmap";
crater_lmap.Name = "crater";

// set the size of the crater layer to 5x5 units
crater_lmap.Size = new Vec2(5.0f, 5.0f);

// set the height scale multiplier 
crater_lmap.HeightScale = 0.5f;

// set the position and rotation of the new layer
crater_lmap.WorldPosition = new Vec3(x, y, 0.0f);
crater_lmap.SetWorldRotation(new quat(vec3.UP, 35.0f));

// set the order of the new layer to place is above the first one (basic)
crater_lmap.Order = 2;
Примечание
Although layer maps are relatively light and fast, genereating too many of them may drop performance.Хотя карты слоев относительно легкие и быстрые, генерация слишком большого их числа может снизить производительность.

This approach is non-destructive (i.e., it does not irreversibly change initial terrain data). To see the underlaying layer again in its initial state simply disable an overlaying layer map:Этот подход является неразрушающим (т.е. он не изменяет необратимо исходные данные о ландшафте). Чтобы снова увидеть нижележащий слой в его исходном состоянии, просто отключите карту наложенного слоя:

Исходный код (C#)
crater_lmap.Enabled = false;

For example, after disabling a layer map representing a crater, you'll have the surface looking just the way it was before the explosion.Например, после отключения карты слоев, представляющей кратер, поверхность будет выглядеть точно так же, как до взрыва.

Let's create the CraterManager component to implement the functionality described above. We'll create the AddCrater() method to spawn a new crater at the specified location. And we shall call it on hitting Enter on the keyboard. The Backspace key shall show/hide all spawned craters at once.Давайте создадим компонент CraterManager для реализации описанной выше функциональности. Мы создадим метод AddCrater() для создания нового кратера в указанном месте. И мы вызовем его, нажав Enter на клавиатуре. Клавиша Backspace должна показывать/скрывать все созданные кратеры одновременно.

Copy the source code below implemented as a C# component, save it to a CraterManager.cs file, and assign the component to the Dummy Node that you created earlier.Скопируйте приведенный ниже исходный код, реализованный как компонент C#, сохраните его в файл CraterManager.cs и назначьте компоненту Dummy Node, который вы создали ранее.

CraterManager.cs

Исходный код (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec2 = Unigine.dvec2;
using Scalar = System.Double;
#else
	using Vec3 = Unigine.vec3;
	using Vec2 = Unigine.vec2;
	using Scalar = System.Single;
#endif 

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class CraterManager : Component
{
	// method spawning a new crater at the specified location
	private LandscapeLayerMap AddCrater(Scalar x, Scalar y){
	// create a new layer map
	LandscapeLayerMap crater_lmap = new LandscapeLayerMap();

	// add the layer map as a child to the active terrain
	crater_lmap.Parent = Landscape.GetActiveTerrain();

	// set a path to the crater.lmap file representing a crater
	crater_lmap.Path = "crater.lmap";
	crater_lmap.Name = "crater";

	// set the size of the crater layer to 5x5 units
	crater_lmap.Size = new Vec2(5.0f, 5.0f);

	// set the height scale multiplier 
	crater_lmap.HeightScale = 0.5f;

	// set the position and rotation of the new layer
	crater_lmap.WorldPosition = new Vec3(x, y, 0.0f);
	crater_lmap.SetWorldRotation(new quat(vec3.UP, 35.0f));

	// set the order of the new layer to place is above the first one (basic)
	crater_lmap.Order = 2;
		return crater_lmap;
	}
	private void Init()
	{
		// write here code to be called on component initialization
	}

	List<LandscapeLayerMap> craters = new List<LandscapeLayerMap>();

	private void Update()
	{
		// spawn a new crater at a random point
		if (Input.IsKeyDown(Input.KEY.BACKSPACE))
			craters.Add(AddCrater(Game.GetRandomFloat(0.0f, (float)GetComponent<LandscapeGenerator>(node).lmapSize.x), Game.GetRandomFloat(0.0f, (float)GetComponent<LandscapeGenerator>(node).lmapSize.y)));

		// toggle visibility for all craters to show initial landscape state
		else if (Input.IsKeyDown(Input.KEY.ENTER))
			craters.ForEach(delegate(LandscapeLayerMap crater)
			{
				crater.Enabled = !crater.Enabled;
			});
	}
}

As the component is assigned, launch your application via the Run button right in UnigineEditor to check the result.Как только компонент назначен, запустите свое приложение с помощью кнопки Run прямо в UnigineEditor, чтобы проверить результат.

GPU-Based Terrain Modification
Модификация ландшафта на стороне графического процессора
#

Terrain modification is performed in asynchronous mode on GPU side by calling the asyncTextureDraw method of the Landscape class, that commences a drawing operation. The operation itself is to be implemented inside an event handler.Модификация ландшафта выполняется в асинхронном режиме на стороне графического процессора путем вызова метода asyncTextureDraw класса Landscape, который запускает операцию отрисовки. Сама операция должна быть реализована внутри обработчика обратного вызова.

The workflow here is as follows:Рабочий процесс здесь выглядит следующим образом:

  1. Implement your GPU-based terrain modification logic in a function.Реализуйте свою логику изменения ландшафта на стороне графического процессора в функции.
  2. Set this handler function when subscribing for the Texture Draw event (when GPU-based terrain modification operation is performed) via getEventTextureDraw().Установите эту функцию, подписываясь на событие Texture Draw (модификация ландшафта на стороне графического процессора) через getEventTextureDraw().
  3. Commence a GPU drawing operation by calling the asyncTextureDraw() method. Here you should specify the GUID of an .lmap file of the landscape layer map to be modified, the coordinates of the top-left corner and the resolution of the segment of data to be modified, you should also define which data layers are to be affected (heights, albedo, masks) via a set of flags.Запустите операцию отрисовки на графическом процессоре, вызвав метод asyncTextureDraw(). Здесь вы должны указать идентификатор GUID файла .lmap карты слоя ландшафта, который необходимо изменить, координаты верхнего левого угла и разрешение сегмента данных, который необходимо изменить. Вы также должны определить с помощью набора флагов, какие слои данных должны быть затронуты (высоты, альбедо, маски).

    Примечание
    If your modification requires additional data beyond the specified area as well as data of other landscape layer maps (e.g. a copy brush), you can enable force loading of required data. In this case you should use this overload of the asyncTextureDraw() method.Если для вашей модификации требуются дополнительные данные за пределами указанной области, а также данные других карт слоев ландшафта (например, кисть для копирования), вы можете включить принудительную загрузку необходимых данных. В этом случае вы должны использовать эту перегрузку метода asyncTextureDraw().

Adding Assets
Добавление ассетов
#

Let us modify the Heights and Albedo data of the terrain, so we need two custom maps for that. Save the following images to your computer and drag them to the Asset Browser window to add them to your project (just like you did before for the terrain's Albedo and Height maps):Давайте изменим данные Высоты и Альбедо ландшафта, для этого нам понадобятся две пользовательские карты. Сохраните следующие изображения на свой компьютер и перетащите их в окно Asset Browser, чтобы добавить их в свой проект (точно так же, как вы делали раньше для карт Альбедо и Высота ландшафта):

Custom Albedo Map Custom Height Map
Custom Albedo Map Custom Height Map

Don't forget to set Image Format to R32F for your height map and set Texture Preset to Albedo (RGB — color, A — opacity) for your albedo map and reimport them with new settings.Не забудьте установить для Image Format значение R32F для вашей карты высот и выбрать Texture Preset Albedo (RGB — color, A — opacity) для вашей карты альбедо и повторно импортировать их с новыми настройками.

Code
Код
#

Исходный код (C#)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 												(1)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

	// handler function to be executed on commencing a texture draw operation
	void my_texture_draw(UGUID guid, int id, LandscapeTextures buffer, ivec2 coord, int data_mask)
	{
		Log.Message("TEXTURE DRAW EVENT\n");

		// resize albedo and height images to fit the area to be modified
		custom_albedo_image.Resize(buffer.Resolution.x, buffer.Resolution.y);
		custom_height_image.Resize(buffer.Resolution.x, buffer.Resolution.y);

		// setting our custom image to the albedo buffer
		buffer.Albedo.SetImage(custom_albedo_image);

		// setting our custom image to the height buffer
		buffer.Height.SetImage(custom_height_image);

	}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 												(2)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

		// add a handler function to be executed on a Texture Draw operation
		Landscape.EventTextureDraw.Connect(my_texture_draw);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 												(3)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

			LandscapeLayerMap lmap = Landscape.GetActiveTerrain().GetChild(0) as LandscapeLayerMap;
			
			// generating a new ID for the draw operation
			int id = Landscape.GenerateOperationID();

			// commencing a Texture Draw operation for the selected landscape map at (100, 100) with the size of [512 x 512]
			Landscape.AsyncTextureDraw(id, lmap.GetGUID(), new ivec2(100, 100), new ivec2(512, 512), (int)(Landscape.FLAGS_FILE_DATA.HEIGHT | Landscape.FLAGS_FILE_DATA.ALBEDO));

Let's create a GPUModifier component to implement the functionality described above. We'll create the AddCrater() method to spawn a new crater at the specified location. And we shall call it on hitting Enter on the keyboard. The Backspace key shall show/hide all spawned craters at once.Давайте создадим компонент GPUModifier для реализации описанной выше функциональности. Мы создадим метод AddCrater() для создания нового кратера в указанном месте. И мы будем вызывать его нажатием клавиши Enter на клавиатуре. Клавиша Backspace должна показывать/скрывать все созданные кратеры одновременно.

Copy the source code below implemented as a C# component, save it to the GPUModifier.cs file, and assign the component to the Dummy Node that you've created earlier.Скопируйте приведенный ниже исходный код, реализованный как компонент C#, сохраните его в файле GPUModifier.cs и назначьте на Dummy Node, который вы создали ранее.

CraterManager.cs

Исходный код (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class GPUModifier : Component
{
	[ParameterFile]
	public string CustomAlbedoImage = "custom_albedo.png";
	[ParameterFile]
	public string CustomHeightlbedoImage = "custom_height.png";

	// images to be used for terrain modification
	Image custom_albedo_image = null;
	Image custom_height_image = null;
	// handler function to be executed on commencing a texture draw operation
	void my_texture_draw(UGUID guid, int id, LandscapeTextures buffer, ivec2 coord, int data_mask)
	{
		Log.Message("TEXTURE DRAW EVENT\n");

		// resize albedo and height images to fit the area to be modified
		custom_albedo_image.Resize(buffer.Resolution.x, buffer.Resolution.y);
		custom_height_image.Resize(buffer.Resolution.x, buffer.Resolution.y);

		// setting our custom image to the albedo buffer
		buffer.Albedo.SetImage(custom_albedo_image);

		// setting our custom image to the height buffer
		buffer.Height.SetImage(custom_height_image);

	}
	private void Init()
	{
		// write here code to be called on component initialization
		// prepare images for terrain modification
		// create a new image to load a custom albedo map to
		custom_albedo_image = new Image(CustomAlbedoImage);

		// set the format required for the albedo map - RGBA8
		custom_albedo_image.ConvertToFormat(Image.FORMAT_RGBA8);

		// create a new image to load a custom height map to
		custom_height_image = new Image(CustomHeightlbedoImage);

		// set the format required for the heightmap - R32F
		custom_height_image.ConvertToFormat(Image.FORMAT_R32F);

		// add a handler function to be executed on a Texture Draw operation
		Landscape.EventTextureDraw.Connect(my_texture_draw);
	}
	
	private void Update()
	{
		// write here code to be called before updating each render frame
		if (Unigine.Input.IsKeyDown(Input.KEY.SPACE)){
			// getting the first layermap that we're going to modify

			LandscapeLayerMap lmap = Landscape.GetActiveTerrain().GetChild(0) as LandscapeLayerMap;
			
			// generating a new ID for the draw operation
			int id = Landscape.GenerateOperationID();

			// commencing a Texture Draw operation for the selected landscape map at (100, 100) with the size of [512 x 512]
			Landscape.AsyncTextureDraw(id, lmap.GetGUID(), new ivec2(100, 100), new ivec2(512, 512), (int)(Landscape.FLAGS_FILE_DATA.HEIGHT | Landscape.FLAGS_FILE_DATA.ALBEDO));
		}
	}
}

As the component is assigned, launch your application via the Run button right in UnigineEditor to check the result. You can assign other textures to modify height and albedo data of the terrain, and add new ones to modify masks, for example.Как только компонент назначен, запустите свое приложение с помощью кнопки Run прямо в UnigineEditor, чтобы проверить результат. Вы можете назначить другие текстуры для изменения данных о высоте и альбедо ландшафта, а также добавить новые, например, для изменения масок.

Последнее обновление: 19.04.2024
Build: ()