Skip to content

Commit

Permalink
Merge branch 'main' into dev/1.2
Browse files Browse the repository at this point in the history
* main:
  "v1.1.0-beta.36"
  Opt the texture filter mode for text renderer and fix renderer error when the text height is 1 (#1943)
  "v1.1.0-beta.35"
  Fix error when set font size to 0 (#1938)
  feat: sprite add config (#1941)
  Fix renderer sort error (#1940)
  "v1.1.0-beta.34"
  Fix performance error when upload same buffer in one frame (#1937)
  "v1.1.0-beta.33"
  fix(mask): fix mask error (#1936)
  Opt subdivsion sphere (#1935)

# Conflicts:
#	packages/core/package.json
#	packages/design/package.json
#	packages/draco/package.json
#	packages/galacean/package.json
#	packages/loader/package.json
#	packages/math/package.json
#	packages/physics-lite/package.json
#	packages/physics-physx/package.json
#	packages/rhi-webgl/package.json
#	packages/shader-lab/package.json
  • Loading branch information
GuoLei1990 committed Jan 5, 2024
2 parents a8b9478 + bb999c4 commit a68d08a
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 124 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/2d/atlas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ export interface AtlasSprite {
pivot: { x: number; y: number };
border: { x: number; y: number; z: number; w: number };
pixelsPerUnit: number;
width: number;
height: number;
}
1 change: 1 addition & 0 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class SpriteMask extends Renderer {
_maskElement: RenderElement;

/** @internal */
@ignoreClone
_verticesData: VertexData2D;

@ignoreClone
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/2d/text/SubFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class SubFont {
const { _engine: engine } = this;
const fontAtlas = new FontAtlas(engine);
const texture = new Texture2D(engine, 256, 256, TextureFormat.R8G8B8A8, false);
texture.filterMode = TextureFilterMode.Point;
texture.filterMode = TextureFilterMode.Bilinear;
fontAtlas.texture = texture;
fontAtlas.isGCIgnored = texture.isGCIgnored = true;
this._fontAtlases.push(fontAtlas);
Expand Down
29 changes: 22 additions & 7 deletions packages/core/src/2d/text/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ export class TextRenderer extends Renderer {
* The bounding volume of the TextRenderer.
*/
override get bounds(): BoundingBox {
if (this._isTextNoVisible()) {
if (this._isContainDirtyFlag(DirtyFlag.WorldBounds)) {
const localBounds = this._localBounds;
localBounds.min.set(0, 0, 0);
localBounds.max.set(0, 0, 0);
this._updateBounds(this._bounds);
this._setDirtyFlagFalse(DirtyFlag.WorldBounds);
}
return this._bounds;
}
this._isContainDirtyFlag(DirtyFlag.SubFont) && this._resetSubFont();
this._isContainDirtyFlag(DirtyFlag.LocalPositionBounds) && this._updateLocalData();
this._isContainDirtyFlag(DirtyFlag.WorldPosition) && this._updatePosition();
Expand Down Expand Up @@ -359,11 +369,7 @@ export class TextRenderer extends Renderer {
* @internal
*/
protected override _render(context: RenderContext): void {
if (
this._text === "" ||
(this.enableWrapping && this.width <= 0) ||
(this.overflowMode === OverflowMode.Truncate && this.height <= 0)
) {
if (this._isTextNoVisible()) {
return;
}

Expand Down Expand Up @@ -481,8 +487,8 @@ export class TextRenderer extends Renderer {
}

private _updateLocalData(): void {
const { color, _charRenderDatas: charRenderDatas, _subFont: charFont } = this;
const { min, max } = this._localBounds;
const { color, _charRenderDatas: charRenderDatas, _subFont: charFont } = this;
const textMetrics = this.enableWrapping
? TextUtils.measureTextWithWrap(this)
: TextUtils.measureTextWithoutWrap(this);
Expand Down Expand Up @@ -555,7 +561,7 @@ export class TextRenderer extends Renderer {
const left = startX * pixelsPerUnitReciprocal;
const right = (startX + w) * pixelsPerUnitReciprocal;
const top = (startY + ascent) * pixelsPerUnitReciprocal;
const bottom = (startY - descent + 1) * pixelsPerUnitReciprocal;
const bottom = (startY - descent) * pixelsPerUnitReciprocal;
localPositions.set(left, top, right, bottom);
i === firstLine && (maxY = Math.max(maxY, top));
minY = Math.min(minY, bottom);
Expand Down Expand Up @@ -601,6 +607,15 @@ export class TextRenderer extends Renderer {
super._onTransformChanged(bit);
this._setDirtyFlagTrue(DirtyFlag.WorldPosition | DirtyFlag.WorldBounds);
}

private _isTextNoVisible(): boolean {
return (
this._text === "" ||
this._fontSize === 0 ||
(this.enableWrapping && this.width <= 0) ||
(this.overflowMode === OverflowMode.Truncate && this.height <= 0)
);
}
}

enum DirtyFlag {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/2d/text/TextUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ export class TextUtils {
"emoji",
"fangsong"
];

// _heightMultiplier used to measure the height of text, but in miniprogram performance is different from h5.
// so can set _heightMultiplier to adapt miniprogram, the larger the value, the worse the performance.
/** @internal */
static _heightMultiplier: number = 2;
/** These characters are all tall to help calculate the height required for text. */
private static _measureString: string = "|ÉqÅ";
private static _measureBaseline: string = "M";
private static _heightMultiplier: number = 2;
private static _baselineMultiplier: number = 1.4;
private static _fontSizeInfoCache: Record<string, FontSizeInfo> = {};
private static _textContext: TextContext = null;
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/RenderPipeline/Basic2DBatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Camera } from "../Camera";
import { Engine } from "../Engine";
import { Buffer, BufferBindFlag, BufferUsage, IndexFormat, MeshTopology, SubMesh, VertexElement } from "../graphic";
import {
Buffer,
BufferBindFlag,
BufferUsage,
IndexFormat,
MeshTopology,
SetDataOptions,
SubMesh,
VertexElement
} from "../graphic";
import { BufferMesh } from "../mesh";
import { ShaderTagKey } from "../shader/ShaderTagKey";
import { ClassPool } from "./ClassPool";
Expand Down Expand Up @@ -221,8 +230,10 @@ export abstract class Basic2DBatcher {
mesh.addSubMesh(this._getSubMeshFromPool(vertexStartIndex, vertexCount));
batchedQueue[curMeshIndex] = preElement;

this._vertexBuffers[_flushId].setData(vertices, 0, 0, vertexIndex);
this._indiceBuffers[_flushId].setData(indices, 0, 0, indiceIndex);
// Set data option use Discard, or will resulted in performance slowdown when open antialias and cross-rendering of 3D and 2D elements.
// Device: iphone X(16.7.2)、iphone 15 pro max(17.1.1)、iphone XR(17.1.2) etc.
this._vertexBuffers[_flushId].setData(vertices, 0, 0, vertexIndex, SetDataOptions.Discard);
this._indiceBuffers[_flushId].setData(indices, 0, 0, indiceIndex, SetDataOptions.Discard);
}

private _getSubMeshFromPool(start: number, count: number): SubMesh {
Expand Down
42 changes: 32 additions & 10 deletions packages/core/src/RenderPipeline/RenderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,44 @@ export class RenderQueue {
* @internal
*/
static _compareFromNearToFar(a: RenderElement, b: RenderElement): number {
return (
a.data.component.priority - b.data.component.priority ||
a.data.material._priority - b.data.material._priority ||
a.data.component._distanceForSort - b.data.component._distanceForSort
);
const dataA = a.data;
const dataB = b.data;
const componentA = dataA.component;
const componentB = dataB.component;
const priorityOrder = componentA.priority - componentB.priority;
if (priorityOrder !== 0) {
return priorityOrder;
}
// make suer from the same renderer.
if (componentA.instanceId === componentB.instanceId) {
return (
dataA.material._priority - dataB.material._priority || componentA._distanceForSort - componentB._distanceForSort
);
} else {
return componentA._distanceForSort - componentB._distanceForSort;
}
}

/**
* @internal
*/
static _compareFromFarToNear(a: RenderElement, b: RenderElement): number {
return (
a.data.component.priority - b.data.component.priority ||
a.data.material._priority - b.data.material._priority ||
b.data.component._distanceForSort - a.data.component._distanceForSort
);
const dataA = a.data;
const dataB = b.data;
const componentA = dataA.component;
const componentB = dataB.component;
const priorityOrder = componentA.priority - componentB.priority;
if (priorityOrder !== 0) {
return priorityOrder;
}
// make suer from the same renderer.
if (componentA.instanceId === componentB.instanceId) {
return (
dataA.material._priority - dataB.material._priority || componentB._distanceForSort - componentA._distanceForSort
);
} else {
return componentB._distanceForSort - componentA._distanceForSort;
}
}

readonly elements: RenderElement[] = [];
Expand Down
72 changes: 26 additions & 46 deletions packages/core/src/mesh/PrimitiveMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Buffer } from "../graphic/Buffer";
import { ModelMesh } from "./ModelMesh";
import {
CapsuleRestoreInfo,
SubdivisionSurfaceSphereRestoreInfo,
ConeRestoreInfo,
CuboidRestoreInfo,
CylinderRestoreInfo,
PlaneRestoreInfo,
PrimitiveMeshRestorer,
SphereRestoreInfo,
SubdivisionSurfaceSphereRestoreInfo,
TorusRestoreInfo
} from "./PrimitiveMeshRestorer";
import { VertexAttribute } from "./enums/VertexAttribute";
Expand Down Expand Up @@ -371,18 +371,35 @@ export class PrimitiveMesh {
for (let i = 0; i < cellsCount; i++) {
const idx = 4 * i;

indices[offset] = cells[idx];
indices[offset + 1] = cells[idx + 1];
indices[offset + 2] = cells[idx + 2];
let indexA = cells[idx];
let indexB = cells[idx + 1];
let indexC = cells[idx + 2];
let indexD = cells[idx + 3];

// Handle seam by replacing vertex index to seam vertex index if necessary
const floatIndexA = 8 * indexA;
const floatIndexB = 8 * indexB;
const floatIndexC = 8 * indexC;
const floatIndexD = 8 * indexD;

// If center Z is negative
if (vertices[floatIndexA + 2] + vertices[floatIndexB + 2] + vertices[floatIndexC + 2] < 0) {
vertices[floatIndexA + 6] === 0 && (indexA = seamVertices[indexA]);
vertices[floatIndexB + 6] === 0 && (indexB = seamVertices[indexB]);
vertices[floatIndexC + 6] === 0 && (indexC = seamVertices[indexC]);
vertices[floatIndexD + 6] === 0 && (indexD = seamVertices[indexD]);
}

indices[offset] = indexA;
indices[offset + 1] = indexB;
indices[offset + 2] = indexC;

this._replaceSeamUV(indices, vertices, offset, seamVertices);
this._generateAndReplacePoleUV(indices, vertices, offset, poleOffset);

indices[offset + 3] = cells[idx];
indices[offset + 4] = cells[idx + 2];
indices[offset + 5] = cells[idx + 3];
indices[offset + 3] = indexA;
indices[offset + 4] = indexC;
indices[offset + 5] = indexD;

this._replaceSeamUV(indices, vertices, offset + 3, seamVertices);
this._generateAndReplacePoleUV(indices, vertices, offset + 3, poleOffset);

offset += 6;
Expand Down Expand Up @@ -604,43 +621,6 @@ export class PrimitiveMesh {
}
}

/**
* Duplicate vertices whose uv normal is flipped and adjust their UV coordinates.
*/
private static _replaceSeamUV(
indices: Uint16Array | Uint32Array,
vertices: Float32Array,
offset: number,
seamVertices: Record<number, number>
) {
const vertexA = 8 * indices[offset];
const vertexB = 8 * indices[offset + 1];
const vertexC = 8 * indices[offset + 2];

const vertexAU = vertices[vertexA + 6];
const vertexAV = vertices[vertexA + 7];

const vertexBU = vertices[vertexB + 6];
const vertexBV = vertices[vertexB + 7];

const vertexCU = vertices[vertexC + 6];
const vertexCV = vertices[vertexC + 7];

const z = (vertexBU - vertexAU) * (vertexCV - vertexAV) - (vertexBV - vertexAV) * (vertexCU - vertexAU);

if (z > 0) {
if (vertexAU === 0) {
indices[offset] = seamVertices[indices[offset]];
}
if (vertexBU === 0) {
indices[offset + 1] = seamVertices[indices[offset + 1]];
}
if (vertexCU === 0) {
indices[offset + 2] = seamVertices[indices[offset + 2]];
}
}
}

/**
* Duplicate vertices at the poles and adjust their UV coordinates.
*/
Expand Down
4 changes: 3 additions & 1 deletion packages/loader/src/SpriteAtlasLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SpriteAtlasLoader extends Loader<SpriteAtlas> {

private _makeSprite(engine: Engine, config: AtlasSprite, texture?: Texture2D): Sprite {
// Generate a SpriteAtlas object.
const { region, atlasRegionOffset, atlasRegion, pivot, border } = config;
const { region, atlasRegionOffset, atlasRegion, pivot, border, width, height } = config;
const sprite = new Sprite(
engine,
texture,
Expand All @@ -103,6 +103,8 @@ class SpriteAtlasLoader extends Loader<SpriteAtlas> {
}
config.atlasRotated && (sprite.atlasRotated = true);
}
isNaN(width) || (sprite.width = width);
isNaN(height) || (sprite.height = height);
return sprite;
}
}
12 changes: 10 additions & 2 deletions packages/loader/src/SpriteLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ class SpriteLoader extends Loader<Sprite> {
// @ts-ignore
.getResourceByRef(data.texture)
.then((texture: Texture2D) => {
return new Sprite(resourceManager.engine, texture, data.region, data.pivot, data.border);
const sprite = new Sprite(resourceManager.engine, texture, data.region, data.pivot, data.border);
const { width, height } = data;
isNaN(width) || (sprite.width = width);
isNaN(height) || (sprite.height = height);
return sprite;
})
);
} else {
return new AssetPromise((resolve) => {
resolve(new Sprite(resourceManager.engine, null, data.region, data.pivot, data.border));
const sprite = new Sprite(resourceManager.engine, null, data.region, data.pivot, data.border);
const { width, height } = data;
isNaN(width) || (sprite.width = width);
isNaN(height) || (sprite.height = height);
resolve(sprite);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/2d/text/TextRenderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe("TextRenderer", () => {
textRenderer.verticalAlignment = TextVerticalAlignment.Top;
textRenderer.horizontalAlignment = TextHorizontalAlignment.Left;
BoundingBox.transform(
new BoundingBox(new Vector3(-1.5, 1.28, 0), new Vector3(1.39, 1.5, 0)),
new BoundingBox(new Vector3(-1.5, 1.27, 0), new Vector3(1.39, 1.5, 0)),
textRendererEntity.transform.worldMatrix,
box
);
Expand Down
Loading

0 comments on commit a68d08a

Please sign in to comment.