Created
July 1, 2016 20:33
-
-
Save hyperlogic/bb6c0d105f2d6f9812010629ecc5cf6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
/* globals Xform */ | |
// | |
// bakeAttachPoint.js | |
// examples | |
// | |
// Created by Anthony Thibault on 2016/06/30 | |
// Copyright 2016 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 | |
// | |
Script.include("/~/system/libraries/Xform.js"); | |
// button helper | |
function ButtonBuddy(x, y, width, height, urls) { | |
this.overlay = Overlays.addOverlay("image", { | |
x: x, y: y, width: width, height: height, | |
subImage: { x: 0, y: 0, width: width, height: height}, | |
imageURL: urls.image, | |
visible: true, | |
alpha: 1.0 | |
}); | |
this.callbacks = []; | |
var self = this; | |
Controller.mousePressEvent.connect(function (event) { | |
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); | |
if (clickedOverlay === self.overlay) { | |
self.onClick(); | |
} | |
}); | |
Script.scriptEnding.connect(function () { | |
self.destroy(); | |
}); | |
} | |
ButtonBuddy.prototype.destroy = function () { | |
Overlays.deleteOverlay(this.overlay); | |
this.callbacks = []; | |
}; | |
ButtonBuddy.prototype.addClickHandler = function (callback) { | |
this.callbacks.push(callback); | |
return callback; | |
}; | |
ButtonBuddy.prototype.removeClickHandler = function (callback) { | |
var index = this.callbacks.indexOf(callback); | |
if (index != -1) { | |
this.callbacks.splice(index, 1); | |
} | |
}; | |
ButtonBuddy.prototype.onClick = function () { | |
var i, l = this.callbacks.length; | |
for (i = 0; i < l; i++) { | |
this.callbacks[i](); | |
} | |
}; | |
var windowDimensions = Controller.getViewportDimensions(); | |
var BUTTON_WIDTH = 64; | |
var BUTTON_HEIGHT = 64; | |
var BUTTON_PADDING = 10; | |
var buttonPositionX = windowDimensions.x - BUTTON_PADDING - BUTTON_WIDTH; | |
var buttonPositionY = (windowDimensions.y - BUTTON_HEIGHT) / 2 - (BUTTON_HEIGHT + BUTTON_PADDING); | |
var equipButton = new ButtonBuddy(buttonPositionX, buttonPositionY, BUTTON_WIDTH, BUTTON_HEIGHT, { | |
image: "https://s3.amazonaws.com/hifi-public/tony/icons/hat-up.svg" | |
}); | |
function getProps(entityID) { | |
var props = Entities.getEntityProperties(entityID, ["name", "position", "rotation", "userData"]); | |
// convert props.userData from a string to an object. | |
var userData = {}; | |
if (props.userData) { | |
try { | |
userData = JSON.parse(props.userData); | |
} catch (err) { | |
print("WARNING: malformed userData on " + entityID + ", name = " + props.name + ", error = " + err); | |
} | |
} | |
props.userData = userData; | |
return props; | |
} | |
function getGrabbedEntity(useLeftHand) { | |
var EQUIP_RADIUS = 0.1; | |
var palmPosition = useLeftHand ? MyAvatar.getLeftPalmPosition() : MyAvatar.getRightPalmPosition(); | |
var entities = Entities.findEntities(palmPosition, EQUIP_RADIUS); | |
var i; | |
for (i = 0; i < entities.length; i++) { | |
var entityID = entities[i]; | |
var props = getProps(entityID); | |
var grabProps = props.userData.grabKey; | |
if (grabProps) { | |
var refCount = ("refCount" in grabProps) ? grabProps.refCount : 0; | |
if (refCount > 0) { | |
return entityID; | |
} | |
} | |
} | |
return undefined; | |
} | |
function writeAttachPoint(entityID, props, jointXform, jointName) { | |
var userData = props.userData; | |
var objXform = new Xform(props.rotation, props.position); | |
var relXform = Xform.mul(jointXform.inv(), objXform); | |
print("objXform = " + objXform.toString()); | |
print("jointXform = " + jointXform.toString()); | |
print("relXform = " + relXform.toString()); | |
if (userData.wearable) { | |
// old style | |
if (!userData.wearable.joints) { | |
userData.wearable.joints = {}; | |
} | |
userData.wearable.joints[jointName] = [relXform.pos, relXform.rot]; | |
} | |
if (userData.equipHotspots) { | |
// new style | |
if (!userData.equipHotspots[0].joints) { | |
userData.equipHotspots[0].joints = {}; | |
} | |
userData.equipHotspots[0].joints[jointName] = [relXform.pos, relXform.rot]; | |
} | |
Entities.editEntity(entityID, { userData: JSON.stringify(userData) }); | |
} | |
equipButton.addClickHandler(function () { | |
var props; | |
// left hand | |
var entityID = getGrabbedEntity(true); | |
if (entityID) { | |
props = getProps(entityID); | |
var leftHandPose = MyAvatar.getLeftHandPose(); | |
var leftHandXform = Xform.mul(new Xform(MyAvatar.orientation, MyAvatar.position), | |
new Xform(leftHandPose.rotation, leftHandPose.translation)); | |
writeAttachPoint(entityID, props, leftHandXform, "LeftHand"); | |
print("found '" + props.name + "' in leftHand"); | |
return; | |
} | |
entityID = getGrabbedEntity(false); | |
if (entityID) { | |
props = getProps(entityID); | |
var rightHandPose = MyAvatar.getRightHandPose(); | |
var rightHandXform = Xform.mul(new Xform(MyAvatar.orientation, MyAvatar.position), | |
new Xform(rightHandPose.rotation, rightHandPose.translation)); | |
writeAttachPoint(entityID, props, rightHandXform, "RightHand"); | |
print("found '" + props.name + "' in rightHand"); | |
return; | |
} | |
print("no entity found!"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment