Camera Switching

  • Hi Anas!

    Glad you're making your first Lens!

    Take a look at BehaviorScript which allows you to do some certain action based on some events:  

    https://lensstudio.snapchat.com/guides/scripting/behavior-script/

    So let's say you have some scene like the following:

     

    Then we can add a new object to control which object is visible depending on the camera. In the Object panel press Add New > Object. Then, in the Inspector panel, press Add Component > Script. Finally press the Add Script button and select the BehaviorScript you've imported from the page above. 

    Now you should see some options. Select the Trigger drop down and choose "Front camera" Then select the Response Type "Set Enabled". Click the "Target" field and select the object that you want to show up in the Front Camera. 

    Then we can similarly add another BehaviorScript to this controller object to disable the objects that should only appear in the rear camera. 

    You can do the same for the Trigger "Back Camera". To enable objects that you want to be visible in the rear camera, and disable objects that you want to not be visible in the rear camera. 

    Your final scene set up should looks something like this:

    And that's it! Let me know if you need any clarification :)

    Cheers,

    Jon

    Comment actions Permalink
  • Dear Jonathan,

    I really appreciate your help it really adds a lot to my knowledge in the Lens studio journey :)

    I tried it but my set up is different i guess to i didn't know how to handle it in my situation.

    what i am planning to do is to have a tracking object like a helmet to set on my face ( which i learned how to do ) 

    now if i opened my mouth lets say or winked i need the camera to switch automatically and show me a 3d object in the world camera ( which i got to know how to make ) with a static txt on it.

     

    i tried the way you explained to me .. but got lots of troubles with the cameras 

    i guess there are lots of thing still to learn, So i really appreciate if you can help me again.

     

    Appreciate your help and best regards

     

    Comment actions Permalink
  • Comment actions Permalink
  • Hi Anas! 

    Ah I see. Thanks for sharing your scene, it's very helpful.

    It is not possible to have the Lens switch which camera is active. However, you can show a hint to tell the user to switch camera. Take a look at this guide: https://lensstudio.snapchat.com/guides/scripting/scripting-hints/ 

    Additionally, I've modified the WorldObjectController script to allow you to add a list of objects to enable only on the front side:

    // ModifiedWorldObjectController.js
    // Version: 0.0.3
    // Event: Lens Initialized
    // Description: Controls the touch, hiding, and showing functionality of the movable character

    //@input bool useGroundGrid
    //@input Asset.Material touchCollisionMaterial
    //@input SceneObject groundGrid
    //@input Component.WorldTracking worldTrackingComponent

    //@input SceneObject[] frontOnlyObjects

    // If an object with a touch component is defined then this will allow the user to double tap through them to
    // perform a camera swap from back to front cam
    if(script.getSceneObject().getComponentCount("Component.TouchComponent") > 0)
    {
    script.getSceneObject().getFirstComponent("Component.TouchComponent").addTouchBlockingException("TouchTypeDoubleTap");
    }

    // Hides the ground grid if the option is chosen to do so
    if(!script.useGroundGrid && script.groundGrid)
    {
    script.groundGrid.enabled = false;
    }

    // Hides the touchCollision object when lens is running by setting the alpha on its material to 0
    if(script.touchCollisionMaterial)
    {
    script.touchCollisionMaterial.mainPass.baseColor = new vec4(1,1,1,0);
    }

    // Event and callback setup
    function onSurfaceReset(eventData)
    {
    script.getSceneObject().getTransform().setLocalPosition(new vec3(0, 0, 0));
    setTrackingTarget();
    }
    var worldTrackingResetEvent = script.createEvent("WorldTrackingResetEvent");
    worldTrackingResetEvent.bind(onSurfaceReset);

    function onFrontCamEvent(eventData)
    {
    for(var i = 0; i < script.getSceneObject().getChildrenCount(); i++)
    {
    var childObject = script.getSceneObject().getChild(i);
    if(childObject)
    {
    childObject.enabled = false;
    }
    }

    for(var j = 0; j < script.frontOnlyObjects.length; j++)
    {
    var object = script.frontOnlyObjects[j];
    if(object)
    {
    object.enabled = true;
    }
    }
    }
    var cameraFrontEvent = script.createEvent("CameraFrontEvent");
    cameraFrontEvent.bind(onFrontCamEvent);

    function onBackCamEvent(eventData)
    {
    for(var i = 0; i < script.getSceneObject().getChildrenCount(); i++)
    {
    var childObject = script.getSceneObject().getChild(i);
    if(childObject)
    {
    childObject.enabled = true;
    }
    }
    if(!script.useGroundGrid && script.groundGrid)
    {
    script.groundGrid.enabled = false;
    }

    for(var j = 0; j < script.frontOnlyObjects.length; j++)
    {
    var object = script.frontOnlyObjects[j];
    if(object)
    {
    object.enabled = false;
    }
    }
    }
    var cameraBackEvent = script.createEvent("CameraBackEvent");
    cameraBackEvent.bind(onBackCamEvent);

    function setTrackingTarget()
    {
    if(script.worldTrackingComponent)
    {
    script.worldTrackingComponent.surfaceTrackingTarget = script.getSceneObject();
    }
    }
    setTrackingTarget();

    To use it, in the Resources panel, open the Scripts folder and select WorldObjectController. Then in the Inspector panel, replace the script and press "Apply Changes."

    Now, in the Objects panel, when you select FaceImagePickerController, in the Inspector panel you should see a new field called "Front Object Only". You can press the "Add Value" button to add objects you want to only show up in the front camera. 

    For example, here I added the "2D Sprite 1" object so that the sprite will only show up when the front camera is active.

     

    Can't wait to see what you come up with!

     

    Cheers,

    Jon

    Comment actions Permalink
  • Awesome, Thanks a lot dear Joe for your help and efforts .. I started getting through ..

    I started understanding how it works. Really appreciate your help .. 

    Thaaaaaanks 

    Best regards

     

    Comment actions Permalink
Have a comment?

Please sign in to leave a comment.

We're here to help! We just need a little bit of information...

What system are you using?

Have you downloaded the latest version of Lens Studio?

Please download the latest version of Lens Studio. If you still run into this issue, please come back and report it!

Is this issue causing Lens Studio to crash?

What system do you run Lens Studio on?

Version

Graphics

Follow the next steps to help us solve your issue:

  • Copy and paste this text into your TerminalCommand Window
    open ~/Library/Preferences/Snap/Lens\ Studio/ %LOCALAPPDATA%\Snap\Lens Studio Copy Text
  • Press ReturnEnter to run the command. The Lens Studio folder will automatically open
  • Prepare to upload your files: zip the "Log" Folder by right-clicking and choosing "compress."
    Locate the Log.txt file right above it.

    Attach a screenshot of the issue:

Name:

Email:

What is this most relevant to?

Please enter a brief description of your issue:

Thanks for submitting this issue.

Unfortunately, it's likely due to the operating system or hardware you're using – since they don't meet the system requirements for Lens Studio.

Still, we hear you loud and clear, and are logging the issue in case there's ever a workaround we can provide!

Minimum Requirements

Operating System: Windows 10 (64 bit); MacOS 10.11+

Hardware: Minimum of Intel Core i3 2.5Ghz or AMD Phenom II 2.6Ghz with 4 GB RAM; Intel HD Graphics 4000 / Nvidia GeForce 710 / AMD Radeon HD 6450 or better; screen resolution of 1280x768 or higher

We'll try to resolve this issue as soon as possible. Thanks for letting us know about it!

Keep an eye out for a followup email from us. We may have a couple more questions for you, or we might already have a solution to offer.

Happy creating!