Render a mesh to a texture.

It’s quite common thing, but I’ve spent a couple of weeks of googling and experiments with zero result. All I need is just render an actor to a texture to show it in the UI. All I could find is just a piece of quite old code: Custom GameViewportClient: Render off-screen - UE4 AnswerHub
It does what I want but only when built for the editor. Any clue?

SceneCaptureComponent2D to RenderTarget

[quote=“wickerman123, post:2, topic:115324”]

SceneCaptureComponent2D to RenderTarget[/QUOTE]

Thanks, but I wrote ‘i’ve spent a couple of weeks’. Not a couple of minutes. SceneCaptureComponent2D obviously is not what i’m looking for. It wont help me to render an actor ‘offscreen’. In that video he spawns the actor in the actual world.
If you check for example the code in editor that renders a thumbnails of static meshes, you wont find that it spawns an actor to your current world to make a simple thumbnail for the Editor UI. Also, you can notices that the link above doesn’t do anything similar. It uses a dummy world. Offscreen render should not interfere into your actual game world. Spawining an actor in (1000000, 0, 0) location is an ugly workaround, but not the proper way to do the capture.

The only way I know would be spawning a different world and add the actor, lights, and the sceneCapture2D there. Someone was trying this on this thread:

The only caveat is that you need to manually update the captures (check the last comment for details).

A workaround is a workaround. What’s wrong with spawning the actor in the world? That’s how HL2 did it, I imagine a lot of games still do that.

In your example, why does it specifically need to be done off-screen in another level?

How does one operate in two dimensions?

Thanks, I’ll check it.

First of all, it’s a sane way to do the job. Second, because I need unique lighting setup.

Technically there is little difference between doing that and setting a separate scene to render that object. If you are not content with that, rendering a mesh with a shader of your choice into a render target is trivial from the code.

Ok, I was finally able to get a picture from an off-screen world following the instructions above. The only snag I’ve got is the fact that UTextureTarget2D::ConstructTexture2D wont work in Game build, and Epic didn’t consider necessary to mention it anywhere, which is a pure crime.

There is a HUGE difference. I need a specific light setup for different kind of items to render and I dont want level lighting to interfere. If I could render it in offscreen world, I could just make a set of world assets with different lighting and give it to artists to play with.
Also avoiding of common sense violation is a good thing too.

This is exactly something what Nick Darnell was planning with viewport widget however he stopped working on that because shipped game supports only one UWorld now. It was UViewport widget which spawned and rendered its own pocket world. I ended up using “(1000000, 0, 0)” hack in my games.


Oh my god, how stupid that is. I’ve spent a MONTH to find out how to render a model to a texture in a private environment and it appears it’s impossible at all. This engine is completely broken.

Ehh what is the exact use case?

If its in Editor - the editor does this in many places.
If its in Game - the solution is SceneCapture + RenderTarget.

For the Game solution - put your rendered model in a black box off to the side of the level. You can do the lighting however you want there. Sounds easier than spawning a world and setting up the scene in code…

There is absolutely no way it could sound easier than spawning a world in a couple of code lines and setup everything you need in separate level. No a single way.
The way with “black box” in millions units away sounds like a hack\workaround and it is a hack\workaround to do a very basic thing that every minor 3D Engine should do in a matter of a couple of clicks\code lines.
The fact that even Nick Darnell couldn’t implement it in a separate world tells me that the structural design of the engine is ill. (And to be honest, I never doubted it)

Hey did you ever find a solution for this?
It’s really hard to understand how UE doesn’t provide a tool to do this in an easy way, considering it’s something that pretty much everyone needs.

Because the engineering effort needed to spawn an actor that cannot be seen is often much smaller than making sure the entire engine works correctly with multiple UWorlds outside the editor, something it wasn’t designed for. Pretty much everything actor-related and gameplay-related is tied to UWorld: GameInstance and GameMode, for example. That’s why you can easily test multiplayer and server/client scenarios in the editor: because each virtual client and server gets their own completely isolated UWorld. The moment you allow multiple UWorlds in runtime scenarios, you’ll need some sort of cross-world communication and you open a 10 ton container of worms: references to AActors from other worlds.

You don’t need a “black box hidden somewhere” either. The SceneCapture supports “render only these” lists, so it only captures the objects you tell it to. You can also keep these actors actually hidden (using the SetActorVisible function) instead of somewhere cumbersome like under the terrain and manually update the scene capture by unhiding them, capturing, then hiding them.