-
-
Notifications
You must be signed in to change notification settings - Fork 399
Refactor sprite and text renderable components #2929
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
Draft
cptbtptpbcptdtptp
wants to merge
10
commits into
galacean:dev/2.0
Choose a base branch
from
cptbtptpbcptdtptp:refactor/sprite-renderable
base: dev/2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92d7a08
refactor: sprite renderable and text renderable
cptbtptpbcptdtptp 3a3f9ed
refactor: sprite renderable and text renderable
cptbtptpbcptdtptp d5d44d9
feat: update code
cptbtptpbcptdtptp 5e9581c
feat: update code
cptbtptpbcptdtptp 1b0e993
feat: merge code
cptbtptpbcptdtptp 42e5098
feat: add ui mask
cptbtptpbcptdtptp b49d005
feat: delete examples
cptbtptpbcptdtptp 8e7fc13
feat: udpate code
cptbtptpbcptdtptp 2c1cecc
feat: add examples
cptbtptpbcptdtptp cc36ac3
refactor(2d): move common renderable properties to base classes
cptbtptpbcptdtptp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /** | ||
| * @title Text Chunk Leak | ||
| * @category 2D | ||
| */ | ||
| import { | ||
| Camera, | ||
| Color, | ||
| Entity, | ||
| Script, | ||
| WebGLEngine, | ||
| TextHorizontalAlignment, | ||
| } from "@galacean/engine"; | ||
| import { | ||
| CanvasRenderMode, | ||
| registerGUI, | ||
| Text, | ||
| UICanvas, | ||
| UITransform, | ||
| } from "@galacean/engine-ui"; | ||
|
|
||
| registerGUI(); | ||
|
|
||
| /** | ||
| * Bug reproduction: | ||
| * | ||
| * Text components share a single PrimitiveChunk (vertex buffer). | ||
| * When entity.isActive = false, _onDisableInScene does NOT call | ||
| * _freeTextChunks(), so the inactive Text's vertices remain in the | ||
| * shared buffer and are still drawn. | ||
| * | ||
| * Steps: | ||
| * 1. Text A ("得分:48") renders at top (allocates SubChunk in shared buffer) | ||
| * 2. After 3s, Text A's parent is disabled (isActive=false) | ||
| * → SubChunk NOT freed, vertices remain in buffer | ||
| * 3. Text B ("历史最高:48") activates at center | ||
| * → allocates new SubChunk in SAME PrimitiveChunk | ||
| * 4. Draw call submits entire buffer → Text A's old vertices still drawn | ||
| */ | ||
| class Controller extends Script { | ||
| textA_parent: Entity; | ||
| textB_parent: Entity; | ||
| textAScore: Text; | ||
|
|
||
| private _elapsed = 0; | ||
| private _score = 0; | ||
| private _switched = false; | ||
|
|
||
| onUpdate(dt: number) { | ||
| this._elapsed += dt; | ||
|
|
||
| if (!this._switched) { | ||
| // Update score every 0.3s to trigger chunk reallocation | ||
| if (this._elapsed > 0.3) { | ||
| this._elapsed -= 0.3; | ||
| this._score += Math.floor(Math.random() * 10) + 1; | ||
| this.textAScore.text = "" + this._score; | ||
| } | ||
| // After score > 40, switch panels | ||
| if (this._score > 40) { | ||
| this._switched = true; | ||
| // Hide panel A → chunks NOT freed (the bug) | ||
| this.textA_parent.isActive = false; | ||
| // Show panel B | ||
| this.textB_parent.isActive = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| const engine = await WebGLEngine.create({ | ||
| canvas: document.getElementById("canvas") as HTMLCanvasElement, | ||
| }); | ||
| engine.canvas.resizeByClientSize(); | ||
|
|
||
| const scene = engine.sceneManager.activeScene; | ||
| scene.background.solidColor.set(0.35, 0.3, 0.25, 1); | ||
| const rootEntity = scene.createRootEntity(); | ||
|
|
||
| const cameraEntity = rootEntity.createChild("camera"); | ||
| cameraEntity.transform.setPosition(0, 0, 10); | ||
| const camera = cameraEntity.addComponent(Camera); | ||
| camera.isOrthographic = true; | ||
| camera.orthographicSize = 5; | ||
|
|
||
| // UICanvas | ||
| const canvasEntity = rootEntity.createChild("Canvas"); | ||
| const uiCanvas = canvasEntity.addComponent(UICanvas); | ||
| uiCanvas.renderMode = CanvasRenderMode.ScreenSpaceOverlay; | ||
| uiCanvas.referenceResolutionX = 750; | ||
| uiCanvas.referenceResolutionY = 1334; | ||
|
|
||
| // ========== Panel A: HUD (visible initially) ========== | ||
| const panelA = canvasEntity.createChild("panelA"); | ||
|
|
||
| const labelA = panelA.createChild("labelA"); | ||
| labelA.getComponent(UITransform).setPosition(-40, 300, 0); | ||
| const textA = labelA.addComponent(Text); | ||
| textA.text = "得分:"; | ||
| textA.fontSize = 36; | ||
| textA.color = new Color(1, 1, 1, 1); | ||
| textA.enableWrapping = true; | ||
|
|
||
| const scoreA = panelA.createChild("scoreA"); | ||
| scoreA.getComponent(UITransform).setPosition(60, 300, 0); | ||
| const textAScore = scoreA.addComponent(Text); | ||
| textAScore.text = "0"; | ||
| textAScore.fontSize = 36; | ||
| textAScore.color = new Color(1, 1, 1, 1); | ||
| textAScore.enableWrapping = true; | ||
|
|
||
| // ========== Panel B: GameOver (hidden initially) ========== | ||
| const panelB = canvasEntity.createChild("panelB"); | ||
| panelB.isActive = false; | ||
|
|
||
| const labelB1 = panelB.createChild("labelB1"); | ||
| labelB1.getComponent(UITransform).setPosition(-60, 50, 0); | ||
| const textB1 = labelB1.addComponent(Text); | ||
| textB1.text = "历史最高:"; | ||
| textB1.fontSize = 40; | ||
| textB1.color = new Color(1, 1, 1, 1); | ||
| textB1.enableWrapping = true; | ||
|
|
||
| const scoreB1 = panelB.createChild("scoreB1"); | ||
| scoreB1.getComponent(UITransform).setPosition(120, 50, 0); | ||
| const textBScore1 = scoreB1.addComponent(Text); | ||
| textBScore1.text = "99"; | ||
| textBScore1.fontSize = 40; | ||
| textBScore1.color = new Color(1, 1, 1, 1); | ||
| textBScore1.enableWrapping = true; | ||
|
|
||
| const labelB2 = panelB.createChild("labelB2"); | ||
| labelB2.getComponent(UITransform).setPosition(-60, -50, 0); | ||
| const textB2 = labelB2.addComponent(Text); | ||
| textB2.text = "当前得分:"; | ||
| textB2.fontSize = 40; | ||
| textB2.color = new Color(1, 1, 1, 1); | ||
| textB2.enableWrapping = true; | ||
|
|
||
| const scoreB2 = panelB.createChild("scoreB2"); | ||
| scoreB2.getComponent(UITransform).setPosition(120, -50, 0); | ||
| const textBScore2 = scoreB2.addComponent(Text); | ||
| textBScore2.text = "0"; | ||
| textBScore2.fontSize = 40; | ||
| textBScore2.color = new Color(1, 1, 1, 1); | ||
| textBScore2.enableWrapping = true; | ||
|
|
||
| // ========== Controller ========== | ||
| const ctrl = rootEntity.addComponent(Controller); | ||
| ctrl.textA_parent = panelA; | ||
| ctrl.textB_parent = panelB; | ||
| ctrl.textAScore = textAScore; | ||
|
|
||
| engine.run(); | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,27 @@ | ||
| import { Matrix, Vector2 } from "@galacean/engine-math"; | ||
| import { ISpriteRenderer } from "./ISpriteRenderer"; | ||
| import { BoundingBox, Color, Matrix, Vector2 } from "@galacean/engine-math"; | ||
| import { PrimitiveChunkManager } from "../../RenderPipeline/PrimitiveChunkManager"; | ||
| import { SpriteTileMode } from "../enums/SpriteTileMode"; | ||
| import { SpritePrimitive } from "../sprite/SpritePrimitive"; | ||
|
|
||
| /** | ||
| * Interface for sprite assembler. | ||
| */ | ||
| export interface ISpriteAssembler { | ||
| resetData(renderer: ISpriteRenderer, vertexCount?: number): void; | ||
| resetData(primitive: SpritePrimitive, chunkManager: PrimitiveChunkManager, vertexCount?: number): void; | ||
| updatePositions( | ||
| renderer: ISpriteRenderer, | ||
| primitive: SpritePrimitive, | ||
| chunkManager: PrimitiveChunkManager, | ||
| worldMatrix: Matrix, | ||
| width: number, | ||
| height: number, | ||
| pivot: Vector2, | ||
| flipX: boolean, | ||
| flipY: boolean, | ||
| referenceResolutionPerUnit?: number | ||
| outBounds: BoundingBox, | ||
| referenceResolutionPerUnit?: number, | ||
| tileMode?: SpriteTileMode, | ||
| tiledAdaptiveThreshold?: number | ||
| ): void; | ||
| updateUVs(renderer: ISpriteRenderer): void; | ||
| updateColor(renderer: ISpriteRenderer, alpha: number): void; | ||
| updateUVs(primitive: SpritePrimitive): void; | ||
| updateColor(primitive: SpritePrimitive, color: Color, alpha: number): void; | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier violations here will break lint checks.
Static analysis already flags import formatting (Line 5–19) and a trailing comma in
WebGLEngine.create(Line 72).🧹 Suggested formatting fix
import { - Camera, - Color, - Entity, - Script, - WebGLEngine, - TextHorizontalAlignment, + Camera, Color, Entity, Script, WebGLEngine, TextHorizontalAlignment } from "@galacean/engine"; import { - CanvasRenderMode, - registerGUI, - Text, - UICanvas, - UITransform, + CanvasRenderMode, registerGUI, Text, UICanvas, UITransform } from "@galacean/engine-ui"; async function main() { const engine = await WebGLEngine.create({ - canvas: document.getElementById("canvas") as HTMLCanvasElement, + canvas: document.getElementById("canvas") as HTMLCanvasElement });Also applies to: 72-72
🧰 Tools
🪛 ESLint
[error] 5-12: Replace
⏎··Camera,⏎··Color,⏎··Entity,⏎··Script,⏎··WebGLEngine,⏎··TextHorizontalAlignment,⏎with·Camera,·Color,·Entity,·Script,·WebGLEngine,·TextHorizontalAlignment·(prettier/prettier)
[error] 13-19: Replace
⏎··CanvasRenderMode,⏎··registerGUI,⏎··Text,⏎··UICanvas,⏎··UITransform,⏎with·CanvasRenderMode,·registerGUI,·Text,·UICanvas,·UITransform·(prettier/prettier)
🤖 Prompt for AI Agents