简体   繁体   English

如何从Phaser的Sprite / Background中消除抖动

[英]How to remove jitterness from Phaser's Sprite / Background

I setup an example jsfiddle to illustrate this with proper assets. 我设置了一个示例jsfiddle ,用适当的资产来说明这一点。

When your character is moving and the camera starts to pan , you will notice the background has a small "jitterness". 当你的角色移动并且相机开始平移时 ,你会注意到背景有一个小的“抖动”。 This can be disabled by setting game.camera.roundPx to true. 可以通过将game.camera.roundPx设置为true来禁用此功能。

However, if that is disabled and you move the character. 但是,如果禁用它并移动角色。 Your character starts to jitter. 你的角色开始抖动。 Some things I've found in this adventure: 我在这次冒险中发现的一些事情:

  • This only happens when moving with body.velocity.x , under both P2 and Arcade physics. 只有在P2Arcade物理下使用body.velocity.x移动时才会发生这种情况。

  • If you move the character with body.x or just x it's absolutely fine. 如果你用body.x移动角色或只是x那就绝对没问题了。

  • If you remove the tilemap texture you can literally see the jitterness happen behold your eyes when moving. 如果你移除了瓷砖贴图纹理,你可以从字面上看到在移动时发生的抖动。 Example here -- Make sure you move far enough for the camera to pan. 示例此处 - 确保您移动足够远以使相机平移。

  • I've also tried game.renderer.renderSession.roundPixels = false; 我也试过game.renderer.renderSession.roundPixels = false; with no prevail. 没有优势。

  • This happens under CANVAS and WEBGL render modes 这在CANVAS和WEBGL渲染模式下发生

Great Question! 好问题! These jitters are caused by subpixel rendering . 这些抖动是由子像素渲染引起的。

Phaser can use non-integer values for the positions of sprites when game.camera.roundPx is false. game.camera.roundPx为false时,Phaser可以对sprite的位置使用非整数值。 According to the documentation for roundPx : 根据roundPx的文档:

If a Camera has roundPx set to true it will call view.floor as part of its update loop, keeping its boundary to integer values. 如果Camera将roundPx设置为true ,它将调用view.floor作为其更新循环的一部分,将其边界保持为整数值。 Set this to false to disable this from happening. 将此设置为false可禁用此操作。

view.floor : view.floor

Runs Math.floor() on both the x and y values of this Rectangle. 在此Rectangle的x和y值上运行Math.floor()。

When drawing to non-integer positions, the browser tries to interpolate to make it appear as if a pixel is placed between two pixels. 当绘制到非整数位置时,浏览器会尝试进行插值,使其看起来好像像素位于两个像素之间。 This can cause a single pixel of your source image to be displayed as two physical pixels. 这可能会导致源图像的单个像素显示为两个物理像素。 The switching between these two states when the camera pans is what is causing the jittering. 当摄像机平移时,这两种状态之间的切换是导致抖动的原因。 Here's an example: 这是一个例子:

兔子在亚像素和正常空间中渲染

That's why setting game.camera.roundPx = true fixes it. 这就是设置game.camera.roundPx = true的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM