SmoothNoiseLoop Actionscript

Table of contents | Previous document | Download SmoothNoiseLoop.as | SWF!T Homepage

RCSfile: SmoothNoiseLoop.as,v  Revision: 1.1  Date: 2003/04/20 13:13:18  

Copyright 2003 Orgdot AS. All Rights Reserved. http://dev.swfit.com
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


Loop script for simple smooth noise generator. Quadratic interpolation in two dimensions.
author      Olaf Havnes
version     Revision: 1.1  Date: 2003/04/20 13:13:18  
since       SWFIT1.0


Generate some noise to start with. We need to seed the methods below,

so that the curve does not start out flat.

if (y0 eq "")
{

Store for other clips.

   _level0.NOISE_RAND_SPACE = RAND_SPACE;
   _level0.NOISE_NUM_POINTS = ARR_LEN;

   sum_y = 0;
   i = 0;
   while (i < ARR_LEN)
   {
       y = random (RAND_SPACE);
       set ("y" add i, y);

Sum needed to calculate smooth derivates along the new curve

       sum_y = y - sum_y;

       i++;
   }

Coeffecients for the last point in the curve. Treat separately, since the curve loops back.

   c = y;
   b = 2 * (sum_y - y);
   a = y0 - b - c;

Store

   i--;
   set ("_c" add i, c);
   set ("_b" add i, b);
   set ("_a" add i, a);

Start from the beginning again.

   i = 0;
   while (i < ARR_LEN - 1)
   {
       c_ = c;
       c = eval ("y" add i);
       b = 2 * (c - c_) - b;
       a = eval ("y" add (i+1)) - b - c;

Store

       set ("_c" add i, c);
       set ("_b" add i, b);
       set ("_a" add i, a);



       i++;
   }

Make sure we do the next block of code on this first run.

   loop_life_counter = LOOP_LIFE;
}


This here big block should be run very seldom ...

loop_life_counter++;
if (loop_life_counter >= LOOP_LIFE)
{
   loop_life_counter = 0;

   sum_y = 0;
   i = 0;
   while (i < ARR_LEN)
   {
       y = random (RAND_SPACE);
       set ("y" add i, y);

Sum needed to calculate smooth derivates along the new curve

       sum_y = y - sum_y;
       i++;
   }

Coeffecients for the last point in the curve. Treat separately, since the curve loops back.

   c = y;
   b = 2 * (sum_y - y);
   a = y0 - b - c;

Shift the old values and store the new ones

   i--;
   set ("c" add i, eval ("_c" add i));
   set ("b" add i, eval ("_b" add i));
   set ("a" add i, eval ("_a" add i));

   set ("_c" add i, c);
   set ("_b" add i, b);
   set ("_a" add i, a);

Start from the beginning again.

   i = 0;
   while (i < ARR_LEN - 1)
   {
       c_ = c;
       c = eval ("y" add i);
       b = 2 * (c - c_) - b;
       a = eval ("y" add (i+1)) - b - c;

Shift the old values and store the new ones

       set ("c" add i, eval ("_c" add i));
       set ("b" add i, eval ("_b" add i));
       set ("a" add i, eval ("_a" add i));

       set ("_c" add i, c);
       set ("_b" add i, b);
       set ("_a" add i, a);

       i++;
   }

Aye, and here comes the rub indeed - we need to interpolate across these

coeffecients to change the curve smoothly over time. Which means we need

coeffecients for the coeffecients ... And this we do with cubic interpolation.


   i = 0;
   while (i < ARR_LEN)
   {

First we do the 'a coeffecient'.

       start_slope = eval ("slope_a" add i);
       coef_0 = eval ("a" add i);
       coef_1 = eval ("_a" add i);
       end_slope = coef_1 - coef_0;

Compute and store the coeffecients for the 'a coeffecient'.

       set ("ad" add i, coef_0);
       set ("ac" add i, start_slope);
       set ("ab" add i, 3 * coef_1 - 3 * coef_0 - end_slope - 2 * start_slope);
       set ("aa" add i, end_slope + start_slope - 2 * coef_1 + 2 * coef_0);

Store the end slope value for the 'a coeffecient'.

       set ("slope_a" add i, end_slope);


Repeat for the 'b coeffecient'.

       start_slope = eval ("slope_b" add i);
       coef_0 = eval ("b" add i);
       coef_1 = eval ("_b" add i);
       end_slope = coef_1 - coef_0;

Compute and store the coeffecients for the 'b coeffecient'.

       set ("bd" add i, coef_0);
       set ("bc" add i, start_slope);
       set ("bb" add i, 3 * coef_1 - 3 * coef_0 - end_slope - 2 * start_slope);
       set ("ba" add i, end_slope + start_slope - 2 * coef_1 + 2 * coef_0);

Store the end slope value for the 'b coeffecient'.

       set ("slope_b" add i, end_slope);


Repeat for the 'c coeffecient'.

       start_slope = eval ("slope_c" add i);
       coef_0 = eval ("c" add i);
       coef_1 = eval ("_c" add i);
       end_slope = coef_1 - coef_0;

Compute and store the coeffecients for the 'c coeffecient'.

       set ("cd" add i, coef_0);
       set ("cc" add i, start_slope);
       set ("cb" add i, 3 * coef_1 - 3 * coef_0 - end_slope - 2 * start_slope);
       set ("ca" add i, end_slope + start_slope - 2 * coef_1 + 2 * coef_0);

Store the end slope value for the 'c coeffecient'.

       set ("slope_c" add i, end_slope);



       i++;
   }
}


Store a full set of coeffecients in _level0. This method is run every frame.


cx = loop_life_counter / LOOP_LIFE;
cx2 = cx * cx;
cx3 = cx2 * cx;

i = 0;
while (i < ARR_LEN)
{
   set
   (
       "/:loop_noise_a_" add i,
       eval ("aa" add i) * cx3 + eval ("ab" add i) * cx2 + eval ("ac" add i) * cx + eval ("ad" add i)
   );
   set
   (
       "/:loop_noise_b_" add i,
       eval ("ba" add i) * cx3 + eval ("bb" add i) * cx2 + eval ("bc" add i) * cx + eval ("bd" add i)
   );
   set
   (
       "/:loop_noise_c_" add i,
       eval ("ca" add i) * cx3 + eval ("cb" add i) * cx2 + eval ("cc" add i) * cx + eval ("cd" add i)
   );
   i++;
}