Paint Material in Play Mode

There are no stupid questions, just stupid answers.
29 posts Page 3 of 3
Site Admin
Posts: 361
Joined: Tue Feb 03, 2015 7:25 pm
by LukasPJ » Wed Nov 22, 2017 10:57 pm
Alright so the issue is that the Terrain Editor isn't properly initialized, and it doesn't have a terrain. You have to either re-implement its getTerrainUnderWorldPoint method, or do something similar. I opted for a simple ray-trace.

Furthermore there were two GridInfo variables, and the scheduleMaterialUpdate doesn't work the way we use it.

Here is a working example:

DefineEngineFunction(paintTerrainUnderPlayer, void, (U32 mat), (1), "")
// Create a new instance of the TerrainEditor so we can use its functionality.
TerrainEditor *mTerrainEditor = new TerrainEditor();

// Get the connection to the player.
GameConnection* connection = GameConnection::getConnectionToServer();

// Fetch the player.
Player* playerObject = dynamic_cast< Player* >(connection->getControlObject());

// Get the player's position.
Point3F pos = playerObject->getPosition();
// Grab a point above the player.
pos.z + 5;

// Fetch the terrain with a ray-trace. There are probably more efficient ways to get this.
TerrainBlock *terrain;
RayInfo r;
bool hit = gClientContainer.castRay(pos, Point3F(pos.x, pos.y, pos.z - 1000), TerrainObjectType, &r);
if (hit) {
terrain = dynamic_cast<TerrainBlock*>(r.object);

// Initialize the GridInfo and GridPoint that our player's position maps to.
GridInfo gi;
GridPoint gp;

// Specify the terrain this GridPoint belongs to.
gp.terrainBlock = terrain;

// Map the player position to a point on the grid.
gp.gridPos = gp.terrainBlock->getGridPos(pos);

// Get the Terrain information at that point on the grid (such as texture, height etc.)
mTerrainEditor->getGridInfo(gp, gi);

// Map the material as dirty.
gi.mMaterialChanged = true;

// Change the material (to a layer which can be between 0 and 31).
gi.mMaterial = mat;

// Update the grid.
mTerrainEditor->setGridInfo(gi, true);

// Repaint the terrain.
terrain->updateGridMaterials(gp.gridPos, gp.gridPos);

Tested it on newest Development branch and it works. Notice that the "mat" parameter, is the index of the desired texture layer as they are ordered in the TerrainPainter.
Furthermore this code is designed to run client-side in a single-player scenario. You would have to do some extra tricks to make it work in multiplayer.
Posts: 153
Joined: Sat Aug 20, 2016 4:58 pm
by subabrain » Thu Nov 23, 2017 7:24 pm
Hello LukasPJ,


YOURE THE BEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

It works GREAT!

Site Admin
Posts: 361
Joined: Tue Feb 03, 2015 7:25 pm
by LukasPJ » Fri Nov 24, 2017 8:38 pm
No problem, glad to hear it's working! Good luck with your game :)
Posts: 153
Joined: Sat Aug 20, 2016 4:58 pm
by subabrain » Fri Jan 19, 2018 2:22 pm
Hi there,

its been a long time that i posted here my last post.
So now its all ok but i have a little question:

how can i prevent from flickering the grass while painting with this method?

would be glad to here from you :)

Thanks a lot -
Posts: 915
Joined: Sun Feb 08, 2015 1:51 am
by Duion » Fri Jan 19, 2018 3:35 pm
What kind of flickering you mean? You probably mean that the grass gets updated everytime you click paint or constantly if you hold mouse button.
So either make the paint a one time action and not constantly as long as you hold it or reduce the update frequency or only update when the material was changed for real.
No idea how to do that, but that are just my thoughts.
Posts: 153
Joined: Sat Aug 20, 2016 4:58 pm
by subabrain » Mon Jan 22, 2018 9:48 am

sorry for the long delay - i think it has to do with the painting interval. and it paints the groundcover completely new. so i have to reduce the interval - but i need it :/
Maybe i can reduce the radius of the groundcover (grass) but i think it isnt working ... - maybe you know some way to reduce this "flickering".

Thank you very much!

Online Bloodknight
Posts: 134
Joined: Tue Feb 03, 2015 8:58 pm
by Bloodknight » Tue Jan 23, 2018 10:21 pm
the grass gets rendered every render tick, mostly because it's animated, perhaps you could add a flag so that it skips most of the cycles while painting so that it only updates every few seconds. I'm not sure how one would fix this permanently since groundcover is now the only method of adding grass, but poke around in the render bins and see what you can do.
Posts: 153
Joined: Sat Aug 20, 2016 4:58 pm
by subabrain » Wed Jan 24, 2018 5:05 pm

thanks a lot for the answer - ill try to reduce this procedure by stop all not needed parameters like wind and so on ...

ill inform you if it helps ;)

but thanks anyway ;)

Posts: 153
Joined: Sat Aug 20, 2016 4:58 pm
  by subabrain » Sat Jan 27, 2018 12:45 pm
Hi there,

ok i got it now to work -> it works with wind scale on 0 and some changes of grid size ... further i made a modification on my tutorial, so the brushsize and brushtype will change only one time when start "mowing" ;)

thanks and have a nice day!

29 posts Page 3 of 3

Who is online

Users browsing this forum: Bloodknight and 3 guests