Feedback

Please leave feedback and comments. I am always interested to hear how people get on using these LScripts!

Wednesday, 24 September 2014

LScript - Channel_VelocityNoise

LScript (Layout) to add noise to a channel based on the velocity of an item.

Compatible with Newtek LightWave 9.6 and above.

// LScript Channel Filter - www.StephenCulley.co.uk
//
// web   address: http://www.stephenculley.co.uk
// email address: email@stephenculley.co.uk

/* 
    LScript Channel Filter - Velocity Noise

    Channel_VelocityNoise.ls

*/

@version 2.2
@warnings
@script channel
@name *Velocity Noise

    // Title
    sTitle = "*Velocity Noise";

    // Version
    sVersion = "v1.0";

    // Item
    Item = nil;
    ItemName = "nil";

    iSeed = 1; // Seed
    iType = 1; // Type noise values.. -1.0..1.0 0.0..1.0 -1.0..0.0
    nScale0 = 0.0; // Scale
    nScale1 = 1.0; // Scale
    nSpeed0 = 100.0; // Speed
    nSpeed1 = 10.0; // Speed
    vOffset; // Offset

    // Controls
    ctrl_c0,ctrl_c1,ctrl_c2,ctrl_c3,ctrl_c4,ctrl_c5,ctrl_c6;

create: channel
{
    setdesc(sTitle);

    vOffset = ;
}

destroy
{
    // take care of final clean-up activities here
}

process: ca, frame, time
{
    nValue = ca.get(time);

    if(ItemName != "nil") {Item = Mesh(ItemName); ItemName = "nil";}
    if(Item)
      {
      // Velocity
      nVelocity = distance3D(Item.getWorldPosition(time - (1 / Scene().fps)),Item.getWorldPosition(time + (1 / Scene().fps)));

      // Scale
      nScale = linear1D(nScale0,nScale1,nVelocity);

      // Speed
      nSpeed = linear1D(nSpeed0,nSpeed1,nVelocity);

      // Add Noise
      nValue += (cosinenoise3D(<0.0,0.0,time * nSpeed> + vOffset) * nScale);

      // ca    
      ca.set(nValue);
      }
}

// CLIP

clip: min,max,n
{
    if(n < min) n = min;
    if(n > max) n = max;
    return(n);
}

// INTERPOLATION

linear1D: n1,n2,i // i = interpolation point (0-1)
{
    return(n1 * (1 - i) + n2 * i);     
}

cosine1D: n1,n2,i // i = interpolation point (0-1)
{
    i2 = (1 - cos(i * 3.1415926535)) * 0.5;
    return(n1 * (1 - i2) + n2 * i2);     
}

// NOISE

_noiseseed = 1;
_noiseresolution = 256;
_noise = nil;
_noiseoffset = nil;
_noiseperm = nil;

noiseinit
{  
    randomseed(_noiseseed); // Random Seed

    if(iType == 3)  // -1.0..0.0
      {      
      for(iN = 1; iN <= _noiseresolution; iN++)
        {
        _noise[iN] = random() - 1.0;
        _noiseperm[iN] = wrap(1,_noiseresolution,floor(random() * _noiseresolution));
        }
      }
    else if(iType == 2)  // 0.0..1.0
      {      
      for(iN = 1; iN <= _noiseresolution; iN++)
        {
        _noise[iN] = random();
        _noiseperm[iN] = wrap(1,_noiseresolution,floor(random() * _noiseresolution));
        }
      }
    else  // -1.0..1.0
      {      
      for(iN = 1; iN <= _noiseresolution; iN++)
        {
        _noise[iN] = (random() * 2) - 1.0;
        _noiseperm[iN] = wrap(1,_noiseresolution,floor(random() * _noiseresolution));
        }
      }

    for(iO = 1; iO <= 64; iO++)
      {
      _noiseoffset[iO] = ;
      }
}

noiseseed: seed
{
    _noiseseed = seed;
    noiseinit(); // Init
}

noiseresolution: resolution
{
    _noiseresolution = resolution;
    noiseinit(); // Init 
}

noiseperm1D: n // i
{
    if(_noiseperm == nil){noiseinit();} // Init
    return(_noiseperm[wrap(1,_noiseresolution,floor(n))]);
}

noiseperm2D: v // n
{
    return(noiseperm1D(v.y + noiseperm1D(v.x)));
}

noiseperm3D: v // n
{
    return(noiseperm1D(v.z + noiseperm2D(v)));
}

noise1D: n // n
{
    if(_noise == nil){noiseinit();} // Init
    return(_noise[noiseperm1D(n)]);
}

noise2D: v // n
{
    if(_noise == nil){noiseinit();} // Init
    return(_noise[noiseperm2D(v)]);
}

