Skip to content
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

implementation of p5.Camera.set() function #6243

Merged
merged 6 commits into from
Jun 29, 2023
Merged

implementation of p5.Camera.set() function #6243

merged 6 commits into from
Jun 29, 2023

Conversation

inaridarkfox4231
Copy link
Contributor

@inaridarkfox4231 inaridarkfox4231 commented Jun 27, 2023

Implement the set() function of p5.Camera.

This function copies the argument camera information to the target camera, just like p5.Vector's set().
The information to be copied is the view matrix and its parameters, projection matrix and its parameters, and cameraType.
There is no particular meaning in transferring the cameraType, but since this information is determined in a way related to how the projection is set, I thought it would be appropriate to copy it.
If the target camera is active, the renderer's view and projection matrices are updated with the same matrices.

Resolves #6238

Example

p5.Camera.set()_DEMO

let cam, initialCam;

function setup() {
  createCanvas(400, 400, WEBGL);
  strokeWeight(3);

  // Set the initial state to initialCamera and set it to the camera
  // used for drawing. Then set cam to be the active camera.
  cam = createCamera();
  initialCam = createCamera();
  initialCam.camera(100, 100, 100, 0, 0, 0, 0, 0, -1);
  cam.set(initialCam);

  setCamera(cam);
}

function draw() {
  orbitControl();
  background(255);
  box(50);
  translate(0, 0, -25);
  plane(100);
}

function doubleClicked(){
  // Double-click to return the camera to its initial position.
  cam.set(initialCam);
}
2023-06-27.22-23-39.mp4

When double-clicked, the camera whose initial state is registered is set as the active camera by set(), and the screen state is reset. As it stands now, this requires resetting the initial state somehow. setCamera() cannot be used because the active camera will change. With this function, you can restore only the settings without changing the active camera.

PR Checklist

Implement the set() function of p5.Camera. This copies the camera view and projection information to the target camera.
If the target is active, the renderer's view and projection matrices are updated accordingly.
Compare the matrix between the camera to which the set() function is applied and the camera of the argument.
Since the applied camera is active, we also compare it with the renderer's matrix.
indent fix
I'm tired of worrying about how to write code, so I decided to write it the way I like it. I'm tired.
fix p5 to myp5
no need _curCamera
@inaridarkfox4231
Copy link
Contributor Author

I wanted to create this function because I wanted to make it easier to write code for smooth camera switching.
Check out the demo below.
p5.Camera.set_DEMO

2023-06-29.01-38-10.mp4

In this demo, 4 cameras are prepared and can be switched by pressing a button.
When switching cameras, a certain function is used so that the camera changes smoothly. I'd like to implement this function someday, but I'm using set() internally, so I'll think about it after this function is implemented.

For that we need the first and last camera, and set() is used to populate that information. However, if only the attached camera is used, the assignment operation is actually sufficient and the set() is not necessary.

This demo allows the camera to be manipulated with orbitControl() after switching cameras. Therefore, setCamera() cannot be used. This is because the information of the prepared camera will be rewritten. Also, if you use substitution when preparing the starting camera, the starting camera will change, so substitution cannot be used. So set() is used effectively. set() is very useful for treating the camera as a data-only object.

If this kind of thing can be done without using set(), we don't need this function. I tried to explain the usefulness as much as possible, but I don't know if I did it well. If the implementation is allowed, please give me a review.

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, great example too!

@davepagurek davepagurek merged commit 0729577 into processing:main Jun 29, 2023
@inaridarkfox4231 inaridarkfox4231 deleted the camera-setFunction branch June 29, 2023 15:48
@inaridarkfox4231
Copy link
Contributor Author

Thanks for review and merge! ('ω')
I have one more pull request left that I want to pass by 1.7.0, so I'll do my best...

@Qianqianye
Copy link
Contributor

Thanks @inaridarkfox4231 for working on it and @davepagurek for reviewing it!

@Qianqianye Qianqianye added this to the 1.7.0 milestone Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implementation of p5.Camera set() function
3 participants