Feedback

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

Wednesday 18 December 2013

LScript - Image_Stars


LScript (Layout) generate stars for space background image maps.

Compatible with Newtek LightWave 9.6 and above.

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

/*  
    LScript Image Filter - Stars
    Image_Stars.ls

*/

@version 2.5
@warnings
@script image
@name *Stars

    // Title
    sTitle = "*Stars";

    // Version
    sVersion = "v1.0";

    // Variable
    Global_nOpacity = 1.0;

    Stars_vColor = <255,255,255>;
    Stars_iAmount = 200; 
    Stars_nOpacity = 0.2;
    Stars_nMinSize = 1.0;
    Stars_nMaxSize = 5.0;

create
{
    setdesc(sTitle);
}

process: ifo
{

    if(runningUnder() != SCREAMERNET) moninit(Stars_iAmount);

    vColor = Stars_vColor * (1/255);
    for(a = 1;a <= Stars_iAmount;++a)
      {
      v = ;
      circleenhancedIFO(ifo,
                        v, // Vector
                        max(Stars_nMinSize,randu() * Stars_nMaxSize), // Size
                        0.0,// fade
                        vColor.x,vColor.y,vColor.z, // Color
                        Stars_nOpacity * Global_nOpacity); // Opacity    

      if(runningUnder() != SCREAMERNET) if(monstep()) return;

      }
}

// IFO

circleenhancedIFO: ifo,v,s,f,r,g,b,o // vector,size,fade,red,green,blue,opacity
{
    // Size <= 1
    if(s <= 1)
      {
      o *= s;
      if (v.x >= 1 && v.x <= ifo.width && v.y >= 1 && v.y <= ifo.height)
        { 
        if(o >= 1.0)
          {
          ifo.red[v.x,v.y] = r;
          ifo.green[v.x,v.y] = g;
          ifo.blue[v.x,v.y] = b;
          }
        else if(o > 0.0)
          {
          ifo.red[v.x,v.y] = (ifo.red[v.x,v.y] * (1-o)) + (r * o);
          ifo.green[v.x,v.y] = (ifo.green[v.x,v.y] * (1-o)) + (g * o);
          ifo.blue[v.x,v.y] = (ifo.blue[v.x,v.y] * (1-o)) + (b * o);
          }
        }
      return; 
      }
    // Half Size
    s *= 0.5; 
    // Grid
    x0 = floor(v.x - s - 1);
    y0 = floor(v.y - s - 1);
    x1 = floor(v.x + s + 1);
    y1 = floor(v.y + s + 1);
    // Fade
    _f = s * f;
    // Draw
    for(y = y0;y <= y1;++y)
      {
      for(x = x0;x <= x1;++x)
        {
        // Wrap
        x2 = x;
        if(x2 < 1){x2 = ifo.width - abs(mod(x2,ifo.width));}
        if(x2 > ifo.width){x2 = mod(x2,ifo.width) + 1;}
        y2 = y;
        if(y2 < 1){y2 = ifo.height - abs(mod(y2,ifo.height));}
        if(y2 > ifo.height){y2 = mod(y2,ifo.height) + 1;}
        // Opacity          
        _o = 0.0;
        d = distance2D(v,);
        f_01 = maprange01(_f,s,d);     
        if(f_01 <= 0.0){_o = o;} // Inside
        else if(f_01 <= 1.0)
          {
          _o = o * (1 - f_01);
   }      
        if(_o >= 1.0)
          {
          ifo.red[x2,y2] = r;
          ifo.green[x2,y2] = g;
          ifo.blue[x2,y2] = b;
          }
        else if (_o > 0.0)
          {
          ifo.red[x2,y2] = (ifo.red[x2,y2] * (1-_o)) + (r * _o);
          ifo.green[x2,y2] = (ifo.green[x2,y2] * (1-_o)) + (g * _o);
          ifo.blue[x2,y2] = (ifo.blue[x2,y2] * (1-_o)) + (b * _o);
          }
        }
      }
}

// MAP RANGE

maprange01: n1,n2,i
{    
    if(n2-n1 == 0.0){return(0.0);}
  else
    {return((1/(n2-n1)) * (i-n1));}
}

// VECTOR 2D

clip2D: v,x0,y0,x1,y1 // b
{
    if((v.x >= x0) && (v.x <= x1) && (v.y >= y0) && (v.y <= y1)){return(false);}
  else
    return(true);
}

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

linedistance2D: v1,v2,v3 // n
{
    // Vector v1 from line v2 to v3
    nSqrLineMagnitude = sqrlinemagnitude2D(v2,v3);
    if(nSqrLineMagnitude < 0.00000000000001)
      {
      return(-1.0);
      }
    u = ((v1.x - v2.x) * (v3.x - v2.x) + (v1.y - v2.y) * (v3.y - v2.y)) / nSqrLineMagnitude;
    if(u < 0.0000001 or u > 1)
      {
      x = distance2D(v1,v2);
      y = distance2D(v1,v3);
      return(min(x,y));
      }
    else
      {
      x = v2.x + u * (v3.x - v2.x);
      y = v2.y + u * (v3.y - v2.y);
      return(distance2D(v1,));
      }
}

sqrlinemagnitude2D: v1,v2 // n
{
    return((v2.x - v1.x) * (v2.x - v1.x) + (v2.y - v1.y) * (v2.y - v1.y));
}

//

load: what,io
{
    if(what == SCENEMODE)
    {
        if(io.read().asStr() == sTitle + " " + sVersion)
            {
            // Global
            Global_nOpacity = io.read().asNum();

            // Stars
            Stars_vColor = io.read().asVec();
            Stars_iAmount = io.read().asInt();
            Stars_nOpacity = io.read().asNum();
            Stars_nMinSize = io.read().asNum();
            Stars_nMaxSize = io.read().asNum();
            }
    }
}

save: what,io
{
    if(what == SCENEMODE)
    {
        // Header
        io.writeln(sTitle + " " + sVersion);

        // Global
        io.writeln(Global_nOpacity);

        // Stars
        io.writeln(Stars_vColor);
        io.writeln(Stars_iAmount); 
        io.writeln(Stars_nOpacity);
        io.writeln(Stars_nMinSize);
        io.writeln(Stars_nMaxSize);
    }
}

options
{
    reqbegin(sTitle + " " + sVersion);

    // Control

    // Global
    ctrl_g0 = ctlnumber("Global Opacity",Global_nOpacity);

    ctlsep();
    ctrl_stars0 = ctlcolor("Color",Stars_vColor);
    ctrl_stars1 = ctlinteger("Amount",Stars_iAmount);
    ctrl_stars2 = ctlnumber("Opacity",Stars_nOpacity);
    ctrl_stars3 = ctlnumber("Min Size",Stars_nMinSize);
    ctrl_stars4 = ctlnumber("Max Size",Stars_nMaxSize);

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

    return if !reqpost();

    Global_nOpacity = getvalue(ctrl_g0);

    Stars_vColor = getvalue(ctrl_stars0);
    Stars_iAmount = getvalue(ctrl_stars1);
    Stars_nOpacity = getvalue(ctrl_stars2); 
    Stars_nMinSize = getvalue(ctrl_stars3); 
    Stars_nMaxSize = getvalue(ctrl_stars4); 

    reqend();
}


All scripts available at my Google Drive at
https://drive.google.com/open?id=1cR_q2GVUAJHumic1-A3eXV16acQnVTWs

No comments:

Post a Comment