Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8.4] [native] Fix that 2d object's world transform not being updated if modifying local position/rotation/scale in Director.EVENT_AFTER_DRAW callback. #17555

Merged
merged 2 commits into from
Aug 20, 2024

Conversation

dumganhar
Copy link
Contributor

@dumganhar dumganhar commented Aug 19, 2024

And run performed functions at the end of Engine::tick instead of Director::AfterPersent.

Added testcase:

cocos/cocos-test-projects#881

cocos/cocos-test-projects#882

Reported at : https://forum.cocos.org/t/topic/159379/380?u=dumganhar

Reason

Review the code in director.ts:

public tick (dt: number): void {
    ......

        this.emit(Director.EVENT_BEFORE_DRAW);
        uiRendererManager.updateAllDirtyRenderers();
        this._root!.frameMove(dt);
        // The old code will invoke Scheduler::runFunctionsToBePerformedInCocosThread in frameMove.
        // And the code in those user callback may call `node.setPosition/setScale/setRotation` to update the local transform for node.
        // The return value of Node's getChangedFlags() will be updated, but since it's invoked after render, the world transform will not be updated in the current frame.
        // In the next frame, Batcher2D will only check whether the return value of Node::getChangedFlags() is dirty, 
        // because it's in the next frame, Node::getChangedFlags will return 0 which means `NO CHANGES`.
        // This make the 2D object's world transform not being updated.
        this.emit(Director.EVENT_AFTER_DRAW);

        Node.resetHasChangedFlags();
        Node.clearNodeArray();
        scalableContainerManager.update(dt);
        this.emit(Director.EVENT_END_FRAME);
        this._totalFrames++;
    }
}
inline uint32_t getChangedFlags() const {
    return _hasChangedFlagsVersion == globalFlagChangeVersion ? _hasChangedFlags : 0;
}

CC_FORCE_INLINE void Batcher2d::handleComponentDraw(RenderEntity* entity, RenderDrawInfo* drawInfo, Node* node) {
    ......
    if (!drawInfo->getIsMeshBuffer()) {
        if (node->getChangedFlags() ||  drawInfo->getVertDirty()) { // Only check getChangedFlags here which is not enough, should also check Node::isTransformDirty().
            fillVertexBuffers(entity, drawInfo);
            drawInfo->setVertDirty(false);
        }
        if (entity->getVBColorDirty()) {
            fillColors(entity, drawInfo);
        }

        fillIndexBuffers(drawInfo);
    }

    if (isMask) {
        _stencilManager->enableMask();
    }
}

Re: #

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Greptile Summary

This pull request addresses an issue with 2D object world transforms not updating correctly when modified in the Director.EVENT_AFTER_DRAW callback, by adjusting the timing of transform updates and async task execution.

  • Modified Batcher2d::handleComponentDraw in native/cocos/2d/renderer/Batcher2d.cpp to check node->isTransformDirty() for more accurate transform updates
  • Removed AfterPresent event listener in native/cocos/bindings/manual/jsb_scene_manual.cpp to adjust async task execution timing
  • Added _scheduler->runFunctionsToBePerformedInCocosThread() call at the end of Engine::tick() in native/cocos/engine/Engine.cpp for improved async task handling
  • These changes ensure proper updating of 2D object world transforms when modified in specific callbacks

…ed if modifying local position/rotation/scale in Director.EVENT_AFTER_DRAW callback.
@dumganhar dumganhar requested a review from minggo August 19, 2024 06:17
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

3 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@dumganhar
Copy link
Contributor Author

@cocos-robot run test cases

Copy link

Interface Check Report

This pull request does not change any public interfaces !

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-line-join
ios PASS PASS PASS
mac PASS PASS PASS

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

@dumganhar
Copy link
Contributor Author

@cocos-robot run test cases

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL director-after-draw-test
ios PASS PASS FAIL director-after-draw-test
mac PASS PASS FAIL director-after-draw-test

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
windows PASS PASS FAIL director-after-draw-test
android PASS PASS FAIL director-after-draw-test
wechatgame PASS PASS FAIL director-after-draw-test

@dumganhar
Copy link
Contributor Author

@cocos-robot run test cases

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-continuous-filling
ios PASS PASS PASS
mac PASS PASS PASS

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

@dumganhar dumganhar merged commit bc73e79 into cocos:v3.8.4 Aug 20, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants