photo

Camera View Component strange init WidgetSpriteViewport

Recommended Posts

Dear Unigine support,

I'm trying to properly setup an external camera view displayed in object gui panel.
For this, I'm using Component System as bellow code shows.

## CameraViewComponent.h

class DLLEI CameraViewComponent :
	public ComponentBase
{
public:
	COMPONENT(CameraViewComponent, ComponentBase);
	COMPONENT_INIT(init);
	COMPONENT_UPDATE(update);
	COMPONENT_POST_UPDATE(render);
	COMPONENT_SHUTDOWN(shutdown);

	void init();
	void update();
	void render();
	void shutdown();

	PROP_NAME("CameraViewComponent");
	PROP_PARAM(Node, BellyCamera);

private:
	ObjectMeshStaticPtr mesh;
	TexturePtr texture;
	PlayerDummyPtr camera;
	ViewportPtr viewport;
	Math::Vec3 position;
	ObjectGuiPtr object_gui;
	GuiPtr gui;
	WidgetSpriteViewportPtr sprite_viewport;
};

## CameraViewComponent.cpp

REGISTER_COMPONENT(CameraViewComponent)

void CameraViewComponent::init()
{
	object_gui = checked_ptr_cast<ObjectGui>(node);
	if (object_gui)
	{
		gui = object_gui->getGui();
		sprite_viewport = WidgetSpriteViewport::create(gui, 512, 512);
		gui->addChild(sprite_viewport, Gui::ALIGN_EXPAND);
		//if (BellyCamera.get())
		//	sprite_viewport->setCamera(checked_ptr_cast<PlayerDummy>(BellyCamera.get())->getCamera());
	}
}

void CameraViewComponent::update()
{
	if (BellyCamera.get())
		sprite_viewport->setCamera(checked_ptr_cast<PlayerDummy>(BellyCamera.get())->getCamera());
}

void CameraViewComponent::render()
{
}

void CameraViewComponent::shutdown()
{
}

I get it working properly only when I repeat sprite_viewport->setCamera in update, whereas to me it is only init.
Then maybe there's still smthg to trigger update when sync is not automatic..

When i do only in init, I get a view and can see it is kind of viewport of the world, but it is not attached to the entity (loaded from World as from CIGI).
Is looks like smthg is not ready yet on Component::init() for a proper link.

Sub-question: if recurrent sync has to be performed, would it be on update (COMPONENT_UPDATE) or on render (COMPONENT_POST_UPDATE)?

Kind regards,
Charles

Share this post


Link to post

Hello Charles,

I'm sorry, but we currently limited in support due to the release preparation and vacation season. Answers might take longer than usual. We'll be right back on this matter next week.

Sorry for the inconvenience.

Share this post


Link to post

Hi,

Thanks already for taking care to give an update ;-)
It is not blocking, just need confirmation it is properly implemented.. doubt about it.

Kind regards,
Charles

Share this post


Link to post

Hi, Charles, 

I tried to reproduce this problem but it seems like it works just fine with sprite_viewport->setCamera in both cases (only in init() or only in update()).

// AppWorldLogic::init()

player = PlayerDummy::create();
Game::setPlayer(player);

player->setPosition(Vec3(17.0f));
player->setDirection(vec3(-1.0f), vec3(0.0f, 0.0f, 1.0f));

gui_obj = ObjectGui::create(512, 512);
auto object = ComponentSystem::get()->addComponent<CameraViewComponent>(gui_obj);
object->BellyCamera = player;

 

void CameraViewComponent::init()
{
    object_gui = checked_ptr_cast<ObjectGui>(node);
    if (object_gui)
    {
        gui = object_gui->getGui();
        sprite_viewport = WidgetSpriteViewport::create(gui, 512, 512);
        gui->addChild(sprite_viewport, Gui::ALIGN_EXPAND);
        if (BellyCamera.get())
            sprite_viewport->setCamera(checked_ptr_cast<PlayerDummy>(BellyCamera.get())->getCamera());
    }
}

void CameraViewComponent::update()
{
	// Also i have tried to setCamera here, instead of ::init(), and it seems like it works just fine in both cases
	//if (BellyCamera.get())
	//	sprite_viewport->setCamera(checked_ptr_cast<PlayerDummy>(BellyCamera.get())->getCamera());
}

 

Share this post


Link to post
Posted (edited)

Hi,

Thanks for supporting in this.

There is a difference between our both context: component is loaded by CIGI.
Would it be possible for you to test with your CIGI example?

Another difference maybe: the belly camera is defined as child of the H/C, as one camera among different predefined ones.
Attached is how ObjectGui set its CameraViewComponent's Belly Camera directly with one of the predefined cam.
Keep in mind, such entity will be loaded with CIGI..

I have the sensation that at init we get a camera, but not the right one.
For later on, the camera will be like fix in the air... but not child of the H/C.

Kind regards,
Charles

UNG-belly-cam-component.PNG

Edited by Lales.Charles

Share this post


Link to post
Posted (edited)

Hi,

I added Ctrl key to activate / deactivate the continuous setCamera.. and behavior is even more weird:

- Ctrl -> belly camera moving with the entity

- Not Ctrl -> belly camera is fixed, not moving with entity anymore.. but the stream is active

- I can switch between all cameras defined in entity -> the belly one is right attached to the entity, despite what display the ObjectGui... 

Question: do you duplicate the cam in setCamera somehow..??

Self answering:
https://developer.unigine.com/en/docs/2.11/api/library/gui/class.widgetspriteviewport?rlang=cpp#setCamera_Camera_void
Copies parameters of the given Camera instance.

So.. doesn't mean it is a child...

## CameraViewComponent.cpp

void CameraViewComponent::update()
{
	if (BellyCamera.get() && App::getKeyState(App::KEY_CTRL))
		sprite_viewport->setCamera(checked_ptr_cast<PlayerDummy>(BellyCamera.get())->getCamera());
}

Kind regards,
Charles

Edited by Lales.Charles

Share this post


Link to post