8000 [3.86.0] Resize RenderTexture/dynamicTexture issue · Issue #6918 · phaserjs/phaser · GitHub
[go: up one dir, main page]

Skip to content

[3.86.0] Resize RenderTexture/dynamicTexture issue #6918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rexrainbow opened this issue Oct 11, 2024 · 3 comments
Open

[3.86.0] Resize RenderTexture/dynamicTexture issue #6918

rexrainbow opened this issue Oct 11, 2024 · 3 comments
Assignees

Comments

@rexrainbow
Copy link
Contributor
rexrainbow commented Oct 11, 2024

Version

  • Phaser Version: 3.86.0
  • Operating system: window
  • Browser: chrome/firefix

Description

In dynamicTexture's setSize method, it invokes renderTarget.resize method.
This renderTarget.resize method will check this.autoResize, but dynamicTexture's autoResize is false. So that the resize action won't run.

@photonstorm
Copy link
Collaborator

Hmm - yes. It's half right and half wrong. We don't want them to auto resize at all, because they should remain the same size they were created and shouldn't get changed if the renderer resizes. But equally, the setSize call needs a way to actually resize them. I'll have a think about it.

@Scross086
Copy link

Hi @photonstorm ,

Would this also be related? We are using the rope functionality and rendertexture in order to print 'curved text' to our game. In phaser 3.80.0, this works correctly, in 3.86 - we are seeing some kind of distortion with the same code:
Many thanks

3.80.0
image

3.86.0
image

`import { FontStyleKey } from '../../../config/font_style_key';
import { BaseScene } from '../../phaser_scenes/base_scene';
import { FontFactory } from '../factories/font_factory';

export type ICurvedTextParams = {
text: string;
textStyleKey: FontStyleKey;
getPoints: (textWidth: number) => { x: number; y: number }[];
offsetY: number;
};

export class CurvedText extends Phaser.GameObjects.Container {
private static id = 0;
protected renderTexture: Phaser.GameObjects.RenderTexture;
protected text = '';
protected textStyleKey: FontStyleKey;
protected tempText: Phaser.GameObjects.Text;
protected rope: Phaser.GameObjects.Rope;
protected targetTextureId: string;
protected getPoints: (textWidth: number) => { x: number; y: number }[];
protected offsetY: number;

constructor(scene: BaseScene, x = 0, y = 0, options: ICurvedTextParams) {
	super(scene, x, y);

	this.targetTextureId = `curved_text_${CurvedText.id}`;
	CurvedText.id += 1;

	this.textStyleKey = options.textStyleKey;
	this.getPoints = options.getPoints;
	this.offsetY = options.offsetY;

	this.renderTexture = scene.add.renderTexture(0, 0);
	this.renderTexture.setOrigin(0, 0);
	this.add(this.renderTexture);

	this.tempText = FontFactory.get.addText(this.textStyleKey, 0, 0, this.text, false, this.scene);
	this.tempText.setResolution(2);
	this.add(this.tempText);

	this.rope = this.scene.add.rope(0, 0, this.targetTextureId, undefined, this.getPoints(this.tempText.width), true);
	this.add(this.rope);

	this.setText(options.text);
}

/**
 * Update text. Please note, this is very expensive!
 * @param text new text
 */
public setText(text: string): void {
	if (text === this.text) {
		return;
	}
	this.text = text;
	this.updateRenderTexture();
}

protected updateRenderTexture(): void {
	this.tempText.text = this.text;
	this.tempText.setVisible(true);
	this.renderTexture.resize(this.tempText.width, this.tempText.height);
	this.renderTexture.clear();
	this.renderTexture.draw(this.tempText);

	this.renderTexture.saveTexture(this.targetTextureId);
	this.rope.setTexture(this.targetTextureId);
	this.rope.width = this.tempText.width;
	this.rope.height = this.tempText.height;
	this.rope.setPoints(this.getPoints(this.tempText.width));

	this.rope.x = 0;
	this.rope.y = this.offsetY;

	this.renderTexture.setVisible(false);
	this.tempText.setVisible(false);
}

protected override preDestroy(): void {
	super.preDestroy();
	this.rope.destroy();
	this.tempText.destroy();
	this.renderTexture.destroy();
}

getCurvedTextWidth() {
	return this.rope.displayWidth;
}
/**
 * Calculates the positions of points on a circle. Can be used to create a curved text as the function in TCurvedTextOptions.
 *
 * @param {number} radius - The radius of the circle.
 * @param {number} pointsNumber - The number of points to be calculated on the circle.
 * @param {boolean} curveUpwards - Optional. Specifies whether the curve should face upwards or downwards. Default is false.
 * @returns {Array} - An array of objects representing the x and y coordinates of the points on the circle.
 */
static getCirclePoints(radius: number, pointsNumber: number, curveUpwards = false) {
	return (textWidth: number) => {
		const step = Math.PI / pointsNumber;
		let ropePoints = pointsNumber - Math.round((textWidth / (radius * Math.PI)) * pointsNumber);
		ropePoints /= 2;
		const points = [];
		for (let i = pointsNumber - ropePoints; i > ropePoints; i--) {
			const x = radius * Math.cos(step * i);
			const y = radius * Math.sin(step * i);
			points.push({ x: x, y: curveUpwards ? y : -y });
		}
		return points;
	};
}

}

`

@photonstorm photonstorm self-assigned this Nov 16, 2024
@Fheuef
Copy link
Fheuef commented May 14, 2025

Not sure if it is entirely related, but I've run into a problem with resizing RenderTextures too.
Basically, I get 2 different results when drawing sprites onto a new RenderTexture, and when drawing onto a RT that has been resized to the same size.

When drawing the sprites on a new RT, they will be the correct size - the size of the original sprite.
But when drawing on a resized RT, the drawn sprites are scaled, it looks it's a ratio of the original RT size to its current one (if resized to 2x its original size, the sprites will be 2x their expected size), even though the scale of the RT is apparently still set to 1.

Might be worth noting that when I use rt.setSize() instead of rt.resize(), the sprites are drawn with the correct size ; but that's useless since the RT hasn't been refreshed in some way so it is just black outside of the original dimensions.

This was not the case last time I ran this code (version 3.60.0 apparently), looks line setSize() used to properly resize the RT and get the correct results when redrawing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0