text webgl renderer P5JS

excuse me if my question is stupid: i am an absolute beginner with P5JS (though i know java && android)

ten years ago i wrote an applet using P5 1.5.1 it runned perfectly and now it runs perfectly with Safari && osx but (since march 2017) does not run anymore with firefox >52 which does not allowany NPAPI plug in : java

So i decided to re- write it using processing.js it runs, yes; but awfully slow...

Therefore i decided to try with P5JS (first time i use it)

For my project i have to use 3D so: WEBGL

in this project i manage 1000 texts (GPU, offscreen); these texts are sentences, rather long, loaded from an external file (i put the file at the end of this post)

i have used for that the "solution" given by P5JS: createGraphics, create plane, use your graphics as texture...

result???

-- it is more quick (fps) than P5JS

but:

--a) i cannot see anyway to get the textAscent text Descent from my text and have to (vertical) center it approximatively

-- b) textWidth(myText) seems to work if i dont use loadFont()

--c) textWidth(my text) returns false values if i change the textSize(); returns exact values when using textSize(12)

--d) i have a class with my textObjects but if i want to "render" from my class i get strange results; so i have put the "rendering" in draw and it works. Why that ?

--e) setting the alpha value for background in the graphics works in a weird way: works at the beginning then not. Can texture accept alpha value?

below the code (of course you have to add some text for loadStrings()); mine is at the end of this post

                var font;
                var pg;
                var tableauTexte;
                var textesCharges;
                var tailleRass;
                var angle = 0;
                var nbre = 1;


                function preload(){
                  font = loadFont("Arial Unicode.ttf");
                  tableauTexte = new Array();
                  tableauTexte = loadStrings("rassuronsnous.txt");// that is the name for the file you can find at the end of the post

                }

                function setup() {

                   textesCharges = new Array();
                   textSize(24);
                 tailleRass = textWidth("RASSURONS_NOUS");

                 for(var i = 0; i<800; i++){// CREATING TEXToBJECTS && putting them in an array

                   var tx = random(tableauTexte);
                   var tw= null;
                   var ppgtext= null;
                   var to = null;
                   //textFont(font);does not work ; returns NaN for textWidth()
                   textSize(12);
                    tW = textWidth(tx);// returns a value but it is not exact (too big) if textSize is set to 14
                   ppgtext = createGraphics(tW,25);
                   ppgtext.background(0);
                   ppgtext.fill(255);
                   ppgtext.text(tx,0,(ppgtext.height/2)+2);
                   to = new TextObjects(random(-500,500), random(-350,350),random(-10,10), tx, random(-10,10),tW, ppgtext);
                   textesCharges.push(to);

                 }
                 createCanvas(1024,768,WEBGL);
                 background(0);
                 pg = createGraphics(tailleRass,30);

                 };


                function draw() {

                  background(0);

                 for(var i = 0; i<textesCharges.length; i++){
                   var t = textesCharges[i];
                  t.update();
                   //t.render();//does not work or in a weird way

                 }


                 for(var j= 0; j< nbre; j++){

                 push();

                 var X = textesCharges[j].renvoieX();
                 var Y = textesCharges[j].renvoieY();
                 var Z = textesCharges[j].renvoieZ();

                    translate(X,Y,Z);
                    var theta = textesCharges[j].renvoieAngle();
                    rotateZ(theta);
                    var textur = textesCharges[j].renvoie();
                    var tW = textesCharges[j].renvoieTW();
                    texture(textur);
                    plane(tW,30,1);

                    pop();

                 }


                  pg.background(0,0,0,10);// PARAM 4 WORKS IN A STRANGE WAY; useless
                  pg.noStroke();
                  pg.fill(255,0,0);
                  pg.textSize(24);
                  pg.text("RASSURONS_NOUS", 0,(pg.height/2)+6);// NO WAY TO CODE EXACTLY THE HEIGHT POS (textAscent() textDescent())

                    push();

                  translate(0,0,20);
                  texture(pg);
                  noFill();
                  plane(tailleRass,30,1);
                  pop();

                  if(nbre< textesCharges.length){
                    nbre++;
                  }
                  };

                  //////////////////////////////////////////////////TEXT OBJECTS CLASS


                function TextObjects(xx, yy, zz, ttxt, Aangle,tw,graphics){



                  this.posX = xx;
                  this.posY = yy;
                  this.posZ = zz;

                  this.position = createVector([this.posX],[this.posY],[this.posZ]);
                  this.txt = ttxt;
                  this.angle = Aangle;
                  this.textW = tw;
                  this.pgtext = graphics;


                }


                TextObjects.prototype.update = function () {//updating the angle rotation Z

                    this.angle = this.angle+0.0005;

                }

                TextObjects.prototype.renvoie = function () { //getter for graphics

                    return this.pgtext;
                    //console.log("je cree update" + this.angle);

                }

                TextObjects.prototype.renvoieTW = function () {//getter for textWidth()

                    return this.textW;


                }

                TextObjects.prototype.renvoieAngle = function () { //getter for angle
                  this.angle = this.angle+0.005;
                    return this.angle;


                }

                TextObjects.prototype.renvoieX = function () { //getter for xpos

                    return this.position.x;
                    //console.log("je cree update" + this.angle);

                }

                TextObjects.prototype.renvoieY = function () { //getter for ypos

                    return this.position.y;

                }

                TextObjects.prototype.renvoieZ = function () { //getter for zPos (those 3 can be made 1 with PVector)

                    return this.position.z;

                }

                TextObjects.prototype.render = function () {//does not work

                    push();



                    translate(this.position.x, this.position.y, this.position.z);
                    rotateZ(this.angle);

                     this.pgtext.textSize(12);
                  this.pgtext.background(0,0,0);
                  this.pgtext.noStroke();
                  this.pgtext.fill(255,255,255);
                  this.pgtext.text(this.txt, this.pgtext.width/20, (this.pgtext.height/2)+2);

                  texture(this.pgtext);
                  plane(this.tW,30,1);




                    pop();
                }

the text file:

une panne mondiale de l’électricité un ouragan monstrueux qui ravage les ¾ de la planète une cyber attaque généralisée contre les grandes places boursières le naufrage d’un supertanker sur les côtes de la Manche l’arrivée d’une soucoupe volante en provenance d’Orion l’explosion simultanée d’une douzaine de volcans en islande la paralysie de tout le transport aérien un tremblement de terre de force 12 sur l’échelle de Richter dont l’épicentre se situerait en basse normandie l’explosion d’une centrale nucléaire de modèle français EPR la fonte des glaces dans l’antartique la pénurie des ressources en pétrole et en gaz l’élection d’un nouvel Hitler à la tête des USA la brusque élévation du niveau des mers et des océans et la disparition du continent Australien l’écrasement sur New York d’une comète ou d’un astéroide de plusieurs millions de tonnes le Krash du système bancaire international l’apparition d’une forme mutante de la peste bubonique faisant 3 milliards de victimes la reconstruction du mur de Berlin et la restauration du système communiste dans l’Europe de l’Est l’élection de Ségolène royal à la présidence française la liquidation du nouveau numero 1 d’Al Qaida par un essaim de libellules drones la réhabilitation de Bernard Tapie le grand retour de DSK le givrage des sondes pitots l’invention d’un programme qui calcule tout il y a des choses qui n’arrivent jamais

Answers

  • Answer ✓

    i have found by myself that WEBGL ignores ALL text commands (not only, as i thought "text("", x, y z); it ignores textFont() && textSize(); that s why textWidth() returns false values when using textSize() and NaN with textFont(); seems that it accepts only default font && values (12).

Sign In or Register to comment.