{"id":1184,"date":"2021-12-26T14:09:16","date_gmt":"2021-12-26T14:09:16","guid":{"rendered":"http:\/\/mobilearlab.bxmc.poly.edu\/?p=1184"},"modified":"2022-01-18T16:17:45","modified_gmt":"2022-01-18T16:17:45","slug":"behavior-scripts-lens-studio-tutorial-__mobile_ar_lab__nyu","status":"publish","type":"post","link":"https:\/\/mobilearlab.bxmc.poly.edu\/?p=1184","title":{"rendered":"Behavior Scripts Lens Studio Tutorial __Mobile_AR_Lab__NYU"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><strong>Calculating Distance with Behavior Scripts in Lens Studio<\/strong><\/h1>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/dixrIKXfL9P-PtUOFuqqi2_Pa-S9eUwguEeG3epsreBlSlFtmzu5ZB2FoRR3MzcBDdMHNPzZ54SaY5ncdbC04HhkQPO0n0SpZV1WPYWefIeGNTbDRJ0taQbJNVeWIv1TO4bhraCR\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Open a New Project in Lens Studio<\/strong><\/p>\n\n\n\n<p>We will be using this Lens to calculate the distance between the user and a 3D object placed in the world.&nbsp; That distance can then be used to change the output of objects in your scene.<\/p>\n\n\n\n<p><br><br><br><br><br><br><br><br><br><br><br><br><br>Click the <strong>Project Info<\/strong> button at the top-left of the screen and Uncheck the <strong>Front Facing Camera<\/strong> option.&nbsp; We will be using this Lens as a window into a 3D augmented world with our phones as the viewport.<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/uvgl-SRmPtrKIN1b2VU_mDrr7aQlK_J0liFTS_Xw9Mx3X9iibqEldTfJnok2FvoFrvzFq9OpHhn8wIGzcRnHUS2a2PQCzAtXQYqzJFaUF4FDKZdsCKS59gFYR4EP8oO9r5P_DpT-\" width=\"326\" height=\"273\"><\/p>\n\n\n\n<p>In the <strong>Objects<\/strong> panel, click the <strong>+<\/strong> create a new <strong><em>Text3D<\/em><\/strong><em> <\/em>component.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/lvvAFvKI5K4Kj0BsTkxbfV4OZBLA4pVHrh7opwEbI9bIGVoQHrJDD0AyyPdzdQ_KH7v4fX30r6JXFshNCBk-YDdDlp0JbtP4e32pOCuZO6NMSCaOrtf9-ZHtCW3Gc2MzFjEHFx7M\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br>Click <strong>+<\/strong> in the Objects Panel and search for <strong><em>World Object Controller<\/em><\/strong> under<\/p>\n\n\n\n<p>Helper Scripts.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Gq38Zq9knHNdfqG1ur3v_5tCKqZifiyPErWfY6gfSS-5qpktIukOuN4XbQV6fvR_prK4vLTdEgDe0BgkjStlZ3PAOUpvg8_nlMNMTc11YFx-9mWFx-5r7twmJ4uNTfA_xu7EjuzJ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Click the disclosure button to the left of the newly created <strong><em>World Object Controller<\/em><\/strong> and delete the example FBX Object.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/n2mczuSXOA27XzrdKTinNkxrCKa-JLcgUxNQqLYpl01tx-chTRaxa9CjFtDN3yWmyJmlccv2QOwbGU3e5ASbZWDe9mDqQewkCsKTVAeu75_uQImMiLEjozxnLrnnfkVG6PcoxGLW\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br>Replace the deleted resource with the 3D text component and drag it to the top of the World Object Controller\u2019s hierarchy.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/PCo2ZRaqSrIa4pI-n2xAzTkxmM9JN_4KqZL4lxGeSi5heefHdSxuypDTrpxlPPMhb6Kh-iP5jUazHAB6KUuPIVZ1Nd4j3w4xuAORhYkGbWrhayhoUyPKpaHKV71WOKzS9jzd3nYH\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>We are going to add a component that will allow device tracking.&nbsp; Select the <strong>Camera <\/strong>in the <strong>Objects<\/strong> panel.&nbsp; Look for the <strong>Inspector <\/strong>and click the <strong>+Add Component<\/strong> button to search and add <strong><em>Device Tracking<\/em><\/strong>.&nbsp; It will now be added as a component of the <strong>Camera.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/xLpNKsRazdGgDytFs8I1lvLAOCoc6xQFH1t8zX2uC49qOydfISwu69RpkUp2WIThptI2oSpgn1lEL-2E6mIujBYdA-6KTNzLVg_L833CUCc0y-QfRDzjoTTc3xq9zAxUEOU2F261\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br><br>Back in the <strong>Objects<\/strong> panel, click the <strong>+<\/strong> to search and add a <strong><em>Behavior Script.<\/em><\/strong>&nbsp; It is located under Helper Scripts -&gt; Behavior Script.&nbsp; Drag the <strong><em>Behavior Script<\/em><\/strong><strong> <\/strong>over the <strong><em>World Object Controller <\/em><\/strong>object to make it a child of the <strong><em>World Object Controller.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/ToNgcrZRnVcogjveS8hx3KVUx6eFhRRbgFG4OPQfWL79bo-nn-YDvHyNisOELJTNaW-DNf0ktaZISXjGBZLSrsoGZtahJwF6rL7Y3ROza7IlyMSaqBovN1cNy2P4hnm_06KpTIoX\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>In the <strong>Scene<\/strong> panel, use \u201cW\u201d, \u201cE\u201d, and \u201cR\u201d to activate the transform gizmos.<\/p>\n\n\n\n<p>\u201cW\u201d &#8211; Position<\/p>\n\n\n\n<p>\u201cE\u201d &#8211; Rotation<\/p>\n\n\n\n<p>\u201cR\u201d &#8211; Scale.<\/p>\n\n\n\n<p>Then move and scale the <strong><em>Text3D<\/em><\/strong> so that it sits on top of the green<strong><em> Ground Grid<\/em><\/strong> in the scene.<\/p>\n\n\n\n<p>Be sure that your cursor is in the scene panel when you use these shortcuts to avoid renaming your <strong>Objects<\/strong>.&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/hqDeQWOx_x66AkBfRff_Tg2q_IfVmBSMc8IvuKj3D1-F--iMwv6zx0rzRjcUpe9ceIl4AN-4oL1dHdCjy5oN07qUflkKNAAxFYxDtf69hjCeR-wJ7p6LMqCeUVW0CKLZoCP2zIX4\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br>Select the newly created <strong><em>Behavior<\/em><\/strong> script and in the Inspector, switch the <strong>trigger<\/strong> to <strong><em>Distance Check.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/4DMTEQ05e83AkWeDyki1-HRd9sG7e2CeIF_d0IO-YswrIG1tEdhG-FOW1QawAxroiUrXPu_pslehdjtE1pSxT4MjOk4wWakZ5VYTQj9_5E7wwA_UwsbkwJBkSgLQbGT2OGbD-DJ8\" alt=\"\"\/><\/figure>\n\n\n\n<p>Underneath <strong><em>Trigger<\/em><\/strong>, set<strong> <\/strong><strong><em>Object A<\/em><\/strong> to the <strong><em>World Object Controller<\/em><\/strong>.&nbsp; Next, set <strong><em>Object B<\/em><\/strong> to the main <strong>Camera <\/strong>object that has the <em>Device Camera Tracker<\/em> component.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/URUavzXjvNRuC5t33QjMgvuoCdYoBPA0-v9OWpAR0_55OJvMxQhnm9L1tsMdLgfprFCUbuxKxIRfKu_k1yFoAkbhYVjpJNjeWguJroGkQMW64Ge_5WAW9gjC8GvUPGp_AJ14dV-X\" alt=\"\"\/><\/figure>\n\n\n\n<p>Set the <strong><em>Compare Type<\/em><\/strong> to <strong><em>Is Less Than<\/em><\/strong> and provide a unit of distance to compare.&nbsp; This example uses 50.0.&nbsp; <em>You may have to play with these numbers depending on your space.<\/em><\/p>\n\n\n\n<p>Under the <strong><em>Response Type<\/em><\/strong> drop down box, select <strong><em>\u201cSend Custom Trigger\u201d<\/em><\/strong> and set the <strong><em>Trigger Name<\/em><\/strong> to \u201c<em>close<\/em>\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/FRuF2yK_5pIbW8_rfL5qEN0fv5jGZi2mLk044_n3CE2vUqpdF--o7KAGyxlP0G6cr08BL1Fy72yaxjWAtgGCHQrhCMSvRD_1KaiwW_cIbSU50LOEBfUbESoZbDsGQN1sMt6Ysr1U\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br><br><br>Then right click on the <strong><em>Behavior<\/em><\/strong><strong> script<\/strong> component and duplicate it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/TpYDsLOA6WEAXGOCYY7PzCBUn-KS15foEs-CmM-5RBQ_EAfqC1An_uCVdO_8Ydgcna8xIMLcAOHofcdYmRwt0wZPRPvHcUeHrIEJYqvPCwG1TXc_mm6I2BQ5vhhcW-DCW1u1rEOc\" alt=\"\"\/><\/figure>\n\n\n\n<p>Paste the copied <strong><em>Behavior<\/em><\/strong> script below and set the Compare Type equal to<strong> <\/strong><strong><em>Is Greater Than<\/em><\/strong> and rename the Trigger Name to <em>\u201cfar\u201d<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/PUD8iacYGjiHbjNCPJv_y0APzDzJ72I1K4wffVVvSpY2-HfnlZSDsuwMsBZD-Glct-PpmsQ0GerVZ2KHulqdgoPKmn97y74H27JzKVkBlYRjyspuaZTyatDDTzxMRDsWxI0c-NgZ\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now create a new <strong><em>Script<\/em><\/strong> in the Resources Panel and attach it to the <strong><em>Behavior Script <\/em><\/strong>object.&nbsp; Drag it below where our two Distance Check <strong><em>Behavior Scripts<\/em><\/strong> are located.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/yBWGDZn_jSl01jDSex5vmTYFk2r7zlNmHsSvp96ap8f6JGnBvdeOA3Ii-fVcoFb0fXNhANzt3hKGp9X5hcrmRgJysI7JKbyVs8QXDSYl2bXDia1OESzUAkx3IPdy4mm5hA1fFonx\" alt=\"\"\/><\/figure>\n\n\n\n<p>Double click the newly created Script in the <strong>Resources<\/strong> window to open the <strong>Script Editor<\/strong>. Start by adding a new component input with the following code. This will allow you to drag in the 3D Text Scene Object to be changed within our script.<\/p>\n\n\n\n<p>\/\/&#8212;&#8211;JS CODE&#8212;&#8211;<\/p>\n\n\n\n<p>\/\/@input Component.text3D words<\/p>\n\n\n\n<p>The \/\/@input adds access to any type of Lens Studio component.&nbsp; So to access a <strong><em>Text3D<\/em><\/strong> component, you would have to declare the type (Component.text3D) and then name the variable you\u2019ll be referring to in your project (words).&nbsp; Save the script and there will be an input on your script.<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/iKhYvxty-vXQOxyNT3ZF2FnMgkLPvcvZjrnWIiVnrDi2cy-U_AywzeUgWK0NT9Q4ItG7LQzGYDodFfR8k8ySNHkWZPiauafvw-CLTWpvI6kwecHqwOilp5nKaMYj4K_r4wlMspEb\" width=\"275\" height=\"103\"><\/p>\n\n\n\n<p>The <strong><em>Behavior Scripts<\/em><\/strong> that we are using to check distance have a method to receive the <strong><em>custom triggers<\/em><\/strong> they are outputting when the Distance Check conditions are met.&nbsp; You can double click the <strong><em>Behavior Script<\/em><\/strong> in the <strong>Resources<\/strong> panel and parse through the various methods included in the <strong><em>Behavior Script<\/em><\/strong>&nbsp; For our case, we are interested in addCustomTriggerResponse() method.<\/p>\n\n\n\n<p>Go to the <strong>Resources <\/strong>pane and open the Scripts Folder under the <strong>World Object Controller<\/strong> to double click and expose the code of the <strong><em>Behavior Script.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/iDCgMpDaVW6sIvsO7mOuwsj_zB03gY95ew8U5JkeRh94iG6TBpbF2SC3ORh_Py9F5AICgGrrSImkBxzFKTg-v_DDKdUVq7_cqxDxNflgInbJmLqAoUVn7xIJBZkEtH-ESNIBI4D8\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let\u2019s extract this line and copy it into the script with the<strong><em> 3D Text <\/em><\/strong>component so we can make changes when either of the distance check conditions are triggered.<\/p>\n\n\n\n<p>&nbsp;This method looks for the custom trigger with the same name as the \u201ctriggerName\u201d and runs the function callback when that trigger is active. As you are bringing in the distance check behavior scripts, make sure to match the triggerName to the trigger name from the <em>Behavior <\/em>scripts. This value is passed in as a \u201cstring\u201d.<\/p>\n\n\n\n<p>Then set up your callback functions in the same script. For simplicity, you can name your callback functions the same as your custom trigger names. Because we have the 3D Text component input, we can set the text for the 3D object with either of these functions that are called when the custom trigger is sent.<\/p>\n\n\n\n<p><strong><br><\/strong><strong><br><\/strong><strong><br><\/strong><strong><br><\/strong><strong><br><\/strong><strong>Example Code:<\/strong><\/p>\n\n\n\n<p>\/\/@input Component.Text3D words<\/p>\n\n\n\n<p>global.behaviorSystem.addCustomTriggerResponse(&#8220;close&#8221;, close);<\/p>\n\n\n\n<p>global.behaviorSystem.addCustomTriggerResponse(&#8220;far&#8221;, far);<\/p>\n\n\n\n<p>function close() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;script.words.text = &#8220;close&#8221;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;}<\/p>\n\n\n\n<p>function far() {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;script.words.text = &#8220;far&#8221;;<\/p>\n\n\n\n<p>&nbsp;&nbsp;}<\/p>\n\n\n\n<p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>Finally, preview the effects in the <strong>Preview Pane<\/strong> by selecting the environment in the upper part of the window.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Z3j2OfxzjeVi9SYHE20HV0CHvsg-JBhjDASYiNjeoSH8K-SVKL9mB4ffVScb37ExzTCgLZ9iSnpYG9YlfOj4AlvWhbGbHUwWQ0iXG941xXsVP3mvR93jr92PhNjL4GdJ25-pI0TC\" alt=\"\"\/><\/figure>\n\n\n\n<p><br><br><br><br><br><br><br><br>You can also pair your Snapchat account and send the Lens to your device.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/VbjtO7Sz0gdBkZyL4DkW3aFP3eTasLKYDbdJHm-NN2gEzKfAB0I8MHlN75oopt8LHxXPKwtNnB-yP3TCof83ARzfmzkXjNEoY4K8kbiBa3IDxLKcA4q0_ZPiQNd6MotAXSRxl-FT\" alt=\"\"\/><\/figure>\n\n\n\n<p>The <strong><em>Text3D<\/em><\/strong><em> <\/em>Component is just one of many possibilities able to be controlled with Behavior Scripts.&nbsp; Look through all of the Triggers on the Behavior script to see what else you can control!<\/p>\n\n\n\n<p>Tags:<\/p>\n\n\n\n<p>snapchat, lensstudio, augmentedreality, ar, nyu, idm, integrateddesignandmedia, newyorkuniversity, nyuar, mobilearlab, tutorials, script, javascript, behavior, filters, arprojects, easyarprojects, easyar, augmented, 3d, condition, distancecheck, seestrawrun, christopherstrawley,  augmentedrealitytutorials, mark skwarek<\/p>\n\n\n<p><a href=\"http:\/\/mobilearlab.bxmc.poly.edu\/wp-content\/uploads\/2021\/12\/Behavior-Scripts-Lens-Studio__Mobile_AR_Lab__NYU.pdf\">Behavior Scripts Lens Studio__Mobile_AR_Lab__NYU<\/a><\/p>\n<p>by Christopher Strawley<\/p>","protected":false},"excerpt":{"rendered":"<p>Calculating Distance with Behavior Scripts in Lens Studio Open a New Project in Lens Studio We will be using this Lens to calculate the distance between the user and a 3D object placed in the world.&nbsp; That distance can then be used to change the output of objects in your scene. Click the Project Info &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/mobilearlab.bxmc.poly.edu\/?p=1184\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Behavior Scripts Lens Studio Tutorial __Mobile_AR_Lab__NYU&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[254,21,251,255,14,241,260,249,259,257,256,253,252,250,16,242,248,240,114,245,243,3,244,247,258,200,246],"class_list":["post-1184","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-3d","tag-ar","tag-arprojects","tag-augmented","tag-augmented-reality","tag-augmentedreality-2","tag-augmentedrealitytutorials","tag-behavior","tag-christopherstrawley","tag-condition","tag-distancecheck","tag-easyar","tag-easyarprojects","tag-filters","tag-idm","tag-integrateddesignandmedia","tag-javascript","tag-lensstudio","tag-mark-skwarek","tag-mobilearlab","tag-newyorkuniversity","tag-nyu","tag-nyuar","tag-script","tag-seestrawrun","tag-snapchat","tag-tutorials"],"_links":{"self":[{"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/posts\/1184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1184"}],"version-history":[{"count":5,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/posts\/1184\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=\/wp\/v2\/posts\/1184\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mobilearlab.bxmc.poly.edu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}