xxxxxxxxxx
517
// ----------------------------------------------
// Declaration des fonctions necessaires a l'affichage
// Notamment les fonctions de Mantra
// ----------------------------------------------
// Prototype de fonction qui retourne une valeur aleatoire entre 0 et 360, mais pas dans [40,180]
// Fonction qui s'appelait avant "getMantraHue()"
// On l'a sorti de la classe Mantra. car elle n'utilise pas la serie
function randomHueLike() {
var h;
do {
h = Math.floor(Math.random()*360);
} while(h>40 && h<180);
return h;
};
// ***********************************************
// ------------ CLASS MANTRA ---------------------
// ***********************************************
// Creation d'une classe Objet Mantra qui permet de recuperer toutes les fonctions d'objet aleatoire
// Constructeur de l'objet Mantra
function Mantra(numlist) {
this.serie = numlist;
}
// Prototype de fonction Get Value de Mantra qui retourne un element aleatoire de la liste
Mantra.prototype.getValue = function () {
var index = Math.floor(Math.random()*this.serie.length);
return this.serie[index];
};
// Prototype de fonction qui retourne la valeur normalisee du Mantra
Mantra.prototype.getValueNorm = function () {
var randNum = this.getValue();
var lastValue = this.serie[[this.serie.length - 1]];
var normValue = randNum/lastValue;
return normValue;
};
// Prototype de fonction qui defini des couleurs aleatoire sur la base de la serie Mantra
// Modif par rapport a l'original, on passe en paramettre les variables de Hue, Sat et Bri
Mantra.prototype.getHSBColor = function (forcedHue, forcedSat, forcedBri) {
var h, s, b;
// HUE
if (forcedHue >= 0) {
h = forcedHue;
}
else {
h = randomHueLike();
}
// SAT
if (forcedSat >= 0) {
s = forcedSat;
}
else {
s = map(this.getValueNorm(), 0, 1, 170, 255);
}
// BRI
if (forcedBri >= 0) {
b = forcedBri;
}
else {
b = map(this.getValueNorm(), 0, 1, 170, 255);
}
return color(h, s, b);
};
// Retourne une position 3D aleatoire en fonction de la liste Mantra
Mantra.prototype.getPosition = function () {
var v;
// axe x
v = min(this.getValue()*2, 400);
pos_x = random(-v, v);
// axe y
v = min(this.getValue()*2, 400);
pos_y = random(-v, v);
// axe z
pos_z = -random(min(1000, this.getValue()*4));
let pos = createVector(pos_x, pos_y, pos_z);
return pos;
}
// Retourne une rotation 3D aleatoire en fonction de la liste Mantra
Mantra.prototype.getRotInc = function () {
// rot x
rot_x = random(-1,+1) / map(this.getValueNorm(), 0, 1, 40, 300);
// rot y
rot_y = random(-1,+1) / map(this.getValueNorm(), 0, 1, 40, 300);
// rot z
pos_z = random(-1,+1) / map(this.getValueNorm(), 0, 1, 40, 300);
// Creer vecteur
let rot = createVector(rot_x, rot_y, pos_z);
return rot;
}
// Retourne un angle 3D aleatoire en fonction de la liste Mantra
Mantra.prototype.getAngles = function () {
// rot x
ang_x = map(this.getValueNorm(), 0, 1, -PI, +PI);
// rot y
ang_y = map(this.getValueNorm(), 0, 1, -PI, +PI);
// rot z
ang_z = map(this.getValueNorm(), 0, 1, -PI, +PI);
// Creer vecteur
let ang = createVector(ang_x, ang_y, ang_z);
return ang;
}
// --------- FIN DE CLASSE MANTRA ------------------
// ********************************************************************
// ------------ RESET GRAPHICS SI APPUI SUR ESPACE ---------------------
// ********************************************************************
// Creation d'une classe Objet Mantra qui permet de recuperer toutes les fonctions d'objet aleatoire
// Constructeur de l'objet Mantra
function keyPressed() {
if (keyCode == 32) {
requestReset = true;
// call reset function
}
return false
}
// ******************************************************************************************
// ------------ INITIALISATION DES VARIABLE GLOBALES DE LA MAIN FUNCTION ---------------------
// ******************************************************************************************
var colorMethod = 0;
var forcedHue = -1;
var forcedSat = -1;
var forcedBri = -1;
var clearOpacity = 255;
var nextReset;
var requestReset ;
var listFX = [];
let graphiki ;
let mantravichat ;
var frate = 30
var chauchat_philo = [1,2,3,7,9,13,15,18,23,27,33,48,55,66,69,77,82,88,99,101,113,123,127,133,142,166,169,199,202,213,222,231,234,242,269,303,312,321,323,333,356,366,369,377,389,399,404,407,416,423,432,444,448,456,466,469,472,486,488,501,505,511,525,533,543,555,566,567,569,575,588,596,599,603,606,613,618,623,633,642,648,654,666];
// ---------------------------------------------
function setup() {
// Initialiser les elements graphiques
// 810 MBR : modification car IDE Processing demande une taille fixe, pas une variable size(w, h, P3D);
createCanvas(windowWidth, windowHeight, WEBGL)
// La representation d'objets 3D via p5.js necessite la declaration du canva en WEBGL, cf. http://bluegalaxy.info/codewalk/2017/12/21/p5-js-how-to-rotate-3d-objects-on-the-x-y-and-z-axis/
frameRate(frate);
colorMode(HSB, 360, 255, 255, 255); //H, S, B, Alpha
smooth(); // https://p5js.org/reference/#/p5/smooth
// Initialiser le Mantra de vichat
mantravichat = new Mantra(chauchat_philo);
// STOP ICI 810 MBR
// tuto create graphics : https://www.youtube.com/watch?v=pNDc8KXWp9E
// graphiki = createGraphics(1024, 512)
graphiki = createGraphics(windowWidth, windowHeight)
background(0);
//
// particleGraphics = createGraphics(windowWidth, windowHeight);
// particleGraphics.background(220);
textureGenerator = new TextureGenerator();
graphiki = textureGenerator.update();
reset(); //, listFX
//reset(mantra_chauchat, textureGenerator, listFX);
}
// ******************************************************************************************
// ------------ MAIN // DRAW ---------------------
// ******************************************************************************************
function draw(){
//draw whats been buffered
image(graphiki, 0, 0)
// frame.setTitle(str(frameRate));
if (nextReset < millis() || requestReset) {
reset();
return;
}
graphiki = textureGenerator.update();
// Camera update
// On aura peut-etre a utiliser ca : https://github.com/diwi/p5.EasyCam
// cam.rotateX(camRotInc.x);
// cam.rotateY(camRotInc.y);
// cam.rotateZ(camRotInc.z);
// Clear screen (remanence) and draw Mantra Numbers in background
// cam.beginHUD();
noStroke();
fill(0,clearOpacity);
rect(-1,-1, width+2, height+2);
// 810 : Pas encore importe
// drawNumbers();
// cam.endHUD();
// Draw effects
for (i = 0 ; i < listFX.length ; i++) {
listFX[i].draw();
}
// need to call camera() to get a full screen blur
if (hasBlur) {
camera();
// ALERTS : p5.js says: filter() was expecting THRESHOLD|GRAY|OPAQUE|INVERT|POSTERIZE|BLUR|ERODE|DILATE|BLUR for the first parameter, received blur instead. (on line 223 in sketch.js [/sketch.js:223:11]). (http://p5js.org/reference/#/p5/filter)
// filter(blur);
}
// MBR 810
// Pour l'instant on va desactiver toute fonction de sauvegarde
// Save
// if (requestSaveFrame || autoSaveFrame) {
// saveFrame("data/screenshots/frame-#####.png");
// requestSaveFrame = false;
// }
// if (enableSyphon) {
// server.sendImage(get());
// }
}
// ******************************************************************************************
// ------------ MAIN // RESET ---------------------
// ******************************************************************************************
// Main reset Function
function reset(){ //, listFX
background(0);
clearOpacity = map(mantravichat.getValueNorm(), 0, 1, 0, 255);
camRotInc = mantravichat.getRotInc();
colorMethod = (mantravichat.getValue()) % 3;
print("ColorMethod: " + colorMethod + "\n");
if (colorMethod == 0) {
// All White
forcedHue = 0;
forcedSat = 0;
forcedBri = 255;
}
else if (colorMethod == 1) {
// Different colors with high saturation and brightness
forcedHue = -1;
forcedSat = map(mantravichat.getValueNorm(), 0, 1, 170, 255);
forcedBri = map(mantravichat.getValueNorm(), 0, 1, 170, 255);
}
else if (colorMethod == 2) {
// Monochromatic with full saturation and high brightness
forcedHue = randomHueLike();
forcedSat = 255;
forcedBri = map(mantravichat.getValueNorm(), 0, 1, 180, 255);
}
hasBlur = ((mantravichat.getValue() % 3) == 0);
//ARRET ICI 810 MBR
// FX LIST A CREER / VERIFIER
// Clear old effects;
// listFX.clear();
listFX =[];
textureGenerator.reset();
// 810 mbr test
// graphiki = textureGenerator.update();
// Insert new effects
// Grid
let count;
count = map(mantravichat.getValueNorm(), 0, 1, 1, 3);
for (i=0; i<count; i++) {
listFX.push(new FXGrid());
}
// Determine la durée de la scene
// 3, 13 ou 23 secondes
t = mantravichat.getValue() % 3;
t = 9 + (t * 9);
nextReset = millis() + t*1000;
requestReset = false;
}
// ******************************************************************************************
// ------------ CLASSE // TEXTURE GENERATOR ---------------------
// ******************************************************************************************
class TextureGenerator {
constructor() {
this.mantra = mantravichat;
this.x = 0.0;
this.y = 0.0;
this.a = 0.0;
this.b = 0.0;
this.capA = 0.0;
this.capB = 1.0;
this.alpha = 0.0;
this.beta = 0.0;
this.alphaInc = random(-0.3, 0.3);
this.betaInc = random(-0.3, 0.3);
this.mainXInc = mantravichat.getValueNorm() * 4;
this.mainYInc = mantravichat.getValueNorm() * 4;
this.strokeColor = this.mantra.getHSBColor() ;
this.buffer = graphiki ;
this.wantReset = false;
this.buffer.colorMode(HSB, 360, 255, 255, 255);
this.buffer.strokeCap(SQUARE);
}
reset() {
this.wantReset = true;
}
update() {
if (this.wantReset) {
this.wantReset = false;
// Clear (more reliable than background(0))
this.buffer.fill(0);
this.buffer.noStroke();
this.buffer.rect(-1, -1, this.buffer.width+2, this.buffer.height+2);
// Set Color to mantra value
this.strokeColor = this.mantra.getHSBColor();
}
// Small random segments
this.alpha += this.alphaInc;
this.beta += this.betaInc;
let xx = sin(this.alpha) * sin(this.beta) * this.buffer.width;
let yy = cos(this.alpha) * sin(this.beta) * this.buffer.height;
this.buffer.strokeWeight(12);
this.buffer.line(xx,yy, xx+random(40),yy+random(40));
// Big scan lines
this.buffer.line(this.x, 0, this.x, this.buffer.height);
if (random(123)>88) {
this.buffer.stroke(this.strokeColor);
this.buffer.line(this.x, 0, this.x, this.buffer.width);
if (random(123) > 66) {
this.buffer.stroke(hue(this.strokeColor), saturation(this.strokeColor), 0);
}
else {
this.buffer.stroke(this.strokeColor);
}
}
this.x= this.x+this.mainXInc;
if (this.x>this.buffer.width) {
this.x=0;
}
this.buffer.line(0, this.y, this.buffer.width, this.y);
this.y= this.y+this.mainYInc;
if (this.y>this.buffer.height) {
this.y=0;
}
if (random(123)<23) {
if (random(123)<69) {
this.buffer.stroke(hue(this.strokeColor), saturation(this.strokeColor), 0);
}
else {
this.buffer.stroke(this.strokeColor);
}
}
// Small rectangles
let a = noise (this.capA)* this.buffer.width;
let b = noise (this.capB)* this.buffer.height;
let X = noise (this.x)* this.buffer.width;
let Y = noise (this.y)* this.buffer.height;
this.capA = this.capA + 0.03;
this.capB = this.capB + 0.02;
this.buffer.rect(a,b,12,12);
this.buffer.rect(X,Y,12,12);
return this.buffer;
}
}
// ******************************************************************************************
// ------------ CLASSE // FX GRID ---------------------
// ******************************************************************************************
//Je ne sais pas pourquoi il y a une classe FX prealable
class FXGrid {
constructor(){
this.ripplenumber = 3;
this.ripplarray = [];
this.pos=mantravichat.getPosition();
this.rot=mantravichat.getRotInc();
this.ang = mantravichat.getAngles();
this.strokeWeight = random(0.8, 2.5);
this.strokeColor = mantravichat.getHSBColor();
this.rendering = round(random(1));
this.spacing = map(mantravichat.getValueNorm(), 0, 1, 15, 50);
this.zoomSpeed = map(mantravichat.getValueNorm(), 0, 1, 5,100);
}
//810 MBR -
// eventuellement faire une classe qui appelle les deux ou trois fonctions necessaires
setripples() {
amp = map(mantravichat.getValueNorm(), 0, 1, 0, 1);
minAmp = 8;
maxAmp = 16;
if (amp > 0.7) {
// high amplitude
minAmp = 40;
maxAmp = 70;
}
this.ripplarray.clear()
for (i=0;i<this.ripplenumber;i++) {
this.ripplarray.add(new source(random(-100,100),random(-100,100),random(minAmp,maxAmp),random(2,12)));
}
}
draw() {
// fade waves out over time
for (i=0;i<this.ripplenumber;i++) {
// ca biug, je commente
// let s=this.ripplarray[i];
// s.fade();
}
//scale = map(sin(PApplet.parseFloat(frameCount)/zoomSpeed), -1, 1, 0.7, 1);
strokeWeight(this.strokeWeight);
let myColor = color(hue(this.strokeColor), saturation(this.strokeColor), brightness(this.strokeColor) * noise(millis()*this.strokeWeight/5.0)*1.3);
stroke(myColor);
noFill();
// 810 error donc comment
// pushMatrix();
translate(this.pos.x,this.pos.y,this.pos.z);
rotateX(this.ang.x);
rotateY(this.ang.y);
rotateZ(this.ang.z);
scale(scale);
if (this.rendering > 0) {
for (let y=-100; y<100; y+=this.spacing) {
for (let x=-100; x<100; x+=this.spacing) {
beginShape(QUAD_STRIP);
for (let dy=0; dy<2; dy++) {
for (let dx=0; dx<2; dx++) {
let nx = x + dx*this.spacing;
let ny = y + dy*this.spacing;
let h = this.getHeight(nx,ny);
// vertex(nx*2,ny*2,h/2);
}
}
endShape();
}
}
}
else {
for (let y=-100; y<100; y+=this.spacing) {
beginShape();
for (let x=-100; x<100; x+=this.spacing) {
let h = this.getHeight(x,y);
vertex(x*2,y*2,h/2);
}
endShape();
}
}
// popMatrix();
// accumule la rotation
this.ang.x += this.rot.x;
this.ang.y += this.rot.y;
this.ang.z += this.rot.z;
}
// sum of waves from different sources at this point
getHeight(x, y) {
let sumHeight=0;
for (i=0;i<this.ripplenumber;i++) {
let s=this.ripplarray[i];
// sumHeight += s.getPart(x,y,frameCount);
}
return sumHeight;
}
}