On Pre Render Event?

Unity3D has a script callback for when a camera starts rendering the scene. Is there a similar event in UE4? I want to be able to change the materials of some objects in the scene depending what camera is being used to render them.

1 Like

I need this too for similar reasons. Any help on this appreciated

Perhaps look at the camera class in the engine to see if there are any virtual methods you could ā€œsneakā€ in some code on top of. Worst case is you have to modify the function of the camera class that UE4 uses to get its transform before rendering to be virtual, then add a little pre-super code to change the materials.

I didnā€™t have a terrible amount of luck going through this approach when I first started investigating. Unfortunately I donā€™t know enough about UE4 as an engine to know if Iā€™m just looking the wrong place or if for whatever reason this isnā€™t a notion in the engine.

Iā€™m still looking for good solutions to this too. I tried what spychiatrist said, but this is not a good place as its called for all active cameras before any rendering is done.
I need camera1 prerender, camera1 render, camera2 prerender, camera2 render, etc
I was thinking to look at creating a custom FViewport and implement the BeginRenderFrame function.
Does anyone know if each camera has a viewport or itā€™s shared ?

You should possibly check the viewtarget for the current playercontroller.

Say there are 2 camera actors placed in your scene then if your player is viewing through Camera 1 then the player controllerā€™s view target should be camera 1 Actor. If you are using a camera component in a pawn or character (lets call it player pawn) then the view target will be PlayerPawn Actor.

Now just check the view target your player controller has to determine which camera is scene currently being rendered with.

EDIT:As an alternative to keep things efficient and simple youā€™d want to set the material and other things just as you change the camera using ā€œSetViewTargetWithBlendā€

Processing on Game thread and Render thread happens simultaneously, but Render thread is one frame behind Game thread.

This allows engine to take advantage of multi-core, at the cost of Game thread not actually knowing if actor has been rendered or not. Hence, there is no easy way to implement callback on Game thread ā€˜beforeā€™ item has been rendered.

However, there are several things you can do, to achieve what you need:

Every actor has Tick() function that you can override and make it execute each frame (and it will always be before rendering of that frame begins). If you need one global processor, you can have one actor without rendering components, just so it can tick each frame and do whatever you need done across the game.

You could also implement this functionality in your levelā€™s Tick().

If you have specific actor that needs to do something, and for performance reasons you only want this done when its rendered, you could do a check inside Tick() for when was this actors LastRenderTime ā€“ see AActor::GetLastRenderTime()

You could do something like this in Actorā€™s Tick:



if (GetWorld()->TimeSince(GetLastRenderTime()) <= 1.f)
{
    // do stuff only when this actor is rendered
}


Thanks for your reply xulture, but i donā€™t think this will work for what i have in mind.
Basically I would like to render a ā€œnormalā€ visible image and an infrared image for the same game time.
This is for a simulation, so performance not critical.

Something like below is what iā€™m after:

Tick
<change materials here>
Render Visible Image
<change materials here>
Render Infrared Image
Tick
<change materials here>
Render Visible Image
<change materials here>
Render Infrared Image
ā€¦

Post above me sounds like what you need to do is either create a new GBuffer and render to that, or do it via a Post-Process material that you can modify on the fly. The latter is the easiest method, especially with the new Custom Depth channels we got recently.

As Xulture says though, there is no overrideable pre-render function in UE4.

In case if you do want to do some extra processing being inside rendering thread, you can use FCoreDelegates::OnBeginFrameRT callback.

PS: xulture solution, in the best case, would cause flickering, I would not recommend to use that.

If its for different modes like an infared mode or whatnot, the question is are the modes visible at the exact same time? For example if you have a world and objects in the world, are certain objects rendered as infrared and others not, or it it a binary global operation (everything is infrared or its rendered normally)

These might help depending on what effect you are going for:Unreal Engine 4 - Widowmaker's Infrared Vision - YouTube
UE4 - Night-vision Infrared Effect - YouTube