106 lines
3.9 KiB
JavaScript
106 lines
3.9 KiB
JavaScript
//
|
|
// ToggleMuteWristControl.js
|
|
//
|
|
// Created by Liv Erickson on June 2, 2017
|
|
// Copyright 2017 High Fidelity, Inc.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE of http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
/* eslint indent: ["error", 4, { "outerIIFEBody" : 0}] */
|
|
|
|
// Model FBX and Textures
|
|
var MODEL_URL = "https://hifi-content.s3.amazonaws.com/liv/3DModels/watch/NewWatch.fbx";
|
|
var MUTED_TEXTURE = "https://hifi-content.s3.amazonaws.com/liv/3DModels/watch/Muted.png";
|
|
var OPEN_TEXTURE = "https://hifi-content.s3.amazonaws.com/liv/3DModels/watch/Open.png";
|
|
|
|
var DISTANCE_TO_CHECK_TAP = 0.1; //cm
|
|
var DISTANCE_TO_TAP = 0.08; //cm
|
|
|
|
var _wearableJointIndex = MyAvatar.getJointIndex("LeftForeArm");
|
|
var _waitingCooldown = false;
|
|
var _cooldowntimer = 0;
|
|
var _textureData = { "Face": Audio.muted ? MUTED_TEXTURE : OPEN_TEXTURE };
|
|
|
|
var isCloseEnough = function (a, b, dist) {
|
|
return (Math.abs(a - b) <= dist);
|
|
};
|
|
|
|
// Create the wearable entity
|
|
var controllerEntity = {
|
|
type: "Model",
|
|
name: "Mute Wearable",
|
|
modelURL: MODEL_URL,
|
|
textures: JSON.stringify(_textureData),
|
|
position: MyAvatar.getJointPosition("LeftHand"),
|
|
localPosition: {x: 0.00, y: 0.2, z: 0.0},
|
|
dimensions: {x: 0.0091, y: 0.0658, z: 0.0658},
|
|
rotation: Quat.fromVec3Degrees({x: 0, y: 0, z:90}),
|
|
translation: {x: 0, y: 0, z: 0},
|
|
parentID: MyAvatar.sessionUUID,
|
|
parentJointIndex: _wearableJointIndex
|
|
};
|
|
|
|
var controllerObject = Entities.addEntity(controllerEntity, true);
|
|
|
|
// Create the particle effect for the entity
|
|
var particleEntity = {
|
|
type: "ParticleEffect",
|
|
name: "Unmuted Particles",
|
|
parentID : controllerObject,
|
|
position: Entities.getEntityProperties(controllerObject, "position"),
|
|
isEmitting: false,
|
|
lifespan: 1,
|
|
textures: "https://hifi-content.s3.amazonaws.com/liv/Particles/notes.png",
|
|
emitRate: 2,
|
|
emitSpeed : 0.005,
|
|
particleRadius : .05,
|
|
radiusSpread: 0,
|
|
radiusStart:0,
|
|
radiusFinish: 0,
|
|
accelerationSpread : {x : 0, y:0,z:0},
|
|
emitAcceleration: {x: 0, y:0, z:0},
|
|
};
|
|
|
|
var particleObject = Entities.addEntity(particleEntity, true);
|
|
|
|
var testPoke = function (entityID) {
|
|
var position = MyAvatar.getJointPosition("LeftHand");
|
|
var jointIndexRight = MyAvatar.getJointIndex("RightHandIndex4");
|
|
var fingertipLocRight = MyAvatar.getJointPosition(jointIndexRight);
|
|
|
|
if (isCloseEnough(position.x, fingertipLocRight.x, DISTANCE_TO_TAP) &&
|
|
isCloseEnough(position.y, fingertipLocRight.y, DISTANCE_TO_TAP) &&
|
|
isCloseEnough(position.z, fingertipLocRight.z, DISTANCE_TO_TAP)) {
|
|
Audio.muted = !Audio.muted;
|
|
_waitingCooldown = true;
|
|
Script.setTimeout(function(){ _waitingCooldown = false}, 1000);
|
|
};
|
|
};
|
|
|
|
Script.update.connect(function () {
|
|
var rightZ = MyAvatar.getJointPosition("RightHand").z;
|
|
var leftZ = MyAvatar.getJointPosition("LeftHand").z;
|
|
if (isCloseEnough(rightZ, leftZ, DISTANCE_TO_CHECK_TAP) && !_waitingCooldown) {
|
|
testPoke(controllerObject);
|
|
};
|
|
|
|
});
|
|
|
|
Script.scriptEnding.connect(function () {
|
|
Entities.deleteEntity(controllerObject);
|
|
Entities.deleteEntity(particleObject);
|
|
});
|
|
|
|
Audio.mutedChanged.connect(function () {
|
|
_textureData = { "Face": Audio.muted ? MUTED_TEXTURE : OPEN_TEXTURE };
|
|
Entities.editEntity(particleObject, {isEmitting: Audio.muted ? false : true});
|
|
Entities.editEntity(controllerObject, { textures: JSON.stringify(_textureData) });
|
|
Script.setTimeout(function(){ Entities.editEntity(particleObject, {isEmitting : false})}, 3000);
|
|
});
|
|
|
|
MyAvatar.skeletonChanged.connect(function() {
|
|
_wearableJointIndex = MyAvatar.getJointIndex("LeftForeArm");
|
|
Entities.editEntity(controllerObject, {position: MyAvatar.getJointPosition("LeftHand"), parentJointIndex : _wearableJointIndex })
|
|
});
|
|
|