From 3fbe3a1ea8af5a4eee0d32bdca768f74965ec74f Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Thu, 29 Jul 2021 18:30:42 +0800 Subject: [PATCH] Treat the scene node as the native root node (#9072) --- cocos/core/renderer/scene/native-scene.jsb.ts | 1 + cocos/core/renderer/scene/native-scene.ts | 6 +++- cocos/core/scene-graph/base-node.ts | 1 - cocos/core/scene-graph/node.ts | 32 +++++++++++-------- cocos/core/scene-graph/scene.ts | 16 +++++++++- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/cocos/core/renderer/scene/native-scene.jsb.ts b/cocos/core/renderer/scene/native-scene.jsb.ts index f7e2972831..7120841cbe 100644 --- a/cocos/core/renderer/scene/native-scene.jsb.ts +++ b/cocos/core/renderer/scene/native-scene.jsb.ts @@ -2,6 +2,7 @@ declare const ns: any; export const NativeNode = ns.Node; +export const NativeScene = ns.Scene; export const NativeModel = ns.Model; export const NativeSkinningModel = ns.SkinningModel; export const NativeBakedSkinningModel = ns.BakedSkinningModel; diff --git a/cocos/core/renderer/scene/native-scene.ts b/cocos/core/renderer/scene/native-scene.ts index 70c3236ba2..4e172766a1 100644 --- a/cocos/core/renderer/scene/native-scene.ts +++ b/cocos/core/renderer/scene/native-scene.ts @@ -6,10 +6,14 @@ import { RenderPriority } from '../../pipeline/define'; import { LightType } from './light'; export const NativeNode: Constructor<{ - initWithData (data: TypedArray, chunk: Uint32Array): void; + initWithData (data: TypedArray, chunk: Uint32Array, computeNodes: NativeNode[]): void; setParent(val: NativeNode | null): void; }> = null!; export type NativeNode = InstanceType; +export const NativeScene: Constructor<{ + setParent(val: NativeScene | null): void; +}> = null!; +export type NativeScene = InstanceType; export const NativeAABB: Constructor<{ initWithData(data: TypedArray): void; diff --git a/cocos/core/scene-graph/base-node.ts b/cocos/core/scene-graph/base-node.ts index 62c9844848..009002a540 100644 --- a/cocos/core/scene-graph/base-node.ts +++ b/cocos/core/scene-graph/base-node.ts @@ -50,7 +50,6 @@ const DontDestroy = CCObject.Flags.DontDestroy; const Deactivating = CCObject.Flags.Deactivating; export const TRANSFORM_ON = 1 << 0; - // const CHILD_ADDED = 'child-added'; // const CHILD_REMOVED = 'child-removed'; diff --git a/cocos/core/scene-graph/node.ts b/cocos/core/scene-graph/node.ts index 2a6a88bc82..580cfb1b97 100644 --- a/cocos/core/scene-graph/node.ts +++ b/cocos/core/scene-graph/node.ts @@ -55,8 +55,8 @@ const qt_1 = new Quat(); const m3_1 = new Mat3(); const m3_scaling = new Mat3(); const m4_1 = new Mat4(); -const array_a: any[] = []; - +const dirtyNodes: any[] = []; +const nativeDirtyNodes: any[] = []; class BookOfChange { private _chunks: Uint32Array[] = []; private _freelists: number[][] = []; @@ -248,7 +248,7 @@ export class Node extends BaseNode implements CustomSerializable { this._nativeLayer[0] = this._layer; this._nativeObj = new NativeNode(); const flagBuffer = new Uint32Array(chunk.buffer, chunk.byteOffset + offset * 4, 1); - this._nativeObj.initWithData(NodePool.getBuffer(this._nodeHandle), flagBuffer); + this._nativeObj.initWithData(NodePool.getBuffer(this._nodeHandle), flagBuffer, nativeDirtyNodes); } else { this._pos = new Vec3(); this._rot = new Quat(); @@ -277,7 +277,6 @@ export class Node extends BaseNode implements CustomSerializable { NodePool.free(this._nodeHandle); this._nodeHandle = NULL_HANDLE; } - this._nativeObj = null; } bookOfChange.free(this._hasChangedFlagsChunk, this._hasChangedFlagsOffset); @@ -695,6 +694,13 @@ export class Node extends BaseNode implements CustomSerializable { this.setWorldRotation(q_a); } + protected _setDirtyNode (idx: number, currNode: this) { + dirtyNodes[idx] = currNode; + if (JSB) { + nativeDirtyNodes[idx] = currNode.native; + } + } + /** * @en Invalidate the world transform information * for this node and all its children recursively @@ -703,11 +709,10 @@ export class Node extends BaseNode implements CustomSerializable { */ public invalidateChildren (dirtyBit: TransformBit) { const childDirtyBit = dirtyBit | TransformBit.POSITION; - array_a[0] = this; - + this._setDirtyNode(0, this); let i = 0; while (i >= 0) { - const cur: this = array_a[i--]; + const cur: this = dirtyNodes[i--]; const hasChangedFlags = cur.hasChangedFlags; if (cur.isValid && (cur._dirtyFlags & hasChangedFlags & dirtyBit) !== dirtyBit) { cur._dirtyFlags |= dirtyBit; @@ -715,7 +720,7 @@ export class Node extends BaseNode implements CustomSerializable { cur.hasChangedFlags = hasChangedFlags | dirtyBit; const children = cur._children; const len = children.length; - for (let j = 0; j < len; ++j) array_a[++i] = children[j]; + for (let j = 0; j < len; ++j) this._setDirtyNode(++i, children[j]); } dirtyBit = childDirtyBit; } @@ -733,13 +738,13 @@ export class Node extends BaseNode implements CustomSerializable { let i = 0; while (cur && cur._dirtyFlags) { // top level node - array_a[i++] = cur; + this._setDirtyNode(i++, cur); cur = cur._parent; } let child: this; let dirtyBits = 0; while (i) { - child = array_a[--i]; + child = dirtyNodes[--i]; dirtyBits |= child._dirtyFlags; if (cur) { if (dirtyBits & TransformBit.POSITION) { @@ -967,12 +972,12 @@ export class Node extends BaseNode implements CustomSerializable { let cur = this; let i = 0; while (cur._parent) { - array_a[i++] = cur; + this._setDirtyNode(i++, cur); cur = cur._parent; } while (i >= 0) { Vec3.transformInverseRTS(out, out, cur._lrot, cur._lpos, cur._lscale); - cur = array_a[--i]; + cur = dirtyNodes[--i]; } return out; } @@ -1289,7 +1294,8 @@ export class Node extends BaseNode implements CustomSerializable { Node.ClearFrame++; } else { Node.ClearFrame = 0; - array_a.length = 0; + dirtyNodes.length = 0; + nativeDirtyNodes.length = 0; } } } diff --git a/cocos/core/scene-graph/scene.ts b/cocos/core/scene-graph/scene.ts index 41150d98eb..123ebecade 100644 --- a/cocos/core/scene-graph/scene.ts +++ b/cocos/core/scene-graph/scene.ts @@ -29,7 +29,7 @@ */ import { ccclass, serializable, editable } from 'cc.decorator'; -import { EDITOR, TEST } from 'internal:constants'; +import { EDITOR, JSB, TEST } from 'internal:constants'; import { CCObject } from '../data/object'; import { Mat4, Quat, Vec3 } from '../math'; import { assert, getError } from '../platform/debug'; @@ -39,6 +39,7 @@ import { legacyCC } from '../global-exports'; import { Component } from '../components/component'; import { SceneGlobals } from './scene-globals'; import { applyTargetOverrides } from '../utils/prefab/utils'; +import { NativeScene } from '../renderer/scene/native-scene'; /** * @en @@ -97,10 +98,22 @@ export class Scene extends BaseNode { protected _dirtyFlags = 0; + protected declare _nativeObj: NativeScene | null; + protected _updateScene () { this._scene = this; } + get native (): any { + return this._nativeObj; + } + + protected _init () { + if (JSB) { + this._nativeObj = new NativeScene(); + } + } + constructor (name: string) { super(name); this._activeInHierarchy = false; @@ -108,6 +121,7 @@ export class Scene extends BaseNode { this._renderScene = legacyCC.director.root.createScene({}); } this._inited = legacyCC.game ? !legacyCC.game._isCloning : true; + this._init(); } /**