-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solves issue #4214 #6734
Solves issue #4214 #6734
Changes from 11 commits
ba0f02e
656f80c
c86235e
2e945b2
e820394
6b0957f
ceec746
69e7276
e0ed90a
f807ca7
3ea9737
6640e17
15ef523
fbd12d9
2e72d2e
517803e
9ebecea
bbb4a3f
53a6c4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -193,6 +193,78 @@ p5.prototype.perspective = function (...args) { | |||||||||
return this; | ||||||||||
}; | ||||||||||
|
||||||||||
|
||||||||||
/** | ||||||||||
* | ||||||||||
* Enable or disable perspective for lines in the WebGL renderer. | ||||||||||
* The behavior of `linePerspective()` is associated with the type of camera projection being used. | ||||||||||
* | ||||||||||
* - When using `perspective()`, which simulates realistic perspective, linePerspective | ||||||||||
* is set to `true` by default. This means that lines will be affected by the current | ||||||||||
* camera's perspective, resulting in a more natural appearance. | ||||||||||
* - When using `ortho()` or `frustum()`, which do not simulate realistic perspective, | ||||||||||
* linePerspective is set to `false` by default. In this case, lines will have a uniform | ||||||||||
* scale regardless of the camera's perspective, providing a more predictable and | ||||||||||
* consistent appearance. | ||||||||||
* - You can override the default behavior by explicitly calling `linePerspective()` after | ||||||||||
* using `perspective()`, `ortho()`, or `frustum()`. This allows you to customize the line | ||||||||||
* perspective based on your specific requirements. | ||||||||||
* | ||||||||||
* @method linePerspective | ||||||||||
* @memberof p5.prototype | ||||||||||
* @param {boolean} enable - Set to `true` to enable line perspective, `false` to disable. | ||||||||||
* @return {boolean} The boolean value representing the current state of linePerspective(). | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For other p5 methods that could either be a setter or a getter, we just do one of them in the doc comment (e.g. here, having a Lines 368 to 371 in 37d3324
Can we do the same thing here too? |
||||||||||
*<br> | ||||||||||
* @example | ||||||||||
* <div> | ||||||||||
* <code> | ||||||||||
* function setup() { | ||||||||||
* createCanvas(100, 100, WEBGL); | ||||||||||
* strokeWeight(3); | ||||||||||
* describe( | ||||||||||
* 'rotated 3D boxes have their stroke weights affected after mouse is clicked.' | ||||||||||
* ); | ||||||||||
* } | ||||||||||
* | ||||||||||
* function draw() { | ||||||||||
* background(220); | ||||||||||
* rotateY(PI/8); | ||||||||||
* rotateZ(PI/8); | ||||||||||
* translate(0, 0, 350); | ||||||||||
* for (let i = 0; i < 12; i++) { | ||||||||||
* translate(0, 0, -70); | ||||||||||
* box(30); | ||||||||||
* } | ||||||||||
* } | ||||||||||
* | ||||||||||
* function mousePressed() { | ||||||||||
* perspective(PI/12, width/height, 1, 10000); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this isn't the default camera perspective any more, when you click, both the perspective in the lines and for the overall image changes. Maybe it would show the difference more clearly if we just call |
||||||||||
* linePerspective(false); | ||||||||||
* } | ||||||||||
* </code> | ||||||||||
* </div> | ||||||||||
* | ||||||||||
* @alt | ||||||||||
* Demonstrates the dynamic control of line perspective in a 3D environment with rotating boxes. | ||||||||||
*/ | ||||||||||
|
||||||||||
p5.prototype.linePerspective = function (enable) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a second overload to this function that has no parameters and returns the current value? Similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davepagurek do you want me to validate its parameters too or this would work fine?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validating parameters would be great too! Looks good so far. |
||||||||||
p5._validateParameters('linePerspective', arguments); | ||||||||||
|
||||||||||
if (!this._renderer._curCamera) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you encounter something in testing where where this wasn't defined out of curiosity? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, actually I got this on the console initially when i didn't write this if block : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that's just happening because your setup is: function setup() {
createCanvas(400, 400);
stroke(255);
strokeWeight(2);
} Doing |
||||||||||
this._renderer._curCamera = new p5.Camera(this._renderer); | ||||||||||
} | ||||||||||
|
||||||||||
if (enable !== undefined) { | ||||||||||
// Set the line perspective if enable is provided | ||||||||||
this._renderer._curCamera.useLinePerspective = enable; | ||||||||||
} else { | ||||||||||
// If no argument is provided, return the current value | ||||||||||
return this._renderer._curCamera.useLinePerspective; | ||||||||||
} | ||||||||||
}; | ||||||||||
|
||||||||||
|
||||||||||
/** | ||||||||||
* Sets an orthographic projection for the current camera in a 3D sketch | ||||||||||
* and defines a box-shaped viewing frustum within which objects are seen. | ||||||||||
|
@@ -487,7 +559,7 @@ p5.Camera = class Camera { | |||||||||
this._renderer = renderer; | ||||||||||
|
||||||||||
this.cameraType = 'default'; | ||||||||||
|
||||||||||
this.useLinePerspective = true; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes maybe, I think it makes sense to associate the behavior of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that makes sense! we can mention in the documentation for |
||||||||||
this.cameraMatrix = new p5.Matrix(); | ||||||||||
this.projMatrix = new p5.Matrix(); | ||||||||||
this.yScale = 1; | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,7 +329,7 @@ p5.Shader = class { | |
this.setUniform('uPerspective', 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was testing out the example and realized that we still force perspective when using a default camera. Maybe we can take out this if statement and just use the else branch? |
||
} else { | ||
// strokes have uniform scale regardless of distance from camera | ||
this.setUniform('uPerspective', 0); | ||
this.setUniform('uPerspective', this._renderer._curCamera.useLinePerspective ? 1 : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
} | ||
} | ||
this.setUniform('uViewMatrix', viewMatrix.mat4); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think elsewhere we tend to use he following syntax to say that it's a global p5 method: