I’m trying to draw 2D stuff onto the screen. I used the sample GUI code with the Urho logo as a base and I’m drawing the pixel into an Image and this is then converted into a Texture2D which is used by a Sprite. The problem is the big FPS impact from ~200 to 40-60.
// init code
_image=new Urho3D::Image(_context);
_sprite=_context->GetSubsystem<Urho3D::UI>()->GetRoot()->CreateChild<Urho3D::Sprite>();
_texture=new Urho3D::Texture2D(_context);
_texture->SetFilterMode(Urho3D::TextureFilterMode::FILTER_NEAREST);
_texture->SetNumLevels(1);
...
void redraw() // this is currently called each update() call
{
// redraw my image, this does take ~0.003s
lfgui::widget::redraw();
// only resize if necessary, 0s
if(_texture->GetWidth()!=width()||_texture->GetHeight()!=height())
_texture->SetSize(width(),height(),Urho3D::Graphics::GetRGBAFormat(),Urho3D::TEXTURE_STATIC);
// only resize if necessary, 0s
if(_image->GetWidth()!=width()||_image->GetHeight()!=height())
_image->SetSize(width(),height(),4);
// copy pixel from my image to the Urho Image (my image has the pixel in a weird format (separated color channels) so that I have to copy each pixel on it's own. This takes 0.01s.
auto h=height();
auto w=width();
uint32_t* data=(uint32_t*)_image->GetData();
for(int y=0;y<h;y++)
for(int x=0;x<w;x++)
{
lfgui::color p=this->img.get_pixel(x,y);
*data=p.value; // this is a bit faster as SetPixelInt(...)
data++;
}
// fill texture from image, 0.003s
_texture->SetData(_image);
// only resize if necessary, 0s
if(_sprite->GetWidth()!=_texture->GetWidth()||_sprite->GetHeight()!=_texture->GetHeight())
_sprite->SetSize(_texture->GetWidth(),_texture->GetHeight());
}
Is there a more direct way to draw on the screen (over a 3D scene)? Any other optimization idea? The performance impact seems kinda high for such a simple thing. I guess I could redraw my image and copy the pixels in another thread but that seems kinda odd.
Also these Sprites in the GUI don’t seem to draw semi transparent pixel? (even with _texture->SetData(_image,true)