From 0dd073bde36230e4f48144d48b215cb8d6d11236 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sat, 5 Apr 2025 05:22:21 +0100 Subject: [PATCH 01/10] Fixed camera tilt up vector --- src/webgl/p5.Camera.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/webgl/p5.Camera.js b/src/webgl/p5.Camera.js index 11a051f32f..dc2044a330 100644 --- a/src/webgl/p5.Camera.js +++ b/src/webgl/p5.Camera.js @@ -1618,6 +1618,15 @@ class Camera { rotatedCenter[1] += this.eyeY; rotatedCenter[2] += this.eyeZ; + // Rotate the up vector to keep the correct camera orientation + /* eslint-disable max-len */ + let up = new p5.Vector( + this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8], + this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9], + this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10] + ); + /* eslint-enable max-len */ + up.normalize() this.camera( this.eyeX, this.eyeY, @@ -1625,9 +1634,9 @@ class Camera { rotatedCenter[0], rotatedCenter[1], rotatedCenter[2], - this.upX, - this.upY, - this.upZ + up.x, + up.y, + up.z ); } From b3b2b503f4ee7d9f9b166092c62c0f1e3d30b8f1 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 22:02:23 +0100 Subject: [PATCH 02/10] Removed P5 Vector --- src/webgl/p5.Camera.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/webgl/p5.Camera.js b/src/webgl/p5.Camera.js index dc2044a330..5b7aa5929a 100644 --- a/src/webgl/p5.Camera.js +++ b/src/webgl/p5.Camera.js @@ -1620,13 +1620,17 @@ class Camera { // Rotate the up vector to keep the correct camera orientation /* eslint-disable max-len */ - let up = new p5.Vector( - this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8], - this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9], - this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10] - ); + + const rotatedUpX = this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8], + const rotatedUpY = this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9], + const rotatedUpZ = this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10] /* eslint-enable max-len */ - up.normalize() + + //Normalize the up vector + const normalizedUpX = rotatedUpX / upLength; + const normalizedUpY = rotatedUpY / upLength; + const normalizedUpZ = rotatedUpZ / upLength; + this.camera( this.eyeX, this.eyeY, @@ -1634,9 +1638,9 @@ class Camera { rotatedCenter[0], rotatedCenter[1], rotatedCenter[2], - up.x, - up.y, - up.z + normalizedUpX, + normalizedUpY, + normalizedUpZ ); } From fb8672c4559fa583dcc1467aa6ebd01ba2067446 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 22:07:26 +0100 Subject: [PATCH 03/10] Fixed syntax error --- src/webgl/p5.Camera.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/webgl/p5.Camera.js b/src/webgl/p5.Camera.js index 5b7aa5929a..aea1327f9d 100644 --- a/src/webgl/p5.Camera.js +++ b/src/webgl/p5.Camera.js @@ -1620,10 +1620,9 @@ class Camera { // Rotate the up vector to keep the correct camera orientation /* eslint-disable max-len */ - - const rotatedUpX = this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8], - const rotatedUpY = this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9], - const rotatedUpZ = this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10] + const rotatedUpX = this.upX * rotation.mat4[0] + this.upY * rotation.mat4[4] + this.upZ * rotation.mat4[8]; + const rotatedUpY = this.upX * rotation.mat4[1] + this.upY * rotation.mat4[5] + this.upZ * rotation.mat4[9]; + const rotatedUpZ = this.upX * rotation.mat4[2] + this.upY * rotation.mat4[6] + this.upZ * rotation.mat4[10]; /* eslint-enable max-len */ //Normalize the up vector From 517946ddcfb6f529b01654d84f0090f93e623984 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 22:14:02 +0100 Subject: [PATCH 04/10] Fixed failed tests --- src/webgl/p5.Camera.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/webgl/p5.Camera.js b/src/webgl/p5.Camera.js index aea1327f9d..c69c44a9ad 100644 --- a/src/webgl/p5.Camera.js +++ b/src/webgl/p5.Camera.js @@ -1626,6 +1626,12 @@ class Camera { /* eslint-enable max-len */ //Normalize the up vector + const upLength = Math.sqrt( + rotatedUpX * rotatedUpX + + rotatedUpY * rotatedUpY + + rotatedUpZ * rotatedUpZ + ); + const normalizedUpX = rotatedUpX / upLength; const normalizedUpY = rotatedUpY / upLength; const normalizedUpZ = rotatedUpZ / upLength; From 80cf66e1fa05ce05fbd1fa2cf9218cea4b5ab84d Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 22:33:20 +0100 Subject: [PATCH 05/10] Added test on Rotated Up Vector --- test/unit/webgl/p5.Camera.js | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index f3e6e24850..c5a7d04ec6 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -289,6 +289,54 @@ suite('p5.Camera', function() { }); }); + suite('Camera Tilt and Up Vector', function() { + test('Tilt() correctly updates the up vector', function() { + var orig = getVals(myCam); // Store original camera values + + // Apply tilt to the camera + myCam.tilt(30); // Tilt by 30 degrees + // Compute expected up vector (normalized) + let forward = myp5.createVector( + myCam.centerX - myCam.eyeX, + myCam.centerY - myCam.eyeY, + myCam.centerZ - myCam.eyeZ + ); + let up = myp5.createVector(orig.ux, orig.uy, orig.uz); + let right = p5.Vector.cross(forward, up); + let expectedUp = p5.Vector.cross(right, forward).normalize(); + // Verify that the up vector has changed + assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); + assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); + assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); + // Verify up vector matches expected values within a small margin of error + assert.closeTo(myCam.upX, expectedUp.x, 0.001, 'upX mismatch'); + assert.closeTo(myCam.upY, expectedUp.y, 0.001, 'upY mismatch'); + assert.closeTo(myCam.upZ, expectedUp.z, 0.001, 'upZ mismatch'); + }); + + test('Tilt() with negative angle correctly updates the up vector', function() { + var orig = getVals(myCam); // Store original camera values + myCam.tilt(-30); // Tilt by -30 degrees + // Compute expected up vector (normalized) + let forward = myp5.createVector( + myCam.centerX - myCam.eyeX, + myCam.centerY - myCam.eyeY, + myCam.centerZ - myCam.eyeZ + ); + let up = myp5.createVector(orig.ux, orig.uy, orig.uz); + let right = p5.Vector.cross(forward, up); + let expectedUp = p5.Vector.cross(right, forward).normalize(); + // Verify that the up vector has changed + assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); + assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); + assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); + // Verify up vector matches expected values within a small margin of error + assert.closeTo(myCam.upX, expectedUp.x, 0.001, 'upX mismatch'); + assert.closeTo(myCam.upY, expectedUp.y, 0.001, 'upY mismatch'); + assert.closeTo(myCam.upZ, expectedUp.z, 0.001, 'upZ mismatch'); + }); + }); + suite('Rotation with angleMode(DEGREES)', function() { beforeEach(function() { myp5.push(); From 9199cd94bdd29aa3babd3c3bef78bdb42d839111 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 22:47:21 +0100 Subject: [PATCH 06/10] Remove P5 Vector --- test/unit/webgl/p5.Camera.js | 123 ++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 45 deletions(-) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index c5a7d04ec6..9c910b51b9 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -290,51 +290,84 @@ suite('p5.Camera', function() { }); suite('Camera Tilt and Up Vector', function() { - test('Tilt() correctly updates the up vector', function() { - var orig = getVals(myCam); // Store original camera values - - // Apply tilt to the camera - myCam.tilt(30); // Tilt by 30 degrees - // Compute expected up vector (normalized) - let forward = myp5.createVector( - myCam.centerX - myCam.eyeX, - myCam.centerY - myCam.eyeY, - myCam.centerZ - myCam.eyeZ - ); - let up = myp5.createVector(orig.ux, orig.uy, orig.uz); - let right = p5.Vector.cross(forward, up); - let expectedUp = p5.Vector.cross(right, forward).normalize(); - // Verify that the up vector has changed - assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); - assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); - assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); - // Verify up vector matches expected values within a small margin of error - assert.closeTo(myCam.upX, expectedUp.x, 0.001, 'upX mismatch'); - assert.closeTo(myCam.upY, expectedUp.y, 0.001, 'upY mismatch'); - assert.closeTo(myCam.upZ, expectedUp.z, 0.001, 'upZ mismatch'); - }); - - test('Tilt() with negative angle correctly updates the up vector', function() { - var orig = getVals(myCam); // Store original camera values - myCam.tilt(-30); // Tilt by -30 degrees - // Compute expected up vector (normalized) - let forward = myp5.createVector( - myCam.centerX - myCam.eyeX, - myCam.centerY - myCam.eyeY, - myCam.centerZ - myCam.eyeZ - ); - let up = myp5.createVector(orig.ux, orig.uy, orig.uz); - let right = p5.Vector.cross(forward, up); - let expectedUp = p5.Vector.cross(right, forward).normalize(); - // Verify that the up vector has changed - assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); - assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); - assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); - // Verify up vector matches expected values within a small margin of error - assert.closeTo(myCam.upX, expectedUp.x, 0.001, 'upX mismatch'); - assert.closeTo(myCam.upY, expectedUp.y, 0.001, 'upY mismatch'); - assert.closeTo(myCam.upZ, expectedUp.z, 0.001, 'upZ mismatch'); - }); + test('Tilt() correctly updates the up vector', function() { + var orig = getVals(myCam); // Store original camera values + // Apply tilt to the camera + myCam.tilt(30); // Tilt by 30 degrees + + // Compute expected up vector without p5.Vector + let forwardX = myCam.centerX - myCam.eyeX; + let forwardY = myCam.centerY - myCam.eyeY; + let forwardZ = myCam.centerZ - myCam.eyeZ; + + let upX = orig.ux; + let upY = orig.uy; + let upZ = orig.uz; + + let rightX = forwardY * upZ - forwardZ * upY; + let rightY = forwardZ * upX - forwardX * upZ; + let rightZ = forwardX * upY - forwardY * upX; + + // Now compute expected up vector (normalize) + let expectedUpX = rightY * forwardZ - rightZ * forwardY; + let expectedUpY = rightZ * forwardX - rightX * forwardZ; + let expectedUpZ = rightX * forwardY - rightY * forwardX; + + // Normalize the expected up vector + let length = Math.sqrt(expectedUpX * expectedUpX + expectedUpY * expectedUpY + expectedUpZ * expectedUpZ); + expectedUpX /= length; + expectedUpY /= length; + expectedUpZ /= length; + + // Verify that the up vector has changed + assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); + assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); + assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); + + // Verify up vector matches expected values within a small margin of error + assert.closeTo(myCam.upX, expectedUpX, 0.001, 'upX mismatch'); + assert.closeTo(myCam.upY, expectedUpY, 0.001, 'upY mismatch'); + assert.closeTo(myCam.upZ, expectedUpZ, 0.001, 'upZ mismatch'); + }); + + test('Tilt() with negative angle correctly updates the up vector', function() { + var orig = getVals(myCam); // Store original camera values + myCam.tilt(-30); // Tilt by -30 degrees + + // Compute expected up vector without p5.Vector + let forwardX = myCam.centerX - myCam.eyeX; + let forwardY = myCam.centerY - myCam.eyeY; + let forwardZ = myCam.centerZ - myCam.eyeZ; + + let upX = orig.ux; + let upY = orig.uy; + let upZ = orig.uz; + + let rightX = forwardY * upZ - forwardZ * upY; + let rightY = forwardZ * upX - forwardX * upZ; + let rightZ = forwardX * upY - forwardY * upX; + + // Now compute expected up vector (normalize) + let expectedUpX = rightY * forwardZ - rightZ * forwardY; + let expectedUpY = rightZ * forwardX - rightX * forwardZ; + let expectedUpZ = rightX * forwardY - rightY * forwardX; + + // Normalize the expected up vector + let length = Math.sqrt(expectedUpX * expectedUpX + expectedUpY * expectedUpY + expectedUpZ * expectedUpZ); + expectedUpX /= length; + expectedUpY /= length; + expectedUpZ /= length; + + // Verify that the up vector has changed + assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); + assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); + assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); + + // Verify up vector matches expected values within a small margin of error + assert.closeTo(myCam.upX, expectedUpX, 0.001, 'upX mismatch'); + assert.closeTo(myCam.upY, expectedUpY, 0.001, 'upY mismatch'); + assert.closeTo(myCam.upZ, expectedUpZ, 0.001, 'upZ mismatch'); + }); }); suite('Rotation with angleMode(DEGREES)', function() { From ff0c8057378df21b3c8014cd952d8bea6b3c1f5f Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 23:48:02 +0100 Subject: [PATCH 07/10] Update p5.Camera.js --- test/unit/webgl/p5.Camera.js | 183 ++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 78 deletions(-) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index 9c910b51b9..51777af8ea 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -290,86 +290,113 @@ suite('p5.Camera', function() { }); suite('Camera Tilt and Up Vector', function() { - test('Tilt() correctly updates the up vector', function() { - var orig = getVals(myCam); // Store original camera values - // Apply tilt to the camera - myCam.tilt(30); // Tilt by 30 degrees - - // Compute expected up vector without p5.Vector - let forwardX = myCam.centerX - myCam.eyeX; - let forwardY = myCam.centerY - myCam.eyeY; - let forwardZ = myCam.centerZ - myCam.eyeZ; - - let upX = orig.ux; - let upY = orig.uy; - let upZ = orig.uz; - - let rightX = forwardY * upZ - forwardZ * upY; - let rightY = forwardZ * upX - forwardX * upZ; - let rightZ = forwardX * upY - forwardY * upX; - - // Now compute expected up vector (normalize) - let expectedUpX = rightY * forwardZ - rightZ * forwardY; - let expectedUpY = rightZ * forwardX - rightX * forwardZ; - let expectedUpZ = rightX * forwardY - rightY * forwardX; - - // Normalize the expected up vector - let length = Math.sqrt(expectedUpX * expectedUpX + expectedUpY * expectedUpY + expectedUpZ * expectedUpZ); - expectedUpX /= length; - expectedUpY /= length; - expectedUpZ /= length; - - // Verify that the up vector has changed - assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); - assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); - assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); - - // Verify up vector matches expected values within a small margin of error - assert.closeTo(myCam.upX, expectedUpX, 0.001, 'upX mismatch'); - assert.closeTo(myCam.upY, expectedUpY, 0.001, 'upY mismatch'); - assert.closeTo(myCam.upZ, expectedUpZ, 0.001, 'upZ mismatch'); - }); - - test('Tilt() with negative angle correctly updates the up vector', function() { - var orig = getVals(myCam); // Store original camera values - myCam.tilt(-30); // Tilt by -30 degrees - - // Compute expected up vector without p5.Vector - let forwardX = myCam.centerX - myCam.eyeX; - let forwardY = myCam.centerY - myCam.eyeY; - let forwardZ = myCam.centerZ - myCam.eyeZ; - - let upX = orig.ux; - let upY = orig.uy; - let upZ = orig.uz; - - let rightX = forwardY * upZ - forwardZ * upY; - let rightY = forwardZ * upX - forwardX * upZ; - let rightZ = forwardX * upY - forwardY * upX; - - // Now compute expected up vector (normalize) - let expectedUpX = rightY * forwardZ - rightZ * forwardY; - let expectedUpY = rightZ * forwardX - rightX * forwardZ; - let expectedUpZ = rightX * forwardY - rightY * forwardX; - - // Normalize the expected up vector - let length = Math.sqrt(expectedUpX * expectedUpX + expectedUpY * expectedUpY + expectedUpZ * expectedUpZ); - expectedUpX /= length; - expectedUpY /= length; - expectedUpZ /= length; - - // Verify that the up vector has changed - assert.notStrictEqual(myCam.upX, orig.ux, 'upX should be updated'); - assert.notStrictEqual(myCam.upY, orig.uy, 'upY should be updated'); - assert.notStrictEqual(myCam.upZ, orig.uz, 'upZ should be updated'); - - // Verify up vector matches expected values within a small margin of error - assert.closeTo(myCam.upX, expectedUpX, 0.001, 'upX mismatch'); - assert.closeTo(myCam.upY, expectedUpY, 0.001, 'upY mismatch'); - assert.closeTo(myCam.upZ, expectedUpZ, 0.001, 'upZ mismatch'); - }); + var myp5; + var myCam; + var delta = 0.001; + + // Function to retrieve the camera's parameters + var getVals = function(cam) { + return { + ex: cam.eyeX, + ey: cam.eyeY, + ez: cam.eyeZ, + cx: cam.centerX, + cy: cam.centerY, + cz: cam.centerZ, + ux: cam.upX, + uy: cam.upY, + uz: cam.upZ + }; + }; + + // Initialize the camera and set up the p5 instance + setup(function() { + myp5 = new p5(); + myCam = myp5.camera; + myp5._renderer = { _pInst: myp5 }; // Simulate the renderer + }); + + // Test: Camera starts with correct initial values + test('Camera initial values', function() { + var vals = getVals(myCam); + assert.closeTo(vals.ex, 0, delta); + assert.closeTo(vals.ey, 0, delta); + assert.closeTo(vals.ez, 500, delta); + assert.closeTo(vals.cx, 0, delta); + assert.closeTo(vals.cy, 0, delta); + assert.closeTo(vals.cz, 0, delta); + assert.closeTo(vals.ux, 0, delta); + assert.closeTo(vals.uy, 1, delta); + assert.closeTo(vals.uz, 0, delta); + }); + + // Test: Camera rotation around a given axis (e.g., X-axis) while maintaining the up vector orientation + test('Camera rotation around X-axis', function() { + myCam._rotateView(90, 1, 0, 0); // Rotate by 90 degrees around X-axis + + var vals = getVals(myCam); + // Check that the center's Y value changes but the up vector remains consistent + assert.notCloseTo(vals.cy, 0, delta); // Y center should change + assert.closeTo(vals.ux, 0, delta); // X up vector should stay the same + assert.closeTo(vals.uy, 0, delta); // Y up vector should change + assert.closeTo(vals.uz, 1, delta); // Z up vector should stay the same + }); + + // Test: Camera rotation around Y-axis, checking up vector normalization + test('Camera rotation around Y-axis', function() { + myCam._rotateView(90, 0, 1, 0); // Rotate by 90 degrees around Y-axis + + var vals = getVals(myCam); + // Ensure the up vector is normalized + var upLength = Math.sqrt(vals.ux * vals.ux + vals.uy * vals.uy + vals.uz * vals.uz); + assert.closeTo(upLength, 1, delta); // Check if up vector is normalized to 1 + }); + + // Test: Rotation around the eye position + test('Camera rotation around eye position', function() { + myCam.eyeX = 100; + myCam.eyeY = 100; + myCam.eyeZ = 100; + + // Rotate by 90 degrees around Z-axis + myCam._rotateView(90, 0, 0, 1); + + var vals = getVals(myCam); + assert.notCloseTo(vals.cx, 0, delta); // The center should have changed after rotation + assert.notCloseTo(vals.cy, 0, delta); + assert.notCloseTo(vals.cz, 0, delta); + }); + + // Edge case: No rotation + test('No rotation results in no change', function() { + var initialVals = getVals(myCam); + + myCam._rotateView(0, 0, 0, 0); // No rotation + + var finalVals = getVals(myCam); + assert.deepEqual(initialVals, finalVals); // Values should remain the same + }); + + // Edge case: Very small rotation + test('Very small rotation', function() { + myCam._rotateView(0.001, 0, 1, 0); // Small rotation around Y-axis + + var vals = getVals(myCam); + assert.closeTo(vals.cy, 0, delta); // Y center should remain almost the same + }); + + // Edge case: Extreme rotation (e.g., 360 degrees) + test('Extreme rotation (360 degrees)', function() { + var initialVals = getVals(myCam); + + myCam._rotateView(360, 0, 1, 0); // 360-degree rotation around Y-axis + + var finalVals = getVals(myCam); + assert.deepEqual(initialVals, finalVals); // Values should be almost identical after a full rotation + }); }); + suite('Rotation with angleMode(DEGREES)', function() { beforeEach(function() { myp5.push(); From f7c9c8c3b80451689bc6844a03f2586f9ee18126 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 6 Apr 2025 23:56:10 +0100 Subject: [PATCH 08/10] Update p5.Camera.js --- test/unit/webgl/p5.Camera.js | 54 +++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index 51777af8ea..b17996213a 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -310,13 +310,43 @@ suite('p5.Camera', function() { }; // Initialize the camera and set up the p5 instance - setup(function() { - myp5 = new p5(); - myCam = myp5.camera; - myp5._renderer = { _pInst: myp5 }; // Simulate the renderer + beforeAll(function() { + myp5 = new p5(function(p) { + p.setup = function() { + p.createCanvas(100, 100, p.WEBGL); + myCam = p.createCamera(); + }; + }); + }); + + afterAll(function() { + myp5.remove(); + }); + + beforeEach(function() { + myp5.angleMode(myp5.RADIANS); + + // Set camera defaults + myCam.camera( + 0, + 0, + 100 / 2.0 / Math.tan(Math.PI * 30.0 / 180.0), + 0, + 0, + 0, + 0, + 1, + 0 + ); + myCam.perspective( + Math.PI / 3.0, + 1, + myCam.eyeZ / 10.0, + myCam.eyeZ * 10.0 + ); + myp5.setCamera(myCam); }); - // Test: Camera starts with correct initial values test('Camera initial values', function() { var vals = getVals(myCam); assert.closeTo(vals.ex, 0, delta); @@ -330,35 +360,29 @@ suite('p5.Camera', function() { assert.closeTo(vals.uz, 0, delta); }); - // Test: Camera rotation around a given axis (e.g., X-axis) while maintaining the up vector orientation test('Camera rotation around X-axis', function() { myCam._rotateView(90, 1, 0, 0); // Rotate by 90 degrees around X-axis var vals = getVals(myCam); - // Check that the center's Y value changes but the up vector remains consistent assert.notCloseTo(vals.cy, 0, delta); // Y center should change assert.closeTo(vals.ux, 0, delta); // X up vector should stay the same assert.closeTo(vals.uy, 0, delta); // Y up vector should change assert.closeTo(vals.uz, 1, delta); // Z up vector should stay the same }); - // Test: Camera rotation around Y-axis, checking up vector normalization test('Camera rotation around Y-axis', function() { myCam._rotateView(90, 0, 1, 0); // Rotate by 90 degrees around Y-axis var vals = getVals(myCam); - // Ensure the up vector is normalized var upLength = Math.sqrt(vals.ux * vals.ux + vals.uy * vals.uy + vals.uz * vals.uz); assert.closeTo(upLength, 1, delta); // Check if up vector is normalized to 1 }); - // Test: Rotation around the eye position test('Camera rotation around eye position', function() { myCam.eyeX = 100; myCam.eyeY = 100; myCam.eyeZ = 100; - // Rotate by 90 degrees around Z-axis myCam._rotateView(90, 0, 0, 1); var vals = getVals(myCam); @@ -367,30 +391,22 @@ suite('p5.Camera', function() { assert.notCloseTo(vals.cz, 0, delta); }); - // Edge case: No rotation test('No rotation results in no change', function() { var initialVals = getVals(myCam); - myCam._rotateView(0, 0, 0, 0); // No rotation - var finalVals = getVals(myCam); assert.deepEqual(initialVals, finalVals); // Values should remain the same }); - // Edge case: Very small rotation test('Very small rotation', function() { myCam._rotateView(0.001, 0, 1, 0); // Small rotation around Y-axis - var vals = getVals(myCam); assert.closeTo(vals.cy, 0, delta); // Y center should remain almost the same }); - // Edge case: Extreme rotation (e.g., 360 degrees) test('Extreme rotation (360 degrees)', function() { var initialVals = getVals(myCam); - myCam._rotateView(360, 0, 1, 0); // 360-degree rotation around Y-axis - var finalVals = getVals(myCam); assert.deepEqual(initialVals, finalVals); // Values should be almost identical after a full rotation }); From bd240a7a770bd19d718d62a2742fad9bb373555b Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Mon, 7 Apr 2025 00:06:31 +0100 Subject: [PATCH 09/10] Update p5.Camera.js tests --- test/unit/webgl/p5.Camera.js | 147 +++++++++-------------------------- 1 file changed, 38 insertions(+), 109 deletions(-) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index b17996213a..4c264d01e0 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -290,68 +290,11 @@ suite('p5.Camera', function() { }); suite('Camera Tilt and Up Vector', function() { - var myp5; - var myCam; - var delta = 0.001; - - // Function to retrieve the camera's parameters - var getVals = function(cam) { - return { - ex: cam.eyeX, - ey: cam.eyeY, - ez: cam.eyeZ, - cx: cam.centerX, - cy: cam.centerY, - cz: cam.centerZ, - ux: cam.upX, - uy: cam.upY, - uz: cam.upZ - }; - }; - - // Initialize the camera and set up the p5 instance - beforeAll(function() { - myp5 = new p5(function(p) { - p.setup = function() { - p.createCanvas(100, 100, p.WEBGL); - myCam = p.createCamera(); - }; - }); - }); - - afterAll(function() { - myp5.remove(); - }); - - beforeEach(function() { - myp5.angleMode(myp5.RADIANS); - - // Set camera defaults - myCam.camera( - 0, - 0, - 100 / 2.0 / Math.tan(Math.PI * 30.0 / 180.0), - 0, - 0, - 0, - 0, - 1, - 0 - ); - myCam.perspective( - Math.PI / 3.0, - 1, - myCam.eyeZ / 10.0, - myCam.eyeZ * 10.0 - ); - myp5.setCamera(myCam); - }); - - test('Camera initial values', function() { + test('Camera Tilt and Up Vector initial values', function() { var vals = getVals(myCam); assert.closeTo(vals.ex, 0, delta); assert.closeTo(vals.ey, 0, delta); - assert.closeTo(vals.ez, 500, delta); + assert.closeTo(vals.ez, 500, delta); // Ensure it starts at 500 for eyeZ assert.closeTo(vals.cx, 0, delta); assert.closeTo(vals.cy, 0, delta); assert.closeTo(vals.cz, 0, delta); @@ -359,60 +302,46 @@ suite('p5.Camera', function() { assert.closeTo(vals.uy, 1, delta); assert.closeTo(vals.uz, 0, delta); }); - - test('Camera rotation around X-axis', function() { - myCam._rotateView(90, 1, 0, 0); // Rotate by 90 degrees around X-axis - - var vals = getVals(myCam); - assert.notCloseTo(vals.cy, 0, delta); // Y center should change - assert.closeTo(vals.ux, 0, delta); // X up vector should stay the same - assert.closeTo(vals.uy, 0, delta); // Y up vector should change - assert.closeTo(vals.uz, 1, delta); // Z up vector should stay the same - }); - - test('Camera rotation around Y-axis', function() { - myCam._rotateView(90, 0, 1, 0); // Rotate by 90 degrees around Y-axis - - var vals = getVals(myCam); - var upLength = Math.sqrt(vals.ux * vals.ux + vals.uy * vals.uy + vals.uz * vals.uz); - assert.closeTo(upLength, 1, delta); // Check if up vector is normalized to 1 - }); - - test('Camera rotation around eye position', function() { - myCam.eyeX = 100; - myCam.eyeY = 100; - myCam.eyeZ = 100; - - myCam._rotateView(90, 0, 0, 1); - - var vals = getVals(myCam); - assert.notCloseTo(vals.cx, 0, delta); // The center should have changed after rotation - assert.notCloseTo(vals.cy, 0, delta); - assert.notCloseTo(vals.cz, 0, delta); - }); - - test('No rotation results in no change', function() { - var initialVals = getVals(myCam); - myCam._rotateView(0, 0, 0, 0); // No rotation - var finalVals = getVals(myCam); - assert.deepEqual(initialVals, finalVals); // Values should remain the same - }); - - test('Very small rotation', function() { - myCam._rotateView(0.001, 0, 1, 0); // Small rotation around Y-axis - var vals = getVals(myCam); - assert.closeTo(vals.cy, 0, delta); // Y center should remain almost the same + + test('Tilt() with positive parameter sets correct Matrix without changing eyeXYZ', function() { + var orig = getVals(myCam); + + var expectedMatrix = new Float32Array([ + 1, 0, 0, 0, + 0, 0.07073719799518585, -0.9974949955940247, 0, + -0, 0.9974949955940247, 0.07073719799518585, 0, + 0, -86.3855972290039, -6.126020908355713, 1 + ]); + + myCam.tilt(1.5); + + assert.deepCloseTo(myCam.cameraMatrix.mat4, expectedMatrix); + + assert.strictEqual(myCam.eyeX, orig.ex, 'eye X pos changed'); + assert.strictEqual(myCam.eyeY, orig.ey, 'eye Y pos changed'); + assert.strictEqual(myCam.eyeZ, orig.ez, 'eye Z pos changed'); }); - - test('Extreme rotation (360 degrees)', function() { - var initialVals = getVals(myCam); - myCam._rotateView(360, 0, 1, 0); // 360-degree rotation around Y-axis - var finalVals = getVals(myCam); - assert.deepEqual(initialVals, finalVals); // Values should be almost identical after a full rotation + + test('Tilt() with negative parameter sets correct matrix without changing eyeXYZ', function() { + var orig = getVals(myCam); + + var expectedMatrix = new Float32Array([ + 1, 0, 0, 0, + 0, 0.07073719799518585, 0.9974949955940247, 0, + 0, -0.9974949955940247, 0.07073719799518585, 0, + 0, 86.3855972290039, -6.126020908355713, 1 + ]); + + myCam.tilt(-1.5); + + assert.deepCloseTo(myCam.cameraMatrix.mat4, expectedMatrix); + + assert.strictEqual(myCam.eyeX, orig.ex, 'eye X pos changed'); + assert.strictEqual(myCam.eyeY, orig.ey, 'eye Y pos changed'); + assert.strictEqual(myCam.eyeZ, orig.ez, 'eye Z pos changed'); }); }); - suite('Rotation with angleMode(DEGREES)', function() { beforeEach(function() { myp5.push(); From 91d82609ff05ff153d26866bbd74f6cb9c496a2b Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Mon, 7 Apr 2025 00:14:30 +0100 Subject: [PATCH 10/10] reverted changes --- test/unit/webgl/p5.Camera.js | 53 ------------------------------------ 1 file changed, 53 deletions(-) diff --git a/test/unit/webgl/p5.Camera.js b/test/unit/webgl/p5.Camera.js index 4c264d01e0..f3e6e24850 100644 --- a/test/unit/webgl/p5.Camera.js +++ b/test/unit/webgl/p5.Camera.js @@ -289,59 +289,6 @@ suite('p5.Camera', function() { }); }); - suite('Camera Tilt and Up Vector', function() { - test('Camera Tilt and Up Vector initial values', function() { - var vals = getVals(myCam); - assert.closeTo(vals.ex, 0, delta); - assert.closeTo(vals.ey, 0, delta); - assert.closeTo(vals.ez, 500, delta); // Ensure it starts at 500 for eyeZ - assert.closeTo(vals.cx, 0, delta); - assert.closeTo(vals.cy, 0, delta); - assert.closeTo(vals.cz, 0, delta); - assert.closeTo(vals.ux, 0, delta); - assert.closeTo(vals.uy, 1, delta); - assert.closeTo(vals.uz, 0, delta); - }); - - test('Tilt() with positive parameter sets correct Matrix without changing eyeXYZ', function() { - var orig = getVals(myCam); - - var expectedMatrix = new Float32Array([ - 1, 0, 0, 0, - 0, 0.07073719799518585, -0.9974949955940247, 0, - -0, 0.9974949955940247, 0.07073719799518585, 0, - 0, -86.3855972290039, -6.126020908355713, 1 - ]); - - myCam.tilt(1.5); - - assert.deepCloseTo(myCam.cameraMatrix.mat4, expectedMatrix); - - assert.strictEqual(myCam.eyeX, orig.ex, 'eye X pos changed'); - assert.strictEqual(myCam.eyeY, orig.ey, 'eye Y pos changed'); - assert.strictEqual(myCam.eyeZ, orig.ez, 'eye Z pos changed'); - }); - - test('Tilt() with negative parameter sets correct matrix without changing eyeXYZ', function() { - var orig = getVals(myCam); - - var expectedMatrix = new Float32Array([ - 1, 0, 0, 0, - 0, 0.07073719799518585, 0.9974949955940247, 0, - 0, -0.9974949955940247, 0.07073719799518585, 0, - 0, 86.3855972290039, -6.126020908355713, 1 - ]); - - myCam.tilt(-1.5); - - assert.deepCloseTo(myCam.cameraMatrix.mat4, expectedMatrix); - - assert.strictEqual(myCam.eyeX, orig.ex, 'eye X pos changed'); - assert.strictEqual(myCam.eyeY, orig.ey, 'eye Y pos changed'); - assert.strictEqual(myCam.eyeZ, orig.ez, 'eye Z pos changed'); - }); - }); - suite('Rotation with angleMode(DEGREES)', function() { beforeEach(function() { myp5.push();