From 01c576e735ea7ef08aeee4db0d4eb100b7e50b0a Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Mon, 26 Jul 2021 15:21:27 +0800 Subject: [PATCH] Native worldTransform synchronization (#3764) --- native/cocos/scene/Node.cpp | 79 +++++++++++++++++++++++++++++++++---- native/cocos/scene/Node.h | 35 ++++++++++------ native/tools/tojs/scene.ini | 2 +- 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/native/cocos/scene/Node.cpp b/native/cocos/scene/Node.cpp index 26e27b0f54..9ab3647025 100644 --- a/native/cocos/scene/Node.cpp +++ b/native/cocos/scene/Node.cpp @@ -32,21 +32,20 @@ void Node::updateWorldTransform() { if (!getDirtyFlag()) { return; } - uint32_t i = 0; - Node * curr = this; - Mat3 mat3; - Mat3 m43; - Quaternion quat; - std::vector arrayA; + uint32_t i = 0; + Node * curr = this; + Mat3 mat3; + Mat3 m43; + Quaternion quat; while (curr && curr->getDirtyFlag()) { i++; - arrayA.push_back(curr); + _computeNodes.push_back(curr); curr = curr->getParent(); } Node * child{nullptr}; uint32_t dirtyBits = 0; while (i) { - child = arrayA[--i]; + child = _computeNodes[--i]; if (!child) { continue; } @@ -100,6 +99,70 @@ void Node::updateWorldRTMatrix() { Mat4::fromRT(_nodeLayout->worldRotation, _nodeLayout->worldPosition, &_rtMat); } +void Node::invalidateChildren(TransformBit dirtyBit) { + uint32_t curDirtyBit{static_cast(dirtyBit)}; + const uint32_t childDirtyBit{curDirtyBit | static_cast(TransformBit::POSITION)}; + if (static_cast(_computeNodes.size()) == 0) { + _computeNodes.resize(1); + } + _computeNodes[0] = this; + int i{0}; + while (i >= 0) { + Node * cur = _computeNodes[i--]; + const uint32_t &hasChangedFlags = cur->getFlagsChanged(); + if ((cur->getDirtyFlag() & hasChangedFlags & curDirtyBit) != curDirtyBit) { + cur->setDirtyFlag(cur->getDirtyFlag() | curDirtyBit); + cur->setFlagsChanged(hasChangedFlags | curDirtyBit); + int childCount{static_cast(cur->_children.size())}; + if (childCount > 0) { + _computeNodes.resize(i < 0 ? childCount : childCount + i); + } + for (Node *curChild : cur->_children) { + _computeNodes[++i] = curChild; + } + } + curDirtyBit = childDirtyBit; + } +} + +void Node::setWorldPosition(float x, float y, float z) { + _nodeLayout->worldPosition.set(x, y, z); + if (_parent) { + Mat4 invertWMat{_parent->getWorldMatrix()}; + _parent->updateWorldTransform(); + invertWMat.inverse(); + _nodeLayout->localPosition.transformMat4(_nodeLayout->worldPosition, invertWMat); + } else { + _nodeLayout->localPosition.set(_nodeLayout->worldPosition); + } + invalidateChildren(TransformBit::POSITION); +} + +void Node::setWorldRotation(float x, float y, float z, float w) { + _nodeLayout->worldRotation.set(x, y, z, w); + if (_parent) { + _parent->updateWorldTransform(); + _nodeLayout->localRotation.set(_parent->_nodeLayout->worldRotation.getConjugated()); + _nodeLayout->localRotation.multiply(_nodeLayout->worldRotation); + } else { + _nodeLayout->localRotation.set(_nodeLayout->worldRotation); + } + invalidateChildren(TransformBit::ROTATION); +} + +void Node::setParent(Node *parent) { + if (_parent == parent) { + return; + } + if (_parent != nullptr) { + _parent->removeChild(this); + } + _parent = parent; + if (_parent) { + _parent->addChild(this); + } +} + void Node::initWithData(uint8_t *data, uint8_t *flagChunk, uint32_t offset) { _nodeLayout = reinterpret_cast(data); _flagChunk = flagChunk; diff --git a/native/cocos/scene/Node.h b/native/cocos/scene/Node.h index 9bcb31f55e..bf26107a0f 100644 --- a/native/cocos/scene/Node.h +++ b/native/cocos/scene/Node.h @@ -34,7 +34,7 @@ namespace cc { namespace scene { - +enum class TransformBit; // This struct defines the memory layout shared between JS and C++. struct NodeLayout { uint32_t dirtyFlag{0}; @@ -58,21 +58,30 @@ public: Node &operator=(Node &&) = delete; void initWithData(uint8_t *, uint8_t *, uint32_t); + void invalidateChildren(TransformBit dirtyBit); + void updateWorldTransform(); void updateWorldRTMatrix(); - inline void setParent(Node *parent) { - _parent = parent; + void setWorldPosition(float x, float y, float z); + void setWorldRotation(float x, float y, float z, float w); + void setParent(Node *parent); + + inline void addChild(Node *node) { _children.emplace_back(node); } + + inline void removeChild(Node *node) { + auto iter = std::find(_children.begin(), _children.end(), node); + if (iter != _children.end()) { + _children.erase(iter); + } } inline void setFlagsChanged(uint32_t value) { *(reinterpret_cast(_flagChunk) + _flagOffest) = value; } inline void setDirtyFlag(uint32_t value) { _nodeLayout->dirtyFlag = value; } inline void setLayer(uint32_t layer) { _nodeLayout->layer = layer; } inline void setWorldMatrix(const Mat4 &matrix) { _nodeLayout->worldMatrix.set(matrix); } - inline void setWorldPosition(const Vec3 &pos) { _nodeLayout->worldPosition.set(pos); } - inline void setWorldPosition(float x, float y, float z) { _nodeLayout->worldPosition.set(x, y, z); } - inline void setWorldRotation(const Quaternion &rotation) { _nodeLayout->worldRotation.set(rotation); } - inline void setWorldRotation(float x, float y, float z, float w) { _nodeLayout->worldRotation.set(x, y, z, w); } + inline void setWorldPosition(const Vec3 &pos) { setWorldPosition(pos.x, pos.y, pos.z); } + inline void setWorldRotation(const Quaternion &rotation) { setWorldRotation(rotation.x, rotation.y, rotation.z, rotation.w); } inline void setWorldScale(const Vec3 &scale) { _nodeLayout->worldScale.set(scale); } inline void setLocalPosition(const Vec3 &pos) { _nodeLayout->localPosition.set(pos); } inline void setLocalPosition(float x, float y, float z) { _nodeLayout->localPosition.set(x, y, z); } @@ -95,11 +104,13 @@ public: inline const Mat4 & getWorldRTMatrix() const { return _rtMat; }; private: - NodeLayout *_nodeLayout{nullptr}; - Node * _parent{nullptr}; - Mat4 _rtMat; - uint8_t * _flagChunk{nullptr}; - uint32_t _flagOffest{0}; + NodeLayout * _nodeLayout{nullptr}; + Node * _parent{nullptr}; + Mat4 _rtMat; + uint8_t * _flagChunk{nullptr}; + uint32_t _flagOffest{0}; + std::vector _children; + std::vector _computeNodes; }; } // namespace scene diff --git a/native/tools/tojs/scene.ini b/native/tools/tojs/scene.ini index 8149a7c962..8c99da01c2 100644 --- a/native/tools/tojs/scene.ini +++ b/native/tools/tojs/scene.ini @@ -43,7 +43,7 @@ classes = Light Node DirectionalLight SpotLight SphereLight Model SubModel Pass skip = Model::[setInstancedAttrBlock getType getLocalBuffer getModelBounds getWorldBounds], SubModel::[getDescriptorSet getInputAssembler getSubMesh setSubMeshBuffers], Light::[getColor getColorTemperatureRGB getNode getType getUseColorTemperature], - Node::[getFlagsChanged getLayer getWorldMatrix getWorldPosition getWorldRotation getWorldScale setFlagsChanged setDirtyFlag setLayer setWorldMatrix setWorldPosition setWorldRotation setWorldScale setLocalPosition setLocalRotation setLocalScale getNodeLayout getWorldRT updateWorldTransform updateWorldRTMatrix getDirtyFlag getPosition getScale getRotation getParent], + Node::[getFlagsChanged getLayer getWorldMatrix getWorldPosition getWorldRotation getWorldScale setFlagsChanged setDirtyFlag setLayer setWorldMatrix setWorldPosition setWorldRotation invalidateChildren setWorldScale setLocalPosition setLocalRotation setLocalScale getNodeLayout getWorldRT updateWorldTransform updateWorldRTMatrix getDirtyFlag getPosition getScale getRotation getParent], DirectionalLight::[getDirection getIlluminance], SpotLight::[getAABB getAngle getAspect getSpotAngle getDirection getFrustum getIlluminance getNeedUpdate getRange getPosition getSize], SphereLight::[getAABB getIlluminance getPosition getRange getSize],