Skip to content

Commit 06bb038

Browse files
authored
Merge pull request #661 from jonobr1/issue-659-client-surface-inconsistencies
Issue 659 Client Surface Inconsistencies
2 parents a5b9762 + 77170ef commit 06bb038

File tree

3 files changed

+86
-23
lines changed

3 files changed

+86
-23
lines changed

extras/js/zui.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,40 @@
128128

129129
}
130130

131-
clientToSurface(x, y) {
131+
clientToSurface(a, b, c) {
132132
this.updateOffset();
133133
const m = this.surfaceMatrix.inverse();
134-
const n = this.viewportOffset.matrix.inverse().multiply(x, y, 1);
135-
return m.multiply.apply(m, [n.x, n.y, n.z]);
134+
let x, y, z;
135+
if (arguments.length === 1) {
136+
const v = a;
137+
x = typeof v.x === 'number' ? v.x : 0;
138+
y = typeof v.y === 'number' ? v.y : 0;
139+
z = typeof v.z === 'number' ? v.z : 1;
140+
} else {
141+
x = typeof a === 'number' ? a : 0;
142+
y = typeof b === 'number' ? b : 0;
143+
z = typeof c === 'number' ? c : 1;
144+
}
145+
const n = this.viewportOffset.matrix.inverse().multiply(x, y, z);
146+
return m.multiply(n.x, n.y, n.z);
136147
}
137148

138-
surfaceToClient(v) {
149+
surfaceToClient(a, b, c) {
139150
this.updateOffset();
140151
const vo = this.viewportOffset.matrix.clone();
141-
const sm = this.surfaceMatrix.multiply.apply(this.surfaceMatrix, [v.x, v.y, v.z]);
142-
return vo.multiply.apply(vo, [sm.x, sm.y, sm.z]);
152+
let x, y, z;
153+
if (arguments.length === 1) {
154+
const v = a;
155+
x = typeof v.x === 'number' ? v.x : 0;
156+
y = typeof v.y === 'number' ? v.y : 0;
157+
z = typeof v.z === 'number' ? v.z : 1;
158+
} else {
159+
x = typeof a === 'number' ? a : 0;
160+
y = typeof b === 'number' ? b : 0;
161+
z = typeof c === 'number' ? c : 1;
162+
}
163+
const sm = this.surfaceMatrix.multiply(x, y, z);
164+
return vo.multiply(sm.x, sm.y, sm.z);
143165
}
144166

145167
zoomBy(byF, clientX, clientY) {

extras/jsm/zui.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,73 @@ export class ZUI {
138138
/**
139139
* @name Two.ZUI#clientToSurface
140140
* @function
141-
* @param {Number} x - The x position of the user's input.
142-
* @param {Number} y - The y position of the user's input.
143-
* @returns {Two.Vector}
144-
* @description Convert an x, y coordinate in user space into projected space.
141+
* @param {Two.Vector} a
142+
* @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.
143+
* @returns {Object} - An object with x, y, and z components
144+
* @overloaded
145145
*/
146-
clientToSurface(x, y) {
146+
147+
/**
148+
* @name Two.ZUI#clientToSurface
149+
* @param {Number} [a=0] - The x component of position to be transformed.
150+
* @param {Number} [b=0] - The y component of position to be transformed.
151+
* @param {Number} [c=1] - The optional z component of position to be transformed.
152+
* @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.
153+
* @returns {Object} - An object with x, y, and z components
154+
* @overloaded
155+
*/
156+
clientToSurface(a, b, c) {
147157
this.updateOffset();
148158
const m = this.surfaceMatrix.inverse();
149-
const n = this.viewportOffset.matrix.inverse().multiply(x, y, 1);
150-
return m.multiply.apply(m, [n.x, n.y, n.z]);
159+
let x, y, z;
160+
if (arguments.length === 1) {
161+
const v = a;
162+
x = typeof v.x === 'number' ? v.x : 0;
163+
y = typeof v.y === 'number' ? v.y : 0;
164+
z = typeof v.z === 'number' ? v.z : 1;
165+
} else {
166+
x = typeof a === 'number' ? a : 0;
167+
y = typeof b === 'number' ? b : 0;
168+
z = typeof c === 'number' ? c : 1;
169+
}
170+
const n = this.viewportOffset.matrix.inverse().multiply(x, y, z);
171+
return m.multiply(n.x, n.y, n.z);
151172
}
152173

153174
/**
154175
* @name Two.ZUI#surfaceToClient
155176
* @function
156-
* @param {Two.Vector}
157-
* @description Convert an x, y coordinate in projected space to the user's space.
177+
* @param {Two.Vector} a
178+
* @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.
179+
* @returns {Object} - An object with x, y, and z components
180+
* @overloaded
158181
*/
159-
surfaceToClient(v) {
182+
183+
/**
184+
* @name Two.ZUI#surfaceToClient
185+
* @param {Number} [a=0] - The x component of position to be transformed.
186+
* @param {Number} [b=0] - The y component of position to be transformed.
187+
* @param {Number} [c=1] - The optional z component of position to be transformed.
188+
* @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.
189+
* @returns {Object} - An object with x, y, and z components
190+
* @overloaded
191+
*/
192+
surfaceToClient(a, b, c) {
160193
this.updateOffset();
161194
const vo = this.viewportOffset.matrix.clone();
162-
const sm = this.surfaceMatrix.multiply.apply(this.surfaceMatrix, [v.x, v.y, v.z]);
163-
return vo.multiply.apply(vo, [sm.x, sm.y, sm.z]);
195+
let x, y, z;
196+
if (arguments.length === 1) {
197+
const v = a;
198+
x = typeof v.x === 'number' ? v.x : 0;
199+
y = typeof v.y === 'number' ? v.y : 0;
200+
z = typeof v.z === 'number' ? v.z : 1;
201+
} else {
202+
x = typeof a === 'number' ? a : 0;
203+
y = typeof b === 'number' ? b : 0;
204+
z = typeof c === 'number' ? c : 1;
205+
}
206+
const sm = this.surfaceMatrix.multiply(x, y, z);
207+
return vo.multiply(sm.x, sm.y, sm.z);
164208
}
165209

166210
/**

types.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,11 +4486,8 @@ declare module "two.js/extras/jsm/zui" {
44864486
surfaces: any[];
44874487
add(surface: any): ZUI;
44884488
addLimits(min: number, max: number, type?: number): ZUI;
4489-
clientToSurface(): Matrix;
4490-
clientToSurface(x: number): Matrix;
4491-
clientToSurface(x: number, y: number): { x: number, y: number, z: number };
4492-
surfaceToClient(v: {}): Matrix;
4493-
surfaceToClient(v: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
4489+
clientToSurface(v?: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
4490+
surfaceToClient(v?: { x?: number, y?: number, z?: number }): { x: number, y: number, z: number };
44944491
zoomBy(byF: any, clientX: any, clientY: any): ZUI;
44954492
zoomSet(zoom: any, clientX: any, clientY: any): ZUI;
44964493
zoom: number;

0 commit comments

Comments
 (0)