Advertisement
Guest User

TweenEyeColor

a guest
Sep 20th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // TweenEyeColor.js
  2. // Version: 0.0.1
  3. // Event: Any Event
  4. // Description: Runs a tween on a Lens Studio color using TweenJS
  5.  
  6. //@input SceneObject sceneObject
  7. //@input string tweenName
  8. //@input bool playAutomatically = true
  9. //@input int loopType = 0 {"widget":"combobox", "values":[{"label":"None", "value":0}, {"label":"Loop", "value":1}, {"label":"Ping Pong", "value":2}]}
  10.  
  11. //@ui {"widget":"separator"}
  12. //@input vec4 start = {1,1,1,1} {"widget":"color"}
  13. //@input vec4 end = {1,1,1,1} {"widget":"color"}
  14. //@input bool recursive = false
  15. //@input bool ignoreAlpha = false
  16. //@input float time = 1.0
  17. //@input float delay = 0.0
  18. //@input bool isLocal = true
  19.  
  20. //@ui {"widget":"separator"}
  21. //@input string easingFunction = "Quadratic" {"widget":"combobox", "values":[{"label":"Linear", "value":"Linear"}, {"label":"Quadratic", "value":"Quadratic"}, {"label":"Cubic", "value":"Cubic"}, {"label":"Quartic", "value":"Quartic"}, {"label":"Quintic", "value":"Quintic"}, {"label":"Sinusoidal", "value":"Sinusoidal"}, {"label":"Exponential", "value":"Exponential"}, {"label":"Circular", "value":"Circular"}, {"label":"Elastic", "value":"Elastic"}, {"label":"Back", "value":"Back"}, {"label":"Bounce", "value":"Bounce"}]}
  22. //@input string easingType = "Out" {"widget":"combobox", "values":[{"label":"In", "value":"In"}, {"label":"Out", "value":"Out"}, {"label":"In / Out", "value":"InOut"}]}
  23.  
  24. // If no scene object is specified, use object the script is attached to
  25. if( !script.sceneObject )
  26. {
  27.     script.sceneObject = script.getSceneObject();
  28. }
  29.  
  30. // Setup the external API
  31. script.api.tweenName = script.tweenName;
  32. script.api.startTween = startTween;
  33. script.api.resetObject = resetObject;
  34. script.api.tween = null;
  35.  
  36. // Play it automatically if specified
  37. if( script.playAutomatically )
  38. {
  39.     // Start the tween
  40.     startTween();
  41. }
  42.  
  43. // Create the tween with passed in parameters
  44. function startTween()
  45. {
  46.     var startValue = {
  47.         "r": script.start.r,
  48.         "g": script.start.g,
  49.         "b": script.start.b,
  50.         "a": script.start.a
  51.     };
  52.  
  53.     var endValue = {
  54.         "r": script.end.r,
  55.         "g": script.end.g,
  56.         "b": script.end.b,
  57.         "a": script.end.a
  58.     };
  59.  
  60.     // Reset object to start
  61.     resetObject();
  62.  
  63.     // Create the tween
  64.     var tween = new TWEEN.Tween(startValue)
  65.         .to( endValue, script.time * 1000.0 )
  66.         .delay( script.delay * 1000.0 )
  67.         .easing( global.tweenManager.getTweenEasingType( script.easingFunction, script.easingType ) )
  68.         .onUpdate( updateValue );
  69.  
  70.     // Configure the type of looping based on the inputted parameters
  71.     global.tweenManager.setTweenLoopType( tween, script.loopType );
  72.  
  73.     // Save reference to tween
  74.     script.api.tween = tween;
  75.  
  76.     // Start the tween
  77.     script.api.tween.start();
  78. }
  79.  
  80. // Resets the object to its start
  81. function resetObject()
  82. {
  83.     var startValue = {
  84.         "r": script.start.r,
  85.         "g": script.start.g,
  86.         "b": script.start.b,
  87.         "a": script.start.a
  88.     };
  89.    
  90.     // Initialize transform to start value
  91.     updateValue( startValue );
  92. }
  93.  
  94. // Here's were the values returned by the tween are used
  95. // to drive the transform of the SceneObject
  96. function updateValue(value)
  97. {
  98.     updateComponentValue( script.sceneObject, "Component.EyeColorVisual", value );
  99. }
  100.  
  101. function updateComponentValue( sceneObject, componentName, value )
  102. {
  103.     for( var i = 0; i < sceneObject.getComponentCount( componentName); i++ )
  104.     {
  105.         var visual = sceneObject.getComponentByIndex( componentName, i );
  106.         if( script.ignoreAlpha )
  107.         {
  108.             var currColor = visual.getMaterial(0).getPass(0).baseColor;
  109.             visual.getMaterial(0).getPass(0).baseColor = new vec4( value.r, value.g, value.b, currColor.a );
  110.         }
  111.         else
  112.         {
  113.             visual.getMaterial(0).getPass(0).baseColor = new vec4( value.r, value.g, value.b, value.a );
  114.         }
  115.     }
  116.  
  117.     if( script.recursive )
  118.     {
  119.         for( var i = 0; i < sceneObject.getChildrenCount(); i++ )
  120.         {
  121.             updateComponentValue( sceneObject.getChild(i), componentName, value );
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement