Skip to content

Optimize code size for tiled-layer.ts #18684

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
wants to merge 1 commit into
base: v3.8.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cocos/game/director.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class Director extends EventTarget {

if (!EDITOR) {
if (isValid(this._scene)) {
this._scene!.destroy();
this._scene.destroy();
}
this._scene = null;
}
Expand Down Expand Up @@ -443,7 +443,7 @@ export class Director extends EventTarget {
console.time('Destroy');
}
if (isValid(oldScene)) {
oldScene!.destroy();
oldScene.destroy();
}
if (!EDITOR) {
// auto release assets
Expand Down
2 changes: 1 addition & 1 deletion cocos/tiledmap/assembler/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let _accessor: StaticVBAccessor = null!;
*/
class Simple implements IAssembler {
private ensureAccessor (): void {
if (!_accessor) {
if (JSB && !_accessor) {
const device = director.root!.device;
const batcher = director.root!.batcher2D;
_accessor = new StaticVBAccessor(device, vfmtPosUvColor);
Expand Down
76 changes: 40 additions & 36 deletions cocos/tiledmap/tiled-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import { ccclass } from 'cc.decorator';

import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { EDITOR_NOT_IN_PREVIEW, JSB } from 'internal:constants';
import { UIRenderer } from '../2d/framework/ui-renderer';
import { SpriteFrame } from '../2d/assets/sprite-frame';
import { Component, Node } from '../scene-graph';
import { TMXMapInfo } from './tmx-xml-parser';
import { Color, IVec2Like, Mat4, Size, Vec2, Vec3, logID, warnID } from '../core';
import { Color, IVec2Like, Mat4, Size, Vec2, Vec3, logID, v2, warnID } from '../core';
import { TiledTile } from './tiled-tile';
import { RenderData } from '../2d/renderer/render-data';
import { IBatcher } from '../2d/renderer/i-batcher';
Expand Down Expand Up @@ -90,6 +90,10 @@ export interface ITiledLayerCullingRect {
};
}

const ANCHOR_CHANGED = NodeEventType.ANCHOR_CHANGED;
const TRANSFORM_CHANGED = NodeEventType.TRANSFORM_CHANGED;
const SIZE_CHANGED = NodeEventType.SIZE_CHANGED;

/**
* @en Render the TMX layer.
* @zh 渲染 TMX layer。
Expand Down Expand Up @@ -187,24 +191,17 @@ export class TiledLayer extends UIRenderer {
protected _diffY1?: number;
protected _useAutomaticVertexZ?: boolean;
protected _vertexZvalue?: number;
protected _offset?: Vec2;
protected _offset = v2();

protected _tiledDataArray: TiledDataArray = [];

protected _cameraNode?: Node;
protected _cameraNode: Node | null = null;

get tiledDataArray (): TiledDataArray { return this._tiledDataArray; }
get leftDownToCenterX (): number { return this._leftDownToCenterX; }
get leftDownToCenterY (): number { return this._leftDownToCenterY; }

private _drawInfoList: RenderDrawInfo[] = [];
private requestDrawInfo (idx: number): RenderDrawInfo {
if (!this._drawInfoList[idx]) {
this._drawInfoList[idx] = new RenderDrawInfo();
this._drawInfoList[idx].setDrawInfoType(RenderDrawInfoType.MIDDLEWARE);
}
return this._drawInfoList[idx];
}

constructor () {
super();
Expand Down Expand Up @@ -259,8 +256,8 @@ export class TiledLayer extends UIRenderer {
this._positionToRowCol(_vec2_temp.x, _vec2_temp.y, _tempRowCol);
this._addUserNodeToGrid(dataComp, _tempRowCol);
this._updateCullingOffsetByUserNode(node);
node.on(NodeEventType.TRANSFORM_CHANGED, this._userNodePosChange, dataComp);
node.on(NodeEventType.SIZE_CHANGED, this._userNodeSizeChange, dataComp);
node.on(TRANSFORM_CHANGED, this._userNodePosChange, dataComp);
node.on(SIZE_CHANGED, this._userNodeSizeChange, dataComp);
return true;
}

Expand All @@ -277,8 +274,8 @@ export class TiledLayer extends UIRenderer {
warnID(7243);
return false;
}
node.off(NodeEventType.TRANSFORM_CHANGED, this._userNodePosChange, dataComp);
node.off(NodeEventType.SIZE_CHANGED, this._userNodeSizeChange, dataComp);
node.off(TRANSFORM_CHANGED, this._userNodePosChange, dataComp);
node.off(SIZE_CHANGED, this._userNodeSizeChange, dataComp);
this._removeUserNodeFromGrid(dataComp);
delete this._userNodeMap[node.uuid];
node._removeComponent(dataComp);
Expand Down Expand Up @@ -432,8 +429,8 @@ export class TiledLayer extends UIRenderer {
if (this._cameraNode !== cameraNode) {
this._uninstallCamera();
if (cameraNode) {
cameraNode.on(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
cameraNode.on(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
cameraNode.on(TRANSFORM_CHANGED, this.updateCulling, this);
cameraNode.on(SIZE_CHANGED, this.updateCulling, this);
this._cameraNode = cameraNode;
}
}
Expand All @@ -442,38 +439,40 @@ export class TiledLayer extends UIRenderer {

protected _uninstallCamera (): void {
if (this._cameraNode) {
this._cameraNode.off(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
this._cameraNode.off(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
delete this._cameraNode;
this._cameraNode.off(TRANSFORM_CHANGED, this.updateCulling, this);
this._cameraNode.off(SIZE_CHANGED, this.updateCulling, this);
this._cameraNode = null;
}
}

onEnable (): void {
super.onEnable();
this.node.on(NodeEventType.ANCHOR_CHANGED, this._syncAnchorPoint, this);
this.node.on(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
this.node.on(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
this.node.parent!.on(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
this.node.parent!.on(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
const node = this.node;
node.on(ANCHOR_CHANGED, this._syncAnchorPoint, this);
node.on(TRANSFORM_CHANGED, this.updateCulling, this);
node.on(SIZE_CHANGED, this.updateCulling, this);
node.parent!.on(TRANSFORM_CHANGED, this.updateCulling, this);
node.parent!.on(SIZE_CHANGED, this.updateCulling, this);
this._markForUpdateRenderData();
// delay 1 frame, since camera's matrix data is dirty
this.scheduleOnce(this.updateCulling.bind(this));
}

onDisable (): void {
super.onDisable();
this.node.parent?.off(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
this.node.parent?.off(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
this.node.off(NodeEventType.SIZE_CHANGED, this.updateCulling, this);
this.node.off(NodeEventType.TRANSFORM_CHANGED, this.updateCulling, this);
this.node.off(NodeEventType.ANCHOR_CHANGED, this._syncAnchorPoint, this);
const node = this.node;
node.parent?.off(SIZE_CHANGED, this.updateCulling, this);
node.parent?.off(TRANSFORM_CHANGED, this.updateCulling, this);
node.off(SIZE_CHANGED, this.updateCulling, this);
node.off(TRANSFORM_CHANGED, this.updateCulling, this);
node.off(ANCHOR_CHANGED, this._syncAnchorPoint, this);
this._uninstallCamera();
}

protected _syncAnchorPoint (): void {
const node = this.node;
const trans = node._getUITransformComp()!;
const scale = node.getScale();
const scale = node.scale;
this._leftDownToCenterX = trans.width * trans.anchorX * scale.x;
this._leftDownToCenterY = trans.height * trans.anchorY * scale.y;
this._cullingDirty = true;
Expand Down Expand Up @@ -805,8 +804,8 @@ export class TiledLayer extends UIRenderer {
reserveLine = 2;
}

const vpx = this._viewPort.x - this._offset!.x + this._leftDownToCenterX;
const vpy = this._viewPort.y - this._offset!.y + this._leftDownToCenterY;
const vpx = this._viewPort.x - this._offset.x + this._leftDownToCenterX;
const vpy = this._viewPort.y - this._offset.y + this._leftDownToCenterY;

let leftDownX = vpx - this._leftOffset;
let leftDownY = vpy - this._downOffset;
Expand Down Expand Up @@ -1072,8 +1071,8 @@ export class TiledLayer extends UIRenderer {
// tileOffset is tileset offset which is related to each grid
// tileOffset coordinate system's y axis is opposite with engine's y axis.
const tileOffset = grid.tileset.tileOffset;
left += this._offset!.x + tileOffset.x + grid.offsetX;
bottom += this._offset!.y - tileOffset.y - grid.offsetY;
left += this._offset.x + tileOffset.x + grid.offsetX;
bottom += this._offset.y - tileOffset.y - grid.offsetY;

topBorder = -tileOffset.y + grid.tileset._tileSize.height - mapth;
topBorder = topBorder < 0 ? 0 : topBorder;
Expand Down Expand Up @@ -1423,7 +1422,7 @@ export class TiledLayer extends UIRenderer {
}

// offset (after layer orientation is set);
self._offset = new Vec2(layerInfo.offset.x, -layerInfo.offset.y);
self._offset.set(layerInfo.offset.x, -layerInfo.offset.y);
self._useAutomaticVertexZ = false;
self._vertexZvalue = 0;
self._syncAnchorPoint();
Expand Down Expand Up @@ -1519,6 +1518,7 @@ export class TiledLayer extends UIRenderer {
}

private fillIndicesBuffer (renderData: RenderData, drawInfo: RenderDrawInfo): void {
if (!JSB) return;
const iBuf = renderData.chunk.meshBuffer.iData;

let indexOffset = renderData.chunk.meshBuffer.indexOffset;
Expand All @@ -1539,7 +1539,11 @@ export class TiledLayer extends UIRenderer {
drawInfo.setIBCount(quadCount * 6);
}

/**
* @engineInternal
*/
public prepareDrawData (): void {
if (!JSB) return;
this._drawInfoList.length = 0;
const entity = this.renderEntity;
entity.clearDynamicRenderDrawInfos();
Expand Down
Loading