Advertisement
Guest User

Untitled

a guest
Sep 27th, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // -----JS CODE-----
  2. // FaceTriggerAnim.js
  3. // Version: 0.0.2
  4. // Event: Lens Initialized
  5. // Description: Plays an animation layer, sound, sprite, on trigger
  6.  
  7. // @input string AnimationTrigger = MouthOpenedEvent { "widget": "combobox", "values": [ { "label": "Brows Lowered", "value": "BrowsLoweredEvent" }, { "label": "Brows Raised", "value": "BrowsRaisedEvent" }, { "label": "Brows Returned To Normal", "value": "BrowsReturnedToNormalEvent" }, { "label": "Face Found", "value": "FaceFoundEvent" }, { "label": "Face Lost", "value": "FaceLostEvent" }, { "label": "Kiss Finished", "value": "KissFinishedEvent" }, { "label": "Kiss Started", "value": "KissStartedEvent" }, { "label": "Mouth Closed", "value": "MouthClosedEvent" }, { "label": "Mouth Opened", "value": "MouthOpenedEvent" }, { "label": "Smile Finished", "value": "SmileFinishedEvent" }, { "label": "Smile Started", "value": "SmileStartedEvent" }, { "label": "Touch Start", "value": "TouchStartEvent" }, { "label": "Touch End", "value": "TouchEndEvent" }, { "label": "Tap", "value": "TapEvent" } ] }
  8. // @input string AnimationStopTrigger = MouthClosedEvent { "widget": "combobox", "values": [ { "label": "Brows Lowered", "value": "BrowsLoweredEvent" }, { "label": "Brows Raised", "value": "BrowsRaisedEvent" }, { "label": "Brows Returned To Normal", "value": "BrowsReturnedToNormalEvent" }, { "label": "Face Found", "value": "FaceFoundEvent" }, { "label": "Face Lost", "value": "FaceLostEvent" }, { "label": "Kiss Finished", "value": "KissFinishedEvent" }, { "label": "Kiss Started", "value": "KissStartedEvent" }, { "label": "Mouth Closed", "value": "MouthClosedEvent" }, { "label": "Mouth Opened", "value": "MouthOpenedEvent" }, { "label": "Smile Finished", "value": "SmileFinishedEvent" }, { "label": "Smile Started", "value": "SmileStartedEvent" }, { "label": "Touch Start", "value": "TouchStartEvent" }, { "label": "Touch End", "value": "TouchEndEvent" }, { "label": "Tap", "value": "TapEvent" } ] }
  9. // @input float openAlpha = 0.0
  10. // @input float closeAlpha = 0.0
  11. // @input int faceIndex = 0
  12.  
  13. // @ui {"widget":"separator"}
  14.  
  15. //  @ui {"widget": "group_start", "label": "3D Anim Control"}
  16. //  @input Component.AnimationMixer AnimationMixer
  17. //  @input string LayerName
  18. //  @input int animationPlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1} // How many times to play the animation, -1 for infinite
  19. //  @ui {"widget": "group_end"}
  20.  
  21. // @ui {"widget":"group_start", "label":"2D Anim Control"}
  22. // @input Component.SpriteVisual[] play2dSprites {"label": "Play Animations"}
  23. // @input int texturePlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1}
  24. // @input float texturePlayOffset = 0 {"label": "Play Offset", "min":0.0, "max":99999.0, "step":1}
  25. // @ui {"widget":"group_end"}
  26.  
  27. // @ui {"widget":"group_start", "label":"Sound Control"}
  28. // @input Asset.AudioTrackAsset[] playAudios {"label": "Play Sounds"}
  29. // @input int audioPlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1}
  30. // @ui {"widget":"group_end"}
  31.  
  32. // @ui {"widget":"separator"}
  33.  
  34. //  @input float TriggerDisableTime = 0
  35.  
  36. var triggerStartTime = getTime() - script.TriggerDisableTime;
  37.  
  38.  
  39. // What happens when event is triggered
  40. function onTriggered()
  41. {
  42.     print("FaceTriggerAnim: " + script.AnimationTrigger + " triggered");
  43.  
  44.     // Called when animation ends
  45.     function animationCallback()
  46.     {
  47.         script.AnimationMixer.setWeight(script.LayerName, 0.0);
  48.     }
  49.  
  50.     // Play animation layer with sound (if available)
  51.     if (script.AnimationMixer && script.LayerName)
  52.     {
  53.         script.AnimationMixer.startWithCallback(script.LayerName, 0.0, script.animationPlayCount, animationCallback);
  54.         script.AnimationMixer.setWeight(script.LayerName, 1.0);            
  55.     }
  56.  
  57.     for (var i = 0; i < script.play2dSprites.length; i++)
  58.     {
  59.         if (script.play2dSprites[i]
  60.             && script.play2dSprites[i].mainPass.baseTex
  61.             && script.play2dSprites[i].mainPass.baseTex.control
  62.             && script.play2dSprites[i].mainPass.baseTex.control.play)
  63.         {
  64.             script.play2dSprites[i].getSceneObject().enabled = true;
  65.             script.play2dSprites[i].mainPass.baseTex.control.play(script.texturePlayCount, script.texturePlayOffset);
  66.             script.play2dSprites[i].mainPass.baseColor = new vec4( script.play2dSprites[i].mainPass.baseColor.r,
  67.                                                                     script.play2dSprites[i].mainPass.baseColor.g,
  68.                                                                     script.play2dSprites[i].mainPass.baseColor.b,
  69.                                                                     script.openAlpha );
  70.         }
  71.     }
  72.  
  73.     for (var i = 0; i < script.playAudios.length; i++)
  74.     {
  75.         if (script.playAudios[i])
  76.         {
  77.             var audioComponent = getAudioComponentForTrack(script.playAudios[i]);
  78.             audioComponent.play(script.audioPlayCount);      
  79.         }
  80.     }
  81.    
  82. }
  83.  
  84. function onStopped()
  85. {
  86.     print("FaceTriggerAnim: " + script.AnimationStopTrigger + " stopped");
  87.  
  88.     // Play animation layer with sound (if available)
  89.     if (script.AnimationMixer && script.LayerName)
  90.     {
  91.         script.AnimationMixer.stop(script.LayerName);
  92.     }
  93.  
  94.     for (var i = 0; i < script.play2dSprites.length; i++)
  95.     {
  96.         if (script.play2dSprites[i]
  97.             && script.play2dSprites[i].mainPass.baseTex
  98.             && script.play2dSprites[i].mainPass.baseTex.control
  99.             && script.play2dSprites[i].mainPass.baseTex.control.play)
  100.         {
  101.             script.play2dSprites[i].getSceneObject().enabled = true;
  102.             script.play2dSprites[i].mainPass.baseTex.control.stop();
  103.             script.play2dSprites[i].mainPass.baseColor = new vec4( script.play2dSprites[i].mainPass.baseColor.r,
  104.                                                                     script.play2dSprites[i].mainPass.baseColor.g,
  105.                                                                     script.play2dSprites[i].mainPass.baseColor.b,
  106.                                                                     script.closeAlpha );
  107.         }
  108.     }
  109.  
  110.     for (var i = 0; i < script.playAudios.length; i++)
  111.     {
  112.         if (script.playAudios[i])
  113.         {
  114.             var audioComponent = getAudioComponentForTrack(script.playAudios[i]);
  115.             audioComponent.stop(true);      
  116.         }
  117.     }
  118. }
  119.  
  120. // Trigger an action if not in cooldown period
  121. function triggerCallback()
  122. {
  123.     if (getTime() >= triggerStartTime + script.TriggerDisableTime) {
  124.         triggerStartTime = getTime();
  125.         onTriggered();
  126.     }
  127. }
  128.  
  129. function triggerStopCallback()
  130. {
  131.     onStopped();
  132. }
  133.  
  134. // Setup the audio component if audio track defined
  135. function getAudioComponentForTrack ( audioTrackAsset )
  136. {
  137.     var trackName = audioTrackAsset.name;
  138.  
  139.     if (!global.ftAudioComponents)
  140.     {
  141.         global.ftAudioComponents = {};
  142.     }
  143.  
  144.     if (!global.ftAudioComponents[trackName])
  145.     {
  146.         var audioComponent = script.getSceneObject().createComponent("Component.AudioComponent");
  147.         audioComponent.audioTrack = audioTrackAsset;
  148.        
  149.         global.ftAudioComponents[trackName] = audioComponent;  
  150.     }
  151.  
  152.     return global.ftAudioComponents[trackName];    
  153. }
  154.  
  155. // Allow fullscreen tapping if trigger is touch based
  156. if (script.AnimationTrigger == "TouchStartEvent"
  157.     || script.AnimationTrigger == "TouchStartEvent"
  158.     || script.AnimationTrigger == "TapEvent")
  159. {
  160.     global.touchSystem.touchBlocking = true;
  161.     global.touchSystem.enableTouchBlockingException("TouchTypeDoubleTap", true);
  162.     global.touchSystem.enableTouchBlockingException("TouchTypeSwipe", true);
  163. }
  164.  
  165. var event = script.createEvent(script.AnimationTrigger);
  166. event.faceIndex = script.faceIndex;
  167. event.bind(triggerCallback);
  168.  
  169. var event = script.createEvent(script.AnimationStopTrigger);
  170. event.faceIndex = script.faceIndex;
  171. event.bind(triggerStopCallback);
  172. triggerStopCallback();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement