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

In p5.Camera.slerp, change the eye-center distance interpolation to be logarithmically based #6259

Merged
merged 2 commits into from Jul 9, 2023
Merged

In p5.Camera.slerp, change the eye-center distance interpolation to be logarithmically based #6259

merged 2 commits into from Jul 9, 2023

Conversation

inaridarkfox4231
Copy link
Contributor

@inaridarkfox4231 inaridarkfox4231 commented Jul 8, 2023

Resolves: #6257

Changes:

In the p5.Camera.slerp() logic, the interpolation of the distance between the viewpoint and the center used linear interpolation.
However, with this interpolation method, there are problems such as the camera breaking when it goes out of range.
Also, when applied to infinite loop animation, there is also the problem that the animation does not connect smoothly.
Furthermore, since orbitControl() changes the distance logarithmically, it is not very compatible with it.
From the above, I came to the conclusion that it is better to use logarithmic interpolation instead of linear interpolation for the distance interpolation method.
Also, along with that, added the content about distance interpolation in the unit test.

Screenshots of the change:

logarithmic interpolation

let cam,cam0,cam1;
function setup(){
  createCanvas(400, 400, WEBGL);

  cam = createCamera();
  cam.camera(200,200,200,0,0,0,0,0,-1);
  cam.perspective(PI/3,width/height,0.1,1000);
  //cam.ortho(-200,200,-200,200,0.1,1000);

  cam0 = createCamera();
  cam0.set(cam);

  cam1 = createCamera();
  cam1.set(cam);
  cam1.camera(50,50,50,0,0,0,0,0,-1);
  //cam1.ortho(-50,50,-50,50,0.1,1000);
  
  setCamera(cam);
}

function draw(){
  noStroke();
  directionalLight(color("gray"),-9,-6,-1);
  ambientLight(color("gray"));
  fill("white");
  ambientMaterial("white");
  
  background(255);
  const p = frameCount % 60 / 60;
  cam.slerp(cam0, cam1, p);
  
  for(let i=0;i<8;i++){
    translate(0,-800,0);box(800);translate(0,800,0);
    translate(-800,0,0);box(800);translate(800,0,0);
    translate(0,0,-800);box(800);translate(0,0,800);
    scale(1/4);
  }
}
2023-07-09.00-37-48.mp4

Although it is a subtle difference, slerp2 (version with specification change) is smoother.
In addition, there is almost no difference in appearance in sketches such as resetting orbitControl().

PR Checklist

Causes the interpolation of eye-to-center distances to be done logarithmically.
This is because linear interpolation will result in distances of 0 or negative numbers, and the appearance will be spoiled.
Changed unit test for ortho() component comparison to support logarithmic interpolation.
This interpolation method also makes it consistent with distance changes in _orbit() and _orbitFree(), so I've changed the unit tests accordingly.
@inaridarkfox4231
Copy link
Contributor Author

It seems fine... Please leave a review.

@inaridarkfox4231
Copy link
Contributor Author

I called logarithmic interpolation, but I don't know the official way to call it..., so I'm just calling it that way, and I don't know if it's appropriate. This means that for each value we take the logarithm in the appropriate base, linearly interpolate it, and return it in the same base.
This assumes that the ratio value is positive. For example, changes in components by orbitControl() satisfy that requirement.

@inaridarkfox4231
Copy link
Contributor Author

For final performance of p5.Camera.slerp(), in my environment it looked like this:

10000 times per frame: almost 40fps
5000 times per frame: 58~59fps

Although the performance has been degraded by rewriting it with matrix operations, I didn't think it would be a problem because it wasn't a function that would be called that many times. Readability is more important. Logarithmic interpolation slows down a little, but there is no problem because it is more convenient in various ways.

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.

Thanks for making these changes!

@davepagurek davepagurek merged commit 91cd13d into processing:main Jul 9, 2023
5 checks passed
@inaridarkfox4231
Copy link
Contributor Author

Thank you for your review and merging! ('ω')
Thank you very much for reviewing many pull requests. I would like to look forward the release of next version...

@inaridarkfox4231 inaridarkfox4231 deleted the Logarithmic-interpolation-of-distance branch July 9, 2023 15:54
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.

Change distance interpolation in p5.Camera.slerp() from linear interpolation to logarithmic interpolation
2 participants