Native worldTransform synchronization (#3764)

This commit is contained in:
GengineJS
2021-07-26 15:21:27 +08:00
committed by GitHub
parent 8f96d02de0
commit 01c576e735
3 changed files with 95 additions and 21 deletions
+71 -8
View File
@@ -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<Node *> 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<uint32_t>(dirtyBit)};
const uint32_t childDirtyBit{curDirtyBit | static_cast<uint32_t>(TransformBit::POSITION)};
if (static_cast<int>(_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<int>(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<NodeLayout *>(data);
_flagChunk = flagChunk;
+23 -12
View File
@@ -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<uint32_t *>(_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<Node *> _children;
std::vector<Node *> _computeNodes;
};
} // namespace scene
+1 -1
View File
@@ -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],