focomoso Posted August 20, 2017 Share Posted August 20, 2017 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 Quote Link to comment Share on other sites More sharing options...
focomoso Posted August 21, 2017 Author Share Posted August 21, 2017 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... Quote Link to comment Share on other sites More sharing options...
brianzinn Posted August 21, 2017 Share Posted August 21, 2017 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. Quote Link to comment Share on other sites More sharing options...
focomoso Posted August 21, 2017 Author Share Posted August 21, 2017 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. Quote Link to comment Share on other sites More sharing options...
brianzinn Posted August 22, 2017 Share Posted August 22, 2017 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? Quote Link to comment Share on other sites More sharing options...
SvenFrankson Posted August 23, 2017 Share Posted August 23, 2017 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 ! Quote Link to comment Share on other sites More sharing options...
brianzinn Posted August 23, 2017 Share Posted August 23, 2017 I think this is solved. There is a question in "bugs" forum and looks like this was for BJS 2.5. Oddly enough my window.devicePixelRatio is 2.5 Quote Link to comment Share on other sites More sharing options...
SvenFrankson Posted August 23, 2017 Share Posted August 23, 2017 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) Quote Link to comment Share on other sites More sharing options...
brianzinn Posted August 23, 2017 Share Posted August 23, 2017 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.