Scene.pick works in playground but not project

Posting this as a new question as requested by Delta.

Ok, so after thinking about it for a while, I started realizing I was overcomplicating things. To make it easier, I just grabbed the point I wanted the camera to rotate around with scene.pick. I don’t know what I was thinking before, but this works great now.

the playground

Now I have a different problem. I put the code into a project and its not working. It doesn’t appear to detect my double-clicks. Here’s my code

    meshes.forEach(function (mesh) {                

        mesh.ellipsoid = new BABYLON.Vector3(1, 1, 1);

        mesh.ellipsoidOffset = new BABYLON.Vector3(0, 0, 0);

        mesh.scaling.x = mesh.scaling.y = mesh.scaling.z = 1; //0.3;

        mesh.material = new BABYLON.StandardMaterial("modelTexture", scene);

        mesh.material.diffuseTexture = modelTexture;

        mesh.material.bumpTexture = new BABYLON.Texture(modeltexture, scene);               

        mesh.material.specularTexture = new BABYLON.Texture(modeltexture, scene);

        mesh.material.emissiveTexture = new BABYLON.Texture(modeltexture, scene);

        mesh.isPickable = true;

        camera.setTarget(mesh, true, true);

        mesh.renderOutline = true;

        mesh.outlineColor = new BABYLON.Color3(0, 0, 0);

        mesh.outlineWidth = .3;

        setMeshActions(mesh);   // Also tried to set the code directly here, 

                                //  without the function but no joy.

    });

    var setMeshActions = function (mesh) {

    mesh.actionManager = new BABYLON.ActionManager(scene);            

    mesh.actionManager.registerAction(

        new BABYLON.ExecuteCodeAction(

            {

                trigger: BABYLON.ActionManager.OnDoublePickTrigger,

            },

            function () {

                console.log("Got inside the action!");   // doesn't even get here

                var dpic = scene.pick(scene.pointerX, scene.pointerY);

                if (dpic.hit) {

                    var pickresult = scene.pick(scene.pointerX, scene.pointerY);

                }

                camera.setTarget(pickresult.pickedPoint);                        

        }

    )

The logging at the start of the setMeshActions method doesn’t even fire off. I’m not sure what to think, as I moved the code from playground to project over in the same place.

@Wingnut posted this in response to this here ActionManager with AssetManager and Camera pivot points

and its freaking awesome. I may end up using it, thank you very much, but I’m still wondering why the code in my playground works but not when it’s placed in the project. Thanks Wing! Oh hey also, notice with the playground I linked I still get that annoying position of 0,0,0 for all meshes in the model but the action manager works how its set up. And did you mean it has all meshes at 0,0,0 because of the way it was created or the way it was exported?

FYI i’m using BABYLON JS version 3.3

Hi CM, thx for the kind words. On your home project, is the canvas inside an HTML iframe element?

Just curious. Also, have you tried the ZIP button in the playground… to download that playground?

Does THAT downloaded playground… work at home?

Is meshes (or ms) the correct .length… in your home project?

Is your forEach working properly? Put a console.log within THAT?

Just some things to check… which you have probably already checked. hmm. I’ll keep thinking. Others will, too. Stay tuned.

1 Like

Hey Wing,
Lets see…

  1. I have no iframes in the project.
  2. Haven’t tried the zip button, but I copied and pasted the playground code into my project. The model loads properly.
  3. The home project has the correct length.
  4. The forEach is working properly.

Also went through and confirmed that the ActionManager is actually being instantiated and assigned to each mesh and that the actions are present/correct.

Whilst you ponder, I will make a new project and use the zip from the playground and see how that goes.

Thanks!

1 Like

Is pep.js correctly loading before Babylon ?

This is use to polyfill pointer event cross plat, it might be related.

1 Like

Seb, pep is loading correctly

I’ve discovered the problem in my home project. I do apologize, I thought I’d grabbed everything previously and put it in the playground but I had multiple projects. So the new playground is here:

The new playground

I threw in the rest of the missing code then narrowed it down. My ActionManager stops functioning when I try to add in a dynamic texture. In the playground you can see that the code runs fine when the dynamic texture and its update function are commented out. Uncomment them and the double click/camera reposition actions stop working.

I imagine that even the cool click boxes Wing made will not work with the dynamic texture as they are action based, too, though I haven’t tried. How can I have both actions and dynamic textures is the real question now?

It is because there are errors in your PG:

First the image being on Wikipedia, the canvas 2d in the dynamicTexture might become tainted.

Then you have twice the function to add actions (not really a bug as they are the same but still a fix is needed).

Finally you are creating texture by passing another texture inside the ctor instead of simply assiging it:
mesh.material.bumpTexture = new BABYLON.Texture(modeltexture, scene);
mesh.material.specularTexture = new BABYLON.Texture(modeltexture, scene);
mesh.material.emissiveTexture = new BABYLON.Texture(modeltexture, scene);

You could simply apply the wanted texture here.

I have added onError callback to display the issues in the console: https://www.babylonjs-playground.com/#STX192#12

and fixed with another texture here: https://www.babylonjs-playground.com/#STX192#14

2 Likes

Seb, that’s great stuff. I’ll apply that to my project tomorrow and report back. Thanks

1 Like