Skip to content

Commit

Permalink
merge node change of v3.1.2 (cocos#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
caryliu1999 committed Jun 8, 2021
1 parent 84bd13c commit 7a26531
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 31 deletions.
129 changes: 100 additions & 29 deletions cocos/core/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,64 @@ import { FloatArray } from '../math/type-define';
const v3_a = new Vec3();
const q_a = new Quat();
const q_b = new Quat();
const array_a = new Array(10);
const qt_1 = new Quat();
const m3_1 = new Mat3();
const m3_scaling = new Mat3();
const m4_1 = new Mat4();
const bookOfChange = new Map<Node, number>();
const array_a: any[] = [];

class BookOfChange {
private _chunks: Uint32Array[] = [];
private _freelists: number[][] = [];

// these should match with native: cocos/renderer/pipeline/helper/SharedMemory.h Node.getHasChangedFlags
private static CAPACITY_PER_CHUNK = 256;

constructor () {
this._createChunk();
}

public alloc () {
const chunkCount = this._freelists.length;
for (let i = 0; i < chunkCount; ++i) {
if (!this._freelists[i].length) continue;
return this._createView(i);
}
this._createChunk();
return this._createView(chunkCount);
}

public free (view: Uint32Array, idx: number) {
const chunkCount = this._freelists.length;
for (let i = 0; i < chunkCount; ++i) {
if (this._chunks[i] !== view) continue;
this._freelists[i].push(idx);
return;
}
}

public clear () {
const chunkCount = this._chunks.length;
for (let i = 0; i < chunkCount; ++i) {
this._chunks[i].fill(0);
}
}

private _createChunk () {
this._chunks.push(new Uint32Array(BookOfChange.CAPACITY_PER_CHUNK));
const freelist: number[] = [];
for (let i = 0; i < BookOfChange.CAPACITY_PER_CHUNK; ++i) freelist.push(i);
this._freelists.push(freelist);
}

private _createView (chunkIdx: number): [Uint32Array, number] {
const chunk = this._chunks[chunkIdx];
const offset = this._freelists[chunkIdx].pop()!;
return [chunk, offset];
}
}

const bookOfChange = new BookOfChange();

/**
* @zh
Expand All @@ -82,8 +134,6 @@ const bookOfChange = new Map<Node, number>();
*/
@ccclass('cc.Node')
export class Node extends BaseNode {
public static bookOfChange = bookOfChange;

/**
* @en Event types emitted by Node
* @zh 节点可能发出的事件类型
Expand Down Expand Up @@ -112,6 +162,13 @@ export class Node extends BaseNode {
// UI 部分的脏数据
public _uiProps = new NodeUIProperties(this);

/**
* @en Counter to clear node array
* @zh 清除节点数组计时器
*/
private static ClearFrame = 0;
private static ClearRound = 1000;

public _static = false;

// world transform, don't access this directly
Expand Down Expand Up @@ -145,6 +202,8 @@ export class Node extends BaseNode {
protected _eulerDirty = false;

protected _nodeHandle: SharedNodeHandle = NULL_HANDLE;
protected declare _hasChangedFlagsChunk: Uint32Array; // has the transform been updated in this frame?
protected declare _hasChangedFlagsOffset: number;

protected declare _nativeObj: NativeNode | null;
protected declare _nativeLayer: Uint32Array;
Expand All @@ -170,6 +229,10 @@ export class Node extends BaseNode {
this._scale = new Vec3(1, 1, 1);
this._mat = new Mat4();
}

const [chunk, offset] = bookOfChange.alloc();
this._hasChangedFlagsChunk = chunk;
this._hasChangedFlagsOffset = offset;
}

constructor (name?: string) {
Expand All @@ -194,6 +257,8 @@ export class Node extends BaseNode {

this._nativeObj = null;
}

bookOfChange.free(this._hasChangedFlagsChunk, this._hasChangedFlagsOffset);
}

public destroy () {
Expand Down Expand Up @@ -383,11 +448,11 @@ export class Node extends BaseNode {
* @zh 这个节点的空间变换信息在当前帧内是否有变过?
*/
get hasChangedFlags () {
return bookOfChange.get(this) || 0;
return this._hasChangedFlagsChunk[this._hasChangedFlagsOffset] as TransformBit;
}

set hasChangedFlags (val: number) {
bookOfChange.set(this, val);
this._hasChangedFlagsChunk[this._hasChangedFlagsOffset] = val;
if (JSB) {
this._nativeFlag[0] = val;
}
Expand Down Expand Up @@ -567,16 +632,25 @@ export class Node extends BaseNode {
* @param dirtyBit The dirty bits to setup to children, can be composed with multiple dirty bits
*/
public invalidateChildren (dirtyBit: TransformBit) {
const hasChanegdFlags = this.hasChangedFlags;
if ((this._dirtyFlags & hasChanegdFlags & dirtyBit) === dirtyBit) { return; }
this._dirtyFlags |= dirtyBit;
this.hasChangedFlags = hasChanegdFlags | dirtyBit;
const newDirtyBit = dirtyBit | TransformBit.POSITION;
const len = this._children.length;
for (let i = 0; i < len; ++i) {
const child = this._children[i];
if (child.isValid) { child.invalidateChildren(newDirtyBit); }
const childDirtyBit = dirtyBit | TransformBit.POSITION;
array_a[0] = this;

// we need to recursively iterate this
// eslint-disable-next-line @typescript-eslint/no-this-alias
let i = 0;
while (i >= 0) {
const cur: this = array_a[i--];
const hasChangedFlags = cur.hasChangedFlags;
if (cur.isValid && (cur._dirtyFlags & hasChangedFlags & dirtyBit) !== dirtyBit) {
cur._dirtyFlags |= dirtyBit;
cur.hasChangedFlags = hasChangedFlags | dirtyBit;
const children = cur._children;
const len = children.length;
for (let j = 0; j < len; ++j) array_a[++i] = children[j];
}
dirtyBit = childDirtyBit;
}
array_a.length = 0;
}

/**
Expand Down Expand Up @@ -639,6 +713,7 @@ export class Node extends BaseNode {
child._dirtyFlags = TransformBit.NONE;
cur = child;
}
array_a.length = 0;
}

// ===============================
Expand Down Expand Up @@ -1132,27 +1207,23 @@ export class Node extends BaseNode {
* @zh
* 清除所有节点的脏标记。
*/
public static clearBooks () {
if (JSB) bookOfChange.forEach((v, k, m) => { if (k.isValid) k.hasChangedFlags = TransformBit.NONE; });
public static resetHasChangedFlags () {
bookOfChange.clear();
}

/**
* @en
* Synchronize the js transform to the native layer.
* @zh
* js 变换信息同步到原生层。
*/
public syncToNativeTransform () {
}

/**
* @en
* Synchronize the native transform to the js layer.
* clear node array
* @zh
* 原生变换信息同步到 js 层。
* 清除节点数组
*/
public syncFromNativeTransform () {
public static clearNodeArray () {
if (Node.ClearFrame < Node.ClearRound) {
Node.ClearFrame++;
} else {
Node.ClearFrame = 0;
array_a.length = 0;
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions platforms/native/engine/jsb-physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,10 @@ class PhysicsWorld {
}

syncSceneToPhysics () {
books.forEach((v) => { v.updateWorldTransform(); });
this._impl.syncSceneToPhysics();
}

syncAfterEvents () {
books.forEach((v) => { v.syncFromNativeTransform(); });
// this._impl.syncSceneToPhysics()
}

Expand Down

0 comments on commit 7a26531

Please sign in to comment.