Skip to content
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

Fix TextRenderer or SpriteRenderer rendering errors #2340

Merged
merged 8 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions e2e/case/text-typed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @title TypedText
* @category Text
*/

import {
Camera,
Logger,
Script,
TextHorizontalAlignment,
TextRenderer,
TextVerticalAlignment,
Vector3,
WebGLEngine
} from "@galacean/engine";
import { initScreenshot, updateForE2E } from "./.mockForE2E";

Logger.enable();
WebGLEngine.create({ canvas: "canvas" }).then((engine) => {
engine.canvas.resizeByClientSize();
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity();

// camera
const cameraEntity = rootEntity.createChild("camera_node");
cameraEntity.transform.position = new Vector3(0, 0, 10);
const camera = cameraEntity.addComponent(Camera);

const entity = rootEntity.createChild("text");
const textRenderer = entity.addComponent(TextRenderer);
textRenderer.fontSize = 64;
textRenderer.horizontalAlignment = TextHorizontalAlignment.Left;
textRenderer.verticalAlignment = TextVerticalAlignment.Top;
textRenderer.enableWrapping = true;
textRenderer.width = 4;

class TypedText extends Script {
private _renderer: TextRenderer;
private _text: string;
private _index = 0;
private _charCount = 0;
private _showText = "";
private _curTime = 0;
private _totalTime = 0.1;
private _isPlaying = false;

onUpdate(deltaTime: number): void {
if (this._isPlaying) {
if (this._curTime >= this._totalTime) {
if (this._index >= this._charCount) {
this._isPlaying = false;
initScreenshot(engine, camera);
} else {
this._showText += this._text[this._index++];
this._renderer.text = this._showText;
}
this._curTime = 0;
} else {
this._curTime += deltaTime;
}
}
}

play(textRenderer: TextRenderer, text: string = ""): void {
this._renderer = textRenderer;
this._text = text;
this._index = 0;
this._charCount = text.length;
this._showText = "";
this._curTime = this._totalTime;
this._isPlaying = true;
}
}

const typedText = entity.addComponent(TypedText);
typedText.play(textRenderer, "我这一生,走过许多地方的桥儿");

updateForE2E(engine, 100, 100);
});
7 changes: 7 additions & 0 deletions e2e/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,12 @@ export const E2E_CONFIG = {
caseFileName: "postProcess-LDR-bloom-neutral",
threshold: 0.2
}
},
Text: {
TypedText: {
category: "Text",
caseFileName: "text-typed",
threshold: 0.4
}
}
};
3 changes: 3 additions & 0 deletions e2e/fixtures/originImage/Text_text-typed.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 37 additions & 26 deletions packages/core/src/RenderPipeline/PrimitiveChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,38 +138,49 @@
}

private _freeArea(area: VertexArea): void {
const areas = this.vertexFreeAreas;
const areaLen = areas.length;
const freeAreas = this.vertexFreeAreas;
const areaLen = freeAreas.length;
if (areaLen === 0) {
areas.push(area);
freeAreas.push(area);

Check warning on line 144 in packages/core/src/RenderPipeline/PrimitiveChunk.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/RenderPipeline/PrimitiveChunk.ts#L144

Added line #L144 was not covered by tests
return;
}

const { start, size } = area;
const end = start + size;
const pool = PrimitiveChunk.areaPool;
let preArea = area;
let notMerge = true;
for (let i = 0; i < areaLen; ++i) {
const curArea = areas[i];
const { start: preStart, size } = preArea;
const { start: curStart } = curArea;
const preEnd = preStart + size;
const curEnd = curStart + curArea.size;
if (preEnd < curStart) {
notMerge && areas.splice(i, 0, preArea);
return;
} else if (preEnd === curStart) {
curArea.start = preStart;
curArea.size += size;
pool.return(preArea);
preArea = curArea;
notMerge = false;
} else if (preStart === curEnd) {
curArea.size += size;
pool.return(preArea);
preArea = curArea;
notMerge = false;
} else if (preStart > curEnd) {
i + 1 === areaLen && areas.push(preArea);
const curFreeArea = freeAreas[i];
const curStart = curFreeArea.start;
const curEnd = curStart + curFreeArea.size;

if (end < curStart) {
// The area to be freed is to the left of the current free area and is not connected
freeAreas.splice(i, 0, area);
break;
} else if (end === curStart) {
// The area to be freed is to the left of the current free area and is connected
curFreeArea.start = start;
curFreeArea.size += size;
pool.return(area);
break;
} else if (start === curEnd) {
// The area to be freed is to the right of the current free area and is connected
curFreeArea.size += size;
pool.return(area);
const nextIndex = i + 1;
if (nextIndex < areaLen) {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
const nextFreeArea = freeAreas[nextIndex];
if (end === nextFreeArea.start) {
// The cur free area after merge is to the left of the next free area and is connected
curFreeArea.size += nextFreeArea.size;
freeAreas.splice(nextIndex, 1);
pool.return(nextFreeArea);
}
}
break;
} else if (start > curEnd) {
// The area to be freed is to the right of the current free area and is not connected
i + 1 === areaLen && freeAreas.push(area);
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waste performance,use fallback mode: if not success, push it at end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
}
Expand Down
Loading