Author Topic: Nutcracker, new effect: user_defined. Do you want to write your own animations?  (Read 1958 times)

Offline smeighan

  • Moderator
  • Sr. Member
  • *****
  • Posts: 2285
    • Nutcracker RGB Sequence Builder
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;
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,$param1,$param2,$start_color,$end_color);

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

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








Example #2

$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





Example #3

$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






Example #4

$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






Example #5

$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





Example #6


$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






Example #7

# 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; 

 
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 "
stuff to see
\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




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
« Last Edit: April 03, 2012, by smeighan »
Sean
Littleton, CO
Latest releases You are not allowed to view links. Register or Login
xLights/Nutcracker Forum You are not allowed to view links. Register or Login
Fbook You are not allowed to view links. Register or Login

Offline rwave

  • Sr. Member
  • ****
  • Posts: 555
Sean,

VERY impressive!   <res.

I have been following your development and hope at some future time I will be able to use your tool.  Your efforts are very much appreciated.

Richard (from SoCal)   :)

Offline mms

  • Sr. Member
  • ****
  • Posts: 421
  • 80124
    • Like us on Facebook for special content and year-round updates.
I think this is great!  Awesome as usual.  (Now, if only I knew how to write code!)
Like us on Facebook for special content and year-round updates: You are not allowed to view links. Register or Login

Voice-over Inquiries:  You are not allowed to view links. Register or Login

Offline caretaker

  • Sr. Member
  • ****
  • Posts: 1770
Everyone knows how to write code, the trick part is making it do something useful! Sean, as other before me have said thanks for all your hard work on this project.  <res.
Jeff Squires
You are not allowed to view links. Register or Login

Offline smeighan

  • Moderator
  • Sr. Member
  • *****
  • Posts: 2285
    • Nutcracker RGB Sequence Builder
You are not allowed to view links. Register or Login
Everyone knows how to write code, the trick part is making it do something useful! Sean, as other before me have said thanks for all your hard work on this project.  <res.

I am going to make a tutorial on some of the things that help in making the animations. I plan on giving a second class at the Academy.
First class is how to use the Nutcracker. Audience: everyone, no technical knowledge needed.
2nd class: How to write animations. Audience: some programming needed. In this second class i will discuss how to get animations, HSV vs RGB, speed of animations, how to optimize timing .etc.


For example, this code


$H = rand(1,100)/100;
$S=rand(1,100)/100;
$V=rand(1,100)/100;
if(($p+$maxFrame-$frame/2)%5==0) { $H=$p/$maxPixel; $S=$V=1; }
$rgb=HSV_TO_RGB ($H, $S, $V);




Notice the rings falling down.

Let me add some comments

$H = rand(1,100)/100; // create a random number between 0.0 and 1.0 and assign it as the Hue
$S=rand(1,100)/100;    // create a random number between 0.0 and 1.0 and assign it as the Saturation. 0=WHITE, 1=Full saturation
$V=rand(1,100)/100;    // create a random number between 0.0 and 1.0 and assign it as the Value.  Brightness value 0=BLACK, 1=Full birghtness
if(($p+$maxFrame-$frame/2)%5==0) { $H=$p/$maxPixel; $S=$V=1; }

// $p pixel number 1 is top of tree, we will decrease the value which affects the modulo operation. %5==0 means every 5th pixel we will
// override the random hue in H$ and assign it a value based on how far we are down the tree. $p/$maxPixel gives us .015 at the
// top of the tree and 1.0 at the bottom.

$rgb=HSV_TO_RGB ($H, $S, $V);

Sean
Littleton, CO
Latest releases You are not allowed to view links. Register or Login
xLights/Nutcracker Forum You are not allowed to view links. Register or Login
Fbook You are not allowed to view links. Register or Login

Offline rwave

  • Sr. Member
  • ****
  • Posts: 555
Sean,

Quote
$H = rand(1,100)/100; // create a random number between 0.0 and 1.0 and assign it as the Hue
$S=rand(1,100)/100;    // create a random number between 0.0 and 1.0 and assign it as the Saturation. 0=WHITE, 1=Full saturation
$V=rand(1,100)/100;    // create a random number between 0.0 and 1.0 and assign it as the Value.  Brightness value 0=BLACK, 1=Full birghtness
if(($p+$maxFrame-$frame/2)%5==0) { $H=$p/$maxPixel; $S=$V=1; }

// $p pixel number 1 is top of tree, we will decrease the value which affects the modulo operation. %5==0 means every 5th pixel we will
// override the random hue in H$ and assign it a value based on how far we are down the tree. $p/$maxPixel gives us .015 at the
// top of the tree and 1.0 at the bottom.

$rgb=HSV_TO_RGB ($H, $S, $V);



Seems simple enough! ;D  Now, I just need hardware.

BTW, a SSC coop in the next five minutes will work for me!   >.d9

Richard (from SoCal)   :)