Visit render component with chain instead of walking scene graph

This commit is contained in:
pandamicro
2018-04-11 14:15:08 +08:00
parent 6e6bdf64a9
commit 3dcaf19376
4 changed files with 228 additions and 32 deletions
+8 -2
View File
@@ -36,6 +36,7 @@ const macro = require('./platform/CCMacro');
const misc = require('./utils/misc');
const Event = require('./event/event');
const transformSys = require('./systems/transform');
const hierarchyChain = require('./renderer/utils/hierarchy-chain');
const Flags = cc.Object.Flags;
const Destroying = Flags.Destroying;
@@ -1006,6 +1007,10 @@ var Node = cc.Class({
},
_onHierarchyChanged (oldParent) {
this._updateOrderOfArrival();
if (this._parent && this._parent._delaySort) {
this._parent._delaySort();
}
this._onHierarchyChangedBase(oldParent);
cc._widgetManager._nodesOrderDirty = true;
},
@@ -1078,6 +1083,8 @@ 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();
@@ -2288,14 +2295,12 @@ var Node = cc.Class({
// invokes the parent setter
child.parent = this;
child._updateOrderOfArrival();
if (zIndex !== undefined) {
child.zIndex = zIndex;
}
if (name !== undefined) {
child.name = name;
}
this._delaySort();
},
/**
@@ -2349,6 +2354,7 @@ var Node = cc.Class({
}
_children[j + 1] = child;
}
hierarchyChain.rebuild(this);
this.emit(CHILD_REORDER);
}
cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this);
@@ -26,6 +26,7 @@
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;
/**
@@ -51,14 +52,28 @@ var RenderComponent = cc.Class({
this.__allocedDatas = [];
this._vertexFormat = defaultVertexFormat;
this._toPostHandle = false;
// Render component chain entries
this._chain = {
comp: this,
next: null
};
this._postChain = null;
},
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 () {
this.node._renderComponent = null;
// When parent children order dirty, its children's render component will be rechained in sortAllChildren
if (!this.node.parent._reorderChildDirty) {
hierarchyChain.rebuildSelf(this.node);
}
},
onDestroy () {
@@ -27,6 +27,7 @@ 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 gfx = renderEngine.gfx;
const RecyclePool = renderEngine.RecyclePool;
const InputAssembler = renderEngine.InputAssembler;
@@ -97,9 +98,6 @@ var RenderComponentWalker = function (device, renderScene) {
this._sortKey = 0;
this._curCameraNode = null;
this._handleRender = this._handleRender.bind(this);
this._postHandleRender = this._postHandleRender.bind(this);
};
RenderComponentWalker.prototype = {
@@ -138,31 +136,6 @@ RenderComponentWalker.prototype = {
this._stencilMgr.reset();
},
_handleRender (node) {
if (node.groupIndex !== 0) {
this._curCameraNode = node;
}
let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex;
node._cullingMask = 1 << group;
let comp = node._renderComponent;
if (comp) {
this.renderComp(comp, comp.constructor._assembler);
}
},
_postHandleRender (node) {
let comp = node._renderComponent;
if (comp && comp.constructor._postAssembler) {
this.renderComp(comp, comp.constructor._postAssembler);
}
if (this._curCameraNode === node) {
this._curCameraNode = null;
}
},
_flush (batchData) {
let vfmt = batchData.vfmt,
material = batchData.material,
@@ -308,8 +281,33 @@ RenderComponentWalker.prototype = {
visit (scene) {
this.reset();
// Store all render components to _queue
scene.walk(this._handleRender, this._postHandleRender);
let entry = hierarchyChain.entry(scene);
while (entry) {
let comp = entry.comp;
// Pre handle
if (!entry.post) {
let node = comp.node;
if (node.groupIndex !== 0) {
this._curCameraNode = node;
}
let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex;
node._cullingMask = 1 << group;
this.renderComp(comp, comp.constructor._assembler);
}
// Post handle
else {
this.renderComp(comp, comp.constructor._postAssembler);
if (this._curCameraNode === node) {
this._curCameraNode = null;
}
}
entry = entry.next;
}
this._flush(_batchData);
}
@@ -0,0 +1,177 @@
/****************************************************************************
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 = {
rebuildSelf (node) {
let previous = findPrevious(node);
let next = findNext(node);
let first = findFirst(node);
// If first not exist, then no render component inside the current node
if (first) {
if (previous) {
previous.next = first;
}
let last = findLast(node);
last.next = next;
}
else if (previous) {
previous.next = next;
}
},
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);
}
}