Nutcracker, new effect: user_defined. Do you want to create your own animation c

Discussion in 'xLights (Nutcracker)' started by smeighan, Apr 4, 2012.

  1. smeighan

    smeighan Dedicated Elf

    Joined:
    Jan 19, 2012
    Messages:
    1,055
    Likes Received:
    4
    Location:
    4217 Greenfinch Dr CO 80126
    Would you like to write your animations? The new effect, user_defined allows you to do this.

    The audience for this effect will be anyone who has some programming knowledge. You are going to provide a code snippet in the PHP language.

    When you select user_defined, the Nutcracker will call a routine called user_defined.

    here is the wrapper for the call
    $maxFrame=80;
    Code:
    for($frame=1;$frame<=$maxFrame;$frame++)
       for($s=1;$s<=$maxStrand;$s++)
           for($p=1;$p<=$maxPixel;$p++)
                 $rgb=user_defined($frame,$s,$p,$maxFrame,$maxStrand,$maxPixel,$user1);
    



    So your job is to create code that will return a rgb value for every frame, strand and pixel.






    Lets have some examples:
    Example #1
    Code:
    if($frame%4==0) $rgb=hexdec("#FFFFFF");  // WHITE
    if($frame%4==1) $rgb=hexdec("#FF0000"); // RED
    if($frame%4==2) $rgb=hexdec("#00FF00"); // GREEN
    if($frame%4==3) $rgb=hexdec("#0000FF");  // BLUE
    





    hexdec(string) converts hexadecimal string into a decimal.

    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER1
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 200
    sparkles 0
    param1 1
    param2 2
    seq_duration 2
    php_program if($frame%4==0) $rgb=hexdec(\"#FFFFFF\"); if($frame%4==1) $rgb=hexdec(\"#FF0000\"); if($frame%4==2) $rgb=hexdec(\"#00FF00\"); if($frame%4==3) $rgb=hexdec(\"#0000FF\");
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]












    Example #2
    Code:
    $rgb=0;
    if($s%2==0)
    {
    if($p<=$frame)
    $rgb=hexdec("FF0000");
    }
    else if($s%2==1)
    {
    if($p>=($maxPixel-$frame))
    $rgb=hexdec("0000FF");
    }

    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER2
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 100
    sparkles 0
    param1 1
    param2 2
    seq_duration 2
    php_program $rgb=0; if($s%2==0) { if($p<=$frame) $rgb=hexdec(\"FF0000\"); } else if($s%2==1) { if($p>=($maxPixel-$frame)) $rgb=hexdec(\"0000FF\"); }
    submit Submit Form to create your effect
    OBJECT_NAME user_defined
    [​IMG]








    Example #3
    Code:
    $H=$S=$V=0;
    if($s%2==0)
    {
    if($p<=$frame)
       {
       $H=$p/$maxPixel;
       $S=$V=1;
       }
    }
    else if($s%2==1)
    {
    if($p>=($maxPixel-$frame))
       {
       $H=1- ($p/$maxPixel);
       $S=$V=1;
       }
    }
      $rgb=HSV_TO_RGB ($H, $S, $V);



    Now we get to using a function that will use HUE, SATURATION and VALUE. H is a value between 0 to 1.0. This varies the hue through the color specturm.
    $S, saturation, is a 1 if we are fully saturated. S=0 means we have a WHITE. $V is the brightness value. $V=1 full brightness, $V=0 = black.


    H,S,V is much better for creating animations because of the ease of sliding through the colors. I wrote a function, HSV_TO_RGB to change HSV into an rgb value.

    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER3
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 100
    sparkles 0
    param1 1
    param2 2
    seq_duration 2
    php_program $H=$S=$V=0; if($s%2==0) { if($p<=$frame) { $H=$p/$maxPixel; $S=$V=1; } } else if($s%2==1) { if($p>=($maxPixel-$frame)) { $H=1- ($p/$maxPixel); $S=$V=1; } } $rgb=HSV_TO_RGB ($H, $S, $V);
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]








    Example #4
    Code:
    $s_ratio=$s/$maxStrand; $p_ration=$p/$maxPixel;
    $H1=sin($s_ratio) * cos($p_ratio);
    $H = $H1 * $frame;
    if($H>1) $H=$H- intval($H);
    $S=$V=1;
    $rgb=HSV_TO_RGB ($H, $S, $V);

    We can use math functions (sin, cos .etc). We need to make sure $H is never greater than 1.

    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER4
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 200
    sparkles 0
    param1 4
    param2 2
    seq_duration 2
    php_program $s_ratio=$s/$maxStrand; $p_ration=$p/$maxPixel; $H1=sin($s_ratio) * cos($p_ratio); $H = $H1 * $frame; if($H>1) $H=$H- intval($H); $S=$V=1; $rgb=HSV_TO_RGB ($H, $S, $V);
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]








    Example #5


    Code:
    $s_ratio=$frame*$s/$maxStrand; $p_ration=$frame*$p/$maxPixel;
    $H1=sin($s_ratio) * sin($s_ratio) +  cos($p_ratio)* cos($p_ratio);
    $H = $H1 * $p/$maxPixel;
    if($H>1) $H=$H- intval($H);
    $S=$V=1;
    $rgb=HSV_TO_RGB ($H, $S, $V);

    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER5
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 200
    sparkles 0
    param1 4
    param2 2
    seq_duration 2
    php_program $s_ratio=$frame*$s/$maxStrand; $p_ration=$frame*$p/$maxPixel; $H1=sin($s_ratio) * sin($s_ratio) + cos($p_ratio)* cos($p_ratio); $H = $H1 * $p/$maxPixel; if($H>1) $H=$H- intval($H); $S=$V=1; $rgb=HSV_TO_RGB ($H, $S, $V);
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]






    Example #6




    Code:
    $rgb=hexdec("#FFFFFF");
    $H=$S=$V=0;
    $p_half = $maxPixel/2; // find half way point going down tree
    $p2=$p*2;  // double speed of pixel movement
    if($p2<$p_half) $p_working = $p2;
    else $p_working = ($p*2-$p_half);
    if($s%2==1)
    {
       $H=$p_working/$maxPixel;
       $H = $H*$frame/$maxFrame;
       $S=$V=1;
    }
    else
    {
       $H=1-$p_working/$maxPixel;
       $H = $H*$frame/$maxFrame;
       $S=$V=1;
    }
    $rgb=HSV_TO_RGB ($H, $S, $V);
    return $rgb;    



    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER6
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 200
    sparkles 0
    param1 4
    param2 2
    seq_duration 2
    php_program $rgb=hexdec(\"#FFFFFF\"); $H=$S=$V=0; $p_half = $maxPixel/2; // find half way point going down tree $p2=$p*2; // double speed of pixel movement if($p2<$p_half) $p_working = $p2; else $p_working = ($p*2-$p_half); if($s%2==1) { $H=$p_working/$maxPixel; $H = $H*$frame/$maxFrame; $S=$V=1; } else { $H=1-$p_working/$maxPixel; $H = $H*$frame/$maxFrame; $S=$V=1; } $rgb=HSV_TO_RGB ($H, $S, $V); return $rgb;
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]








    Example #7


    Code:
    # function user_defined($frame,$s,$p,$maxFrame,$maxStrand,$maxPixel,$param1,$param2,$start_color,$end_color) 
    $rgb=hexdec("#FFFFFF");
    $H=$S=$V=1;
    $slice=$param1; // use first parameter passed in from user
    $slice_size=$maxPixel/$slice;
    $H0 = (($frame% $slice) * $slice_size);
    $H=($p+$H0)/$maxPixel;
    if($H>1) $H = $H-intval($H);
    
    
    $rgb=HSV_TO_RGB ($H, $S, $V);
    //echo "<pre>f,s,p=$frame,$s,$p. slice_size=$slice_size. H0=$H0, H=$H</pre>";
    //echo "<pre> maxP=$maxPixel. H,S,V=$H,$S,$V, rgb=$rgb</pre>";
    
    
    return $rgb;  


    Here you see we are using the passed in parameter from the user form, $param1. You have 4 variables you can use:
    $param1, $param2, $start_color, $end_color.


    Also, see how to debug your code by outputting a echo "<pre>stuff to see</pre>\n"; These debug statements are currently commented out.


    array_to_save
    username f
    user_target ZZ_ZZ
    effect_class user_defined
    effect_name USER7
    window_degrees 180
    start_color #FFFFFF
    end_color #FFFFFF
    frame_delay 200
    sparkles 0
    param1 3
    param2 2
    seq_duration 2
    php_program 1) $H = $H-intval($H); $rgb=HSV_TO_RGB ($H, $S, $V); //echo \"
    f,s,p=$frame,$s,$p. slice_size=$slice_size. H0=$H0, H=$H
    \"; //echo \"
    maxP=$maxPixel. H,S,V=$H,$S,$V, rgb=$rgb
    \"; return $rgb; ># function user_defined($frame,$s,$p,$maxFrame,$maxStrand,$maxPixel,$param1,$param2,$start_color,$end_color) $rgb=hexdec(\"#FFFFFF\"); $H=$S=$V=1; $slice=$param1; // use first parameter passed in from user $slice_size=$maxPixel/$slice; $H0 = (($frame% $slice) * $slice_size); $H=($p+$H0)/$maxPixel; if($H>1) $H = $H-intval($H); $rgb=HSV_TO_RGB ($H, $S, $V); //echo \"
    f,s,p=$frame,$s,$p. slice_size=$slice_size. H0=$H0, H=$H
    \"; //echo \"
    maxP=$maxPixel. H,S,V=$H,$S,$V, rgb=$rgb
    \"; return $rgb;
    submit Submit Form to create your effect
    OBJECT_NAME user_defined


    [​IMG]




    Well, have fun creating your own animation effects. If you find a cool one, please post it. I will add it into the effects library.


    thanks


    sean
     

Share This Page