xxxxxxxxxx
232
// play with
// https://discourse.processing.org/t/referenceerror-loadmodel-is-not-defined/16877/3
// https://editor.p5js.org/micheal.ocathain/sketches/kM26r6njZ
// KLL p5.js PTZ
let simplehull;
function preload() {
simplehull = loadModel('assets/3d-model.obj');
}
function draw_object() { //_________________called by / from inside PTZ
// axis();
fill(0,100,0);
model(simplehull);
}
function axis() {
push();
stroke(200, 0, 0);
box(100,4,4);
ellipse(0, 0, height / 2, height / 2, 50);
rotateX(HALF_PI);
stroke(0, 200, 0);
box(4,100,4);
ellipse(0, 0, height / 2, height / 2, 50);
rotateY(HALF_PI);
stroke(0, 0, 200);
box(100,4,4);
ellipse(0, 0, height / 2, height / 2, 50);
pop();
}
function setup() {
createCanvas(400, 400, WEBGL);
//noFill();
noSmooth();
strokeWeight(2);
info_print();
}
function draw() {
background(0,0,255);
PTZ();
}
document.oncontextmenu = function() {
return false;
}
//_____________________________________ PTZ tab
let mode = 0;
let Zmag = 2;
let Zaxis=-30;
let Xmag=0, Ymag = 0;
let newXmag=0, newYmag=0, newZmag=0;
let newxpos=0, newypos = 0; // for PAN
let xposd=0, yposd = 0; // for PAN
let diagp = false;
//_________________________________________________________________ ROTATE / TILDE and MOVE / PAN
function mousePressed() {
if (mouseButton == LEFT) mode=1; // ORBIT
else if (mouseButton == RIGHT) mode=2; // PAN
}
//_________________________________________________________________ mouse PT end
function mouseReleased() {
mode = 0;
}
//_________________________________________________________________ mouseWheel ZOOM
function mouseWheel(event) {
if (diagp) print(event.delta);
Zmag += event.delta*0.1;
if (diagp) print("Zmag: "+nf(Zmag, 1, 2));
return false;
}
//_________________________________________________________________ key up down left right Page up page Down
function keyPressed() {
if ( keyCode == UP_ARROW ) Ymag -= 0.1 ;
if ( keyCode == DOWN_ARROW ) Ymag += 0.1 ;
if ( keyCode == RIGHT_ARROW) Xmag -= 0.1 ;
if ( keyCode == LEFT_ARROW ) Xmag += 0.1 ;
if ( keyCode == 33 ) Zmag -= 0.5 ; // page UP
if ( keyCode == 34 ) Zmag += 0.5 ; // page DOWN
if ( key == 'i' ) console.log("windowWidth " + windowWidth + " windowHeight " + windowHeight + " width " + width + " height " + height);
if (diagp) print("key: "+key+" keyCode: "+keyCode);
}
//_________________________________________________________________ Pan Tilde Zoom
function PTZ() {
push();
translate(0,0, Zaxis); // WEBGL centers automatic
if ( mode == 2 ) { // PAN ( right mouse button pressed)
xposd = (mouseX-float(width/2));
yposd = (mouseY-float(height/2));
}
newxpos = xposd;// xposd = 0;
newypos = yposd;// yposd = 0;
translate(newxpos, newypos, 0); // move object
if ( mode == 1 ) { // ORBIT ( left mouse button pressed)
newXmag = mouseX/float(width) * TWO_PI;
newYmag = mouseY/float(height) * TWO_PI;
let diff = Xmag-newXmag;
if (abs(diff) > 0.01) Xmag -= diff/4.0;
diff = Ymag-newYmag;
if (abs(diff) > 0.01) Ymag -= diff/4.0;
}
rotateX(-Ymag);
rotateY(-Xmag);
scale(Zmag);
draw_object(); // THE OBJECT see main tab
pop();
}
function info_print() {
print("PTZ info:");
print("key UP DOWN RIGHT LEFT -> rotate // key PAGE UP DOWN -> zoom");
print("mouse LEFT press drag up down right left -> rotate // mouse RIGHT press -> move // mouse WHEEL turn -> zoom");
}
/*
// processing 3.5.3 JAVA version
// 3D spiral ( SPRING ) from points
float ang, dang=0.02, r=10, w=2, x, y, z, dz=0.03;
int points=2000;
void draw_object() { //_________________called by / from inside PTZ
strokeWeight(w);
stroke(0,120,120);
x=0; y=0; z=0; ang = 0;
for ( int i = 0; i <= points; i++ ) {
x = r*sin(ang);
y = r*cos(ang);
ang = i*dang;
z += dz;
point(x,y,z);
}
}
void setup() {
size(200, 200, P3D);
info_print();
}
void draw() {
background(200,200,0);
PTZ();
}
//_____________________________________ PTZ tab
int mode = 0;
float Zmag = 5;
int Zaxis=-10;
float Xmag, Ymag = 0;
float newXmag, newYmag = 0;
int newZmag = 0;
float newxpos, newypos = 0; // for PAN
float xposd, yposd = 0; // for PAN
boolean diagp = false;
//_________________________________________________________________ ROTATE / TILDE and MOVE / PAN
void mousePressed() {
if (mouseButton == LEFT) mode=1; // ORBIT
else if (mouseButton == RIGHT) mode=2; // PAN
// else if (mouseButton == CENTER) mode=3; // zoom mouse wheel works same , presssed or not
}
//_________________________________________________________________ mouse PT end
void mouseReleased() {
mode = 0;
}
//_________________________________________________________________ mouseWheel ZOOM
void mouseWheel(MouseEvent event) {
int newZmag = event.getCount(); // +- 1
//if (Zmag > 10) { Zmag += newZmag * 5; } else { Zmag += newZmag; }// from 1 to 11 go in step 1 else in step 5
Zmag += newZmag*0.3;
if (diagp) println("Zmag: "+nf(Zmag, 1, 2));
}
void keyPressed() {
if ( keyCode == UP ) Ymag -= 0.1 ;
if ( keyCode == DOWN ) Ymag += 0.1 ;
if ( keyCode == RIGHT) Xmag -= 0.1 ;
if ( keyCode == LEFT ) Xmag += 0.1 ;
if ( keyCode == 16 ) Zmag -= 0.5 ;
if ( keyCode == 11 ) Zmag += 0.5 ;
if (diagp) println("key: "+key+" keyCode: "+keyCode);
}
//_________________________________________________________________ Pan Tilde Zoom
void PTZ() {
pushMatrix();
translate(width/2, height/2, Zaxis);
// get new mouse operation
if ( mode == 2 ) { // PAN ( right mouse button pressed)
xposd = (mouseX-float(width/2));
yposd = (mouseY-float(height/2));
}
newxpos = xposd;// xposd=0;
newypos = yposd;// yposd = 0;
translate(newxpos, newypos, 0); // move object
if ( mode == 1 ) { // ORBIT ( left mouse button pressed)
newXmag = mouseX/float(width) * TWO_PI;
newYmag = mouseY/float(height) * TWO_PI;
float diff = Xmag-newXmag;
if (abs(diff) > 0.01) Xmag -= diff/4.0;
diff = Ymag-newYmag;
if (abs(diff) > 0.01) Ymag -= diff/4.0;
}
rotateX(-Ymag);
rotateY(-Xmag);
scale(Zmag);
draw_object(); // THE OBJECT see main tab
popMatrix();
}
//_______________________________________________ SETUP PRINT INFO
void info_print() {
println("PTZ info:");
println("key UP DOWN RIGHT LEFT -> rotate // key PAGE UP DOWN -> zoom");
println("mouse LEFT press drag up down right left -> rotate // mouse RIGHT press -> move // mouse WHEEL turn -> zoom");
}
*/