noise3D: v // n
{
    if(_noise == nil){noiseinit();} // Init
    return(_noise[noiseperm3D(v)]);
}

cosinenoise3D: v // n
{
    v = wrap(1,_noiseresolution,v); 
    vFrac = frac(v);
    vInt = v - vFrac;
    nA1 = noise3D();
    nA2 = noise3D();
    nA3 = noise3D();
    nA4 = noise3D();
    nA = cosine1D(cosine1D(nA1,nA2,vFrac.x),cosine1D(nA3,nA4,vFrac.x),vFrac.z);
    nB1 = noise3D();
    nB2 = noise3D();
    nB3 = noise3D();
    nB4 = noise3D();
    nB = cosine1D(cosine1D(nB1,nB2,vFrac.x),cosine1D(nB3,nB4,vFrac.x),vFrac.z);
    return(cosine1D(nA,nB,vFrac.y));
}

// RANDOM

_randomseed = 0; // n Seed

randomseed: seed
{
    _randomseed = seed;
}

random
{
    n = (_randomseed * 214013 + 2531011) % 2^^24;
    _randomseed = n;
    n /= 2^^24; // 0..1
    return(n);
}

// WRAP

wrap: min,max,n
{
    n = n - floor((n - min) / (max - min)) * (max - min);
    if(n < 0) n = n + max - min; // error check
    return(n);
}

// VECTOR 3D

distance3D: v1, v2 // n
{
    // Vector
    return(sqrt(((v2.x - v1.x) * (v2.x - v1.x)) +
                ((v2.y - v1.y) * (v2.y - v1.y)) + 
                ((v2.z - v1.z) * (v2.z - v1.z))));
}

load: what,io
{
    if(what == SCENEMODE)   // processing an ASCII scene file
    {
        ItemName = io.read().asStr();
        iSeed = io.read().asInt();
        iType = io.read().asInt();
        nScale0 = io.read().asNum();
        nScale1 = io.read().asNum();
        nSpeed0 = io.read().asNum();
        nSpeed1 = io.read().asNum();

        noiseseed(iSeed); // Noise Seed 
    }
}

save: what,io
{
    if(what == SCENEMODE)
    {
        if(Item != nil)
            {
            io.writeln(string(Item.name));
            }
        else
            {
            io.writeln("nil");
            } 

        io.writeln(iSeed);
        io.writeln(iType);
        io.writeln(nScale0);
        io.writeln(nScale1);
        io.writeln(nSpeed0);
        io.writeln(nSpeed1);
    }
}

options
{

    if(reqisopen())
      {
      reqend();
      return;
      }

    reqbegin(sTitle + " " + sVersion);

    ctrl_c0 = ctlallitems("Item",Item);
    ctrl_c1 = ctlinteger("Seed",iSeed);
    ctrl_c2 = ctlchoice("Type",iType,@"+/-","+","-"@);
    ctrl_c3 = ctlpercent("Scale @ 0m/s",nScale0);
    ctrl_c4 = ctlpercent("Scale @ 1m/s",nScale1);
    ctrl_c5 = ctlpercent("Speed @ 0m/s",nSpeed0 * 0.01);
    ctrl_c6 = ctlpercent("Speed @ 1m/s",nSpeed1 * 0.01);

    // Developer
    ctlsep();
    ctrl_dev0 = ctltext("","developer: Stephen Culley","http://www.stephenculley.co.uk");

    // Refresh
    ctlrefresh(ctrl_c0,"refresh_c0"); // Item
    ctlrefresh(ctrl_c1,"refresh_c1"); // Seed
    ctlrefresh(ctrl_c2,"refresh_c2"); // Type
    ctlrefresh(ctrl_c3,"refresh_c3"); // Scale 0
    ctlrefresh(ctrl_c4,"refresh_c4"); // Scale 1
    ctlrefresh(ctrl_c5,"refresh_c5"); // Speed 0
    ctlrefresh(ctrl_c6,"refresh_c6"); // Speed 1

    reqopen();
}

refresh_c0:value // Item
{
    Item = value;
}

refresh_c1:value // Seed
{
    iSeed = clip(1,9999999999,value);
    setvalue(ctrl_c1,iSeed);
    noiseseed(iSeed); // Noise Seed
}

refresh_c2:value // Type
{
    iType = value;
    noiseseed(iSeed); // Noise Seed
}

refresh_c3:value // Scale 0
{
    nScale0 = value;
}

refresh_c4:value // Scale 1
{
    nScale1 = value;
}

refresh_c5:value // Speed 0
{
    nSpeed0 = value * 100.0;
}

refresh_c6:value // Speed 1
{
    nSpeed1 = value * 100.0;
}
All scripts available at my Google Drive at
https://drive.google.com/open?id=1cR_q2GVUAJHumic1-A3eXV16acQnVTWs

No comments:

Post a comment