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

Fix bug #2277 - GltfLoader #2309

Merged
merged 5 commits into from
Sep 4, 2024
Merged

Conversation

JNightRider
Copy link
Contributor

This PR fixes bug #2277 (GltfLoader issues).

This error originates when comparing 2 objects, the return is always false (result). jme3-plugins-json-gson always returns a new object of type com.jme3.plugins.json.JsonElement* (when accessing JSON elements), each new object confuses the gltf loader when compared using the equals(java.lang.Object) method.

A clear example where this error occurs is on line 1054 when searching for a JSON object within a list using the indexOf() method (class - GltfLoader).

Using the code created by @stephengold, now numComposers is 1

import com.jme3.anim.AnimComposer;
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.jme3.scene.plugins.gltf.GltfModelKey;
import java.util.List;

public class TestYBot extends SimpleApplication {

    public static void main(String[] args) {
        TestYBot app = new TestYBot();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        GltfModelKey modelKey = new GltfModelKey("YBot.gltf");
        Spatial modelRoot = assetManager.loadModel(modelKey);
        int numComposers = countControls(modelRoot, AnimComposer.class);
        System.out.println("numComposers = " + numComposers);
        stop();
    }

    /**
     * Count all controls of the specified type in the specified subtree of a
     * scene graph. Note: recursive!
     *
     * @param <T> subclass of Control
     * @param subtree the subtree to analyze (may be null, unaffected)
     * @param controlType the subclass of Control to search for
     * @return the count (&ge;0)
     */
    private static <T extends Control> int countControls(
            Spatial subtree, Class<T> controlType) {
        int result = 0;

        if (subtree != null) {
            int numControls = subtree.getNumControls();
            for (int controlI = 0; controlI < numControls; ++controlI) {
                Control control = subtree.getControl(controlI);
                if (controlType.isAssignableFrom(control.getClass())) {
                    ++result;
                }
            }
        }

        if (subtree instanceof Node) {
            Node node = (Node) subtree;
            List<Spatial> children = node.getChildren();
            for (Spatial child : children) {
                result += countControls(child, controlType);
            }
        }

        return result;
    }
}

@stephengold
Copy link
Member

Thank you @JNightRider for investigating the issue and proposing a fix.

@@ -11,6 +11,8 @@ sourceSets {

dependencies {
api project(':jme3-core')
api 'com.google.code.gson:gson:2.9.1'
Copy link
Contributor

@pspeed42 pspeed42 Sep 2, 2024

Choose a reason for hiding this comment

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

I don't understand why all of these import and library changes were necessary.

Edit: or rather, I don't understand how Ric's changes were working at all before without this.... and if it was working, I don't know why suddenly it needs to change just to fix how .equals()/.hashCode() is working. I'm very confused.

Copy link
Member

Choose a reason for hiding this comment

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

@pspeed42 I disabled most of Riccardo's changes to work around the bug.

Copy link
Contributor Author

@JNightRider JNightRider Sep 2, 2024

Choose a reason for hiding this comment

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

or rather, I don't understand how Ric's changes were working at all before without this.... and if it was working

These changes were removed as a temporary fix; Currently modules jme3-plugins-json and jme3-plugins-json-gson are not used.

I don't know why suddenly it needs to change just to fix how .equals()/.hashCode() is working.

@riccardobl's changes are no longer present in the loader (for the moment), it wouldn't make sense to fix something that isn't being used, which is why library imports (reconnect) are important.

@JNightRider
Copy link
Contributor Author

With this 'commit', the changes have already been applied.

@tonihele tonihele self-requested a review September 3, 2024 05:26
@stephengold
Copy link
Member

Thanks for the comments and review.
@pspeed42 have all your concerns been addressed?

@pspeed42
Copy link
Contributor

pspeed42 commented Sep 4, 2024

Yeah, I think so. Mostly I was confused. :)

@stephengold stephengold merged commit 6709ee8 into jMonkeyEngine:master Sep 4, 2024
14 checks passed
scenemax3d pushed a commit that referenced this pull request Oct 21, 2024
* fix bug #2277

* update

* script error

* module implementation

* corrections...

---------

Co-authored-by: wil <wil@debian>
@stephengold stephengold added this to the v3.7.0 milestone Oct 22, 2024
@stephengold stephengold added the bug Something that is supposed to work, but doesn't. More severe than a "defect". label Oct 22, 2024
@JNightRider JNightRider deleted the fix_2277 branch November 9, 2024 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that is supposed to work, but doesn't. More severe than a "defect".
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants