Recursively visit node instead of walk through comp chain

This commit is contained in:
pandamicro
2018-04-19 16:58:44 +08:00
parent 427fc3c7c6
commit d8ca6001d8
5 changed files with 48 additions and 243 deletions
-4
View File
@@ -35,7 +35,6 @@ const eventManager = require('./event-manager');
const macro = require('./platform/CCMacro');
const misc = require('./utils/misc');
const Event = require('./event/event');
const hierarchyChain = require('./renderer/utils/hierarchy-chain');
const Flags = cc.Object.Flags;
const Destroying = Flags.Destroying;
@@ -1100,8 +1099,6 @@ var Node = cc.Class({
eventManager.pauseTarget(this);
}
hierarchyChain.rebuild(this);
let children = this._children;
for (let i = 0, len = children.length; i < len; i++) {
children[i]._onBatchCreated();
@@ -2481,7 +2478,6 @@ var Node = cc.Class({
}
this.emit(CHILD_REORDER);
}
hierarchyChain.rebuild(this);
cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this);
}
},
@@ -26,7 +26,6 @@
const Component = require('./CCComponent');
const defaultVertexFormat = require('../renderer/vertex-format');
const renderEngine = require('../renderer/render-engine');
const hierarchyChain = require('../renderer/utils/hierarchy-chain');
const RenderData = renderEngine.RenderData;
/**
@@ -62,10 +61,6 @@ var RenderComponent = cc.Class({
onEnable () {
this.node._renderComponent = this;
// When parent children order dirty, its children's render component will be rechained in sortAllChildren
if (!this.node.parent._reorderChildDirty) {
hierarchyChain.rebuildSelf(this.node);
}
},
onDisable () {
@@ -27,7 +27,6 @@ const macro = require('../platform/CCMacro');
const renderEngine = require('./render-engine');
const defaultVertexFormat = require('./vertex-format');
const StencilManager = require('./stencil-manager');
const hierarchyChain = require('./utils/hierarchy-chain');
const atlasManager = require('./utils/dynamic-atlas/manager');
const gfx = renderEngine.gfx;
@@ -64,7 +63,6 @@ var RenderComponentWalker = function (device, renderScene) {
MAX_INDICE: MAX_INDICE
};
let defaultFormat = new gfx.VertexFormat([]);
this._iaPool = new RecyclePool(function () {
return new InputAssembler();
@@ -74,7 +72,6 @@ var RenderComponentWalker = function (device, renderScene) {
return new renderEngine.Model();
}, 16);
// buffers
this._vb = null;
this._ib = null;
@@ -323,48 +320,57 @@ RenderComponentWalker.prototype = {
return null;
},
_visitNode (node) {
if (!node._activeInHierarchy) {
return;
}
let batchData = this._batchData;
// Transform
if (node._worldMatDirty) {
node._updateWorldMatrix();
batchData.worldMatUpdated = true;
}
// Culling mask
if (node.groupIndex !== 0) {
this._curCameraNode = node;
}
let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex;
let cullingMask = node._cullingMask = 1 << group;
// Render self
let comp = node._renderComponent;
let needDraw = !!(comp && comp._enabled);
if (needDraw) {
let assembler = comp._assembler || comp.constructor._assembler;
this._commitComp(comp, assembler, cullingMask);
}
// Reset worldMatUpdated
batchData.worldMatUpdated = false;
// Render children
let children = node._children;
for (let i = 0, l = children.length; i < l; i++) {
this._visitNode(children[i]);
}
// Post render
if (needDraw && comp.constructor._postAssembler) {
this._commitComp(comp, comp.constructor._postAssembler, cullingMask);
}
// Reset current camera node
if (this._curCameraNode === node) {
this._curCameraNode = null;
}
},
visit (scene) {
this.reset();
let batchData = this._batchData;
let entry = this._findEntry(scene);
let comp, node, assembler, group, cullingMask;
for (; entry; entry = entry.next) {
comp = entry.comp;
node = comp.node;
if (!comp._enabled || !node._activeInHierarchy) {
continue;
}
assembler = null;
// Pre handle
if (!entry.post) {
if (node.groupIndex !== 0) {
this._curCameraNode = node;
}
group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex;
node._cullingMask = 1 << group;
assembler = comp._assembler || comp.constructor._assembler;
}
// Post handle
else {
if (this._curCameraNode === node) {
this._curCameraNode = null;
}
assembler = comp.constructor._postAssembler;
}
// Transform
if (node._worldMatDirty) {
node._updateWorldMatrix();
batchData.worldMatUpdated = true;
}
// Commit component render data
cullingMask = node._cullingMask;
this._commitComp(comp, assembler, cullingMask);
this._batchData.worldMatUpdated = false;
for (let i = 0, l = scene._children.length; i < l; i++) {
this._visitNode(scene._children[i]);
}
atlasManager.update();
@@ -1,187 +0,0 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
function findPrevious (node) {
let parent = node._parent;
// reach root scene
if (!parent) {
return null;
}
// Find previous render component in siblings
let children = parent._children;
let index = children.indexOf(node);
let previous = null, comp = null;
for (let i = index - 1; i >= 0; i--) {
let sibling = children[i];
previous = findLast(sibling);
if (previous) {
return previous;
}
}
// Find previous render component in parent level
if (parent._renderComponent) {
return parent._renderComponent._chain;
}
else {
return findPrevious(parent);
}
}
function findNext (node) {
let parent = node._parent;
// reach root scene
if (!parent) {
return null;
}
// Find next render component in siblings
let children = parent._children;
let index = children.indexOf(node);
let next = null;
for (let i = index + 1; i < children.length; i++) {
let sibling = children[i];
next = findFirst(sibling);
if (next) {
return next;
}
}
let comp = parent._renderComponent;
if (comp && comp.constructor._postAssembler) {
if (!comp._postChain) {
comp._postChain = {
post: true,
comp: comp,
next: null
}
}
// If parent's component has post assembler, we need to chain it to the next component
return comp._postChain;
}
// Find next render component in parent level
return findNext(parent);
}
function findFirst (node) {
if (node._renderComponent) {
return node._renderComponent._chain;
}
let children = node._children;
let first;
for (let i = 0; i < children.length; i++) {
let child = children[i];
first = findFirst(child);
if (first) {
return first;
}
}
return null;
}
function findLast (node) {
let comp = node._renderComponent;
if (comp && comp.constructor._postAssembler) {
if (!comp._postChain) {
comp._postChain = {
post: true,
comp: comp,
next: null
}
}
// If parent's component has post assembler, we need to chain it to the next component
return comp._postChain;
}
let children = node._children;
let last;
for (let i = children.length - 1; i >= 0; i--) {
last = findLast(children[i]);
if (last) {
return last;
}
}
return comp ? comp._chain : null;
}
module.exports = {
unchainSelf (node) {
let last = findLast(node);
if (last) {
// Chain previous node to the next node
let previous = findPrevious(node);
if (previous) {
previous.next = last.next;
}
}
},
rebuildSelf (node) {
let previous = findPrevious(node);
let first = findFirst(node);
// If first not exist, then no render component inside the current node
if (first) {
let last = findLast(node);
if (previous) {
last.next = previous.next;
previous.next = first;
}
else {
last.next = null;
}
}
},
rebuild (node) {
let previous = node._renderComponent ? node._renderComponent._chain : findPrevious(node);
// Rebuild for all children
let children = node._children;
let curr, first;
for (let i = 0; i < children.length; i++) {
curr = children[i];
first = findFirst(curr);
// If first not exist, then no render component inside the current node, nothing to do
if (first) {
if (previous) {
// Rebuild chain for previous render component and the first render component in sub tree
previous.next = first;
}
// Set previous to the last render component in sub tree
previous = findLast(curr);
}
}
if (previous) {
// Link the last render component's next to the outside next render component
previous.next = findNext(node);
}
},
entry (scene) {
return findFirst(scene);
}
}
-5
View File
@@ -30,7 +30,6 @@ const misc = require('./misc');
const js = require('../platform/js');
const IdGenerater = require('../platform/id-generater');
const eventManager = require('../event-manager');
const hierarchyChain = require('../renderer/utils/hierarchy-chain');
const Destroying = Flags.Destroying;
const DontDestroy = Flags.DontDestroy;
@@ -353,10 +352,6 @@ var BaseNode = cc.Class({
if (CC_DEBUG && oldParent && (oldParent._objFlags & Deactivating)) {
cc.errorID(3821);
}
if (oldParent) {
// Unchain from old parent
hierarchyChain.unchainSelf(this);
}
this._parent = value || null;
this._onSetParent(value);