Unigine::Landscape Class
Header: | #include <UnigineObjects.h> |
This class is used to manage landscape terrain rendering and modification.
Terrain modification is performed in asynchronous mode on GPU side by calling a corresponding method, that commences a drawing operation. When calling such a method you should specify the GUID of an .lmap file landscape layer map to be modified, the coordinates of the upper-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. The operation itself is to be implemented inside a callback handler.
GPU-Based Terrain Modification#
The workflow is as follows:
- Implement your GPU-based terrain modification logic in a function.
- Set this callback function to be fired when a Texture Draw (GPU-based terrain modification) operation is performed by calling the addTextureDrawCallback() method.
- Commence a GPU drawing operation by calling the asyncTextureDraw() method.
Here you should specify the GUID of an .lmap file landscape layer map to be modified, the coordinates of the upper-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
In case your modification requires additional data beyond the specified area as well as the 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.
// GPU-based modification
void my_texture_draw(UGUID guid, int id, LandscapeTexturesPtr buffer, ivec2 coord, int data_mask)
{
// getting a built-in "cirle_soft" Editor's brush
MaterialPtr mat = Materials::findMaterial("circle_soft");
if (mat) {
// setting necessary textures (e.g., albedo and heights)
mat->setTexture("terrain_albedo", buffer->getAlbedo());
mat->setTexture("terrain_height", buffer->getHeight());
// setting up brush material parameters (size, color, etc. and specifying masks to be affected by the brush)
mat->setParameterFloat("size", 100.0f);
mat->setParameterFloat("height", 10.0f);
mat->setParameterFloat4("color", vec4::RED);
mat->setParameterInt("data_mask", Landscape::FLAGS_DATA_ALBEDO | Landscape::FLAGS_DATA_HEIGHT);
// running material's "brush" expression
mat->runExpression("brush", buffer->getResolution().x, buffer->getResolution().y);
// resetting material textures
mat->setTexture("terrain_albedo", nullptr);
mat->setTexture("terrain_height", nullptr);
}
}
// ...
virtual int AppWorldLogic::init()
{
// adding a callback to be fired on a Texture Draw operation
Landscape::addTextureDrawCallback(MakeCallback(my_texture_draw));
// getting an existing landscape terrain object named "MyTerrain" from the world
ObjectLandscapeTerrainPtr terrain = checked_ptr_cast<ObjectLandscapeTerrain>(World::getNodeByName("MyTerrain"));
// getting the first layermap that we're going to modify
LandscapeLayerMapPtr lmap = checked_ptr_cast<LandscapeLayerMap>(terrain->getChild(0));
// generating a new ID for the draw operation
int id =Landscape::generateOperationID();
// user's code (bounding to ID)
// commencing a Texture Draw operation for the selected landscape map at (1, 1) with the size of [32 x 32]
Landscape::asyncTextureDraw(id, lmap->getGUID(), ivec2(1, 1), ivec2(32, 32), ~0);
return 1;
}
// ...
virtual int shutdown()
{
// clearing callbacks
Landscape::clearTextureDrawCallbacks();
return 1;
}
And the process:
- After commencing of a terrain modification operation with all necessary parameters specified, the Engine copies a fragment of terrain data from the specified landscape layer map file at the specified coordinates to a buffer (LandscapeTextures) of the specified resolution.
- Upon completion of the copying process a callback function set via the addTextureDrawCallback() method is called. This function modifies the buffer.
- After this selected data layers of the modified buffer are pasted back to the landscape layer map file.
Landscape Class
Enums
TYPE_DATA#
FLAGS_DATA#
TYPE_FILE_DATA#
File data types.FLAGS_FILE_DATA#
File data layer flags.Members
Ptr<LandscapeTextures> getTemporaryTexture ( const Math::ivec2 & resolution ) #
Returns a fragment of terrain data as a LandscapeTextures of the specified resolution.Arguments
- const Math::ivec2 & resolution - Resolution of a temporary texture to be obtained.
Return value
LandscapeTextures instance containing a fragment of terrain data.void releaseTemporaryTexture ( const Ptr<LandscapeTextures> & texture ) #
Releases the specified temporary texture.Arguments
- const Ptr<LandscapeTextures> & texture - Temporary landscape texture to be released.
bool terrainLoad ( const UNIGINE_BOUND_BOX & bb ) #
Loads terrain data (tiles) for all landscape layer maps within the specified bounding box to cache.Arguments
- const UNIGINE_BOUND_BOX & bb - Bounding box, defining landscape layer maps for which the data is to be loaded.
Return value
true if terrain data was successfully loaded for all landscape layer maps within the specified bounding box; otherwise, false.bool render ( const Ptr<LandscapeTextures> & buffers, const Math::Mat4 & transform, Math::Scalar texel_size ) #
Renders the area of the specified landscape layer maps defined by the specified transform and texel size, to the specified buffers.Arguments
- const Ptr<LandscapeTextures> & buffers - Target texture buffers to which the specified landscape layer maps are to be rendered.
- const Math::Mat4 & transform - Transformation of the landscape terrain area to be rendered (Z-coordinate is ignored).
- Math::Scalar texel_size - Texel size to be used. Defines the size of the area depending on the buffers resolution.
Return value
true if the data of the landscape terrain area (all landscape layer maps) defined by the specified transformation and texel size, was successfully rendered to the specified buffers; otherwise, false.bool render ( const Ptr<LandscapeTextures> & buffers, const Math::Mat4 & transform, Math::Scalar texel_size, int padding ) #
Renders the area of the specified landscape layer maps defined by the specified transform and texel size, to the specified buffers. Use the padding parameter to set inner padding size for the area (when necessary).Arguments
- const Ptr<LandscapeTextures> & buffers - Target texture buffers to which the specified landscape layer maps are to be rendered.
- const Math::Mat4 & transform - Transformation of the landscape terrain area to be rendered (Z-coordinate is ignored).
- Math::Scalar texel_size - Texel size to be used. Defines the size of the area depending on the buffers resolution.
- int padding - Inner padding size for the area to be rendered (if necessary).
Return value
true if the data of the landscape terrain area (all landscape layer maps) defined by the specified transformation and texel size, was successfully rendered to the specified buffers; otherwise, false.bool render ( const Vector< Ptr<LandscapeLayerMap> > & maps, const Ptr<LandscapeTextures> & buffers, const Math::Mat4 & transform, Math::Scalar texel_size ) #
Renders the area of the specified landscape layer maps defined by the specified transform and texel size, to the specified buffers. Use the padding parameter to set inner padding size for the area (when necessary).Arguments
- const Vector< Ptr<LandscapeLayerMap> > & maps - List of the landscape layer maps to be rendered.
- const Ptr<LandscapeTextures> & buffers - Target texture buffers to which the specified landscape layer maps are to be rendered.
- const Math::Mat4 & transform - Transformation of the landscape terrain area to be rendered (Z-coordinate is ignored).
- Math::Scalar texel_size - Texel size to be used. Defines the size of the area depending on the buffers resolution.
Return value
true if the data of the landscape terrain area (specified landscape layer maps) defined by the specified transformation and texel size, was successfully rendered to the specified buffers; otherwise, false.bool render ( const Vector< Ptr<LandscapeLayerMap> > & maps, const Ptr<LandscapeTextures> & buffers, const Math::Mat4 & transform, Math::Scalar texel_size, int padding ) #
Renders the area of the specified landscape layer maps defined by the specified transform and texel size, to the specified buffers. Use the padding parameter to set inner padding size for the area (when necessary).Arguments
- const Vector< Ptr<LandscapeLayerMap> > & maps - List of the landscape layer maps to be rendered.
- const Ptr<LandscapeTextures> & buffers - Target texture buffers to which the specified landscape layer maps are to be rendered.
- const Math::Mat4 & transform - Transformation of the landscape terrain area to be rendered (Z-coordinate is ignored).
- Math::Scalar texel_size - Texel size to be used. Defines the size of the area depending on the buffers resolution.
- int padding - Inner padding size for the area to be rendered.
Return value
true if the data of the landscape terrain area (specified landscape layer maps) defined by the specified transformation and texel size, was successfully rendered to the specified buffers; otherwise, false.void asyncTextureDraw ( int operation_id, const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution, int flags_file_data, const Vector< UNIGINE_BOUND_BOX > & bounds_preload ) #
Commences an asynchronous GPU-based drawing operation with forced pre-loading of terrain data within the specified bounding box (all landscape layer maps). The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining data layers defined by the specified flags. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes) that requires additional data beyond the area specified by coords and resolution parameters as well as the data of other landscape layer maps (e.g. a copy brush).Arguments
- int operation_id - Draw operation ID.
- const UGUID & guid - GUIGUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
- int flags_file_data - Data layer mask. A combination of FLAGS_FILE_DATA_* flags indicating data layers to be affected (heights, albedo, certain masks).
- const Vector< UNIGINE_BOUND_BOX > & bounds_preload - Bounding box (world) specifying the area containing terrain tiles (all landscape layer maps) to be loaded to memory prior to making modifications.
void asyncTextureDraw ( int operation_id, const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution, int flags_file_data ) #
Commences an asynchronous GPU-based drawing operation. The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining data layers defined by the specified flags. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes).Arguments
- int operation_id - Draw operation ID.
- const UGUID & guid - GUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
- int flags_file_data - Data layer mask. A combination of FLAGS_FILE_DATA_* flags indicating data layers to be affected (heights, albedo, certain masks).
void asyncTextureDraw ( int operation_id, const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution ) #
Commences an asynchronous GPU-based drawing operation. The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining all data layers. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes).Arguments
- int operation_id - Draw operation ID.
- const UGUID & guid - GUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
void asyncTextureDraw ( const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution, int flags_file_data, const Vector< UNIGINE_BOUND_BOX > & bounds_preload ) #
Commences an asynchronous GPU-based drawing operation with forced pre-loading of terrain data within the specified bounding box (all landscape layer maps). The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining data layers defined by the specified flags. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes) that requires additional data beyond the area specified by coords and resolution parameters as well as the data of other landscape layer maps (e.g. a copy brush).Arguments
- const UGUID & guid - GUIGUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
- int flags_file_data - Data layer mask. A combination of FLAGS_FILE_DATA_* flags indicating data layers to be affected (heights, albedo, certain masks).
- const Vector< UNIGINE_BOUND_BOX > & bounds_preload - Bounding box (world) specifying the area containing terrain tiles (all landscape layer maps) to be loaded to memory prior to making modifications.
void asyncTextureDraw ( const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution, int flags_file_data ) #
Commences an asynchronous GPU-based drawing operation. The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining data layers defined by the specified flags. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes).Arguments
- const UGUID & guid - GUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
- int flags_file_data - Data layer mask. A combination of FLAGS_FILE_DATA_* flags indicating data layers to be affected (heights, albedo, certain masks).
void asyncTextureDraw ( const UGUID & guid, const Math::ivec2 & coord, const Math::ivec2 & resolution ) #
Commences an asynchronous GPU-based drawing operation. The drawing operation represents modification of the texture buffer of the specified size taken at specified coordinates and combining all data layers. The operation itself is to be implemented in the TextureDrawCallback handler. This method can be used for implementing GPU-based terrain modification (e.g. brushes).Arguments
- const UGUID & guid - GUID of the landscape layer map file to be modified.
- const Math::ivec2 & coord - Coordinates of the upper left corner of the landscape layer map data segment to be modified along the X and Y axes.
- const Math::ivec2 & resolution - Resolution of the landscape layer map data segment to be modified along the X and Y axes.
void asyncApplyDiff ( int operation_id, const UGUID & guid, const char * path ) #
Applies the state of the landscape layer map stored in the specified file to the landscape layer map file with the specified GUID.Arguments
- int operation_id - Operation ID.
- const UGUID & guid - GUID of the landscape layer map file to which a state stored at the specified path is to be applied.
- const char * path - Path to a file where the current landscape map modification state is stored.
void asyncApplyDiff ( const UGUID & guid, const char * path ) #
Applies the state of the landscape layer map stored in the specified file to the landscape layer map file with the specified GUID.Arguments
- const UGUID & guid - GUID of the landscape layer map file to which a state stored at the specified path is to be applied.
- const char * path - Path to a file where the current landscape map modification state is stored.
void asyncSaveFile ( int operation_id, const UGUID & file_guid ) #
Saves the landscape layer map file with the specified GUID.Arguments
- int operation_id - Operation ID.
- const UGUID & file_guid - GUID of the landscape layer map file.
void asyncSaveFile ( const UGUID & file_guid ) #
Saves the landscape layer map file with the specified GUID.Arguments
- const UGUID & file_guid - GUID of the landscape layer map file.
void asyncSaveFile ( int operation_id, const UGUID & guid, const char * path_new_state, const char * path_old_state ) #
Saves the specified landscape layer map file applying all changes along with saving old and new states (diff) to temporary files. These temporary files can be used to perform undo/redo operations via the applyDiff() method.Arguments
- int operation_id - Operation ID.
- const UGUID & guid - GUID of the landscape layer map file.
- const char * path_new_state - Path to a file to store the new landscape layer map state.
- const char * path_old_state - Path to a file to store the old landscape layer map state.
void asyncSaveFile ( const UGUID & guid, const char * path_new_state, const char * path_old_state ) #
Saves the specified landscape layer map file applying all changes along with saving old and new states (diff) to temporary files. These temporary files can be used to perform undo/redo operations via the applyDiff() method.UGUID lmap_file_guid; // .lmap file GUID
String cache_path; // path to some cache directory
String new_filepath; // path to store a new state
String old_filepath; // path to store an old state
// method implementing the "apply" operation
void apply() override
{
// generating necessary paths to save states (generate_filepath - some abstract method)
new_filepath = generate_filepath(cache_path);
old_filepath_ = generate_filepath(cache_path);
// applying changes and saving current and previous states
Landscape::asyncSaveFile(lmap_file_guid, new_filepath, old_filepath);
}
// method implementing the "undo" operation by applying the previous saved state
void undo() override
{
if (!old_filepath.empty())
Landscape::asyncApplyDiff(lmap_file_guid, old_filepath);
}
// method implementing the "redo" operation by applying the new saved state
void redo() override
{
if (!new_filepath.empty())
Landscape::asyncApplyDiff(lmap_file_guid, new_filepath);
}
Arguments
- const UGUID & guid - GUID of the landscape layer map file.
- const char * path_new_state - Path to a file to store the new landscape layer map state.
- const char * path_old_state - Path to a file to store the old landscape layer map state.
bool isFilesClosed ( ) #
Returns a value indicating if .lmap files for all landscape layer maps are closed. Call this method before making any changes (modification, deletion, renaming) to .lmap files of the landscape terrain object to ensure that these files are not currently used by the Engine to avoid conflicts. If not, you can use the filesClose() method co close them.Return value
true if .lmap files for all landscape layer maps are closed; otherwise, false.void filesClose ( const Vector< UGUID > & reload_files ) #
Closes .lmap files for all landscape layer maps and reloads the ones with specified GUIDs. This method should be called before making any changes (modification, deletion, renaming) to .lmap files of the landscape terrain object to avoid conflicts, as these files are streamed continuosly by the Engine. Thus, by calling this method you inform the Engine that it should stop streaming terrain data. The list informs the Engine which files are no longer valid and should be reloaded or removed. As you're done with modification you should call the filesOpen() method to resume streaming operations.Arguments
- const Vector< UGUID > & reload_files - List of GUIDs of .lmap files to be reloaded. This list should contain .lmap files, that were deleted or having their data changed (albedo, heights, masks). If there are no such files, you can simply call the filesClose() method.
void filesClose ( ) #
Closes .lmap files for all landscape layer maps. This method should be called before making any changes (modification, deletion, renaming) to .lmap files of the landscape terrain object to avoid conflicts, as these files are streamed continuosly by the Engine. Thus, by calling this method you inform the Engine that it should stop streaming terrain data. As you're done with modification you should call the filesOpen() method to resume streaming operations.void filesOpen ( ) #
Opens .lmap files for all landscape layer maps. This method should be called after making any changes (modification, deletion, renaming) to .lmap files of the landscape terrain object. Prior to making such modifications the filesClose() method should be called.Ptr<ObjectLandscapeTerrain> getActiveTerrain ( ) #
Returns the current active Landscape Terrain object.Return value
Landscape Terrain object that is currently active.void * addTextureDrawCallback ( Unigine::CallbackBase5< UGUID, int, Ptr<LandscapeTextures>, Math::ivec2, int > * func ) #
Adds a callback function to be called when a Texture Draw (GPU-based terrain modification) operation is performed. The signature of the callback function must be as follows:void texture_draw_callback(UGUID guid, int id, LandscapeTexturesPtr buffer, ivec2 coord, int data_mask);
You can set a callback function as follows:
addTextureDrawCallback(MakeCallback(texture_draw_callback));
Example: Setting a texture draw callback function for a certain class:
// callback function implementing modification of the buffer
void SomeClass::textureDraw(UGUID guid, int id, LandscapeTexturesPtr buffer, ivec2 coord, int data_mask)
{
// insert your buffer modification code here (the following lines can be used as an example)
// getting a brush material to be used (e.g., built-in "cirle_soft" Editor's brush)
MaterialPtr mat = Materials::findMaterial("circle_soft");
if (mat) {
// setting necessary textures to be modified by the brush
mat->setTexture("terrain_albedo", buffer->getAlbedo());
mat->setTexture("terrain_height", buffer->getHeight());
// setting up brush material parameters
mat->setParameterFloat("size", 100.0f);
mat->setParameterFloat("height", 10.0f);
mat->setParameterFloat4("color", vec4::RED);
mat->setParameterInt("data_mask", Landscape::FLAGS_DATA_ALBEDO | Landscape::FLAGS_DATA_HEIGHT);
// running material's "brush" expression
mat->runExpression("brush", buffer->getResolution().x, buffer->getResolution().y);
// resetting material textures
mat->setTexture("terrain_albedo", nullptr);
mat->setTexture("terrain_height", nullptr);
}
}
// ...
virtual int AppWorldLogic::init()
{
// setting the textureDraw() function to handle texture drawing
Landscape::addTextureDrawCallback(MakeCallback(SomeClass::textureDraw));
}
Arguments
- Unigine::CallbackBase5< UGUID, int, Ptr<LandscapeTextures>, Math::ivec2, int > * func - Callback pointer.
Return value
ID of the last added texture draw callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeTextureDrawCallback ( void * id ) #
Removes the specified callback from the list of texture draw callbacks.Arguments
- void * id - Texture draw callback ID obtained when adding it.
Return value
True if the texture draw callback with the given ID was removed successfully; otherwise false.void clearTextureDrawCallback ( ) #
Clears all added texture draw callbacks.void * addApplyDiffCallback ( Unigine::CallbackBase3< UGUID, int, const char* > * func ) #
Adds a callback function to be called on applying a diff to the landscape layer map file. The signature of the callback function must be as follows:void apply_diff_callback(UGUID guid, int id, const char*);
You can set a callback function as follows:
addApplyDiffCallback(MakeCallback(apply_diff_callback));
Example: Setting an apply diff callback function for a certain class:
// callback function implementing certain actions to be performed on applying a diff
void applyDiff(UGUID guid, int id, const char* path)
{
// insert your specific handling code here
}
// ...
virtual int AppWorldLogic::init()
{
// setting the applyDiff() function to handle an apply diff event
Landscape::addApplyDiffCallback(MakeCallback(&applyDiff));
}
Arguments
- Unigine::CallbackBase3< UGUID, int, const char* > * func - Callback pointer.
Return value
ID of the last added apply diff callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeApplyDiffCallback ( void * id ) #
Removes the specified callback from the list of apply diff callbacks.Arguments
- void * id - Apply diff callback ID obtained when adding it.
Return value
True if the apply diff callback with the given ID was removed successfully; otherwise false.void clearApplyDiffCallback ( ) #
Clears all added apply diff callbacks.void * addSaveFileCallback ( Unigine::CallbackBase4< UGUID, int, const char*, const char* > * func ) #
Adds a callback function to be called on applying changes made to a landscape layer map file and saving old and new states to temporary files. The signature of the callback function must be as follows:void save_file_callback(UGUID guid, int id, const char*, const char*);
You can set a callback function as follows:
addFileSaveCallback(MakeCallback(save_file_callback));
Example: Setting an apply diff callback function for a certain class:
// callback function implementing certain actions to be performed on applying a certain state
void saveFile(UGUID guid, int id, const char* path_new_state, const char* path_old_state)
{
// insert your specific handling code here
}
// ...
virtual int AppWorldLogic::init()
{
// setting the saveFile() function to handle file saving
Landscape::addSaveFileCallback(MakeCallback(&saveFile));
}
Arguments
- Unigine::CallbackBase4< UGUID, int, const char*, const char* > * func - Callback pointer.
Return value
ID of the last added save file callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeSaveFileCallback ( void * id ) #
Removes the specified callback from the list of save file callbacks.Arguments
- void * id - Save file callback ID obtained when adding it.
Return value
True if the save file callback with the given ID was removed successfully; otherwise false.void clearSaveFileCallback ( ) #
Clears all added save file callbacks.int generateOperationID ( ) #
Generates a new ID for the operation. This ID is used to manage operations.int id = Landscape::generateOperationID();
// user's code (bounding to ID)
Landscape::asyncTextureDraw(id, file_guid, coord, resolution, flags_file_data);