Math Clamp node - Request - solved

Smart people ~ Great Scripts
User avatar
Draise
Captain
Posts: 3200
Joined: 21 Sep 2009, 19:33
Type the number ten into the box: 0
Location: Bogota, Colombia

Math Clamp node - Request - solved

Post by Draise »

How can I get a Min/Max clamp for any form of math, integrals, nominal, etc?

I know there is Component clamp for shaders, but I can't seem to use it for simple chunks of numbers.
Last edited by Draise on 10 Sep 2012, 03:41, edited 1 time in total.
User avatar
Draise
Captain
Posts: 3200
Joined: 21 Sep 2009, 19:33
Type the number ten into the box: 0
Location: Bogota, Colombia

Re: Math Clamp node - Request

Post by Draise »

Code: Select all


    // this one-liner is good to know:
    // Math.max(minValue, Math.min(maxValue, valueToClamp));
     
    // wrapped in a function it looks like this:
    const MIN:Number = 0;
    const MAX:Number = 100;
     
    function clamp(val:Number, min:Number = MIN, max:Number = MAX){
        return Math.max(min, Math.min(max, val))
    }
     
    for (var i:int = 0; i<1000; i++){
        var val:Number = Math.random()*1000 - 500;
        // clamp the above random value between -100 and +100
        trace(clamp(val, -100, 100));
    }

Found this javascript code, but I'm not sure how to implement it into a trueSpace script node.

Or this one:

Code: Select all

/**
 * Returns a number whose value is limited to the given range.
 *
 * Example: limit the output of this computation to between 0 and 255
 * (x * 255).clamp(0, 255)
 *
 * @param {Number} min The lower boundary of the output range
 * @param {Number} max The upper boundary of the output range
 * @returns A number in the range [min, max]
 * @type Number
 */
Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};
User avatar
Draise
Captain
Posts: 3200
Joined: 21 Sep 2009, 19:33
Type the number ten into the box: 0
Location: Bogota, Colombia

Re: Math Clamp node - Request

Post by Draise »

Ah, don't worry. Found a node in the Smart Sphere that has a Min/Max clamp plus a scale that deals with what I needed. Perfect. Exactly what I needed!

It is the Transform node located in the Smart Sphere.

I have attached it, in-case it will come in handy for others.
Attachments
Min_Max.zip
(7.91 KiB) Downloaded 10 times

Return to “Scripts and SDK”