Jump to content

How To Switch Cameras in A Scene On Buttone Click?


ancientholiday
 Share

Recommended Posts

Hello,

 

In Babylon JS, only one camera is active at a time, howver you can construct multiple cameras in a scene.  How can you allow the users to swtich cameras on a button click.

 

For example, in this Three Js scene, you can hit 1 or 2 button to switch cameras, making an active camera false or true.

https://stemkoski.github.io/Three.js/Multiple-Cameras.html

 

The only example i have seen in Babylon JS is the Train example, however there is no explanation of how to switch the cameas on button click.

 

Thanks,

Moses

Link to comment
Share on other sites

 

Hello, 

scene.activeCamera = myCamera;
i tried to add button 1 and button 2 attach to different cameras.  It does not work        
 
 
 var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
            //var currentCAM= camera;
            console.log(currentCAM);
            function show() {
            alert(currentCAM);
            }
            b1.onclick = function() {
                var currentCAM = camera2;
                show();
                return currentCAM;
               console.log(currentCAM);
            };
           b2.onclick = function() {
                currentCAM = camera;
               show();
                return speed;
               console.log(currentCAM);
            };
           console.log(currentCAM);
Link to comment
Share on other sites

Hi again, AH!

 

http://urbanproductions.com/wingy/babylon/cameras/10cam/index.html  (view source).  That's how I did it, once (BJS v1.14).  I'm not a very good coder, but... maybe there's something in there you can use.  I created and styled my buttons dynamically... with JS.  *shrug* 

 

I did a playground version of it, too, but it quit working with one of the BJS versions and I haven't fixed it yet, sorry.

 

Start simple... get some buttons, make sure they can run functions with their onclicks.  Maybe each function has a simple alert.  Flesh-out the functions (make them switch cameras) AFTER you know that your buttons are causing functions to run.  This way, you have separated the HTML/DOM activity (onclicks)... from the BJS activity (camera stuff).   Party on.

Link to comment
Share on other sites

Hi again, AH!

 

http://urbanproductions.com/wingy/babylon/cameras/10cam/index.html  (view source).  That's how I did it, once (BJS v1.14).  I'm not a very good coder, but... maybe there's something in there you can use.  I created and styled my buttons dynamically... with JS.  *shrug* 

 

I did a playground version of it, too, but it quit working with one of the BJS versions and I haven't fixed it yet, sorry.

 

Start simple... get some buttons, make sure they can run functions with their onclicks.  Maybe each function has a simple alert.  Flesh-out the functions (make them switch cameras) AFTER you know that your buttons are causing functions to run.  This way, you have separated the HTML/DOM activity (onclicks)... from the BJS activity (camera stuff).   Party on.

Your code has an interesting strategy.  Instead of switchng camera names, you actually create a new camera on button click that has the same camea name.  it seems there must be something that prevents changing the active camera names after run-time rendering has started.  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...