Jump to content

What might make engine.getRenderWidth and getRenderHeight return doubled?


focomoso
 Share

Recommended Posts

I'm trying to convert a 3d point to the 2d screen coordinates. Everything seems to work fine, except that engine.getRenderWidth() and engine.getRenderHeight() are returning exactly double the actual canvas width and height so my points are coming back twice as big.

I tried to create a playground for this, but could not reproduce the problem.

Does anyone have any ideas what might make the renderWidth and height come back doubled?

Thanks

Link to comment
Share on other sites

I figured out the issue, but not a work around. This only happens on my mac's retina display. On a non-retina the sizes come back as expected. 

Edit: It only happens when the engine is created on the retina display. If start the engine on a regular display and then move it to retina. Everything works fine. If I start the engine on a retina display and then move over to regular, I get this doubled size. So, there must be something funky going on when the engine initializes and those values are set...

Link to comment
Share on other sites

retina display has window.devicePixelRation of 2.  Look at setting the engine.setScalingLevel(...), if you change after engine is initialized?

Just reading a bit and if that's the case when changing displays then the window.resize is not fired.  Looks like you would need something in the game loop to check window width/height.

Link to comment
Share on other sites

I found a fix. If you check engine.getHardwareScalingLevel(), it'll be .5 on the retina and 1 on regular displays.

I think this qualifies as a bug because it means that engine.getRenderWidth() / height give unexpected results on retina displays (and I haven't even tested other hi-res displays). It should take the hardwareScaling into account as scene.pointerX and Y apparently do.

Link to comment
Share on other sites

Do you start your engine with adaptToDeviceRatio : true?  (last constructor parameter default = false for Engine: http://doc.babylonjs.com/classes/3.0/engine).

I don't think the engine resize event is triggered when you switch displays as I don't think there is a listener for that in the browser, but only on resize.  It's an obvious over simplification, but did you try something in your game loop like:

scene.registerBeforeRender(() =>
   engine.setHardwareScalingLevel(1.0/window.devicePixelRatio)
)

If you look in the engine constructor that behaviour makes sense. https://github.com/BabylonJS/Babylon.js/blob/master/src/babylon.engine.ts#L772
Maybe if adaptToDeviceRatio is true that should be default behaviour in BabylonJS?

Link to comment
Share on other sites

Why don't you use canvas.clientHeight and canvas.clientWidth ? Unless I misunderstood your issue, it's the value you're looking for.

(or engine.getRenderingCanvas().clientWidth, if for some reason you can't access the canvas object directly, like in the playground)

For what it worth, to me, it does make sense that the renderWidth is different from the canvas width when you're working on a device with a pixel ratio not equal to 1. The engine renders on a texture, which resolution is set in hardware pixel. If you wan't to CSS this texture or mouse over it, it's expressed in logical pixels.

I have a lot of issues with the concept of pixel ratio lately, when I got my first screen with a pixel ratio > 1. These articles were kinda helpful :

https://stackoverflow.com/questions/8785643/what-exactly-is-device-pixel-ratio

https://alistapart.com/article/a-pixel-identity-crisis

As well as brianzinn inputs here : 

 

Good luck !

Link to comment
Share on other sites

Ok maybe I misunderstood the issue ^^

Here's a playground were hardwareScaling is forced to 0.5 http://playground.babylonjs.com/#EE38I5#1 , I think it displays the values focomoso described in his first post

It's canvas.clientHeight, which takes into account the device pixel ratio (not canvas.height ,as I thought)

Link to comment
Share on other sites

No, it's more likely me that would misunderstand an issue!  I thought it was an issue switching screens with different pixel ratios.  When I switch screens my devicePixelRatio switches from 1 and 2.5, although hardware scaling does not change.  At least with engine settings in that PG...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...