-
Notifications
You must be signed in to change notification settings - Fork 1.5k
ESM Scripts #5764
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
ESM Scripts #5764
Changes from 125 commits
f520c67
40c664c
fe3efec
9d24a07
274309d
4841323
e768312
2a96c0c
762d18d
f1696af
6ef4dd1
d51230a
52c3471
cb05191
02fb662
6c4c68b
e3b9d1e
9f3ff07
4e6b960
3dc7ae3
7f0955c
762caa1
1a30002
a66fe79
9e2da68
bd3e347
35c93e6
1d45873
a659d03
a56c825
99c5b1f
b9f3eee
ddd4199
24a646f
fafd773
ecc3e3d
eafe253
ee0bd96
61990a4
2cd077e
893e2bf
96e3272
ad9dd97
673aa2c
177de66
806201f
8fa2b26
d429201
0b76ad7
1392a62
22eacf3
97649b3
8244e2b
d6db607
c4049e1
e0aa409
443304a
b047e98
99cca3f
e34ca79
3639e17
ef57d6c
9e011cb
9a02061
fe34450
633533e
0c0b8cc
500e6cd
22db44d
ef4f2be
0429a3c
d1ce21f
9186e18
dad139d
e1cd169
6dd758f
b76978d
381caa5
291ba3a
230e412
0710453
bff3bb0
009ced3
9b5f29a
a1b8672
b2559d3
0c04a7f
444d967
24d1866
944a020
f48643a
4c5321b
db59d0f
c032b26
cae0aea
daeace9
47ca448
0522633
4fe02bb
c05ed1a
d0982e1
6e8bc0c
5789b07
407903a
118e376
8ea742b
84c5c67
aa2688b
9a70f07
bba2a7d
902d533
9937fa4
7c6e1cc
36cbd0f
6f55849
880579a
8f67918
3da4293
3953e8d
440a145
de693ae
1731a4b
a083eab
89eb9b5
97740a3
4bd907d
af90e32
271b76c
333606f
aff2085
fdcf4d0
70c9b88
ec3af91
8e4918f
1810d22
f12188b
9042dae
0099dc8
3f83c35
cee0c70
22cf09f
972e7c4
c4153dd
c552ee8
79ca774
026f33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// Orbit Camera Mouse Input Script // | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
const attributes = { | ||
orbitSensitivity: { | ||
type: 'number', | ||
default: 0.3, | ||
title: 'Orbit Sensitivity', | ||
description: 'How fast the camera moves around the orbit. Higher is faster' | ||
}, | ||
distanceSensitivity: { | ||
type: 'number', | ||
default: 0.15, | ||
title: 'Distance Sensitivity', | ||
description: 'How fast the camera moves in and out. Higher is faster' | ||
} | ||
}; | ||
|
||
export default class OrbitCameraInputMouse { | ||
static attributes = attributes; | ||
|
||
// initialize code called once per entity | ||
initialize() { | ||
this.orbitCamera = this.entity.esmscript.get('OrbitCamera'); | ||
|
||
if (this.orbitCamera) { | ||
|
||
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this); | ||
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this); | ||
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this); | ||
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this); | ||
|
||
// Listen to when the mouse travels out of the window | ||
window.addEventListener('mouseout', this.onMouseOut, false); | ||
|
||
} | ||
|
||
// Disabling the context menu stops the browser displaying a menu when | ||
// you right-click the page | ||
this.app.mouse.disableContextMenu(); | ||
|
||
this.lookButtonDown = false; | ||
this.panButtonDown = false; | ||
this.lastPoint = new pc.Vec2(); | ||
} | ||
|
||
// Remove the listeners so if this entity is destroyed | ||
destroy() { | ||
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onMouseDown, this); | ||
this.app.mouse.off(pc.EVENT_MOUSEUP, this.onMouseUp, this); | ||
this.app.mouse.off(pc.EVENT_MOUSEMOVE, this.onMouseMove, this); | ||
this.app.mouse.off(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this); | ||
|
||
window.removeEventListener('mouseout', this.onMouseOut, false); | ||
} | ||
|
||
|
||
static fromWorldPoint = new pc.Vec3(); | ||
|
||
static toWorldPoint = new pc.Vec3(); | ||
|
||
static worldDiff = new pc.Vec3(); | ||
|
||
|
||
pan(screenPoint) { | ||
var fromWorldPoint = OrbitCameraInputMouse.fromWorldPoint; | ||
var toWorldPoint = OrbitCameraInputMouse.toWorldPoint; | ||
var worldDiff = OrbitCameraInputMouse.worldDiff; | ||
|
||
// For panning to work at any zoom level, we use screen point to world projection | ||
// to work out how far we need to pan the pivotEntity in world space | ||
var camera = this.entity.camera; | ||
var distance = this.orbitCamera.distance; | ||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint); | ||
camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint); | ||
|
||
worldDiff.sub2(toWorldPoint, fromWorldPoint); | ||
|
||
this.orbitCamera.pivotPoint.add(worldDiff); | ||
} | ||
|
||
onMouseDown(event) { | ||
switch (event.button) { | ||
case pc.MOUSEBUTTON_LEFT: | ||
this.lookButtonDown = true; | ||
break; | ||
case pc.MOUSEBUTTON_MIDDLE: | ||
case pc.MOUSEBUTTON_RIGHT: | ||
this.panButtonDown = true; | ||
break; | ||
} | ||
} | ||
|
||
onMouseUp(event) { | ||
switch (event.button) { | ||
case pc.MOUSEBUTTON_LEFT: | ||
this.lookButtonDown = false; | ||
break; | ||
case pc.MOUSEBUTTON_MIDDLE: | ||
case pc.MOUSEBUTTON_RIGHT: | ||
this.panButtonDown = false; | ||
break; | ||
} | ||
} | ||
|
||
onMouseMove(event) { | ||
if (this.lookButtonDown) { | ||
this.orbitCamera.pitch -= event.dy * this.orbitSensitivity; | ||
this.orbitCamera.yaw -= event.dx * this.orbitSensitivity; | ||
|
||
} else if (this.panButtonDown) { | ||
this.pan(event); | ||
} | ||
|
||
this.lastPoint.set(event.x, event.y); | ||
} | ||
|
||
onMouseWheel(event) { | ||
this.orbitCamera.distance -= event.wheel * this.distanceSensitivity * (this.orbitCamera.distance * 0.1); | ||
event.event.preventDefault(); | ||
} | ||
|
||
onMouseOut(event) { | ||
this.lookButtonDown = false; | ||
this.panButtonDown = false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||||||||||
//////////////////////////////////////////////////////////////////////////////// | ||||||||||||||
// Orbit Camera Touch Input Script // | ||||||||||||||
//////////////////////////////////////////////////////////////////////////////// | ||||||||||||||
|
||||||||||||||
const attributes = { | ||||||||||||||
orbitSensitivity: { | ||||||||||||||
type: 'number', | ||||||||||||||
default: 0.3, | ||||||||||||||
title: 'Orbit Sensitivity', | ||||||||||||||
description: 'How fast the camera moves around the orbit. Higher is faster' | ||||||||||||||
}, | ||||||||||||||
distanceSensitivity: { | ||||||||||||||
type: 'number', | ||||||||||||||
default: 0.15, | ||||||||||||||
title: 'Distance Sensitivity', | ||||||||||||||
description: 'How fast the camera moves in and out. Higher is faster' | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
export default class OrbitCameraInputTouch { | ||||||||||||||
static attributes = attributes; | ||||||||||||||
|
||||||||||||||
// initialize code called once per entity | ||||||||||||||
initialize() { | ||||||||||||||
this.orbitCamera = this.entity.esmscript.get('OrbitCamera'); | ||||||||||||||
|
||||||||||||||
// Store the position of the touch so we can calculate the distance moved | ||||||||||||||
this.lastTouchPoint = new pc.Vec2(); | ||||||||||||||
this.lastPinchMidPoint = new pc.Vec2(); | ||||||||||||||
this.lastPinchDistance = 0; | ||||||||||||||
|
||||||||||||||
if (this.orbitCamera && this.app.touch) { | ||||||||||||||
// Use the same callback for the touchStart, touchEnd and touchCancel events as they | ||||||||||||||
// all do the same thing which is to deal the possible multiple touches to the screen | ||||||||||||||
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this); | ||||||||||||||
this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this); | ||||||||||||||
this.app.touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this); | ||||||||||||||
|
||||||||||||||
this.app.touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this); | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
destroy() { | ||||||||||||||
this.app.touch.off(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this); | ||||||||||||||
this.app.touch.off(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this); | ||||||||||||||
this.app.touch.off(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this); | ||||||||||||||
|
||||||||||||||
this.app.touch.off(pc.EVENT_TOUCHMOVE, this.onTouchMove, this); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
getPinchDistance(pointA, pointB) { | ||||||||||||||
// Return the distance between the two points | ||||||||||||||
var dx = pointA.x - pointB.x; | ||||||||||||||
var dy = pointA.y - pointB.y; | ||||||||||||||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
return Math.sqrt((dx * dx) + (dy * dy)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
calcMidPoint(pointA, pointB, result) { | ||||||||||||||
result.set(pointB.x - pointA.x, pointB.y - pointA.y); | ||||||||||||||
result.mulScalar(0.5); | ||||||||||||||
result.x += pointA.x; | ||||||||||||||
result.y += pointA.y; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
onTouchStartEndCancel(event) { | ||||||||||||||
// We only care about the first touch for camera rotation. As the user touches the screen, | ||||||||||||||
// we stored the current touch position | ||||||||||||||
var touches = event.touches; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
if (touches.length === 1) { | ||||||||||||||
this.lastTouchPoint.set(touches[0].x, touches[0].y); | ||||||||||||||
|
||||||||||||||
} else if (touches.length === 2) { | ||||||||||||||
// If there are 2 touches on the screen, then set the pinch distance | ||||||||||||||
this.lastPinchDistance = this.getPinchDistance(touches[0], touches[1]); | ||||||||||||||
this.calcMidPoint(touches[0], touches[1], this.lastPinchMidPoint); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
static fromWorldPoint = new pc.Vec3(); | ||||||||||||||
|
||||||||||||||
static toWorldPoint = new pc.Vec3(); | ||||||||||||||
|
||||||||||||||
static worldDiff = new pc.Vec3(); | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
pan(midPoint) { | ||||||||||||||
var fromWorldPoint = OrbitCameraInputTouch.fromWorldPoint; | ||||||||||||||
var toWorldPoint = OrbitCameraInputTouch.toWorldPoint; | ||||||||||||||
var worldDiff = OrbitCameraInputTouch.worldDiff; | ||||||||||||||
Comment on lines
+89
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
// For panning to work at any zoom level, we use screen point to world projection | ||||||||||||||
// to work out how far we need to pan the pivotEntity in world space | ||||||||||||||
var camera = this.entity.camera; | ||||||||||||||
var distance = this.orbitCamera.distance; | ||||||||||||||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint); | ||||||||||||||
camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint); | ||||||||||||||
|
||||||||||||||
worldDiff.sub2(toWorldPoint, fromWorldPoint); | ||||||||||||||
|
||||||||||||||
this.orbitCamera.pivotPoint.add(worldDiff); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
static pinchMidPoint = new pc.Vec2(); | ||||||||||||||
|
||||||||||||||
onTouchMove(event) { | ||||||||||||||
var pinchMidPoint = OrbitCameraInputTouch.pinchMidPoint; | ||||||||||||||
|
||||||||||||||
// We only care about the first touch for camera rotation. Work out the difference moved since the last event | ||||||||||||||
// and use that to update the camera target position | ||||||||||||||
var touches = event.touches; | ||||||||||||||
if (touches.length === 1) { | ||||||||||||||
var touch = touches[0]; | ||||||||||||||
Comment on lines
+114
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
this.orbitCamera.pitch -= (touch.y - this.lastTouchPoint.y) * this.orbitSensitivity; | ||||||||||||||
this.orbitCamera.yaw -= (touch.x - this.lastTouchPoint.x) * this.orbitSensitivity; | ||||||||||||||
|
||||||||||||||
this.lastTouchPoint.set(touch.x, touch.y); | ||||||||||||||
|
||||||||||||||
} else if (touches.length === 2) { | ||||||||||||||
// Calculate the difference in pinch distance since the last event | ||||||||||||||
var currentPinchDistance = this.getPinchDistance(touches[0], touches[1]); | ||||||||||||||
var diffInPinchDistance = currentPinchDistance - this.lastPinchDistance; | ||||||||||||||
this.lastPinchDistance = currentPinchDistance; | ||||||||||||||
|
||||||||||||||
this.orbitCamera.distance -= (diffInPinchDistance * this.distanceSensitivity * 0.1) * (this.orbitCamera.distance * 0.1); | ||||||||||||||
|
||||||||||||||
// Calculate pan difference | ||||||||||||||
this.calcMidPoint(touches[0], touches[1], pinchMidPoint); | ||||||||||||||
this.pan(pinchMidPoint); | ||||||||||||||
this.lastPinchMidPoint.copy(pinchMidPoint); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.