Skip to content

Commit

Permalink
Merge pull request #661 from jonobr1/issue-659-client-surface-inconsi…
Browse files Browse the repository at this point in the history
…stencies

Issue 659 Client Surface Inconsistencies
  • Loading branch information
jonobr1 authored Aug 16, 2022
2 parents a5b9762 + 77170ef commit 06bb038
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 23 deletions.
34 changes: 28 additions & 6 deletions extras/js/zui.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,40 @@

}

clientToSurface(x, y) {
clientToSurface(a, b, c) {
this.updateOffset();
const m = this.surfaceMatrix.inverse();
const n = this.viewportOffset.matrix.inverse().multiply(x, y, 1);
return m.multiply.apply(m, [n.x, n.y, n.z]);
let x, y, z;
if (arguments.length === 1) {
const v = a;
x = typeof v.x === 'number' ? v.x : 0;
y = typeof v.y === 'number' ? v.y : 0;
z = typeof v.z === 'number' ? v.z : 1;
} else {
x = typeof a === 'number' ? a : 0;
y = typeof b === 'number' ? b : 0;
z = typeof c === 'number' ? c : 1;
}
const n = this.viewportOffset.matrix.inverse().multiply(x, y, z);
return m.multiply(n.x, n.y, n.z);
}

surfaceToClient(v) {
surfaceToClient(a, b, c) {
this.updateOffset();
const vo = this.viewportOffset.matrix.clone();
const sm = this.surfaceMatrix.multiply.apply(this.surfaceMatrix, [v.x, v.y, v.z]);
return vo.multiply.apply(vo, [sm.x, sm.y, sm.z]);
let x, y, z;
if (arguments.length === 1) {
const v = a;
x = typeof v.x === 'number' ? v.x : 0;
y = typeof v.y === 'number' ? v.y : 0;
z = typeof v.z === 'number' ? v.z : 1;
} else {
x = typeof a === 'number' ? a : 0;
y = typeof b === 'number' ? b : 0;
z = typeof c === 'number' ? c : 1;
}
const sm = this.surfaceMatrix.multiply(x, y, z);
return vo.multiply(sm.x, sm.y, sm.z);
}

zoomBy(byF, clientX, clientY) {
Expand Down
68 changes: 56 additions & 12 deletions extras/jsm/zui.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,73 @@ export class ZUI {
/**
* @name Two.ZUI#clientToSurface
* @function
* @param {Number} x - The x position of the user's input.
* @param {Number} y - The y position of the user's input.
* @returns {Two.Vector}
* @description Convert an x, y coordinate in user space into projected space.
* @param {Two.Vector} a
* @description Convert an x, y coordinate in the user’s space to the object's projected space. Optionally pass a z property on the object to apply depth.
* @returns {Object} - An object with x, y, and z components
* @overloaded
*/
clientToSurface(x, y) {

/**
* @name Two.ZUI#clientToSurface
* @param {Number} [a=0] - The x component of position to be transformed.
* @param {Number} [b=0] - The y component of position to be transformed.
* @param {Number} [c=1] - The optional z component of position to be transformed.
* @description Convert an x, y coordinate in the user’s space to the object's projected space. Optionally pass a z property on the object to apply depth.
* @returns {Object} - An object with x, y, and z components
* @overloaded
*/
clientToSurface(a, b, c) {
this.updateOffset();
const m = this.surfaceMatrix.inverse();
const n = this.viewportOffset.matrix.inverse().multiply(x, y, 1);
return m.multiply.apply(m, [n.x, n.y, n.z]);
let x, y, z;
if (arguments.length === 1) {
const v = a;
x = typeof v.x === 'number' ? v.x : 0;
y = typeof v.y === 'number' ? v.y : 0;
z = typeof v.z === 'number' ? v.z : 1;
} else {
x = typeof a === 'number' ? a : 0;
y = typeof b === 'number' ? b : 0;
z = typeof c === 'number' ? c : 1;
}
const n = this.viewportOffset.matrix.inverse().multiply(x, y, z);
return m.multiply(n.x, n.y, n.z);
}

/**
* @name Two.ZUI#surfaceToClient
* @function
* @param {Two.Vector}
* @description Convert an x, y coordinate in projected space to the user's space.
* @param {Two.Vector} a
* @description Convert an x, y coordinate in projected space to the user’s space. Optionally pass a z property on the object to apply depth.
* @returns {Object} - An object with x, y, and z components
* @overloaded
*/
surfaceToClient(v) {

/**
* @name Two.ZUI#surfaceToClient
* @param {Number} [a=0] - The x component of position to be transformed.
* @param {Number} [b=0] - The y component of position to be transformed.
* @param {Number} [c=1] - The optional z component of position to be transformed.
* @description Convert an x, y coordinate in projected space to the user’s space. Optionally pass a z property on the object to apply depth.
* @returns {Object} - An object with x, y, and z components
* @overloaded
*/
surfaceToClient(a, b, c) {
this.updateOffset();
const vo = this.viewportOffset.matrix.clone();
const sm = this.surfaceMatrix.multiply.apply(this.surfaceMatrix, [v.x, v.y, v.z]);
return vo.multiply.apply(vo, [sm.x, sm.y, sm.z]);
let x, y, z;
if (arguments.length === 1) {
const v = a;
x = typeof v.x === 'number' ? v.x : 0;
y = typeof v.y === 'number' ? v.y : 0;
z = typeof v.z === 'number' ? v.z : 1;
} else {
x = typeof a === 'number' ? a : 0;
y = typeof b === 'number' ? b : 0;
z = typeof c === 'number' ? c : 1;
}
const sm = this.surfaceMatrix.multiply(x, y, z);
return vo.multiply(sm.x, sm.y, sm.z);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4486,11 +4486,8 @@ declare module "two.js/extras/jsm/zui" {
surfaces: any[];
add(surface: any): ZUI;
addLimits(min: number, max: number, type?: number): ZUI;
clientToSurface(): Matrix;
clientToSurface(x: number): Matrix;
clientToSurface(x: number, y: number): { x: number, y: number, z: number };
surfaceToClient(v: {}): Matrix;
surfaceToClient(v: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
clientToSurface(v?: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
surfaceToClient(v?: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
zoomBy(byF: any, clientX: any, clientY: any): ZUI;
zoomSet(zoom: any, clientX: any, clientY: any): ZUI;
zoom: number;
Expand Down

0 comments on commit 06bb038

Please sign in to comment.