Skip to content

Commit 20a74e0

Browse files
fix: setAdditionalTransform support reference
1 parent 8341df8 commit 20a74e0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

cocos/2d/CCNode.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,15 +1788,25 @@ void Node::setAdditionalTransform(Mat4* additionalTransform)
17881788
if (additionalTransform == nullptr)
17891789
{
17901790
_useAdditionalTransform = false;
1791+
_additionalTransform = Mat4::IDENTITY;
17911792
}
17921793
else
17931794
{
1795+
// FIXME: Why "set" multiplies with the previous one.
1796+
// "set" should just replace the old one.
1797+
// in any case, we should add a new method called "concatAdditionalTransform"
17941798
_additionalTransform = *additionalTransform;
17951799
_useAdditionalTransform = true;
17961800
}
17971801
_transformUpdated = _transformDirty = _inverseDirty = true;
17981802
}
17991803

1804+
void Node::setAdditionalTransform(const Mat4& additionalTransform)
1805+
{
1806+
_useAdditionalTransform = true;
1807+
_additionalTransform = additionalTransform;
1808+
_transformUpdated = _transformDirty = _inverseDirty = true;
1809+
}
18001810

18011811
AffineTransform Node::getParentToNodeAffineTransform() const
18021812
{

cocos/2d/CCNode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,7 @@ class CC_DLL Node : public Ref
16441644
* @param additionalTransform An additional transform matrix.
16451645
*/
16461646
void setAdditionalTransform(Mat4* additionalTransform);
1647+
void setAdditionalTransform(const Mat4& additionalTransform);
16471648
void setAdditionalTransform(const AffineTransform& additionalTransform);
16481649

16491650
/// @} end of Coordinate Converters

cocos/2d/CCScene.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,14 @@ void Scene::render(Renderer* renderer, const Mat4& eyeTransform)
201201
defaultCamera = Camera::_visitingCamera;
202202
}
203203

204-
Mat4 eyeCopy = camera->getNodeToParentTransform();
205-
// eyeTransform is in "camera/view" coordinates. Convert it to "model" coordinates
206-
camera->setNodeToParentTransform(eyeCopy * eyeTransform.getInversed());
204+
// There are two ways to modify the "default camera" with the eye Transform:
205+
// a) modify the "nodeToParentTransform" matrix
206+
// b) modify the "additional transform" matrix
207+
// both alternatives are correct, if the user manually modifies the camera with a camera->setPosition()
208+
// then the "nodeToParent transform" will be lost.
209+
// And it is important that the change is "permament", because the matrix might be used for calculate
210+
// culling and other stuff.
211+
camera->setAdditionalTransform(eyeTransform.getInversed());
207212

208213
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
209214
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix());
@@ -224,7 +229,9 @@ void Scene::render(Renderer* renderer, const Mat4& eyeTransform)
224229

225230
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
226231

227-
camera->setNodeToParentTransform(eyeCopy);
232+
// we shouldn't restore the transform matrix since it could be used
233+
// from "update" or other parts of the game to calculate culling or something else.
234+
// camera->setNodeToParentTransform(eyeCopy);
228235
}
229236

230237
#if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION

0 commit comments

Comments
 (0)