Last active
June 24, 2017 00:10
-
-
Save humbletim/516f445eba8cbe17d7eaa8b2ac135466 to your computer and use it in GitHub Desktop.
Test Entity / Overlay models with parented children
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
// test Client script to compare Model Overlay vs. Model Entity behavior for attached child Overlays | |
// humbletim @ 2017.06.23 | |
var overlayProps = { | |
url: MyAvatar.skeletonModelURL, | |
position: Vec3.sum(Quat.getForward(MyAvatar.orientation), MyAvatar.position), | |
scale: 1, | |
}; | |
var ids = { | |
overlays: [ | |
Overlays.addOverlay("model", overlayProps) | |
], | |
entities: [ | |
Entities.addEntity({ | |
type: 'Model', | |
modelURL: overlayProps.url, | |
position: Vec3.sum(overlayProps.position, {x:0,y:2,z:0}), | |
}, !Entities.canRezTmp()) | |
], | |
}; | |
function wobblyRotation(now) { | |
var t = now / 200; | |
return Quat.fromVec3Degrees({ | |
x: 10 * Math.sin(t), | |
y: 60 * Math.cos(t), | |
z: 0 | |
}); | |
} | |
function animate(context) { | |
// add a visual tracer on the left hand | |
ids.overlays.push( | |
Overlays.addOverlay("shape", { | |
shape: "icosahedron", | |
parentID: context.parentID, | |
parentJointIndex: context.attachJointIndex, | |
localPosition: Vec3.ZERO, | |
localRotation: Quat.fromPitchYawRollDegrees(0,90,0), | |
scale: 0.25, | |
color: { red: 0xff, green: 0x00, blue: 0xff }, | |
}) | |
); | |
var initialRot = context.jointRotations[context.animateJointIndex]; | |
// wobble the left arm around... | |
Script.setInterval(function() { | |
context.jointRotations[context.animateJointIndex] = Quat.multiply(initialRot, wobblyRotation(new Date)); | |
context.callback({ | |
jointRotations: context.jointRotations, | |
position: Vec3.sum(context.position, { x: Math.sin(new Date / 1000), y:0, z: 0}), | |
}); | |
}, 1000/30); | |
} | |
function testOverlayModel() { | |
var overlays = ids.overlays, | |
parentID = overlays[0], | |
jointRotations = Overlays.getProperty(parentID, 'jointRotations'), | |
jointNames = Overlays.getProperty(parentID, 'jointNames'), | |
position = Overlays.getProperty(parentID, 'position'), | |
leftArmIndex = jointNames.indexOf('LeftArm'), | |
leftHandIndex = jointNames.indexOf('LeftHand'); | |
print(">>> Overlay LeftArm index:", leftArmIndex); | |
print(">>> Overlay LeftHand index:", leftHandIndex); | |
animate({ | |
parentID: parentID, | |
position: position, | |
jointRotations: jointRotations, | |
attachJointIndex: leftHandIndex, | |
animateJointIndex: leftArmIndex, | |
callback: function(props) { | |
Overlays.editOverlay(parentID, props); | |
}, | |
}); | |
} | |
function testEntityModel() { | |
var entities = ids.entities, | |
parentID = entities[0], | |
jointNames = Entities.getJointNames(parentID), | |
jointRotations = jointNames.map(function(name, index) { | |
return Entities.getLocalJointRotation(parentID, index); | |
}), | |
leftArmIndex = jointNames.indexOf('LeftArm'), | |
leftHandIndex = jointNames.indexOf('LeftHand'); | |
print(">>> Entity LeftArm index:", leftArmIndex); | |
print(">>> Entity LeftHand index:", leftHandIndex); | |
var props = Entities.getEntityProperties(parentID, ['naturalDimensions','position']); | |
Entities.editEntity(parentID, { | |
dimensions: props.naturalDimensions | |
}); | |
animate({ | |
parentID: parentID, | |
position: props.position, | |
jointRotations: jointRotations, | |
attachJointIndex: leftHandIndex, | |
animateJointIndex: leftArmIndex, | |
callback: function(props) { | |
Entities.setLocalJointRotations(parentID, props.jointRotations); | |
Entities.editEntity(parentID, { | |
position: props.position | |
}); | |
}, | |
}); | |
} | |
Script.setTimeout(function() { | |
// move slightly once at start to trigger "updateModel = true" internally within Model Overlay | |
// (which visually confirms that the model has reached the "active" state before parenting is attempted) | |
Overlays.editOverlay(ids.overlays[0], { | |
position: Vec3.sum(Overlays.getProperty(ids.overlays[0], 'position'), {x:0,y:-.1,z:0}) | |
}); | |
Script.setTimeout(function() { | |
testOverlayModel(); | |
testEntityModel(); | |
}, 1000); | |
}, 1000); | |
Script.scriptEnding.connect(function() { | |
ids.overlays.splice(0, ids.overlays.length).forEach(Overlays.deleteOverlay); | |
ids.entities.splice(0, ids.entities.length).forEach(Entities.deleteEntity); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment