Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a6e2ad4
created fix for camera settings getting reset each time resizeCanvas(…
RandomGamingDev Jun 16, 2023
786934a
linting issue fix for cam-fix
RandomGamingDev Jun 16, 2023
6fdde8c
fixed the trailing whitespace problem for cam-fix commit
RandomGamingDev Jun 16, 2023
56af4e1
made it so that it copies the whole camera
RandomGamingDev Jun 17, 2023
7c70e77
fixed typo
RandomGamingDev Jun 17, 2023
351bde7
flipped equal system regarding custom type projection matrix updating
RandomGamingDev Jun 17, 2023
c41e769
wait no that wasn't a typo sorry about that
RandomGamingDev Jun 17, 2023
1b97e17
added safeguard for compatibility with NonWebGL sketches
RandomGamingDev Jun 17, 2023
32fef72
placed cam variable in higher scope
RandomGamingDev Jun 17, 2023
2151987
thanks dave & inari
RandomGamingDev Jul 13, 2023
dd00fca
lmao accidentally used git add . in the wrong dir here's the cam fix
RandomGamingDev Jul 13, 2023
685365a
removed now unnecessary overload for FrameBufferCamera
RandomGamingDev Jul 31, 2023
ad45efe
Merge branch 'processing:main' into cam-fix
RandomGamingDev Aug 17, 2023
1f5361f
Merge branch 'processing:main' into cam-fix
RandomGamingDev Sep 23, 2023
c9609ca
Merge branch 'processing:main' into cam-fix
RandomGamingDev Oct 17, 2023
409d980
made it so that functions that customize a camera set the cameraType …
RandomGamingDev Oct 25, 2023
3aad5b0
forgot to remove the extra parameters that I accidentally added
RandomGamingDev Oct 25, 2023
7804a99
Merge branch 'processing:main' into cam-fix
RandomGamingDev Oct 25, 2023
da99eb4
made it so that p5.Camera.camera() uses an optional parameter for the…
RandomGamingDev Oct 25, 2023
cdff3ea
added some changes so that default cameras only reset their fov as su…
RandomGamingDev Oct 25, 2023
6512043
removed the custom setters for cameraType since position is now longe…
RandomGamingDev Oct 25, 2023
58b3862
added the code from dave's example for keeping the camera z constant
RandomGamingDev Oct 25, 2023
055e133
removed the default cameraType setter in p5.Camera.camera()
RandomGamingDev Oct 25, 2023
9858a68
swapped the lines so that defaultEyeZ's value of 800 applies in the c…
RandomGamingDev Oct 25, 2023
627836c
removed _setDefaultCamera() and added back _computeCameraDefaultSetti…
RandomGamingDev Oct 25, 2023
7f597ca
Change order of applying perspective scale to get better fidelity joi…
davepagurek Oct 26, 2023
8487149
Update tests
davepagurek Oct 26, 2023
adf66c4
Merge pull request #1 from davepagurek/test-fixes
RandomGamingDev Oct 26, 2023
70bee38
added camera position test
RandomGamingDev Oct 26, 2023
9142e13
added rotation test and changed it so that it uses a WEBGL canvas
RandomGamingDev Oct 26, 2023
a50f4f9
added myp5. in front of WEBGL
RandomGamingDev Oct 26, 2023
85bdf92
added myp5. in front of resizeCanvas()
RandomGamingDev Oct 26, 2023
71fc134
removed a comment that I accidentally added
RandomGamingDev Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/webgl/p5.Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -1946,12 +1946,12 @@ p5.Camera = class Camera {
// @TODO: combine this function with _setDefaultCamera to compute these values
// as-needed
_computeCameraDefaultSettings() {
this.defaultCameraFOV = 60 / 180 * Math.PI;
this.defaultAspectRatio = this._renderer.width / this._renderer.height;
this.defaultEyeX = 0;
this.defaultEyeY = 0;
this.defaultEyeZ =
this._renderer.height / 2.0 / Math.tan(this.defaultCameraFOV / 2.0);
this.defaultEyeZ = 800;
this.defaultCameraFOV =
2 * Math.atan(this._renderer.height / 2 / this.defaultEyeZ);
this.defaultCenterX = 0;
this.defaultCenterY = 0;
this.defaultCenterZ = 0;
Expand Down Expand Up @@ -1986,12 +1986,9 @@ p5.Camera = class Camera {
// If we're using the default camera, update the aspect ratio
if (this.cameraType === 'default') {
this._computeCameraDefaultSettings();
this._setDefaultCamera();
} else {
this.perspective(
this.cameraFOV,
this._renderer.width / this._renderer.height
);
this.cameraFOV = this.defaultCameraFOV;
this.aspectRatio = this.defaultAspectRatio;
this.perspective();
}
}

Expand Down
26 changes: 2 additions & 24 deletions src/webgl/p5.Framebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,8 @@ class FramebufferCamera extends p5.Camera {
_computeCameraDefaultSettings() {
super._computeCameraDefaultSettings();
this.defaultAspectRatio = this.fbo.width / this.fbo.height;
this.defaultEyeZ =
this.fbo.height / 2.0 / Math.tan(this.defaultCameraFOV / 2.0);
this.defaultCameraNear = this.defaultEyeZ * 0.1;
this.defaultCameraFar = this.defaultEyeZ * 10;
}

/**
* Resets the camera to a default perspective camera sized to match
* the p5.Framebuffer it is attached to.
*
* @method resize
* @private
*/
_resize() {
// If we're using the default camera, update the aspect ratio
if (this.cameraType === 'default') {
this._computeCameraDefaultSettings();
this._setDefaultCamera();
} else {
this.perspective(
this.cameraFOV,
this.fbo.width / this.fbo.height
);
}
this.defaultCameraFOV =
2 * Math.atan(this.fbo.height / 2 / this.defaultEyeZ);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/webgl/shaders/line.vert
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ void main() {
// find where the lines intersect to find the elbow of the join
vec2 c = (posp.xy/posp.w + vec2(1.,1.)) * 0.5 * uViewport.zw;
vec2 intersection = lineIntersection(
c + (side * normalIn * uStrokeWeight / 2.) * curPerspScale,
c + (side * normalIn * uStrokeWeight / 2.),
tangentIn,
c + (side * normalOut * uStrokeWeight / 2.) * curPerspScale,
c + (side * normalOut * uStrokeWeight / 2.),
tangentOut
);
offset = (intersection - c);
Expand All @@ -187,9 +187,9 @@ void main() {
offset *= maxMag / mag;
}
} else if (sideEnum == 1.) {
offset = side * normalIn * curPerspScale * uStrokeWeight / 2.;
offset = side * normalIn * uStrokeWeight / 2.;
} else if (sideEnum == 3.) {
offset = side * normalOut * curPerspScale * uStrokeWeight / 2.;
offset = side * normalOut * uStrokeWeight / 2.;
}
}
if (uStrokeJoin == STROKE_JOIN_BEVEL) {
Expand All @@ -208,12 +208,12 @@ void main() {
// extends out from the line
float tangentOffset = abs(aSide) - 1.;
offset = (normal * normalOffset + tangent * tangentOffset) *
uStrokeWeight * 0.5 * curPerspScale;
uStrokeWeight * 0.5;
vMaxDist = uStrokeWeight / 2.;
}
vPosition = vCenter + offset / curPerspScale;
vPosition = vCenter + offset;

gl_Position.xy = p.xy + offset.xy;
gl_Position.xy = p.xy + offset.xy * curPerspScale;
gl_Position.zw = p.zw;

vColor = (uUseLineColor ? aVertexColor : uMaterialColor);
Expand Down
75 changes: 67 additions & 8 deletions test/unit/webgl/p5.Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ suite('p5.Camera', function() {

test('perspective() with no parameters specified (sets default)', function() {
var expectedMatrix = new Float32Array([
1.7320507764816284,0,0,0,
0,-1.7320507764816284,0,0,
16,0,0,0,
0,-16,0,0,
0,0,-1.0202020406723022,-1,
0,0,-17.49546241760254,0
0,0,-161.6161651611328,0
]);

myCam.perspective();
Expand Down Expand Up @@ -726,10 +726,10 @@ suite('p5.Camera', function() {

test('frustum() with no parameters specified (sets default)', function() {
var expectedMatrix = new Float32Array([
1.7320507764816284, 0, 0, 0,
0, 1.7320507764816284, 0, 0,
0, -0, -1.0202020406723022, -1,
0, 0, -17.49546241760254, 0
16,0,0,0,
0,16,0,0,
0,-0,-1.0202020406723022,-1,
0,0,-161.6161651611328,0
]);

myCam.frustum();
Expand Down Expand Up @@ -1013,4 +1013,63 @@ suite('p5.Camera', function() {
assert.deepEqual(myCam2._renderer, myp5._renderer);
});
});
});

suite('Camera attributes after resizing', function() {
test('Camera position is the same', function() {
myp5.createCanvas(1, 1, myp5.WEBGL);
myp5.noStroke();
myp5.pixelDensity(1);

let cam = myp5.createCamera();

myp5.fill(255, 0, 0);

const testShape = () => {
myp5.clear();
myp5.rect(-myp5.width, -myp5.height, myp5.width * 2, myp5.height * 2);
};

testShape();
assert.deepEqual(myp5.get(0, 0), [255, 0, 0, 255]);
assert.equal(cam.eyeX, 0);

cam.move(10, 0, 0);
testShape();
assert.deepEqual(myp5.get(0, 0), [0, 0, 0, 0]);
assert.equal(cam.eyeX, 10);

myp5.resizeCanvas(2, 1);
myp5.resizeCanvas(1, 1);
testShape();
assert.deepEqual(myp5.get(0, 0), [0, 0, 0, 0]);
assert.equal(cam.eyeX, 10);
});

test('Camera rotation is the same', function() {
myp5.createCanvas(1, 1, myp5.WEBGL);
myp5.noStroke();
myp5.pixelDensity(1);

let cam = myp5.createCamera();

myp5.fill(255, 0, 0);

const testShape = () => {
myp5.clear();
myp5.rect(-myp5.width, -myp5.height, myp5.width * 2, myp5.height * 2);
};

testShape();
assert.deepEqual(myp5.get(0, 0), [255, 0, 0, 255]);

cam.pan(10);
testShape();
assert.deepEqual(myp5.get(0, 0), [0, 0, 0, 0]);

myp5.resizeCanvas(2, 1);
myp5.resizeCanvas(1, 1);
testShape();
assert.deepEqual(myp5.get(0, 0), [0, 0, 0, 0]);
});
});
});
15 changes: 11 additions & 4 deletions test/unit/webgl/p5.Framebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ suite('p5.Framebuffer', function() {
myp5.plane(myp5.width, -myp5.height);

// Just check the red channel, other channels might vary across browsers
assert.equal(myp5.get(5, 5)[0], 221);
assert.equal(myp5.get(5, 5)[0], 232);
});
});

Expand Down Expand Up @@ -287,7 +287,11 @@ suite('p5.Framebuffer', function() {
suite('the default camera', function() {
test('it uses the aspect ratio of the framebuffer', function() {
expect(fbo.defaultCamera.aspectRatio).to.equal(5 / 15);
const z = -fbo.height / 2.0 / Math.tan(Math.PI / 3 / 2);
expect(fbo.defaultCamera.cameraFOV).to.be.closeTo(
2.0 * Math.atan(15 / 2 / 800),
0.01
);
const z = -800;
const expectedCameraMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
Expand All @@ -303,8 +307,11 @@ suite('p5.Framebuffer', function() {
test('it updates the aspect ratio after resizing', function() {
fbo.resize(20, 10);
expect(fbo.defaultCamera.aspectRatio).to.equal(2);

const z = -fbo.height / 2.0 / Math.tan(Math.PI / 3 / 2);
expect(fbo.defaultCamera.cameraFOV).to.be.closeTo(
2.0 * Math.atan(10 / 2 / 800),
0.01
);
const z = -800;
const expectedCameraMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
Expand Down