Random color for each particle in javascript particle system

TY BOWMAN's icon

I have this particle system sample by Federico Foderaro, https://www.dropbox.com/s/e1k4oep7p9081s2/js_jitter_02.zip?dl=0 in which all the particles are gray. I need a random color for each particle much like in this sample video of mine, https://www.youtube.com/watch?v=fPDWwVnXFY4 . All of this was done in processing and P5js so it's way different. I want to achieve the same effects in Max using javascript if possible because I have all this code. Below is the patch and the js file contents. I was able to get random colors but all the particles change to the same color on every frame. I want each particle to be encapsulated as its own object with unique properties different from all the other particles. How can I set the color property of the particle using something like this.gl_color = [Math.random()*2.-1, Math.random()*2.-1, Math.random()*2.-1, 0.7];? (btw, I did contact Federico, no response)

Max Patch
Copy patch and select New From Clipboard in Max.
autowatch = 1;

var myWindow = new JitterObject("jit.window", "js");
myWindow.floating = 1;
myWindow.size = [200, 200];
myWindow.fsaa = 1;
myWindow.pos = [1000, 100];
myWindow.depthbuffer = 0;

var myRender = new JitterObject("jit.gl.render", "js");
myRender.erase_color = [1, 1, 1, 0.7];

var mySketch = new JitterObject("jit.gl.sketch", "js");
mySketch.blend_enable = 1;

var Vector = {
    x: 0.0,
    y: 0.0,
    z: 0.0,

    add: function(Vector) {
        this.x += Vector.x;
        this.y += Vector.y;
        this.z += Vector.z;
    }  
};


function Particle() {
    this.location = Object.create(Vector);
    this.velocity = Object.create(Vector);
    this.acceleration = Object.create(Vector);
    this.location.y = 0.7;
    this.acceleration.y = -0.0008;
    this.velocity.x = (Math.random()*2 - 1) / 70.0;
    this.velocity.y = (Math.random()*2 - 1) / 70.0;
    this.lifespan = 255;
    mySketch.gl_color = [Math.random()*2.-1, Math.random()*2.-1, Math.random()*2.-1, 0.7];
}; 

Particle.prototype.update = function() {
    this.velocity.add(this.acceleration);
    this.location.add(this.velocity);
    this.lifespan -= 2;
};

Particle.prototype.display = function() {
    mySketch.moveto(this.location.x, this.location.y, this.location.z);
    var alpha = this.lifespan / 255.0;
    //mySketch.glcolor(0.3, 0.3, 0.3, alpha);
    //mySketch.gl_color = [Math.random()*2.-1, Math.random()*2.-1, Math.random()*2.-1, 0.7];
    mySketch.circle(0.02);
//    mySketch.glcolor(0, 0, 0, alpha);
    mySketch.gllinewidth(2);
    mySketch.framecircle(0.02);
};

Particle.prototype.run = function() {
    this.update();
    this.display();
}

Particle.prototype.isDead = function() {
    if(this.lifespan < 0.0) {
        return true;
    } else {
        return false;
    }
};

// Particles Array
var pArray = [];
var total = 100;

function setup() {
    for(var i = 0; i < total; i++) {
        pArray.push(new Particle());
    }
}

setup();


function draw() {
    
    pArray.push(new Particle());
    pArray.push(new Particle());
    pArray.push(new Particle());

    for(var i = pArray.length-1; i >= 0; i--) {
        pArray[i].run();
        if(pArray[i].isDead()) {
            pArray.splice(i, 1);
        }
    }

    myRender.erase();
    myRender.drawswap();

    mySketch.reset();
}
Rob Ramirez's icon

to change the current active color of a jit.gl.sketch draw command, you have to use the "glcolor" message (function in JS), not the color or gl_color attribute:

mySketch.glcolor([Math.random()*2.-1, Math.random()*2.-1, Math.random()*2.-1, 0.7]);

John Daniel's icon

Siiince we are on this subject... : ]

Here are the particle engine guts i am using in a much larger, much more convoluted patch.

I have been trying to figure how to recolor the particles, but when I change the color on the left jit.gl.mesh it changes the color of ALL of the particles, including the ones "in the air".

How would i go about coloring just the "next particles out the door" but not the ones "in the air"

Additionally - I found that sending a image into the "color_array" input on the jit.gl.mesh gives me the a nice multicolored effect on the particles, but I don't understand what is exactly going on or how to control it. If I send a single color image it seems to "lean" to the red side of things too...

I can find precious little documentation on jit.gl.mesh color_array so am not sure if I am barking up the wrong tree. (or even in the right forest]

Thanks for any insights gang!

cheers!

jd

Max Patch
Copy patch and select New From Clipboard in Max.

John Daniel's icon

Just in case copy compressed doesn't get the embedded .png file in the patch... Does it get embedded fpics?

thanks!

jd

Particle color question.maxpat
mime/type 11.34 KB