Repository navigation
Expand file tree
/
Copy pathsitClient.js
More file actions
908 lines (684 loc) · 29.7 KB
/
Copy pathsitClient.js
File metadata and controls
908 lines (684 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//
// sitClient.js
//
// Updated by Robin Wilson 1/17/2019
// Modified by Robin Wilson, Rebecca Stankus, and Alexia Mandeville June 2018
// Originally created by Clement Brisset on 3/3/17
//
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Allows avatars to sit in High Fidelity after clicking on an overlay. Works with sitServer.js.
//
// Configurable variables are:
// - SHOW_PRESIT_OVERLAY_IN_HMD
// - CHAIR_OFFSET_RATIO
//
/* globals Entities, Script, AnimationCache, Settings, MyAvatar, DriveKeys, AvatarList,
Vec3, HMD, Overlays, Camera, isInEditMode */
(function () {
// #region CONFIGURABLE VARIABLES
// true - shows "Face Forward Overlay" before sit; false - sits immediately with no overlay
var SHOW_PRESIT_OVERLAY_IN_HMD = true;
// Used to calculate pin hip position. Adds CHAIR_OFFSET_RATIO * chair's y dimension to the y center of the seat.
// Avatar sit position y = CHAIR_OFFSET_RATIO * height of chair
var CHAIR_OFFSET_RATIO = 0.1;
// Debug statements
var SITTING_DEBUG = false; // turn on sitting debug print statements
var OVERLAY_DEBUG = false; // turn on overlay debug print statements
// #endregion CONFIGURABLE VARIABLES
// #region DEPENDENCIES
Script.include("/~/system/libraries/utils.js");
// #endregion DEPENDENCIES
// #region SITTABLE OVERLAY - Overlay on chair "Click to Sit" or "Trigger to Sit"
// Constants
var OVERLAY_SITTABLE_FADE_MS = 50; // "Click/Trigger to Sit" overlay fade after 50 ms
var OVERLAY_SITTABLE_DISTANCE_SHOW_M = 3; // m
var OVERLAY_SITTABLE_DISTANCE_MAX_M = 5; // m
var OVERLAY_SITTABLE_START_ALPHA = 0.7;
var OVERLAY_SITTABLE_MIN_END_ALPHA = 0.075; // fades to this alpha value
var OVERLAY_URL_SITTABLE_HMD = Script.resolvePath("./resources/images/triggerToSit.png");
var OVERLAY_URL_SITTABLE_DESKTOP = Script.resolvePath("./resources/images/clickToSit.png");
// Dynamic variables
var overlaySittable = null;
var intervalSittableTransparency = null;
// Create sittable overlay
function sittableCreate() {
if (OVERLAY_DEBUG) {
print("SittableCreate");
}
setChairProperties();
// change the image based on what modality the user is in
var url = HMD.active
? OVERLAY_URL_SITTABLE_HMD
: OVERLAY_URL_SITTABLE_DESKTOP;
var overlayPosition = Vec3.sum(chairProperties.position, { x: 0, y: 0.01, z: 0 });
overlaySittable = Overlays.addOverlay("image3d", {
position: overlayPosition,
rotation: Quat.multiply(chairProperties.rotation, Quat.fromVec3Degrees({ x: -90, y: 180, z: 0 })),
dimensions: {
x: 0.3,
y: 0.3
},
url: url,
ignoreRayIntersection: false,
alpha: OVERLAY_SITTABLE_START_ALPHA,
visible: true,
emissive: true
});
HMD.displayModeChanged.connect(sittableSwitchHMDToDesktopText);
sittableLerpTransparency();
}
// Fade's the sittable overlay over time
function sittableLerpTransparency() {
if (OVERLAY_DEBUG) {
print("sittableLerpTransparency");
}
var startAlpha = OVERLAY_SITTABLE_START_ALPHA;
var changeAlpha = 0.01;
intervalSittableTransparency = Script.setInterval(function () {
startAlpha = startAlpha - changeAlpha; // My new alpha
Overlays.editOverlay(overlaySittable, { alpha: startAlpha });
if (startAlpha <= OVERLAY_SITTABLE_MIN_END_ALPHA) {
// Stop fading and keep overlay at the minimum alpha
Script.clearInterval(intervalSittableTransparency);
intervalSittableTransparency = null;
}
}, OVERLAY_SITTABLE_FADE_MS);
}
// Switch sittable overlay text from desktop mode "Click to Sit" to HMD mode "Trigger to Sit"
function sittableSwitchHMDToDesktopText() {
if (overlaySittable) {
var url = HMD.active
? OVERLAY_URL_SITTABLE_HMD
: OVERLAY_URL_SITTABLE_DESKTOP;
Overlays.editOverlay(overlaySittable, { url: url });
}
}
// Remove sittable overlay if it exists
function sittableRemove() {
if (OVERLAY_DEBUG) {
print("sittableRemove");
}
if (overlaySittable) {
Overlays.deleteOverlay(overlaySittable);
overlaySittable = null;
if (intervalSittableTransparency) {
Script.clearInterval(intervalSittableTransparency);
intervalSittableTransparency = null;
}
HMD.displayModeChanged.disconnect(sittableSwitchHMDToDesktopText);
}
}
// Show or remove sittable overlay
// Show overlays when I'm close to the seat
function sittableShowOrRemove() {
var ableToSit = canSit();
if (isInEditMode() || !ableToSit || overlayPreSit) {
if (OVERLAY_DEBUG) {
print("sittableShowOrRemove calling sittableRemove");
}
sittableRemove();
} else if (ableToSit && !overlaySittable && !overlayPreSit && shouldShowSittable()) {
if (OVERLAY_DEBUG) {
print("sittableShowOrRemove calling sittableCreate");
}
// Make an overlay if there isn't one
sittableCreate();
}
}
// Is avatar in range with available seat?
// Return boolean to whether sittable overlay should be shown
function shouldShowSittable() {
var seatPosition = chairProperties.position;
var distanceFromSeat = Vec3.distance(MyAvatar.position, seatPosition);
return (distanceFromSeat < OVERLAY_SITTABLE_DISTANCE_SHOW_M && !isAvatarSittingInSeat() && !overlayPreSit);
}
// #endregion SITTABLE OVERLAY
// #region PRESIT OVERLAY - Overlay shown in HMD before sitting and after clicking sittable overlay
// Has the sitting animation and "Please Face Forward"
// Constants
var OVERLAY_PRESIT_FRAME_DURATION = 160; // ms time duration for HMD presit overlay
var OVERLAY_PRESIT_URL_ROOT = "./resources/images/presit/sitConfirm";
var OVERLAY_PRESIT_URL_POSTFIX = ".png";
var OVERLAY_PRESIT_URL_NUM = 12;
var OVERLAY_PRESIT_URL_TEXT = Script.resolvePath("./resources/images/presit/pleaseFaceForward.png");
// Dynamic variables
var overlayPreSit = null;
var overlayPreSitText = null;
var overlayPreSitLoaded = [];
var overlayPreSitTextLoaded = null;
var intervalUpdatePreSitImage = null;
var preSitLoadIndex = 0;
// Prefetch all presit overlay images into user's client
function prefetchPresitOverlayImages() {
var str;
for (var i = 0; i < OVERLAY_PRESIT_URL_NUM; i++) {
str = i + 1;
overlayPreSitLoaded[i] =
TextureCache.prefetch(Script.resolvePath(OVERLAY_PRESIT_URL_ROOT + str + OVERLAY_PRESIT_URL_POSTFIX));
}
overlayPreSitTextLoaded = TextureCache.prefetch(OVERLAY_PRESIT_URL_TEXT);
}
function preSitCreate() {
if (overlayPreSit) {
return;
}
overlayPreSit = Overlays.addOverlay(
"image3d",
getInFrontOverlayProperties(
{ x: 0, y: 0.1, z: -1 },
{ x: 0.2, y: 0.2 },
overlayPreSitLoaded[preSitLoadIndex].url
)
);
overlayPreSitText = Overlays.addOverlay(
"image3d",
getInFrontOverlayProperties(
{ x: 0, y: -0.05, z: -1 },
{ x: 0.425, y: 0.425 },
overlayPreSitTextLoaded.url
)
);
// Flash through the presit animation images via overlay for a smooth avatar sitting animation
intervalUpdatePreSitImage = Script.setInterval(function () {
if (overlayPreSit) {
if (preSitLoadIndex > overlayPreSitLoaded.length) {
preSitEndUpdateInterval();
}
preSitLoadIndex = preSitLoadIndex + 1;
Overlays.editOverlay(overlayPreSit, { url: overlayPreSitLoaded[preSitLoadIndex].url });
}
}, OVERLAY_PRESIT_FRAME_DURATION);
}
function preSitEndUpdateInterval() {
if (intervalUpdatePreSitImage) {
Script.clearInterval(intervalUpdatePreSitImage);
intervalUpdatePreSitImage = null;
}
}
function preSitRemove() {
if (overlayPreSit) {
preSitEndUpdateInterval();
Overlays.deleteOverlay(overlayPreSit);
overlayPreSit = null;
preSitLoadIndex = 0;
if (overlayPreSitText !== null) {
Overlays.deleteOverlay(overlayPreSitText);
}
}
}
// #endregion PRESIT OVERLAY
// #region STAND UP OVERLAY - Overlay shown when holding a drive key after sitting
var OVERLAY_URL_STANDUP = Script.resolvePath("./resources/images/holdToStandUp.png");
var overlayStandUp = null;
function standUpCreate() {
if (!overlayStandUp) {
overlayStandUp = Overlays.addOverlay(
"image3d",
getInFrontOverlayProperties(
{ x: 0, y: 0, z: -1 },
{ x: 0.2, y: 0.2 },
OVERLAY_URL_STANDUP
)
);
}
}
function standUpRemove() {
if (overlayStandUp) {
Overlays.deleteOverlay(overlayStandUp);
overlayStandUp = null;
}
}
// #endregion STAND UP OVERLAY
// #region CREATE MODE OVERLAY
// alpha value change during edit mode
var MINIMUM_ALPHA = 0.3; // 50% alpha value
var OVERLAY_ALPHA = 0.1; // 10% alpha value for createModeOverlay
var CREATE_OVERLAY_DIMENSIONS_OFFSET = 0.02; // add 0.02 m to the sides of the cube to avoid z fighting
var checkAlpha = false; // false if chair alpha is > MINIMUM_ALPHA, true if chair alpha is < MINIMUM_ALPHA
var createModeOverlay = null; // overlay id for create mode overlay
// Creates an overlay if the user is in create mode
// Enabled only if the chair alpha value is <= MINIMUM_ALPHA
function checkOrCreateCreateModeOverlay() {
if (!checkAlpha) {
return;
}
// check Alpha is enabled
if (checkAlpha) {
// is in Edit mode && alpha value has not changed
if (isInEditMode() && !createModeOverlay) {
setChairProperties();
var position = chairProperties.position;
var registrationPoint = chairProperties.registrationPoint;
var dimensions = chairProperties.dimensions;
var rotation = chairProperties.rotation;
// Local position relative to cube
// And adjust for registrationPoint to match the cube exactly
var localOffset = {
x: NEG_ONE * (registrationPoint.x - HALF) * dimensions.x,
y: NEG_ONE * (registrationPoint.y - HALF) * dimensions.y,
z: NEG_ONE * (registrationPoint.z - HALF) * dimensions.z
};
var worldOffset = Vec3.multiplyQbyV(rotation, localOffset);
var worldPosition = Vec3.sum(position, worldOffset);
// Create visible cube
createModeOverlay = Overlays.addOverlay("cube", {
position: {
x: worldPosition.x,
y: worldPosition.y,
z: worldPosition.z
},
rotation: rotation,
dimensions: {
x: dimensions.x + CREATE_OVERLAY_DIMENSIONS_OFFSET,
y: dimensions.y - CREATE_OVERLAY_DIMENSIONS_OFFSET * HALF, // Able to select from top in HMD create mode
z: dimensions.z + CREATE_OVERLAY_DIMENSIONS_OFFSET
},
solid: true,
alpha: OVERLAY_ALPHA,
parentID: entityID
});
}
// Is in Edit mode && alpha value has changed
if (!isInEditMode() && createModeOverlay) {
// Set alpha back to 0
if (createModeOverlay) {
Overlays.deleteOverlay(createModeOverlay);
createModeOverlay = null;
}
}
}
}
// #endregion CREATE MODE OVERLAY
// #region UTILS
// Constants
var SITTING_SEARCH_RADIUS = 0.01; // m to check if avatar is sitting in chair
var HALF = 0.5;
var NEG_ONE = -1;
// Dynamic variables
var chairProperties = null;
// Is another avatar sitting in the chair?
function isAvatarSittingInSeat() {
// Used canSit()
var nearbyAvatars = AvatarList.getAvatarsInRange(seatCenterPosition, SITTING_SEARCH_RADIUS);
if (nearbyAvatars.length === 0) {
// chair is empty
return null;
} else {
return nearbyAvatars[0];
}
}
// String utility
function startsWith(str, searchString, position) {
position = position || 0;
return str.substr(position, searchString.length) === searchString;
}
function canSit() {
if (OVERLAY_DEBUG) {
print("Sittable overlay id is: ", overlaySittable);
}
if (!chairProperties) {
setChairProperties();
}
var distanceFromSeat = Vec3.distance(MyAvatar.position, chairProperties.position);
var isWithinSitDistance = distanceFromSeat < OVERLAY_SITTABLE_DISTANCE_MAX_M;
var isOpenSeat = !isAvatarSittingInSeat();
if (SITTING_DEBUG) {
print("canSit(): ", isWithinSitDistance, isOpenSeat);
}
return isWithinSitDistance && isOpenSeat;
}
function rolesToOverride() {
// Get all animation roles that sit will override
return MyAvatar.getAnimationRoles().filter(function (role) {
return !(startsWith(role, "right") || startsWith(role, "left"));
});
}
// Overlay properties in front of Camera
// Arguments: position in front of Camera, overlay dimensions, overlay image url
function getInFrontOverlayProperties(positionInFront, dimensions, url) {
var index = MyAvatar.getJointIndex("Head");
return {
position: Vec3.sum(Camera.position, Vec3.multiplyQbyV(Camera.orientation, positionInFront)),
rotation: Camera.orientation,
parentID: MyAvatar.sessionUUID,
parentJointIndex: index,
dimensions: dimensions,
url: url,
ignoreRayIntersection: false,
drawInFront: true,
visible: true,
emissive: true
};
}
function setChairProperties() {
chairProperties = Entities.getEntityProperties(
entityID,
[
"dimensions",
"registrationPoint",
"position",
"rotation",
"alpha"
]
);
}
// #endregion UTILS
// #region SIT ACTIONS
// Constants
var ANIMATION_URL = Script.resolvePath("https://hifi-content.s3.amazonaws.com/howell/Prototypes/Idle_app/mat_laying.fbx");
var ANIMATION_FPS = 30;
var ANIMATION_FIRST_FRAME = 1;
var ANIMATION_LAST_FRAME = 350;
var SIT_SETTLE_TIME_MS = 350; // ms
var CHAIR_DISMOUNT_OFFSET_M = -0.5; // m in front of chair
var SETTING_KEY_AVATAR_SITTING = "com.highfidelity.avatar.isSitting";
var STANDUP_DELAY_MS = 25; // ms for timeout in standup
var SIT_DELAY_MS = 50; // ms for timeouts in sit
var STANDUP_DISTANCE_M = 0.5; // m
var OVERRIDDEN_DRIVE_KEYS = [
DriveKeys.TRANSLATE_X,
DriveKeys.TRANSLATE_Y,
DriveKeys.TRANSLATE_Z,
DriveKeys.STEP_TRANSLATE_X,
DriveKeys.STEP_TRANSLATE_Y,
DriveKeys.STEP_TRANSLATE_Z
];
// Dynamic variables
var sittingDown = false;
var canStand = false;
var seatCenterPosition = null;
var lockChairOnStandUp = null;
// User can click on overlay to sit down
function mousePressOnOverlay(overlayID, pointerEvent) {
if (overlayID === overlaySittable && pointerEvent.isLeftButton) {
// Server checks if seat is occupied
// if not occupied will call startSitDown()
Entities.callEntityServerMethod(
entityID,
"onSitDown",
[MyAvatar.sessionUUID]
);
}
}
function sitAndPinAvatar() {
MyAvatar.characterControllerEnabled = false;
MyAvatar.hmdLeanRecenterEnabled = false;
var roles = rolesToOverride();
for (i in roles) { // restore roles to prevent overlap
MyAvatar.restoreRoleAnimation(roles[i]);
MyAvatar.overrideRoleAnimation(roles[i], ANIMATION_URL, ANIMATION_FPS, true,
ANIMATION_FIRST_FRAME, ANIMATION_LAST_FRAME);
}
for (var i in OVERRIDDEN_DRIVE_KEYS) {
MyAvatar.disableDriveKey(OVERRIDDEN_DRIVE_KEYS[i]);
}
MyAvatar.centerBody();
var index = MyAvatar.getJointIndex("Hips");
Script.setTimeout(function () {
if (HMD.active && SHOW_PRESIT_OVERLAY_IN_HMD) {
preSitRemove();
}
sittingDown = true;
canStand = true;
sitDownSettlePeriod = Date.now() + SIT_SETTLE_TIME_MS;
MyAvatar.pinJoint(index, seatCenterPosition, chairProperties.rotation);
Script.update.connect(update);
MyAvatar.scaleChanged.connect(standUp);
MyAvatar.onLoadComplete.connect(standUp);
location.hostChanged.connect(standUp);
Script.scriptEnding.connect(standUp);
}, SIT_DELAY_MS);
}
function sitDown() {
// Remove sittable overlay from chair
sittableRemove();
// Set isSitting value in Settings
Settings.setValue(SETTING_KEY_AVATAR_SITTING, entityID);
// Set the value of head to hips distance
// If avatar deviates outside of the minimum and maximum, the avatar will pop out of the chair
setHeadToHipsDistance();
// Chair does not move when the avatar is about to sit
lockChairOnStandUp = Entities.getEntityProperties(entityID, 'locked').locked;
Entities.editEntity(entityID, { locked: true });
// Get the place where
calculatePinHipPosition();
Script.setTimeout(function () {
// In HMD mode show presit overlay
if (HMD.active && SHOW_PRESIT_OVERLAY_IN_HMD) {
preSitCreate();
Script.setTimeout(function () {
sitAndPinAvatar();
preSitEndUpdateInterval();
}, OVERLAY_PRESIT_FRAME_DURATION * overlayPreSitLoaded.length);
} else {
// No presit overlay in desktop mode
sitAndPinAvatar();
}
}, SIT_DELAY_MS);
// Checks for Avatar Spine Error
function setHeadToHipsDistance() {
// get hips world pos while sitting
if ((MyAvatar.getJointIndex("Head") === NEG_ONE) || (MyAvatar.getJointIndex("Hips") === NEG_ONE)) {
// this can probably be adjusted to be more accurate
headToHipsDistance = MyAvatar.getHeight() * HALF;
} else {
var headIndex = MyAvatar.getJointIndex("Head");
var hipsIndex = MyAvatar.getJointIndex("Hips");
var headTranslation = MyAvatar.getAbsoluteDefaultJointTranslationInObjectFrame(headIndex);
var hipsTranslation = MyAvatar.getAbsoluteDefaultJointTranslationInObjectFrame(hipsIndex);
headToHipsDistance = Math.abs(headTranslation.y - hipsTranslation.y);
}
}
// Calculates where the avatar's hips will seat
function calculatePinHipPosition() {
if (!chairProperties) {
setChairProperties();
}
// get chairProperties to calculate middle of chair
seatCenterPosition = chairProperties.position;
var yOffset = chairProperties.dimensions.y * CHAIR_OFFSET_RATIO;
seatCenterPosition = {
x: chairProperties.position.x,
y: chairProperties.position.y + yOffset,
z: chairProperties.position.z
};
}
}
function standUp() {
var avatarDistance = Vec3.distance(MyAvatar.position, seatCenterPosition);
Script.setTimeout(function () {
// Move avatar in front of the chair to avoid getting stuck in collision hulls
if (avatarDistance < STANDUP_DISTANCE_M) {
var index = MyAvatar.getJointIndex("Hips");
MyAvatar.clearPinOnJoint(index);
var offset = {
x: 0,
y: 1.0,
z: CHAIR_DISMOUNT_OFFSET_M - chairProperties.dimensions.z * chairProperties.registrationPoint.z
};
var position = Vec3.sum(chairProperties.position, Vec3.multiplyQbyV(chairProperties.rotation, offset));
MyAvatar.position = position;
}
}, SIT_DELAY_MS);
if (!lockChairOnStandUp) {
Entities.editEntity(entityID, { locked: false });
}
driveKeyPressedStart = null;
canStand = false;
sitDownSettlePeriod = null;
sittingDown = false;
standUpRemove();
Entities.callEntityServerMethod(entityID, "onStandUp");
if (Settings.getValue(SETTING_KEY_AVATAR_SITTING) === entityID) {
// Check if avatar is getting out of this chair
Settings.setValue(SETTING_KEY_AVATAR_SITTING, "");
for (var i in OVERRIDDEN_DRIVE_KEYS) {
MyAvatar.enableDriveKey(OVERRIDDEN_DRIVE_KEYS[i]);
}
var roles = rolesToOverride();
for (i in roles) {
MyAvatar.restoreRoleAnimation(roles[i]);
}
MyAvatar.characterControllerEnabled = true;
MyAvatar.hmdLeanRecenterEnabled = true;
Script.setTimeout(function () {
MyAvatar.centerBody();
}, STANDUP_DELAY_MS);
} else {
// Avatar switched to another chair, do not reapply the animation roles
}
Script.update.disconnect(update);
MyAvatar.scaleChanged.disconnect(standUp);
MyAvatar.onLoadComplete.disconnect(standUp);
location.hostChanged.disconnect(standUp);
Script.scriptEnding.disconnect(standUp);
}
// #endregion SIT ACTIONS
// #region UPDATE
// Constants
var DISTANCE_FROM_CHAIR_TO_STAND_AT_M = 0.5;
var MAX_HEAD_DEVIATION_RATIO = 1.2;
var MIN_HEAD_DEVIATION_RATIO = 0.8;
var DRIVE_KEY_RELEASE_TIME_MS = 800; // ms
// Dynamic variables
var sitDownSettlePeriod = null;
var driveKeyPressedStart = null;
var deviationTimeStart = null;
var headToHipsDistance = null;
function update(dt) {
var hasAvatarSpineError = false;
var hasHeldDriveKey = false;
var hasAvatarMovedTooFar = false;
if (sittingDown === true && canStand) {
sittableRemove();
var now = Date.now();
// ACTIVE DRIVE KEY
// Check if a drive key is pressed
var hasActiveDriveKey = false;
for (var i in OVERRIDDEN_DRIVE_KEYS) {
if (MyAvatar.getRawDriveKey(OVERRIDDEN_DRIVE_KEYS[i]) !== 0.0) {
hasActiveDriveKey = true;
if (driveKeyPressedStart === null) {
driveKeyPressedStart = now;
}
if (overlayStandUp === null) {
standUpCreate();
}
break;
}
}
// Only standup if user has been pushing a drive key for DRIVE_KEY_RELEASE_TIME_MS
if (hasActiveDriveKey) {
if (driveKeyPressedStart !== null) {
var elapsed = now - driveKeyPressedStart;
hasHeldDriveKey = elapsed > DRIVE_KEY_RELEASE_TIME_MS;
}
} else {
if (overlayStandUp) {
driveKeyPressedStart = null;
standUpRemove();
}
}
// AVATAR DISTANCE
var avatarDistance = Vec3.distance(MyAvatar.position, seatCenterPosition);
if (avatarDistance > DISTANCE_FROM_CHAIR_TO_STAND_AT_M && sitDownSettlePeriod && now > sitDownSettlePeriod) {
hasAvatarMovedTooFar = true;
}
// AVATAR SPINE ERROR
if (HMD.active) {
var headWorldPosition;
// If head is more than certain distance from hips or less than certain distance, stand up
var headPoseValue = Controller.getPoseValue(Controller.Standard.Head);
headWorldPosition = Vec3.sum(Vec3.multiplyQbyV(MyAvatar.orientation, headPoseValue.translation),
MyAvatar.position);
var headDeviation = Vec3.distance(headWorldPosition, seatCenterPosition);
var headDeviationRatio = headDeviation / headToHipsDistance;
if ((headDeviationRatio > MAX_HEAD_DEVIATION_RATIO || headDeviationRatio < MIN_HEAD_DEVIATION_RATIO)
&& sitDownSettlePeriod && now > sitDownSettlePeriod) {
if (deviationTimeStart === null) {
deviationTimeStart = now;
}
var deviationTimeToStand = 1000;
if (now - deviationTimeStart > deviationTimeToStand) {
hasAvatarSpineError = true;
deviationTimeStart = null;
}
}
}
if (SITTING_DEBUG) {
print("update standup conditionals: ", hasAvatarSpineError, hasHeldDriveKey, hasAvatarMovedTooFar);
}
if (hasAvatarSpineError || hasHeldDriveKey || hasAvatarMovedTooFar) {
standUp();
}
}
}
// #endregion UPDATE
// #region ENTITY METHODS
// Constants
var ENCOUNTERED_CHAIR_CHECK_INTERVAL = 500; // ms
// Dynamic variables
var entityID = null;
var intervalEncounteredChairCheck = null;
function SitClient() {}
SitClient.prototype = {
remotelyCallable: [
"startSitDown",
"check"
],
preload: function (id) {
entityID = id;
prefetchAnimation();
prefetchPresitOverlayImages();
setChairProperties();
// Set if chair alpha value is small enough to enable the create mode overlay
checkAlpha = chairProperties.alpha <= MINIMUM_ALPHA;
Overlays.mousePressOnOverlay.connect(mousePressOnOverlay);
// Check if we've encountered a chair
intervalEncounteredChairCheck = Script.setInterval(function () {
sittableShowOrRemove();
checkOrCreateCreateModeOverlay();
}, ENCOUNTERED_CHAIR_CHECK_INTERVAL);
function prefetchAnimation() {
AnimationCache.prefetch(ANIMATION_URL);
}
},
// Called by entity server script
// To ensure only 1 avatar sits in chair at a time
startSitDown: function (id, param) {
sitDown();
},
// Called by entity server script to ensure client is still sitting in chair
// If client does not call "checkResolved" the entity server script will allow other avatars to sit
check: function () {
Entities.callEntityServerMethod(entityID, "checkResolved");
},
unload: function () {
if (Settings.getValue(SETTING_KEY_AVATAR_SITTING) === entityID) {
standUp();
}
Entities.callEntityServerMethod(entityID, "onStandUp");
if (intervalSittableTransparency) {
Script.clearInterval(intervalSittableTransparency);
intervalSittableTransparency = null;
}
if (intervalEncounteredChairCheck) {
Script.clearInterval(intervalEncounteredChairCheck);
intervalEncounteredChairCheck = null;
}
sittableRemove();
preSitRemove();
standUpRemove();
if (createModeOverlay) {
Overlays.deleteOverlay(createModeOverlay);
}
Overlays.mousePressOnOverlay.disconnect(mousePressOnOverlay);
}
};
// #endregion ENTITY METHODS
return new SitClient();
});