Matrix output from js (gl)

Dalmazzo's icon

How can I have the a matrix output from this js objects that has a simple particle system?
In the cod there is a matrix output but it is just a test, not really related to the rest.

autowatch = 1;
outlets = 2;

var myWindow = new JitterObject("jit.window", "js");
myWindow.floating = 0;
myWindow.size = [640, 480];
myWindow.fsaa = 1;
myWindow.pos = [10, 50];
myWindow.depthbuffer = 0;

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

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

var image_out = new JitterObject("jit.gl.asyncread", "js");
image_out.automatic = 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(x, y) {
    this.location = Object.create(Vector);
    this.velocity = Object.create(Vector);
    this.acceleration = Object.create(Vector);
    this.gravity = Object.create(Vector); 
    this.color = Object.create(Vector); 
    this.location.y = y;
    this.location.x = x;
    this.gravity.y = -0.000;
    this.acceleration.y = (Math.random()*2 - 1)/3000.0;
    this.velocity.x = (Math.random()*2 - 1) / 120.0;
    this.velocity.y = (Math.random()*2 - 1) / 200.0;
    this.color.x = Math.random()*0.73;
    this.color.y = Math.random()*0.77;
    this.color.z = 1.0;
    this.lifespan = 255;        
}; 

Particle.prototype.update = function() {
    this.velocity.add(this.acceleration);
    this.velocity.add(this.gravity);
    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(this.color.x, this.color.y, this.color.z, alpha);
    mySketch.circle(this.lifespan * 0.000077);
    if(this.location.y > 0.85 || this.location.y < -0.85){
        this.lifespan = 0.0;
    }
};

Particle.prototype.getPos = function() {
    return this.location;
};

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 maximum = 5000;

function maxParticle(m){
    maximum = m;
}

function createParticle(x, y) {
    pArray.push(new Particle(x, y));
}


function bang() {
    var matrix = new JitterMatrix("noisemat");    // defined in max patch
    for(i=0; i<7; i++) {
        for(j=0; j<12; j++) {
            var val = matrix.getcell(j, i);
            matrix.setcell(j,i,"val",val[0],val[1],val[2],val[3]);
        }
    }
    outlet(1,"jit_matrix",matrix.name);    
    
    for(var i = pArray.length-1; i >= 0; i--) {
        pArray[i].run();
        
        if(pArray[i].isDead()) {
            pArray.splice(i, 1);
        }
        if(pArray.length > maximum){
            pArray.splice(i, 1);
        }
    }
        
    myRender.erase();
    myRender.drawswap();
    mySketch.reset();
}

Rob Ramirez's icon

add the following line to the end of your draw routine:
outlet(0, "jit_matrix", image_out.out_name);

in the future, please provide a working example including max patch.

Dalmazzo's icon

Hello,
Here are both samples. What I don't really get is how to output the particles as a matrix to be seen by jit.pwindow

js_particles.maxpat
Max Patch

js_jitter_02.js
application/x-javascript 2.25 KB

Rob Ramirez's icon

did you try what i suggested above?

Dalmazzo's icon

yes of course, it gives me the error "image_out is not defined" I place it at the end of the bang function, can be that?

Dalmazzo's icon

ah ok! I saw the error. Any suggestion in how to preserve the scale? In jit.pwindow it appears in a quarter part of the screen.
Thanks for the help

Rob Ramirez's icon

unfortunately that's a bug in jit.gl.asyncread in Max 7 that has been fixed in Max 8. Simply set jit.window @fsaa 0 if on Max 7.