Skip to content

Commit bbf188e

Browse files
committed
implemented resetMatrix and applyMatrix
1 parent 73a44d1 commit bbf188e

5 files changed

+107
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This function provides a solution to the missing screenX and screenY functions i
44

55
**This is work in progress.**
66

7-
I'm aware of some missing stuff like tracking resetMatrix() and applyMatrix() in 2d-mode.
7+
As far as I could find out there should be anything implemented to make it work perfectly in any mode (2D, WEBGL, angleMode DEGREES or RADIANS, normal or instance mode). If you'll find a case that isn't working correctly, please file an issue.

addScreenPositionFunction.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function addScreenPositionFunction(p5Instance) {
1212
const R_WEBGL = 1;
1313
let context = getObjectName(p._renderer.drawingContext).search("2D") >= 0 ? R_2D : R_WEBGL;
1414

15-
// this will be stack to push and pop matrices
15+
// the stack to keep track of matrices when using push and pop
1616
if (context == R_2D) {
1717
p._renderer.matrixStack = [new p5.Matrix()];
1818
}
@@ -28,6 +28,14 @@ function addScreenPositionFunction(p5Instance) {
2828
}
2929

3030

31+
if (p.resetMatrix instanceof Function) {
32+
let resetMatrixNative = p.resetMatrix;
33+
p.resetMatrix = function(...args) {
34+
if (context == R_2D) p._renderer.matrixStack = [new p5.Matrix()];
35+
resetMatrixNative.apply(p, args);
36+
};
37+
}
38+
3139
if (p.translate instanceof Function) {
3240
let translateNative = p.translate;
3341
p.translate = function(...args) {
@@ -92,7 +100,6 @@ function addScreenPositionFunction(p5Instance) {
92100
};
93101
}
94102

95-
96103
// Help needed: don't know what transformation matrix to use
97104
// Solved: Matrix multiplication had to be in reversed order.
98105
// Still, this looks like it could be simplified.
@@ -130,6 +137,27 @@ function addScreenPositionFunction(p5Instance) {
130137
}
131138

132139

140+
if (p.applyMatrix instanceof Function) {
141+
let applyMatrixNative = p.applyMatrix;
142+
p.applyMatrix = function(...args) {
143+
if (context == R_2D) {
144+
let stack = p._renderer.matrixStack;
145+
let m = last(stack);
146+
let sm = new p5.Matrix();
147+
sm.mat4[0] = args[0];
148+
sm.mat4[1] = args[1];
149+
sm.mat4[4] = args[2];
150+
sm.mat4[5] = args[3];
151+
sm.mat4[12] = args[4];
152+
sm.mat4[13] = args[5];
153+
sm.mult(m);
154+
stack[stack.length - 1] = sm;
155+
}
156+
applyMatrixNative.apply(p, args);
157+
};
158+
}
159+
160+
133161
if (p.push instanceof Function) {
134162
let pushNative = p.push;
135163
p.push = function(...args) {
@@ -148,6 +176,8 @@ function addScreenPositionFunction(p5Instance) {
148176
};
149177
}
150178

179+
180+
151181
p.screenPosition = function(x, y, z) {
152182
if (x instanceof p5.Vector) {
153183
let v = x;

test-01-basic.html

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
if (mode == "2D") rotate(frameCount);
4242
else rotateY(frameCount);
4343

44-
// doesn't work yet:
4544
shearX(sin(frameCount) * 25);
4645
shearY(cos(frameCount*0.73) * 25);
4746

test-02-instance-mode.html

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
if (mode == "2D") p.rotate(p.radians(p.frameCount));
3737
else p.rotateY(p.radians(p.frameCount));
3838

39+
p.shearX(p.sin(p.frameCount / 100));
40+
p.shearY(p.cos(p.frameCount / 73));
41+
3942
p.scale(1.2, 1.5, 0.8);
4043

4144
p.line(-100, -100, 100, 100);

test-03-reset-apply.html

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!DOCTYPE html>
2+
<html lang="">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>p5.js example</title>
7+
<style> body {padding: 0; margin: 0;} </style>
8+
9+
<script src="addScreenPositionFunction.js"></script>
10+
<script src="lib/p5.js"></script>
11+
</head>
12+
<body>
13+
<script type="text/javascript">
14+
15+
p5.disableFriendlyErrors = true;
16+
17+
18+
// set mode to "2D" or "3D" to quickly test if WEBGL is also working
19+
var mode = "2D";
20+
21+
function setup() {
22+
if (mode == "2D") createCanvas(windowWidth, windowHeight);
23+
else createCanvas(windowWidth, windowHeight, WEBGL);
24+
25+
addScreenPositionFunction();
26+
27+
rectMode(CENTER);
28+
stroke(255);
29+
}
30+
31+
function draw() {
32+
// resetTransformationTracker();
33+
34+
background(0);
35+
36+
push();
37+
38+
// just for testing: this should be reset
39+
scale(1.4);
40+
41+
resetMatrix();
42+
43+
if (mode == "2D") translate(width / 2, height / 2);
44+
45+
let f = frameCount / 100;
46+
applyMatrix(noise(f) * 2, noise(f+100)-0.5, noise(f+200)-0.5, noise(f+300) * 2, noise(f+400) * 200 - 100, noise(f+500) * 200 - 100);
47+
48+
line(-100, -100, 100, 100);
49+
50+
var p1 = screenPosition(-100, -100, 0);
51+
var p2 = screenPosition(100, 100, 0);
52+
53+
if (mode != "2D") rotateY(90);
54+
55+
fill(128);
56+
rect(0, 0, 100, 100);
57+
58+
pop();
59+
60+
fill(255, 0, 0);
61+
circle(p1.x, p1.y, 10);
62+
circle(p2.x, p2.y, 10);
63+
}
64+
65+
function mousePressed() {
66+
noLoop();
67+
}
68+
69+
</script>
70+
</body>
71+
</html>

0 commit comments

Comments
 (0)