Skip to content

Commit 7eeed45

Browse files
committed
new mousePosition / pointerPosition flip param
1 parent f0bde27 commit 7eeed45

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ function keyPressed() {
249249
Retrieve image offset, mouse position, pointer position and screen resolution which are common `uniform vec2` variables
250250

251251
1. `texOffset(image)` which is the same as: `return [1 / image.width, 1 / image.height]`.
252-
2. `mousePosition()` which is the same as: `return [this.mouseX * this.pixelDensity(), (this.height - this.mouseY) * this.pixelDensity()]`.
253-
3. `pointerPosition(pointerX, pointerY)` which is the same as: `return [pointerX * this.pixelDensity(), (this.height - pointerY) * this.pixelDensity()]`. Available to both, the `p5` object and [p5.RendererGL](https://p5js.org/reference/#/p5.Renderer) instances. Note that `pointerX` should always be the first parameter and `pointerY` the second.
254-
4. `resolution()` which is the same as: `return [this.width * this.pixelDensity(), this.height * this.pixelDensity()]`. Available to both, the `p5` object and [p5.RendererGL](https://p5js.org/reference/#/p5.Renderer) instances.
252+
2. `mousePosition([flip = true])` which is the same as: `return [this.pixelDensity() * this.mouseX, this.pixelDensity() * (flip ? this.height - this.mouseY : this.mouseY)]`.
253+
3. `pointerPosition(pointerX, pointerY, [flip = true])` which is the same as: `return [this.pixelDensity() * pointerX, this.pixelDensity() * (flip ? this.height - pointerY : pointerY)]`. Available to both, the `p5` object and [p5.RendererGL](https://p5js.org/reference/#/p5.Renderer) instances. Note that `pointerX` should always be the first parameter and `pointerY` the second.
254+
4. `resolution()` which is the same as: `return [this.pixelDensity() * this.width, this.pixelDensity() * this.height]`. Available to both, the `p5` object and [p5.RendererGL](https://p5js.org/reference/#/p5.Renderer) instances.
255255

256256
# Space transformations
257257

p5.treegl.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
var Tree = (function (ext) {
1212
const INFO = {
1313
LIBRARY: 'p5.treegl',
14-
VERSION: '0.10.0',
14+
VERSION: '0.10.1',
1515
HOMEPAGE: 'https://github.com/VisualComputing/p5.treegl'
1616
};
1717
Object.freeze(INFO);
@@ -416,6 +416,15 @@ window.Tree = Tree;
416416

417417
// 2. Space transformations
418418

419+
// vertical flip based on (screenX, screenY):
420+
/*
421+
beginHUD();
422+
translate(screenX, screenY); // move origin to (screenX, screenY)
423+
scale(1, -1); // Flip vertically
424+
image(fbo, 0, 0, fboWidth, fboHeight); // draw fbo at the new origin
425+
endHUD();
426+
*/
427+
419428
p5.prototype.beginHUD = function (...args) {
420429
if (this._renderer instanceof p5.RendererGL) {
421430
this._renderer.beginHUD(...args);
@@ -591,7 +600,7 @@ window.Tree = Tree;
591600
point = new p5.Vector(0, 0, 0.5),
592601
pMatrix,
593602
vMatrix,
594-
pvMatrix = this.pvMatrix({ pMatrix: pMatrix, vMatrix: vMatrix })
603+
pvMatrix = this.pvMatrix({ pMatrix, vMatrix })
595604
} = {}) {
596605
let target = pvMatrix._mult4([point.x, point.y, point.z, 1]);
597606
if (target[3] == 0) {
@@ -1333,24 +1342,35 @@ void main() {
13331342
return [1 / image.width, 1 / image.height];
13341343
}
13351344

1336-
p5.prototype.mousePosition = function () {
1337-
return [this.mouseX * this.pixelDensity(), (this.height - this.mouseY) * this.pixelDensity()];
1345+
p5.prototype.mousePosition = function (flip = true) {
1346+
return [this.pixelDensity() * this.mouseX, this.pixelDensity() * (flip ? this.height - this.mouseY : this.mouseY)];
13381347
}
13391348

13401349
p5.prototype.pointerPosition = function (...args) {
13411350
return this._renderer.pointerPosition(...args);
13421351
}
13431352

1344-
p5.RendererGL.prototype.pointerPosition = function (pointerX, pointerY) {
1345-
return [pointerX * this.pixelDensity(), (this.height - pointerY) * this.pixelDensity()];
1346-
}
1353+
p5.RendererGL.prototype.pointerPosition = function (...args) {
1354+
let pointerX, pointerY, flip = true;
1355+
args.forEach(arg =>
1356+
typeof arg === 'number'
1357+
? pointerX === undefined ? pointerX = arg : pointerY = arg
1358+
: typeof arg === 'boolean' && (flip = arg)
1359+
);
1360+
/*
1361+
// TODO: if below hack were cleaner mousePosition can be removed
1362+
pointerX = pointerX ?? window.mouseX;
1363+
pointerY = pointerY ?? window.mouseY;
1364+
// */
1365+
return [this.pixelDensity() * pointerX, this.pixelDensity() * (flip ? this.height - pointerY : pointerY)];
1366+
};
13471367

13481368
p5.prototype.resolution = function (...args) {
13491369
return this._renderer.resolution(...args);
13501370
}
13511371

13521372
p5.RendererGL.prototype.resolution = function () {
1353-
return [this.width * this.pixelDensity(), this.height * this.pixelDensity()];
1373+
return [this.pixelDensity() * this.width, this.pixelDensity() * this.height];
13541374
}
13551375

13561376
// 4. Utility functions
@@ -1382,7 +1402,7 @@ void main() {
13821402
* @param {p5.Vector | Array} corner2 box corner2, use it with corner1.
13831403
* @param {p5.Vector | Array} center sphere (or point) center.
13841404
* @param {Number} radius sphere radius.
1385-
* @param {Array} bounds frustum equations 6x4 matrix.
1405+
* @param {Object} bounds frustum equations 6x4 matrix.
13861406
*/
13871407
p5.RendererGL.prototype.visibility = function ({
13881408
corner1,
@@ -1939,7 +1959,9 @@ void main() {
19391959
const l = pMatrix.lPlane();
19401960
const r = pMatrix.rPlane();
19411961
// TODO hack: fixes frustum() drawing
1942-
// camera projections should be called as:
1962+
// i. prioritizes pgs over fbos currently. It should be the other way around.
1963+
// (test with ball and box visibility)
1964+
// ii. camera projections should be called as:
19431965
// pg.ortho(lPlane.value(), rPlane.value(), bPlane.value(), tPlane.value(), nPlane.value(), fPlane.value());
19441966
// pg.frustum(lPlane.value(), rPlane.value(), tPlane.value(), bPlane.value(), nPlane.value(), fPlane.value());
19451967
const t = pMatrix.isOrtho() ? -pMatrix.tPlane() : pMatrix.tPlane();

0 commit comments

Comments
 (0)