2
\$\begingroup\$

I have some custom code that renders to Direct3D11 texture. Is it possible to use this texture on an object in unreal 4? Or alternatively, is it possible to draw custom geometry directly to the scene in unreal 4 using raw Direct3d?

So far, it seems like I need to make a custom instance of UPrimitiveComponent which creates and returns an FPrimitiveSceneProxy. In the FPrimitiveSceneProxy::CreateRenderThreadResources I can call RHIGetNativeDevice and cast its return to ID3D11Device and use it to create Direct3D instances, however, I'm not sure where to put my per-frame draw calls.

\$\endgroup\$
2
  • \$\begingroup\$ Did you read this post? answers.unrealengine.com/questions/255780/… \$\endgroup\$
    – Tim Holt
    Commented Oct 26, 2017 at 4:24
  • \$\begingroup\$ I have read that. I'm stuck at "Hard part is to synchronize rendering from the GUI framework with engine:)". \$\endgroup\$
    – default
    Commented Oct 26, 2017 at 12:45

1 Answer 1

3
\$\begingroup\$

I figured out a way to render to texture.

  1. Create a new c++ component that has a UTextureRenderTarget* property
  2. Create a render target in unreal. I followed the steps in this short tutorial create a render target, but instead of assigning it to the screen capture camera, I assigned it to the component I made in step 1.
  3. Call ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER from TickComponent (or some other game thread function). In my case, my queued code just calls a Render method on a class that has a pointer to the UTextureRenderTarget named renderTarget.
  4. In my Render method, I can access the ID3D11Texture2D as follows:

    FTextureRenderTarget2DResource* rtRes = static_cast<FTextureRenderTarget2DResource*>(renderTarget->Resource); ID3D11Texture2D* d3dTexture = static_cast<ID3D11Texture2D*>(rtRes->GetTextureRHI()->GetNativeResource());

Then you can create an ID3D11RenderTargetView that maps to the ID3D11Texture2D and render. In my case, I got it working by using the DXGI_FORMAT_R16G16B16A16_FLOAT and also setting my render target to the equivalent setting in unreal.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .