Skip to content

Commit

Permalink
Added view size to camera calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsmalm committed Mar 1, 2021
1 parent 0b6fdb3 commit b392e63
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/camera/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export class Camera extends Container3D {
* @param y Screen y coordinate.
* @param distance Distance from the camera.
* @param point Point to set.
* @param viewSize The size of the view when not rendering to the entire screen.
*/
screenToWorld(x: number, y: number, distance: number, point = new ObservablePoint3D(() => { }, undefined)) {
screenToWorld(x: number, y: number, distance: number, point = new ObservablePoint3D(() => { }, undefined), viewSize: { width: number, height: number } = this.renderer) {
// Make sure the transform is updated in case something has been changed,
// otherwise it may be using wrong values.
this.transform.updateTransform()
Expand All @@ -108,7 +109,7 @@ export class Camera extends Container3D {
return
}
let clipSpace = Vec4.set(
(x / this.renderer.width) * 2 - 1, ((y / this.renderer.height) * 2 - 1) * -1, 1, 1, vec4
(x / viewSize.width) * 2 - 1, ((y / viewSize.height) * 2 - 1) * -1, 1, 1, vec4
)
this.far = far

Expand All @@ -126,8 +127,9 @@ export class Camera extends Container3D {
* @param y World y coordinate.
* @param z World z coordinate.
* @param point Point to set.
* @param viewSize The size of the view when not rendering to the entire screen.
*/
worldToScreen(x: number, y: number, z: number, point = new PIXI.Point()) {
worldToScreen(x: number, y: number, z: number, point = new PIXI.Point(), viewSize: { width: number, height: number } = this.renderer) {
// Make sure the transform is updated in case something has been changed,
// otherwise it may be using wrong values.
this.transform.updateTransform()
Expand All @@ -141,9 +143,8 @@ export class Camera extends Container3D {
clipSpace[i] /= clipSpace[3]
}
}
let { width, height } = this.renderer
return point.set((clipSpace[0] + 1) / 2 * width,
height - (clipSpace[1] + 1) / 2 * height)
return point.set((clipSpace[0] + 1) / 2 * viewSize.width,
viewSize.height - (clipSpace[1] + 1) / 2 * viewSize.height)
}

private _fieldOfView = 60
Expand Down

0 comments on commit b392e63

Please sign in to comment.