Jump to content

Implementing WATER System


photo

Recommended Posts

Hi 

What is the proper C++ code to add the water feature and all the different water materials? 

I would like to have a button that turns on the world water system and a menu of all the water materials 

Similar to how it works in the unigine editor. 

If u could help me post the codes to do this i would be so grateful 

Thank you. 

Also i would like a sun position changer implemented in my project using maybe Imgui. 

Link to comment

Hello @3danimation,

Let me answer some of your questions.

On 9/10/2021 at 8:56 PM, 3danimation said:

What is the proper C++ code to add the water feature and all the different water materials? 

I would like to have a button that turns on the world water system and a menu of all the water materials 

First of all, water is UNIGINE is represented by two types of objects:

Depending on what you need, you can create a corresponding object like so:

#include <UnigineObjects.h>
/* .. */
// create a new ObjectWaterGlobal node
ObjectWaterGlobalPtr water = ObjectWaterGlobal::create();
// enable rendering of water in the world in case it was disabled
Render::setWaterEnabled(true);

It's not really clear for me what "a button that turns on the water system" means, but if you could create a WidgetButton  that would be accountable for toggling the water rendering:

Spoiler

int AppWorldLogic::init()
{
  // create a water node
	ObjectWaterGlobalPtr water = ObjectWaterGlobal::create();
  // create a button widget and add a callback
	GuiPtr gui = Gui::get();
	WidgetButtonPtr toggle = WidgetButton::create(gui, "Toggle Water");
	toggle->setToggleable(1);
	toggle->setFontSize(24);
	toggle->setPosition(128, 128);
	gui->addChild(toggle, Gui::ALIGN_OVERLAP);
	toggle->addCallback(Gui::CLICKED, MakeCallback(this, &AppWorldLogic::toggleWater));

	return 1;
}

   // toggle water on button pressed callback
void AppWorldLogic::toggleWater()
{
	Render::setWaterEnabled(!Render::isWaterEnabled());
}

 

For a list of water materials you can use WidgetListBox or WidgetComboBox. Check out the examples in the dedicated API docs.

On 9/10/2021 at 8:56 PM, 3danimation said:

Also i would like a sun position changer implemented in my project using maybe Imgui. 

I can't speak for ImGui right now, but the most natural way to adjust the sun rotation is by using the WidgetManipulatorRotator, why not use it? Please, clarify a bit more details about the desired behaviour so we can find the most appropriate solutions.

Thanks!

Link to comment

The code is showing a build error.

Are u sure this is right?

toggle->addCallback(Gui::CLICKED, MakeCallback(this, &AppWorldLogic::toggleWater));

	return 1;
}

   // toggle water on button pressed callback
void AppWorldLogic::toggleWater()
{
	Render::setWaterEnabled(!Render::isWaterEnabled());
}

This is not working... Everything else worked

How do i actually make the button perform water on and water off command??

Edited by 3danimation
Link to comment

That's just a code snippet showing the use case, but it must be workable. Make sure you have included all the necessary headers and defined all the methods and class members in the AppWorldLogic.h.

If it's not the case, please attach the build log with errors so we check the problem.

You can also check out the following sample that I prepared for you, it is capable of toggling water rendering on and off and provides a list of default water materials assigned to the global water object when selected.

WaterAndGui.gif

AppWorldLogic.cpp

AppWorldLogic.h

Thanks!

  • Like 1
Link to comment
×
×
  • Create New...