-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Feat:The first version of text system #686
Merged
+1,193
−2
Merged
Changes from 97 commits
Commits
Show all changes
106 commits
Select commit
Hold shift + click to select a range
2b94bb7
feat(text): init TextRenderer
singlecoder 279bc2b
Merge branch 'dev/0.6' of https://github.com/oasis-engine/engine into…
singlecoder a707c94
feat(text): add bounds
singlecoder 0108031
Merge branch 'dev/0.6' of https://github.com/oasis-engine/engine into…
singlecoder 35355e2
Merge branch 'dev/0.6' of https://github.com/oasis-engine/engine into…
singlecoder 81f8fe5
feat(text): first basic version
singlecoder 1a0105a
feat(text): fix black border
singlecoder e57fdf9
feat(text): opt canvas for 2d
singlecoder 1a0862d
feat(text): opt text color
singlecoder 359ba5f
merge main (#576)
GuoLei1990 c1a7473
feat(text): fix conflicts
singlecoder 28455a8
feat(text): opt code
singlecoder 99dde83
feat(text): modify version
singlecoder 7df7a01
feat(text): fix conflicts
singlecoder 80d3fa8
feat(text): fix conflicts
singlecoder 9a30dbe
feat: text (#586)
singlecoder dbd84ad
Dev/0.6 plus (#590)
GuoLei1990 1f1b45c
Merge latest main (#596)
GuoLei1990 1337de8
feat(text): text renderer handle text is empty (#597)
singlecoder df6ef22
Plus/ibl rotation (#599)
zhuxudong d56ed3d
fix: stop use collison detection i n lite package (#606)
yangfengzzz 565c59d
Merge latest `main` to `dev/0.6-plus` (#612)
GuoLei1990 338512b
fix: front face bug (#613)
GuoLei1990 b3f5503
Fix Reject Error (#624)
gz65555 9a24887
Fix iOS not support promise.finally (#626)
gz65555 422fa16
feat: adapt text for paladin (#625)
singlecoder 041f94b
fix `entity` and `glTF` destroy bug (#629)
GuoLei1990 d37b4c7
fixBug:No exception is thrown after the atlas fails to load (#631)
cptbtptpbcptdtptp e65cc89
fix(text): conflicts
7a6885b
feat(text): chagne TextRenderer base class change to Renderer
9d27c44
feat(text): text first version
1fcf71d
feat(text): text first version
singlecoder 598dcac
Merge branch 'feat/text' of https://github.com/singlecoder/engine int…
singlecoder eb8a2e6
fix(text): wrap width not avalid
singlecoder 2ad5d8a
feat(text): opt code
singlecoder 7053a4d
feat(text): opt code
singlecoder 7128985
feat(text): add dynamic atlas
singlecoder a6caa94
feat(text): temp code
singlecoder a91c1d4
feat(text): dynamic atlas first version
singlecoder d3e4ed9
feat(text): opt code for addSprite
singlecoder 916abb0
feat(text): opt memory for text renderer
singlecoder c88417f
feat(text): opt code
singlecoder 840c998
feat(text): opt code
singlecoder 09fe2fe
feat(text): opt code
singlecoder 7969a6b
feat(text): opt code
singlecoder a90938a
feat(text): opt code
singlecoder 6ef0499
feat(text): fix texture mipmap error
singlecoder efd617d
feat(text): fix conflicts
singlecoder 2eccca9
fix: normal attributer is need in unlit (#637)
zhuxudong b28c936
fix: physx bug (#651)
yangfengzzz 55ce80f
v0.6.7
GuoLei1990 a704854
fix: get pixel buffer use mip level should right shift (#656)
gz65555 070e621
Update AnimatorState.ts (#657)
luzhuang ffe530c
Rename `ability` to `component`. (#608)
eyworldwide bec99aa
fix: resize max uniform vector count (#660)
zhuxudong 1bb360f
fix: fix conflicts
singlecoder c31a820
Merge branch 'dev/0.7' of https://github.com/oasis-engine/engine into…
singlecoder 42ab0bb
feat(text): fix when text change but position not update
singlecoder 2db0f20
Merge branch 'dev/0.7' of https://github.com/oasis-engine/engine into…
singlecoder ce9575b
feat(text): fix conflicts
singlecoder 9942fd5
Merge remote-tracking branch 'origin' into feat/text
singlecoder 85d9871
feat(text): fix conflicts
singlecoder 709e3e9
feat(text): fix conflicts
singlecoder 120a86f
feat(text): fix conflicts
singlecoder 793dc2b
feat(text): fix conflicts
singlecoder ff379b2
Merge branch 'dev/0.7' of https://github.com/oasis-engine/engine into…
singlecoder 7ddd1ec
feat(text): opt code
singlecoder 7797e47
feat(text): move dynamic atlas manager to internal
singlecoder a117250
feat(text): opt code
singlecoder a5eca02
feat(text): delete pre multiple
singlecoder 6ef9176
feat(text): delete line
singlecoder bbb5c9e
feat(text): opt code, opt property name from lineSpace to lineSpacing
singlecoder 0383302
feat(text): add text style
singlecoder 0a68e20
Merge branch 'dev/0.7' of https://github.com/oasis-engine/engine into…
singlecoder 01585ca
feat(text): add Font asset, and change fontName to font
singlecoder 5c5e6eb
feat(text): opt api change _horizontalOverflow to _enableWarpping
singlecoder 697be15
feat(text): rename overflow and wrap
singlecoder 6ee9989
feat(text): opt recomment
singlecoder 12dc4df
feat(text): spelling error correction
singlecoder 03e3d10
feat(text): opt font style
singlecoder 52c6552
feat(text): opt code
singlecoder a06182f
feat(text): close export dynamic text atlas manager
singlecoder c2945fb
feat(text): opt property name for Font
singlecoder 33fc8eb
feat(text): opt recommend
singlecoder 423a22b
feat(text): opt comment
singlecoder 5b871c2
feat(text): opt dynamic atlas for text
singlecoder 64f9b39
Merge branch 'dev/0.7' of https://github.com/oasis-engine/engine into…
singlecoder f4c66c7
feat(text): delete invalid import
singlecoder 0483562
feat(text): opt code
singlecoder 9fc8888
feat(text): opt code
singlecoder 3669896
feat(text): add generic font families
singlecoder f2f839f
feat(text): remove TextRenderer from TextUtils
singlecoder 177db10
feat(text): opt code
singlecoder ee84f20
feat(text): opt code
singlecoder d284767
feat(text): opt code
singlecoder 8a1baf7
feat(text): opt code
singlecoder 33f89ab
feat(text): fix type error
singlecoder b3cbdd4
feat(text): opt code for Font
singlecoder 10f548d
feat(text): opt code for create font
singlecoder 4f5bd7d
feat(text): opt code
singlecoder 197f3f2
feat(text): opt code
singlecoder 67a127d
feat(text): opt code
singlecoder cc7edb2
feat(text): opt code
singlecoder f8c22d4
feat(text): opt code
singlecoder 0aea6d0
feat(text): opt code
singlecoder fc4434c
feat(text): opt code for trimCanvas
singlecoder 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 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 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,95 @@ | ||
import { Rect } from "@oasis-engine/math"; | ||
import { Engine } from "../../Engine"; | ||
import { Texture2D } from "../../texture/Texture2D"; | ||
import { Sprite } from "../sprite/Sprite"; | ||
|
||
/** | ||
* Dynamic atlas for text. | ||
*/ | ||
export class DynamicTextAtlas { | ||
private static _region: Rect = new Rect(); | ||
|
||
private _texture: Texture2D; | ||
private _width: number; | ||
private _height: number; | ||
|
||
private _space: number = 1; | ||
private _curX: number = 1; | ||
private _curY: number = 1; | ||
private _nextY: number = 1; | ||
|
||
private _sprites: Record<number, Sprite> = {}; | ||
|
||
constructor(engine: Engine, width: number, height: number) { | ||
this._width = width; | ||
this._height = height; | ||
this._texture = new Texture2D(engine, width, height); | ||
this._texture._addRefCount(1); | ||
} | ||
|
||
/** | ||
* Destroy atlas, it will release the texture. | ||
*/ | ||
public destroy() { | ||
this._sprites = {}; | ||
this._texture.destroy(true); | ||
} | ||
|
||
/** | ||
* Add a sprite. | ||
* @param sprite - the sprite to add | ||
* @param imageSource - The source of texture | ||
* @returns true if add sprite success, otherwise false | ||
*/ | ||
public addSprite(sprite: Sprite, imageSource: TexImageSource | OffscreenCanvas): boolean { | ||
const { _space: space, _texture: texture } = this; | ||
const { width, height } = imageSource; | ||
|
||
const endX = this._curX + width + space; | ||
if (endX >= this._width) { | ||
this._curX = space; | ||
this._curY = this._nextY + space; | ||
} | ||
|
||
const endY = this._curY + height + space; | ||
if (endY > this._nextY) { | ||
this._nextY = endY; | ||
} | ||
|
||
if (this._nextY >= this._height) { | ||
return false; | ||
} | ||
|
||
texture.setImageSource(imageSource, 0, false, false, this._curX, this._curY); | ||
texture.generateMipmaps(); | ||
|
||
const { _width, _height } = this; | ||
const region = DynamicTextAtlas._region; | ||
region.setValue(this._curX / _width, this._curY / _height, width / _width, height / _height); | ||
|
||
// destroy origin texture. | ||
sprite.texture && sprite.texture.destroy(); | ||
// Update atlas texture. | ||
sprite.atlasRegion = region; | ||
sprite.texture = texture; | ||
this._curX = endX + space; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Remove a sprite. | ||
* @param sprite - the sprite to remove | ||
* @returns true if remove sprite success, otherwise false | ||
*/ | ||
public removeSprite(sprite: Sprite): boolean { | ||
const id = sprite.instanceId; | ||
const { _sprites } = this; | ||
if (_sprites[id]) { | ||
delete _sprites[id]; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
126 changes: 126 additions & 0 deletions
126
packages/core/src/2d/dynamic-atlas/DynamicTextAtlasManager.ts
This file contains 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,126 @@ | ||
import { Sprite } from "../sprite/Sprite"; | ||
import { Engine } from "../../Engine"; | ||
import { DynamicTextAtlas } from "./DynamicTextAtlas"; | ||
|
||
/** | ||
* Dynamic atlas manager for text. | ||
*/ | ||
export class DynamicTextAtlasManager { | ||
private _maxAtlasCount: number = 2; | ||
private _textureSize: number = 1024; | ||
private _atlases: Array<DynamicTextAtlas> = []; | ||
private _atlasIndex: number = -1; | ||
private _spritesInAtlasIndex: Record<number, number> = {}; | ||
|
||
/** | ||
* Indicates how many atlases should be created. | ||
*/ | ||
get maxAtlasCount(): number { | ||
return this._maxAtlasCount; | ||
} | ||
|
||
set maxAtlasCount(val: number) { | ||
this._maxAtlasCount = val; | ||
} | ||
|
||
/** | ||
* Indicates the size of the texture. | ||
*/ | ||
get textureSize(): number { | ||
return this._textureSize; | ||
} | ||
|
||
set textureSize(val: number) { | ||
this._textureSize = Math.min(val, 2048); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
constructor(public readonly engine: Engine) {} | ||
|
||
/** | ||
* Add a sprite to atlas. | ||
* @param sprite - the sprite to add | ||
* @param imageSource - The source of texture | ||
* @returns true if add sprite success, otherwise false | ||
*/ | ||
public addSprite(sprite: Sprite, imageSource: TexImageSource | OffscreenCanvas): boolean { | ||
// Remove sprite if the sprite has been add. | ||
const { _spritesInAtlasIndex, _atlases } = this; | ||
const id = sprite.instanceId; | ||
const atlasIndex = _spritesInAtlasIndex[id]; | ||
if (atlasIndex) { | ||
_atlases[atlasIndex].removeSprite(sprite); | ||
delete _spritesInAtlasIndex[id]; | ||
} | ||
|
||
if (this._atlasIndex >= this._maxAtlasCount) { | ||
return false; | ||
} | ||
|
||
let atlas = _atlases[this._atlasIndex]; | ||
if (!atlas) { | ||
atlas = this._createAtlas(); | ||
} | ||
|
||
if (atlas.addSprite(sprite, imageSource)) { | ||
_spritesInAtlasIndex[id] = this._atlasIndex; | ||
return true; | ||
} | ||
|
||
if (this._atlasIndex + 1 >= this._maxAtlasCount) { | ||
this._atlasIndex = this._maxAtlasCount; | ||
return false; | ||
} | ||
|
||
atlas = this._createAtlas(); | ||
if (atlas.addSprite(sprite, imageSource)) { | ||
_spritesInAtlasIndex[id] = this._atlasIndex; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Remove a sprite from atlas. | ||
* @param sprite - the sprite to remove | ||
* @returns true if remove sprite success, otherwise false | ||
*/ | ||
public removeSprite(sprite: Sprite): boolean { | ||
if (!sprite) return false; | ||
|
||
const { _atlases } = this; | ||
for (let i = _atlases.length - 1; i >= 0; --i) { | ||
const atlas = _atlases[i]; | ||
if(atlas.removeSprite(sprite)) { | ||
delete this._spritesInAtlasIndex[i]; | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Reset all atlases. | ||
*/ | ||
public reset() { | ||
const { _atlases } = this; | ||
for (let i = 0, l = _atlases.length; i < l; ++i) { | ||
_atlases[i].destroy(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
_atlases.length = 0; | ||
this._atlasIndex = -1; | ||
this._spritesInAtlasIndex = {}; | ||
} | ||
|
||
private _createAtlas(): DynamicTextAtlas { | ||
this._atlasIndex++; | ||
const { _textureSize } = this; | ||
const atlas = new DynamicTextAtlas(this.engine, _textureSize, _textureSize); | ||
this._atlases.push(atlas); | ||
return atlas; | ||
} | ||
} |
This file contains 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,11 @@ | ||
/** | ||
* The style of the font. | ||
*/ | ||
export enum FontStyle { | ||
/** Set font without style */ | ||
None = 0x0, | ||
/** Set font bold */ | ||
Bold = 0x1, | ||
/** Set font italic */ | ||
Italic = 0x2 | ||
} |
This file contains 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,23 @@ | ||
/** | ||
* The horizontal alignment of the text. | ||
*/ | ||
export enum TextHorizontalAlignment { | ||
/** Align left horizontally */ | ||
Left = 0, | ||
/** Align center horizontally */ | ||
Center = 1, | ||
/** Align right horizontally */ | ||
Right = 2 | ||
} | ||
|
||
/** | ||
* The vertical alignment of the text. | ||
*/ | ||
export enum TextVerticalAlignment { | ||
/** Align top vertically */ | ||
Top = 0, | ||
/** Align center vertically */ | ||
Center = 1, | ||
/** Align bottom vertically */ | ||
Bottom = 2 | ||
} |
This file contains 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,9 @@ | ||
/** | ||
* The way to handle the situation where wrapped text is too tall to fit in the height. | ||
*/ | ||
export enum OverflowMode { | ||
/** Overflow when the text is too tall */ | ||
Overflow = 0, | ||
/** Truncate with height when the text is too tall */ | ||
Truncate = 1 | ||
} |
This file contains 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,4 +1,8 @@ | ||
export { SpriteMaskInteraction } from "./enums/SpriteMaskInteraction"; | ||
export { SpriteMaskLayer } from "./enums/SpriteMaskLayer"; | ||
export { TextHorizontalAlignment, TextVerticalAlignment } from "./enums/TextAlignment"; | ||
export { OverflowMode } from "./enums/TextOverflow"; | ||
export { FontStyle } from "./enums/FontStyle"; | ||
export { SpriteAtlas } from "./atlas/SpriteAtlas"; | ||
export * from "./sprite/index"; | ||
export * from "./text/index"; |
This file contains 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,50 @@ | ||
import { RefObject } from "../../asset/RefObject" | ||
import { Engine } from "../../Engine"; | ||
import { UpdateFlag } from "../../UpdateFlag"; | ||
import { UpdateFlagManager } from "../../UpdateFlagManager"; | ||
|
||
/** | ||
* Font. | ||
*/ | ||
export class Font extends RefObject { | ||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private _name: string = "Arial"; | ||
private _updateFlagManager: UpdateFlagManager = new UpdateFlagManager(); | ||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* The name of the font. | ||
*/ | ||
get name(): string { | ||
return this._name; | ||
} | ||
|
||
set name(value: string) { | ||
value = value || "Arial"; | ||
if (this._name !== value) { | ||
this._name = value; | ||
this._updateFlagManager.distribute(); | ||
} | ||
} | ||
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
GuoLei1990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Create a font instance. | ||
* @param engine - Engine to which the font belongs | ||
* @param name - The name of font | ||
*/ | ||
constructor(engine: Engine, name: string = "") { | ||
super(engine); | ||
this._name = name || "Arial"; | ||
} | ||
singlecoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @internal | ||
*/ | ||
_registerUpdateFlag(): UpdateFlag { | ||
return this._updateFlagManager.register(); | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
protected _onDestroy(): void {} | ||
} | ||
|
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.
If I pass in a larger word first and cause an atlas to be filtered, there will be a lot of wasted space in the filtered atlas?
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.
Never going back to use old atlas?