render texture


photo

Recommended Posts

Can't I create and use a render texture in  editor? Like Unity.

Should I make the code myself  like an example of mirror programming?

Link to post

Hello @dongju.jeong,

If I got this right, you want to render a camera view to a texture right in the Editor (just like Render Textures in Unity). There is no special texture types in UNIGINE for that, it is available via API. UnigineScript is the only coding workflow that runs logic inside the Editor at the moment. Below I've described the simplest way to render to a texture using engine.render.renderImage2D:

image.png

All you need is to create a player and a target mesh (e.g. a plane primitive having an arbitrary material inherited from mesh_base) and make them children of a WorldExpression node. Then use the following expression to update the plane's albedo texture:

{
#include <core/unigine.h> // in order to use node_cast
    int size = 1024; // image width
    float aspect_ratio = 2.0f; // aspect ratio to fit the target plane (which is 2×1 units here)
    Object plane = node_cast(getChild(0)); // get the target plane being the first child
    Material mat = plane.getMaterial(0);
  
    Player dummy = node_cast(getChild(1)); // get the camera being the second child in this example
    Camera cam = dummy.getCamera();
  
    Image image = new Image();
  
    engine.render.renderImage2D(cam, image, size, size / aspect_ratio, 0, 0);
    mat.setTextureImage(mat.findTexture("albedo"),image);
    image.clear();
}

And we can see the desired result:

rtt.gif

It's not the best approach as all necessary objects are retrieved each frame (due to limitation of WorldExpression), I would suggest using the same code in the world script, where you can split the logic to the init and update stages.

If you seek the best performance and fine tuning, take a look at the Samples > C++ API > Render > RenderTarget sample (it's in C++ but turning it into UnigineScript is a quite trivial task). You may also find these samples and articles helpful:

Thank you!

  • Like 2
Link to post

Samples > C++ API > Render >RenderTarget 

 

RenderTarget sample does not exist...

Edited by dongju.jeong
Link to post

dongju.jeong

For some reason this sample is not displayed in SDK Browser in 2.9 SDK release. You can navigate to it manually by opening the <SDK_DIR>/source/samples/Api/Render/RenderTarget. In 2.8 and earlier this sample named as TextureRender.

Thanks!

  • Like 1

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to post

woot. where have you been hiding??? oO (... render texture I mean)

Edited by werner.poetzelberger
Link to post

What is the difference between using Image of Render class and texture of viewport class?

 

the way of using a Render class is much heavier.

Edited by dongju.jeong
Link to post
  • 10 months later...
On 2/27/2020 at 7:53 AM, dongju.jeong said:

What is the difference between using Image of Render class and texture of viewport class?

I could be wrong but I think the difference is:

  •  Unigine::ViewPort::renderTexture2D -> you render your world, from the given camera
  • Unigine::RenderTarget -> You render the geometry you want. Not necessarily the world
Link to post
On 2/27/2020 at 12:23 PM, dongju.jeong said:

What is the difference between using Image of Render class and texture of viewport class?

 

the way of using a Render class is much heavier.

The only thing that matters here.

Textures reside on GPU video memory.

Image reside on System Memory (RAM).

Rohit

Link to post