Using a DepthRenderer with a specific texture size

Hello,

I’m trying to set a specific size for a depth texture created with the DepthRenderer, and therefore with a different camera than the main camera which has an aspect ratio that depends on the screen.

Unfortunately there’s no size parameter in enableDepthRenderer().

So far the only option I’ve found is calling resize() on the depthMap (RenderTargetTexture) after it has been created.

The problem is that the depth camera is still on a wrong aspect ratio, moreover it changes when I resize the window. I’ve found no way to force a specific aspect ratio, nor preventing it from changing with resizes.

Here’s a playground

Copying the source for Camera.getProjectionMatrix(), I can set the projection matrix manually with the RTT’s aspect ratio, and use Camera.freezeProjectionMatrix().

Does anyone know of a better way without accessing private fields?

You could always do and RTT, and then set the materials to a depth shader on application then restore their material after the effect propagates. Then on the RTT you can set a rendering ratio and size.

I’ve done half and quarter size rendering passes this way in the past.

Don’t forget to update the size on an engine resize event though.

Usually, once you set the size of the depth texture like you have, it won’t change but you can enforce these two properties:

  • useOnlyInActiveCamera: force using the texture ONLY for the active camera.. swapping cameras shouldn’t change it’s size at that point.
  • ignoreCameraViewport: to ignore the camera viewport and force using the entire texture specified for rendering. i.e. don’t just clip to camera viewport.

If you need to monitor the texture size because of window sizing changes, you would need to recreate the texture and resize again according to whatever aspect ratio you would want to enforce… BUT that shouldn’t be required.

@Pryme8 I can’t see how that would help with my issue

@ntech ignoreCameraViewport is already set in DepthRendere’s constructor, your playground changes nothing (the depth texture displayed on the plane still changes when resizing)

I don’t think useOnlyInActiveCamera would help, as according the the description I wouldn’t be able to use it when rendering with the main camera

Specifies that the depth renderer will only be used within the camera it is created for. This can help forcing its rendering during the camera processing.

Anyway thanks, freezing the projection transform and setting it manually works.

Also to answer my own question, using depthCamera.getProjectionMatrix() instead of depthCamera._projectionMatrix works as once the projection matrix is frozen, it’s acting as an accessor.

I wish there would be an easier/documented way, but it looks as the way to go for now.

because you can set the texture size and render a depth shader with it? I gave you exactly how you would do it…

With RTT you can specify the size of the texture and set a custom material for when its applied IDK what else you would need. I have done it several times in the past. Its really quite easy.

Also looks like ntech showed you specifically how to do it with a depthRenderer

And there-in is the issue, still because on mine it doesn’t. Desktop nor mobile change size when resizing. If you have to absolutely freeze the matrix, and that solves the issue, then do that but it shouldn’t have been needed. :person_shrugging:

Is this on an iPhone?

I’m not getting the resizing either. What browser are you on?

The problem is with the doNotChangeAspectRatio parameter of the RenderTargetTexture constructor, which is set to true by the depth renderer when it creates an instance of RenderTargetTexture.

You can overcome this limitation in a hacky way by doing:

This PR will let you pass your own RenderTargetTexture instance to the depth renderer:

Once it is merged, this PR will work:

That is a cool solution Evgeni! Also saves some steps than doing it with your own RTT and shader swizzling.

Thanks @Evgeni_Popov

doNotChangeAspectRatio?: boolean

True (default) to not change the aspect ratio of the scene in the RTT

I think this was rather unclear in my head when I read it previously. Does it mean True will apply the aspect ratio of the RTT instead of the aspect ratio of the camera/engine? (not sure what the aspect ratio of a scene with multiple cameras would be)

@Pryme8 I understand your suggestion was to create a RTT and render with a custom depth shader… that’s what the DepthRenderer is doing and what is used in my playground. I’d rather use existing components than rewrite everything, right?

About the resizing: Firefox or Chrome give the same result on laptop. Try move the slider in the middle, the content of depth texture will change, outlining that it was influenced by the engine aspect ratio. Sorry if this was unclear.
(you can also see it change on the camera’s frustum)

The value of doNotChangeAspectRatio is relative to the default aspect ratio, that is the one coming from the screen (screen_width / screen_height, or the other way around!). When false, it means the RTT will use rtt_width / rtt_height instead when calculating the projection matrix.