question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebGL: RenderTexture frames do not render correctly

See original GitHub issue

Version

  • Phaser Version: 3.55, 3.6-beta4
  • Operating system: Windows (but also probably OSX)
  • Browser: Chrome, FF, etc

Description

Adding and referencing frames on a RenderTexture produces visual errors, depending on how that RT frame is rendered. This only occurs in WebGL mode.

The following demonstrates the expected behavior, using Canvas mode. Each quadrant/square is a separate frame on the original render texture. Canvas

The following demonstrates the exact same code, but in WebGL mode: WebGL

With some fiddling, it’s apparent that there is some issue with Y-flipping; If I adjust the Y-coords of the frames*, I’m able to get some frames to render - albeit incorrectly flipped: * ex: rt.texture.add('my-frame', 0, x, y, frameWidth, textureHeight - frameHeight); - Note the textureHeight - adjustment

WebGL + semi-Y-flipped frames

I believe there are two issues at hand here (based on my current understanding of the Phaser codebase):

  • Frames registered on RenderTextures need to be Y-Flipped
  • Certain rendering objects need to account for Y-flipped textures

Example Test Code

This codepen reproduces this issue - note the USE_WEBGL flag up top to easily change the current rendering mode.

https://codepen.io/andymikulski/pen/oNoyBGy

The gist, in psuedocode, is really:

const rt = this.add.renderTexture(...);
drawStuffToRT(rt);
rt.saveTexture('my-texture');
rt.texture.add('my-frame', frameX, frameY, frameWidth, frameHeight);

this.add.image(x, y, 'my-texture', 'my-frame'); // Renders upside down - or not at all

Additional Information

I have dug around the code and noticed that checking if a texture is a glTexture and y-flipping accordingly seems to work, but this requires touching MultiPipeline, Blitter, etc., and I wonder if there is a more ‘central’ fix available.

I’m happy to work on these solutions, but I wanted to get some sort of input before putting in the effort! Thank you!

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
photonstormcommented, May 30, 2022

Spent some more time on this today - and getting close to a fix. Just trying to make it universal, which isn’t easy.

3reactions
photonstormcommented, May 30, 2022

Ok, a fix for this is now in the master branch. Use it as follows:

        const origin = this.add.renderTexture(0, 0, 256, 256).setIsSpriteTexture(true);

        origin.draw('mario', 0, 0);

        origin.saveTexture('test-texture');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Render Texture HTML5 WebGL Broken - Unity Forum
It looks like the Custom Render Texture is broken for html5 builds. It renders one frame then stops updating the texture.
Read more >
WebGL2 Rendering to a Texture
We don't need to supply any data. We just need WebGL to allocate the texture. Next we create a framebufffer. A framebuffer is...
Read more >
WebGL best practices - Web APIs - MDN Web Docs - Mozilla
WebGL is a complicated API, and it's often not obvious what the recommended ways to use it are. This page tackles recommendations across...
Read more >
Phaser 3 API Documentation - Class: RenderTexture
It's a way to generate dynamic textures at run-time that are WebGL friendly and don't invoke expensive GPU uploads. Note that under WebGL...
Read more >
Scripting API: RenderTexture - Unity - Manual
Keep in mind that render texture contents can become "lost" on certain events, like loading a new level, system going to a screensaver...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found