From c67eafffba8c0398dc3098a1e75cce8907d64a04 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 20 Oct 2015 16:59:03 -0700 Subject: [PATCH 01/35] Add pitching.js --- examples/pitching.js | 365 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 examples/pitching.js diff --git a/examples/pitching.js b/examples/pitching.js new file mode 100644 index 0000000000..046113cbc3 --- /dev/null +++ b/examples/pitching.js @@ -0,0 +1,365 @@ +//var PITCH_THUNK_SOUND_URL = "file:///C:/Users/Ryan/Downloads/323725__reitanna__thunk.wav"; +var PITCH_THUNK_SOUND_URL = "file:///C:/Users/Ryan/Downloads/thunk.wav"; +var PITCH_THUNK_SOUND_URL = "http://hifi-public.s3.amazonaws.com/sounds/ping_pong_gun/pong_sound.wav"; +var pitchSound = SoundCache.getSound(PITCH_THUNK_SOUND_URL, false); + +function info(message) { + print("[INFO] " + message); +} + +function error(message) { + print("[ERROR] " + message); +} + +var PITCHING_MACHINE_URL = "atp:87d4879530b698741ecc45f6f31789aac11f7865a2c3bec5fe9b061a182c80d4.fbx"; +var PITCHING_MACHINE_OUTPUT_OFFSET_PCT = { + x: 0.0, + y: 0.25, + z: -1.05, +}; +var PITCHING_MACHINE_PROPERTIES = { + name: "Pitching Machine", + type: "Model", + position: { + x: 0, + y: 0.8, + z: -22.3, + }, + velocity: { + x: 0, + y: -0.01, + z: 0 + }, + gravity: { + x: 0.0, + y: -9.8, + z: 0.0 + }, + registrationPoint: { + x: 0.5, + y: 0.5, + z: 0.5, + }, + rotation: Quat.fromPitchYawRollDegrees(0, 180, 0), + modelURL: PITCHING_MACHINE_URL, + dimensions: { + x: 0.4, + y: 0.61, + z: 0.39 + }, + collisionsWillMove: false, + shapeType: "Box", +}; +PITCHING_MACHINE_PROPERTIES.dimensions = Vec3.multiply(2.5, PITCHING_MACHINE_PROPERTIES.dimensions); + + +var PITCH_RATE = 5000; + +var BASEBALL_MODEL_URL = "atp:7185099f1f650600ca187222573a88200aeb835454bd2f578f12c7fb4fd190fa.fbx"; +var BASEBALL_SPEED = 2.7; +var BASEBALL_RADIUS = 0.07468; +var BASEBALL_PROPERTIES = { + name: "Baseball", + type: "Model", + modelURL: BASEBALL_MODEL_URL, + shapeType: "Sphere", + position: { + x: 0, + y: 0, + z: 0 + }, + dimensions: { + x: BASEBALL_RADIUS, + y: BASEBALL_RADIUS, + z: BASEBALL_RADIUS + }, + collisionsWillMove: true, + angularVelocity: { + x: 17.0, + y: 0, + z: -8.0, + }, + angularDamping: 0.0, + damping: 0.0, + restitution: 0.5, + friction: 0.0, + lifetime: 20, + collisionSoundURL: PITCH_THUNK_SOUND_URL, + gravity: { + x: 0, + y: 0,//-9.8, + z: 0 + } +}; + + +var pitchingMachineID = Entities.addEntity(PITCHING_MACHINE_PROPERTIES); + +var pitchFromPosition = { x: 0, y: 1.0, z: 0 }; +var pitchDirection = { x: 0, y: 0, z: 1 }; + +function shallowCopy(obj) { + var copy = {} + for (var key in obj) { + copy[key] = obj[key]; + } + return copy; +} + +function randomInt(low, high) { + return low + (Math.random() * (high - low)); +} + +var ACCELERATION_SPREAD = 10.15; + +function createBaseball(position, velocity, ballScale) { + var properties = shallowCopy(BASEBALL_PROPERTIES); + properties.position = position; + properties.velocity = velocity; + properties.acceleration = { + x: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), + y: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), + z: 0.0, + }; + properties.dimensions = Vec3.multiply(ballScale, properties.dimensions); + var entityID = Entities.addEntity(properties); + Script.addEventHandler(entityID, "collisionWithEntity", buildBaseballHitCallback(entityID)); + if (false && Math.random() < 0.5) { + for (var i = 0; i < 50; i++) { + Script.setTimeout(function() { + Entities.editEntity(entityID, { + gravity: { + x: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), + y: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), + z: 0.0, + } + }) + }, i * 100); + } + } + return entityID; +} + +var buildBaseballHitCallback = function(entityID) { + var f = function(entityA, entityB, collision) { + var properties = Entities.getEntityProperties(entityID, ['position', 'velocity']); + var line = new InfiniteLine(properties.position, { red: 255, green: 128, blue: 89 }); + var lastPosition = properties.position; + Vec3.print("Velocity", properties.velocity); + Vec3.print("VelocityChange", collision.velocityChange); + Script.setInterval(function() { + var properties = Entities.getEntityProperties(entityID, ['position']); + if (Vec3.distance(properties.position, lastPosition)) { + line.enqueuePoint(properties.position); + lastPosition = properties.position; + } + }, 50); + Entities.editEntity(entityID, { + velocity: Vec3.multiply(3, properties.velocity), + gravity: { + x: 0, + y: -2.8, + z: 0 + } + }); + print("Baseball hit!"); + Script.removeEventHandler(entityID, "collisionWithEntity", f); + }; + return f; +} + + +function vec3Mult(a, b) { + return { + x: a.x * b.x, + y: a.y * b.y, + z: a.z * b.z, + }; +} + +var injector = null; +function pitchBall() { + var machineProperties = Entities.getEntityProperties(pitchingMachineID, ["dimensions", "position", "rotation"]); + var pitchFromPositionBase = machineProperties.position; + var pitchFromOffset = vec3Mult(machineProperties.dimensions, PITCHING_MACHINE_OUTPUT_OFFSET_PCT); + pitchFromOffset = Vec3.multiplyQbyV(machineProperties.rotation, pitchFromOffset); + var pitchFromPosition = Vec3.sum(pitchFromPositionBase, pitchFromOffset); + var pitchDirection = Quat.getFront(machineProperties.rotation); + var ballScale = machineProperties.dimensions.x / PITCHING_MACHINE_PROPERTIES.dimensions.x; + print("Creating baseball"); + var ballEntityID = createBaseball(pitchFromPosition, Vec3.multiply(BASEBALL_SPEED, pitchDirection), ballScale); + if (!injector) { + injector = Audio.playSound(pitchSound, { + position: pitchFromPosition, + volume: 1.0 + }); + } else { + injector.restart(); + } +} + +Script.scriptEnding.connect(function() { + Entities.deleteEntity(pitchingMachineID); +}) + +Script.setInterval(pitchBall, PITCH_RATE); + + + + +/****************************************************************************** + * PolyLine + *****************************************************************************/ +var LINE_DIMENSIONS = { x: 2000, y: 2000, z: 2000 }; +var MAX_LINE_LENGTH = 40; // This must be 2 or greater; +var PolyLine = function(position, color, defaultStrokeWidth) { + //info("Creating polyline"); + //Vec3.print("New line at", position); + this.position = position; + this.color = color; + this.defaultStrokeWidth = 0.10; + this.points = [ + { x: 0, y: 0, z: 0 }, + ]; + this.strokeWidths = [ + this.defaultStrokeWidth, + ] + this.normals = [ + { x: 1, y: 0, z: 0 }, + ] + this.entityID = Entities.addEntity({ + type: "PolyLine", + position: position, + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + dimensions: LINE_DIMENSIONS, + color: color, + lifetime: 20, + }); +}; + +PolyLine.prototype.enqueuePoint = function(position) { + if (this.isFull()) { + error("Hit max PolyLine size"); + return; + } + + //Vec3.print("pos", position); + //info("Number of points: " + this.points.length); + + position = Vec3.subtract(position, this.position); + this.points.push(position); + this.normals.push({ x: 1, y: 0, z: 0 }); + this.strokeWidths.push(this.defaultStrokeWidth); + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.dequeuePoint = function() { + if (this.points.length == 0) { + error("Hit min PolyLine size"); + return; + } + + this.points = this.points.slice(1); + this.normals = this.normals.slice(1); + this.strokeWidths = this.strokeWidths.slice(1); + + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.getFirstPoint = function() { + return Vec3.sum(this.position, this.points[0]); +}; + +PolyLine.prototype.getLastPoint = function() { + return Vec3.sum(this.position, this.points[this.points.length - 1]); +}; + +PolyLine.prototype.getSize = function() { + return this.points.length; +} + +PolyLine.prototype.isFull = function() { + return this.points.length >= MAX_LINE_LENGTH; +}; + +PolyLine.prototype.destroy = function() { + Entities.deleteEntity(this.entityID); + this.points = []; +}; + + +/****************************************************************************** + * InfiniteLine + *****************************************************************************/ +InfiniteLine = function(position, color) { + this.position = position; + this.color = color; + this.lines = [new PolyLine(position, color)]; + this.size = 0; +}; + +InfiniteLine.prototype.enqueuePoint = function(position) { + var currentLine; + + if (this.lines.length == 0) { + currentLine = new PolyLine(position, this.color); + this.lines.push(currentLine); + } else { + currentLine = this.lines[this.lines.length - 1]; + } + + if (currentLine.isFull()) { + //info("Current line is full, creating new line"); + //Vec3.print("Last line is", currentLine.getLastPoint()); + //Vec3.print("New line is", position); + var newLine = new PolyLine(currentLine.getLastPoint(), this.color); + this.lines.push(newLine); + currentLine = newLine; + } + + currentLine.enqueuePoint(position); + + ++this.size; +}; + +InfiniteLine.prototype.dequeuePoint = function() { + if (this.lines.length == 0) { + error("Trying to dequeue from InfiniteLine when no points are left"); + return; + } + + var lastLine = this.lines[0]; + lastLine.dequeuePoint(); + + if (lastLine.getSize() <= 1) { + this.lines = this.lines.slice(1); + } + + --this.size; +}; + +InfiniteLine.prototype.getFirstPoint = function() { + return this.lines.length > 0 ? this.lines[0].getFirstPoint() : null; +}; + +InfiniteLine.prototype.getLastPoint = function() { + return this.lines.length > 0 ? this.lines[lines.length - 1].getLastPoint() : null; +}; + +InfiniteLine.prototype.destroy = function() { + for (var i = 0; i < this.lines.length; ++i) { + this.lines[i].destroy(); + } + + this.size = 0; +}; From 21903b746cb3e9f02214bbc9028b341cd8063884 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 21 Oct 2015 10:06:51 -0700 Subject: [PATCH 02/35] CP --- examples/baseballEntityScript.js | 0 examples/libraries/walkApi.js | 1884 ++++++++++++------------ examples/map.js~ | 323 ++++ examples/pitching.js | 6 +- examples/toys/flashlight/flashlight.js | 536 +++---- examples/walk.js | 906 ++++++------ 6 files changed, 1990 insertions(+), 1665 deletions(-) create mode 100644 examples/baseballEntityScript.js create mode 100644 examples/map.js~ diff --git a/examples/baseballEntityScript.js b/examples/baseballEntityScript.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/libraries/walkApi.js b/examples/libraries/walkApi.js index d1192deee7..7f5070f5d0 100644 --- a/examples/libraries/walkApi.js +++ b/examples/libraries/walkApi.js @@ -1,943 +1,943 @@ -// -// walkApi.js -// version 1.3 -// -// Created by David Wooldridge, June 2015 -// Copyright © 2014 - 2015 High Fidelity, Inc. -// -// Exposes API for use by walk.js version 1.2+. -// -// Editing tools for animation data files available here: https://github.com/DaveDubUK/walkTools -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// included here to ensure walkApi.js can be used as an API, separate from walk.js -Script.include("./libraries/walkConstants.js"); - -Avatar = function() { - // if Hydras are connected, the only way to enable use is to never set any arm joint rotation - this.hydraCheck = function() { - // function courtesy of Thijs Wenker (frisbee.js) - var numberOfButtons = Controller.getNumberOfButtons(); - var numberOfTriggers = Controller.getNumberOfTriggers(); - var numberOfSpatialControls = Controller.getNumberOfSpatialControls(); - const HYDRA_BUTTONS = 12; - const HYDRA_TRIGGERS = 2; - const HYDRA_CONTROLLERS_PER_TRIGGER = 2; - var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers; - if (numberOfButtons == HYDRA_BUTTONS && - numberOfTriggers == HYDRA_TRIGGERS && - controllersPerTrigger == HYDRA_CONTROLLERS_PER_TRIGGER) { - print('walk.js info: Razer Hydra detected. Setting arms free (not controlled by script)'); - return true; - } else { - print('walk.js info: Razer Hydra not detected. Arms will be controlled by script.'); - return false; - } - } - // settings - this.headFree = true; - this.armsFree = this.hydraCheck(); // automatically sets true to enable Hydra support - temporary fix - this.makesFootStepSounds = false; - this.blenderPreRotations = false; // temporary fix - this.animationSet = undefined; // currently just one animation set - this.setAnimationSet = function(animationSet) { - this.animationSet = animationSet; - switch (animationSet) { - case 'standardMale': - this.selectedIdle = walkAssets.getAnimationDataFile("MaleIdle"); - this.selectedWalk = walkAssets.getAnimationDataFile("MaleWalk"); - this.selectedWalkBackwards = walkAssets.getAnimationDataFile("MaleWalkBackwards"); - this.selectedSideStepLeft = walkAssets.getAnimationDataFile("MaleSideStepLeft"); - this.selectedSideStepRight = walkAssets.getAnimationDataFile("MaleSideStepRight"); - this.selectedWalkBlend = walkAssets.getAnimationDataFile("WalkBlend"); - this.selectedHover = walkAssets.getAnimationDataFile("MaleHover"); - this.selectedFly = walkAssets.getAnimationDataFile("MaleFly"); - this.selectedFlyBackwards = walkAssets.getAnimationDataFile("MaleFlyBackwards"); - this.selectedFlyDown = walkAssets.getAnimationDataFile("MaleFlyDown"); - this.selectedFlyUp = walkAssets.getAnimationDataFile("MaleFlyUp"); - this.selectedFlyBlend = walkAssets.getAnimationDataFile("FlyBlend"); - this.currentAnimation = this.selectedIdle; - return; - } - } - this.setAnimationSet('standardMale'); - - // calibration - this.calibration = { - hipsToFeet: 1, - strideLength: this.selectedWalk.calibration.strideLength - } - this.distanceFromSurface = 0; - this.calibrate = function() { - // Triple check: measurements are taken three times to ensure accuracy - the first result is often too large - const MAX_ATTEMPTS = 3; - var attempts = MAX_ATTEMPTS; - var extraAttempts = 0; - do { - for (joint in walkAssets.animationReference.joints) { - var IKChain = walkAssets.animationReference.joints[joint].IKChain; - - // only need to zero right leg IK chain and hips - if (IKChain === "RightLeg" || joint === "Hips" ) { - MyAvatar.setJointRotation(joint, Quat.fromPitchYawRollDegrees(0, 0, 0)); - } - } - this.calibration.hipsToFeet = MyAvatar.getJointPosition("Hips").y - MyAvatar.getJointPosition("RightToeBase").y; - - // maybe measuring before Blender pre-rotations have been applied? - if (this.calibration.hipsToFeet < 0 && this.blenderPreRotations) { - this.calibration.hipsToFeet *= -1; - } - - if (this.calibration.hipsToFeet === 0 && extraAttempts < 100) { - attempts++; - extraAttempts++;// Interface can sometimes report zero for hips to feet. if so, we try again. - } - } while (attempts-- > 1) - - // just in case - if (this.calibration.hipsToFeet <= 0 || isNaN(this.calibration.hipsToFeet)) { - this.calibration.hipsToFeet = 1; - print('walk.js error: Unable to get a non-zero measurement for the avatar hips to feet measure. Hips to feet set to default value ('+ - this.calibration.hipsToFeet.toFixed(3)+'m). This will cause some foot sliding. If your avatar has only just appeared, it is recommended that you re-load the walk script.'); - } else { - print('walk.js info: Hips to feet calibrated to '+this.calibration.hipsToFeet.toFixed(3)+'m'); - } - } - - // pose the fingers - this.poseFingers = function() { - for (knuckle in walkAssets.animationReference.leftHand) { - if (walkAssets.animationReference.leftHand[knuckle].IKChain === "LeftHandThumb") { - MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(0, 0, -4)); - } else { - MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(16, 0, 5)); - } - } - for (knuckle in walkAssets.animationReference.rightHand) { - if (walkAssets.animationReference.rightHand[knuckle].IKChain === "RightHandThumb") { - MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(0, 0, 4)); - } else { - MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(16, 0, -5)); - } - } - }; - this.calibrate(); - this.poseFingers(); - - // footsteps - this.nextStep = RIGHT; // the first step is right, because the waveforms say so - this.leftAudioInjector = null; - this.rightAudioInjector = null; - this.makeFootStepSound = function() { - // correlate footstep volume with avatar speed. place the audio source at the feet, not the hips - const SPEED_THRESHOLD = 0.4; - const VOLUME_ATTENUATION = 0.8; - const MIN_VOLUME = 0.5; - var volume = Vec3.length(motion.velocity) > SPEED_THRESHOLD ? - VOLUME_ATTENUATION * Vec3.length(motion.velocity) / MAX_WALK_SPEED : MIN_VOLUME; - volume = volume > 1 ? 1 : volume; // occurs when landing at speed - can walk faster than max walking speed - var options = { - position: Vec3.sum(MyAvatar.position, {x:0, y: -this.calibration.hipsToFeet, z:0}), - volume: volume - }; - if (this.nextStep === RIGHT) { - if (this.rightAudioInjector === null) { - this.rightAudioInjector = Audio.playSound(walkAssets.footsteps[0], options); - } else { - this.rightAudioInjector.setOptions(options); - this.rightAudioInjector.restart(); - } - this.nextStep = LEFT; - } else if (this.nextStep === LEFT) { - if (this.leftAudioInjector === null) { - this.leftAudioInjector = Audio.playSound(walkAssets.footsteps[1], options); - } else { - this.leftAudioInjector.setOptions(options); - this.leftAudioInjector.restart(); - } - this.nextStep = RIGHT; - } - } -}; - -// constructor for the Motion object -Motion = function() { - this.isLive = true; - // locomotion status - this.state = STATIC; - this.nextState = STATIC; - this.isMoving = false; - this.isWalkingSpeed = false; - this.isFlyingSpeed = false; - this.isAccelerating = false; - this.isDecelerating = false; - this.isDeceleratingFast = false; - this.isComingToHalt = false; - this.directedAcceleration = 0; - - // used to make sure at least one step has been taken when transitioning from a walk cycle - this.elapsedFTDegrees = 0; - - // the current transition (any layered transitions are nested within this transition) - this.currentTransition = null; - - // orientation, locomotion and timing - this.velocity = {x:0, y:0, z:0}; - this.acceleration = {x:0, y:0, z:0}; - this.yaw = Quat.safeEulerAngles(MyAvatar.orientation).y; - this.yawDelta = 0; - this.yawDeltaAcceleration = 0; - this.direction = FORWARDS; - this.deltaTime = 0; - - // historical orientation, locomotion and timing - this.lastDirection = FORWARDS; - this.lastVelocity = {x:0, y:0, z:0}; - this.lastYaw = Quat.safeEulerAngles(MyAvatar.orientation).y; - this.lastYawDelta = 0; - this.lastYawDeltaAcceleration = 0; - - // Quat.safeEulerAngles(MyAvatar.orientation).y tends to repeat values between frames, so values are filtered - var YAW_SMOOTHING = 22; - this.yawFilter = filter.createAveragingFilter(YAW_SMOOTHING); - this.deltaTimeFilter = filter.createAveragingFilter(YAW_SMOOTHING); - this.yawDeltaAccelerationFilter = filter.createAveragingFilter(YAW_SMOOTHING); - - // assess locomotion state - this.assess = function(deltaTime) { - // calculate avatar frame speed, velocity and acceleration - this.deltaTime = deltaTime; - this.velocity = Vec3.multiplyQbyV(Quat.inverse(MyAvatar.orientation), MyAvatar.getVelocity()); - var lateralVelocity = Math.sqrt(Math.pow(this.velocity.x, 2) + Math.pow(this.velocity.z, 2)); - - // MyAvatar.getAcceleration() currently not working. bug report submitted: https://worklist.net/20527 - var acceleration = {x:0, y:0, z:0}; - this.acceleration.x = (this.velocity.x - this.lastVelocity.x) / deltaTime; - this.acceleration.y = (this.velocity.y - this.lastVelocity.y) / deltaTime; - this.acceleration.z = (this.velocity.z - this.lastVelocity.z) / deltaTime; - - // MyAvatar.getAngularVelocity and MyAvatar.getAngularAcceleration currently not working. bug report submitted - this.yaw = Quat.safeEulerAngles(MyAvatar.orientation).y; - if (this.lastYaw < 0 && this.yaw > 0 || this.lastYaw > 0 && this.yaw < 0) { - this.lastYaw *= -1; - } - var timeDelta = this.deltaTimeFilter.process(deltaTime); - this.yawDelta = filter.degToRad(this.yawFilter.process(this.lastYaw - this.yaw)) / timeDelta; - this.yawDeltaAcceleration = this.yawDeltaAccelerationFilter.process(this.lastYawDelta - this.yawDelta) / timeDelta; - - // how far above the surface is the avatar? (for testing / validation purposes) - var pickRay = {origin: MyAvatar.position, direction: {x:0, y:-1, z:0}}; - var distanceFromSurface = Entities.findRayIntersectionBlocking(pickRay).distance; - avatar.distanceFromSurface = distanceFromSurface - avatar.calibration.hipsToFeet; - - // determine principle direction of locomotion - var FWD_BACK_BIAS = 100; // helps prevent false sidestep condition detection when banking hard - if (Math.abs(this.velocity.x) > Math.abs(this.velocity.y) && - Math.abs(this.velocity.x) > FWD_BACK_BIAS * Math.abs(this.velocity.z)) { - if (this.velocity.x < 0) { - this.directedAcceleration = -this.acceleration.x; - this.direction = LEFT; - } else if (this.velocity.x > 0){ - this.directedAcceleration = this.acceleration.x; - this.direction = RIGHT; - } - } else if (Math.abs(this.velocity.y) > Math.abs(this.velocity.x) && - Math.abs(this.velocity.y) > Math.abs(this.velocity.z)) { - if (this.velocity.y > 0) { - this.directedAcceleration = this.acceleration.y; - this.direction = UP; - } else if (this.velocity.y < 0) { - this.directedAcceleration = -this.acceleration.y; - this.direction = DOWN; - } - } else if (FWD_BACK_BIAS * Math.abs(this.velocity.z) > Math.abs(this.velocity.x) && - Math.abs(this.velocity.z) > Math.abs(this.velocity.y)) { - if (this.velocity.z < 0) { - this.direction = FORWARDS; - this.directedAcceleration = -this.acceleration.z; - } else if (this.velocity.z > 0) { - this.directedAcceleration = this.acceleration.z; - this.direction = BACKWARDS; - } - } else { - this.direction = NONE; - this.directedAcceleration = 0; - } - - // set speed flags - if (Vec3.length(this.velocity) < MOVE_THRESHOLD) { - this.isMoving = false; - this.isWalkingSpeed = false; - this.isFlyingSpeed = false; - this.isComingToHalt = false; - } else if (Vec3.length(this.velocity) < MAX_WALK_SPEED) { - this.isMoving = true; - this.isWalkingSpeed = true; - this.isFlyingSpeed = false; - } else { - this.isMoving = true; - this.isWalkingSpeed = false; - this.isFlyingSpeed = true; - } - - // set acceleration flags - if (this.directedAcceleration > ACCELERATION_THRESHOLD) { - this.isAccelerating = true; - this.isDecelerating = false; - this.isDeceleratingFast = false; - this.isComingToHalt = false; - } else if (this.directedAcceleration < DECELERATION_THRESHOLD) { - this.isAccelerating = false; - this.isDecelerating = true; - this.isDeceleratingFast = (this.directedAcceleration < FAST_DECELERATION_THRESHOLD); - } else { - this.isAccelerating = false; - this.isDecelerating = false; - this.isDeceleratingFast = false; - } - - // use the gathered information to build up some spatial awareness - var isOnSurface = (avatar.distanceFromSurface < ON_SURFACE_THRESHOLD); - var isUnderGravity = (avatar.distanceFromSurface < GRAVITY_THRESHOLD); - var isTakingOff = (isUnderGravity && this.velocity.y > OVERCOME_GRAVITY_SPEED); - var isComingInToLand = (isUnderGravity && this.velocity.y < -OVERCOME_GRAVITY_SPEED); - var aboutToLand = isComingInToLand && avatar.distanceFromSurface < LANDING_THRESHOLD; - var surfaceMotion = isOnSurface && this.isMoving; - var acceleratingAndAirborne = this.isAccelerating && !isOnSurface; - var goingTooFastToWalk = !this.isDecelerating && this.isFlyingSpeed; - var movingDirectlyUpOrDown = (this.direction === UP || this.direction === DOWN) - var maybeBouncing = Math.abs(this.acceleration.y > BOUNCE_ACCELERATION_THRESHOLD) ? true : false; - - // we now have enough information to set the appropriate locomotion mode - switch (this.state) { - case STATIC: - var staticToAirMotion = this.isMoving && (acceleratingAndAirborne || goingTooFastToWalk || - (movingDirectlyUpOrDown && !isOnSurface)); - var staticToSurfaceMotion = surfaceMotion && !motion.isComingToHalt && !movingDirectlyUpOrDown && - !this.isDecelerating && lateralVelocity > MOVE_THRESHOLD; - - if (staticToAirMotion) { - this.nextState = AIR_MOTION; - } else if (staticToSurfaceMotion) { - this.nextState = SURFACE_MOTION; - } else { - this.nextState = STATIC; - } - break; - - case SURFACE_MOTION: - var surfaceMotionToStatic = !this.isMoving || - (this.isDecelerating && motion.lastDirection !== DOWN && surfaceMotion && - !maybeBouncing && Vec3.length(this.velocity) < MAX_WALK_SPEED); - var surfaceMotionToAirMotion = (acceleratingAndAirborne || goingTooFastToWalk || movingDirectlyUpOrDown) && - (!surfaceMotion && isTakingOff) || - (!surfaceMotion && this.isMoving && !isComingInToLand); - - if (surfaceMotionToStatic) { - // working on the assumption that stopping is now inevitable - if (!motion.isComingToHalt && isOnSurface) { - motion.isComingToHalt = true; - } - this.nextState = STATIC; - } else if (surfaceMotionToAirMotion) { - this.nextState = AIR_MOTION; - } else { - this.nextState = SURFACE_MOTION; - } - break; - - case AIR_MOTION: - var airMotionToSurfaceMotion = (surfaceMotion || aboutToLand) && !movingDirectlyUpOrDown; - var airMotionToStatic = !this.isMoving && this.direction === this.lastDirection; - - if (airMotionToSurfaceMotion){ - this.nextState = SURFACE_MOTION; - } else if (airMotionToStatic) { - this.nextState = STATIC; - } else { - this.nextState = AIR_MOTION; - } - break; - } - } - - // frequency time wheel (foot / ground speed matching) - const DEFAULT_HIPS_TO_FEET = 1; - this.frequencyTimeWheelPos = 0; - this.frequencyTimeWheelRadius = DEFAULT_HIPS_TO_FEET / 2; - this.recentFrequencyTimeIncrements = []; - const FT_WHEEL_HISTORY_LENGTH = 8; - for (var i = 0; i < FT_WHEEL_HISTORY_LENGTH; i++) { - this.recentFrequencyTimeIncrements.push(0); - } - this.averageFrequencyTimeIncrement = 0; - - this.advanceFrequencyTimeWheel = function(angle){ - this.elapsedFTDegrees += angle; - // keep a running average of increments for use in transitions (used during transitioning) - this.recentFrequencyTimeIncrements.push(angle); - this.recentFrequencyTimeIncrements.shift(); - for (increment in this.recentFrequencyTimeIncrements) { - this.averageFrequencyTimeIncrement += this.recentFrequencyTimeIncrements[increment]; - } - this.averageFrequencyTimeIncrement /= this.recentFrequencyTimeIncrements.length; - this.frequencyTimeWheelPos += angle; - const FULL_CIRCLE = 360; - if (this.frequencyTimeWheelPos >= FULL_CIRCLE) { - this.frequencyTimeWheelPos = this.frequencyTimeWheelPos % FULL_CIRCLE; - } - } - - this.saveHistory = function() { - this.lastDirection = this.direction; - this.lastVelocity = this.velocity; - this.lastYaw = this.yaw; - this.lastYawDelta = this.yawDelta; - this.lastYawDeltaAcceleration = this.yawDeltaAcceleration; - } -}; // end Motion constructor - -// animation manipulation object -animationOperations = (function() { - - return { - - // helper function for renderMotion(). calculate joint translations based on animation file settings and frequency * time - calculateTranslations: function(animation, ft, direction) { - var jointName = "Hips"; - var joint = animation.joints[jointName]; - var jointTranslations = {x:0, y:0, z:0}; - - // gather modifiers and multipliers - modifiers = new FrequencyMultipliers(joint, direction); - - // calculate translations. Use synthesis filters where specified by the animation data file. - - // sway (oscillation on the x-axis) - if (animation.filters.hasOwnProperty(jointName) && 'swayFilter' in animation.filters[jointName]) { - jointTranslations.x = joint.sway * animation.filters[jointName].swayFilter.calculate - (filter.degToRad(modifiers.swayFrequencyMultiplier * ft + joint.swayPhase)) + joint.swayOffset; - } else { - jointTranslations.x = joint.sway * Math.sin - (filter.degToRad(modifiers.swayFrequencyMultiplier * ft + joint.swayPhase)) + joint.swayOffset; - } - // bob (oscillation on the y-axis) - if (animation.filters.hasOwnProperty(jointName) && 'bobFilter' in animation.filters[jointName]) { - jointTranslations.y = joint.bob * animation.filters[jointName].bobFilter.calculate - (filter.degToRad(modifiers.bobFrequencyMultiplier * ft + joint.bobPhase)) + joint.bobOffset; - } else { - jointTranslations.y = joint.bob * Math.sin - (filter.degToRad(modifiers.bobFrequencyMultiplier * ft + joint.bobPhase)) + joint.bobOffset; - - if (animation.filters.hasOwnProperty(jointName) && 'bobLPFilter' in animation.filters[jointName]) { - jointTranslations.y = filter.clipTrough(jointTranslations.y, joint, 2); - jointTranslations.y = animation.filters[jointName].bobLPFilter.process(jointTranslations.y); - } - } - // thrust (oscillation on the z-axis) - if (animation.filters.hasOwnProperty(jointName) && 'thrustFilter' in animation.filters[jointName]) { - jointTranslations.z = joint.thrust * animation.filters[jointName].thrustFilter.calculate - (filter.degToRad(modifiers.thrustFrequencyMultiplier * ft + joint.thrustPhase)) + joint.thrustOffset; - } else { - jointTranslations.z = joint.thrust * Math.sin - (filter.degToRad(modifiers.thrustFrequencyMultiplier * ft + joint.thrustPhase)) + joint.thrustOffset; - } - return jointTranslations; - }, - - // helper function for renderMotion(). calculate joint rotations based on animation file settings and frequency * time - calculateRotations: function(jointName, animation, ft, direction) { - var joint = animation.joints[jointName]; - var jointRotations = {x:0, y:0, z:0}; - - if (avatar.blenderPreRotations) { - jointRotations = Vec3.sum(jointRotations, walkAssets.blenderPreRotations.joints[jointName]); - } - - // gather frequency multipliers for this joint - modifiers = new FrequencyMultipliers(joint, direction); - - // calculate rotations. Use synthesis filters where specified by the animation data file. - - // calculate pitch - if (animation.filters.hasOwnProperty(jointName) && - 'pitchFilter' in animation.filters[jointName]) { - jointRotations.x += joint.pitch * animation.filters[jointName].pitchFilter.calculate - (filter.degToRad(ft * modifiers.pitchFrequencyMultiplier + joint.pitchPhase)) + joint.pitchOffset; - } else { - jointRotations.x += joint.pitch * Math.sin - (filter.degToRad(ft * modifiers.pitchFrequencyMultiplier + joint.pitchPhase)) + joint.pitchOffset; - } - // calculate yaw - if (animation.filters.hasOwnProperty(jointName) && - 'yawFilter' in animation.filters[jointName]) { - jointRotations.y += joint.yaw * animation.filters[jointName].yawFilter.calculate - (filter.degToRad(ft * modifiers.yawFrequencyMultiplier + joint.yawPhase)) + joint.yawOffset; - } else { - jointRotations.y += joint.yaw * Math.sin - (filter.degToRad(ft * modifiers.yawFrequencyMultiplier + joint.yawPhase)) + joint.yawOffset; - } - // calculate roll - if (animation.filters.hasOwnProperty(jointName) && - 'rollFilter' in animation.filters[jointName]) { - jointRotations.z += joint.roll * animation.filters[jointName].rollFilter.calculate - (filter.degToRad(ft * modifiers.rollFrequencyMultiplier + joint.rollPhase)) + joint.rollOffset; - } else { - jointRotations.z += joint.roll * Math.sin - (filter.degToRad(ft * modifiers.rollFrequencyMultiplier + joint.rollPhase)) + joint.rollOffset; - } - return jointRotations; - }, - - zeroAnimation: function(animation) { - for (i in animation.joints) { - for (j in animation.joints[i]) { - animation.joints[i][j] = 0; - } - } - }, - - blendAnimation: function(sourceAnimation, targetAnimation, percent) { - for (i in targetAnimation.joints) { - targetAnimation.joints[i].pitch += percent * sourceAnimation.joints[i].pitch; - targetAnimation.joints[i].yaw += percent * sourceAnimation.joints[i].yaw; - targetAnimation.joints[i].roll += percent * sourceAnimation.joints[i].roll; - targetAnimation.joints[i].pitchPhase += percent * sourceAnimation.joints[i].pitchPhase; - targetAnimation.joints[i].yawPhase += percent * sourceAnimation.joints[i].yawPhase; - targetAnimation.joints[i].rollPhase += percent * sourceAnimation.joints[i].rollPhase; - targetAnimation.joints[i].pitchOffset += percent * sourceAnimation.joints[i].pitchOffset; - targetAnimation.joints[i].yawOffset += percent * sourceAnimation.joints[i].yawOffset; - targetAnimation.joints[i].rollOffset += percent * sourceAnimation.joints[i].rollOffset; - if (i === "Hips") { - // Hips only - targetAnimation.joints[i].thrust += percent * sourceAnimation.joints[i].thrust; - targetAnimation.joints[i].sway += percent * sourceAnimation.joints[i].sway; - targetAnimation.joints[i].bob += percent * sourceAnimation.joints[i].bob; - targetAnimation.joints[i].thrustPhase += percent * sourceAnimation.joints[i].thrustPhase; - targetAnimation.joints[i].swayPhase += percent * sourceAnimation.joints[i].swayPhase; - targetAnimation.joints[i].bobPhase += percent * sourceAnimation.joints[i].bobPhase; - targetAnimation.joints[i].thrustOffset += percent * sourceAnimation.joints[i].thrustOffset; - targetAnimation.joints[i].swayOffset += percent * sourceAnimation.joints[i].swayOffset; - targetAnimation.joints[i].bobOffset += percent * sourceAnimation.joints[i].bobOffset; - } - } - }, - - deepCopy: function(sourceAnimation, targetAnimation) { - // calibration - targetAnimation.calibration = JSON.parse(JSON.stringify(sourceAnimation.calibration)); - - // harmonics - targetAnimation.harmonics = {}; - if (sourceAnimation.harmonics) { - targetAnimation.harmonics = JSON.parse(JSON.stringify(sourceAnimation.harmonics)); - } - - // filters - targetAnimation.filters = {}; - for (i in sourceAnimation.filters) { - // are any filters specified for this joint? - if (sourceAnimation.filters[i]) { - targetAnimation.filters[i] = sourceAnimation.filters[i]; - // wave shapers - if (sourceAnimation.filters[i].pitchFilter) { - targetAnimation.filters[i].pitchFilter = sourceAnimation.filters[i].pitchFilter; - } - if (sourceAnimation.filters[i].yawFilter) { - targetAnimation.filters[i].yawFilter = sourceAnimation.filters[i].yawFilter; - } - if (sourceAnimation.filters[i].rollFilter) { - targetAnimation.filters[i].rollFilter = sourceAnimation.filters[i].rollFilter; - } - // LP filters - if (sourceAnimation.filters[i].swayLPFilter) { - targetAnimation.filters[i].swayLPFilter = sourceAnimation.filters[i].swayLPFilter; - } - if (sourceAnimation.filters[i].bobLPFilter) { - targetAnimation.filters[i].bobLPFilter = sourceAnimation.filters[i].bobLPFilter; - } - if (sourceAnimation.filters[i].thrustLPFilter) { - targetAnimation.filters[i].thrustLPFilter = sourceAnimation.filters[i].thrustLPFilter; - } - } - } - // joints - targetAnimation.joints = JSON.parse(JSON.stringify(sourceAnimation.joints)); - } - } - -})(); // end animation object literal - -// ReachPose datafile wrapper object -ReachPose = function(reachPoseName) { - this.name = reachPoseName; - this.reachPoseParameters = walkAssets.getReachPoseParameters(reachPoseName); - this.reachPoseDataFile = walkAssets.getReachPoseDataFile(reachPoseName); - this.progress = 0; - this.smoothingFilter = filter.createAveragingFilter(this.reachPoseParameters.smoothing); - this.currentStrength = function() { - // apply optionally smoothed (D)ASDR envelope to reach pose's strength / influence whilst active - var segmentProgress = undefined; // progress through chosen segment - var segmentTimeDelta = undefined; // total change in time over chosen segment - var segmentStrengthDelta = undefined; // total change in strength over chosen segment - var lastStrength = undefined; // the last value the previous segment held - var currentStrength = undefined; // return value - - // select parameters based on segment (a segment being one of (D),A,S,D or R) - if (this.progress >= this.reachPoseParameters.sustain.timing) { - // release segment - segmentProgress = this.progress - this.reachPoseParameters.sustain.timing; - segmentTimeDelta = this.reachPoseParameters.release.timing - this.reachPoseParameters.sustain.timing; - segmentStrengthDelta = this.reachPoseParameters.release.strength - this.reachPoseParameters.sustain.strength; - lastStrength = this.reachPoseParameters.sustain.strength; - } else if (this.progress >= this.reachPoseParameters.decay.timing) { - // sustain phase - segmentProgress = this.progress - this.reachPoseParameters.decay.timing; - segmentTimeDelta = this.reachPoseParameters.sustain.timing - this.reachPoseParameters.decay.timing; - segmentStrengthDelta = this.reachPoseParameters.sustain.strength - this.reachPoseParameters.decay.strength; - lastStrength = this.reachPoseParameters.decay.strength; - } else if (this.progress >= this.reachPoseParameters.attack.timing) { - // decay phase - segmentProgress = this.progress - this.reachPoseParameters.attack.timing; - segmentTimeDelta = this.reachPoseParameters.decay.timing - this.reachPoseParameters.attack.timing; - segmentStrengthDelta = this.reachPoseParameters.decay.strength - this.reachPoseParameters.attack.strength; - lastStrength = this.reachPoseParameters.attack.strength; - } else if (this.progress >= this.reachPoseParameters.delay.timing) { - // attack phase - segmentProgress = this.progress - this.reachPoseParameters.delay.timing; - segmentTimeDelta = this.reachPoseParameters.attack.timing - this.reachPoseParameters.delay.timing; - segmentStrengthDelta = this.reachPoseParameters.attack.strength - this.reachPoseParameters.delay.strength; - lastStrength = 0; //this.delay.strength; - } else { - // delay phase - segmentProgress = this.progress; - segmentTimeDelta = this.reachPoseParameters.delay.timing; - segmentStrengthDelta = this.reachPoseParameters.delay.strength; - lastStrength = 0; - } - currentStrength = segmentTimeDelta > 0 ? lastStrength + segmentStrengthDelta * segmentProgress / segmentTimeDelta - : lastStrength; - // smooth off the response curve - currentStrength = this.smoothingFilter.process(currentStrength); - return currentStrength; - } -}; - -// constructor with default parameters -TransitionParameters = function() { - this.duration = 0.5; - this.easingLower = {x:0.25, y:0.75}; - this.easingUpper = {x:0.75, y:0.25}; - this.reachPoses = []; -} - -const QUARTER_CYCLE = 90; -const HALF_CYCLE = 180; -const THREE_QUARTER_CYCLE = 270; -const FULL_CYCLE = 360; - -// constructor for animation Transition -Transition = function(nextAnimation, lastAnimation, lastTransition, playTransitionReachPoses) { - - if (playTransitionReachPoses === undefined) { - playTransitionReachPoses = true; - } - - // record the current state of animation - this.nextAnimation = nextAnimation; - this.lastAnimation = lastAnimation; - this.lastTransition = lastTransition; - - // collect information about the currently playing animation - this.direction = motion.direction; - this.lastDirection = motion.lastDirection; - this.lastFrequencyTimeWheelPos = motion.frequencyTimeWheelPos; - this.lastFrequencyTimeIncrement = motion.averageFrequencyTimeIncrement; - this.lastFrequencyTimeWheelRadius = motion.frequencyTimeWheelRadius; - this.degreesToTurn = 0; // total degrees to turn the ft wheel before the avatar stops (walk only) - this.degreesRemaining = 0; // remaining degrees to turn the ft wheel before the avatar stops (walk only) - this.lastElapsedFTDegrees = motion.elapsedFTDegrees; // degrees elapsed since last transition start - motion.elapsedFTDegrees = 0; // reset ready for the next transition - motion.frequencyTimeWheelPos = 0; // start the next animation's frequency time wheel from zero - - // set parameters for the transition - this.parameters = new TransitionParameters(); - this.liveReachPoses = []; - if (walkAssets && lastAnimation && nextAnimation) { - // overwrite this.parameters with any transition parameters specified for this particular transition - walkAssets.getTransitionParameters(lastAnimation, nextAnimation, this.parameters); - // fire up any reach poses for this transition - if (playTransitionReachPoses) { - for (poseName in this.parameters.reachPoses) { - this.liveReachPoses.push(new ReachPose(this.parameters.reachPoses[poseName])); - } - } - } - this.startTime = new Date().getTime(); // Starting timestamp (seconds) - this.progress = 0; // how far are we through the transition? - this.filteredProgress = 0; - - // coming to a halt whilst walking? if so, will need a clean stopping point defined - if (motion.isComingToHalt) { - - const FULL_CYCLE_THRESHOLD = 320; - const HALF_CYCLE_THRESHOLD = 140; - const CYCLE_COMMIT_THRESHOLD = 5; - - // how many degrees do we need to turn the walk wheel to finish walking with both feet on the ground? - if (this.lastElapsedFTDegrees < CYCLE_COMMIT_THRESHOLD) { - // just stop the walk cycle right here and blend to idle - this.degreesToTurn = 0; - } else if (this.lastElapsedFTDegrees < HALF_CYCLE) { - // we have not taken a complete step yet, so we advance to the second stop angle - this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos; - } else if (this.lastFrequencyTimeWheelPos > 0 && this.lastFrequencyTimeWheelPos <= HALF_CYCLE_THRESHOLD) { - // complete the step and stop at 180 - this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos; - } else if (this.lastFrequencyTimeWheelPos > HALF_CYCLE_THRESHOLD && this.lastFrequencyTimeWheelPos <= HALF_CYCLE) { - // complete the step and next then stop at 0 - this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos + HALF_CYCLE; - } else if (this.lastFrequencyTimeWheelPos > HALF_CYCLE && this.lastFrequencyTimeWheelPos <= FULL_CYCLE_THRESHOLD) { - // complete the step and stop at 0 - this.degreesToTurn = FULL_CYCLE - this.lastFrequencyTimeWheelPos; - } else { - // complete the step and the next then stop at 180 - this.degreesToTurn = FULL_CYCLE - this.lastFrequencyTimeWheelPos + HALF_CYCLE; - } - - // transition length in this case should be directly proportional to the remaining degrees to turn - var MIN_FT_INCREMENT = 5.0; // degrees per frame - var MIN_TRANSITION_DURATION = 0.4; - const TWO_THIRDS = 0.6667; - this.lastFrequencyTimeIncrement *= TWO_THIRDS; // help ease the transition - var lastFrequencyTimeIncrement = this.lastFrequencyTimeIncrement > MIN_FT_INCREMENT ? - this.lastFrequencyTimeIncrement : MIN_FT_INCREMENT; - var timeToFinish = Math.max(motion.deltaTime * this.degreesToTurn / lastFrequencyTimeIncrement, - MIN_TRANSITION_DURATION); - this.parameters.duration = timeToFinish; - this.degreesRemaining = this.degreesToTurn; - } - - // deal with transition recursion (overlapping transitions) - this.recursionDepth = 0; - this.incrementRecursion = function() { - this.recursionDepth += 1; - - // cancel any continued motion - this.degreesToTurn = 0; - - // limit the number of layered / nested transitions - if (this.lastTransition !== nullTransition) { - this.lastTransition.incrementRecursion(); - if (this.lastTransition.recursionDepth > MAX_TRANSITION_RECURSION) { - this.lastTransition = nullTransition; - } - } - }; - if (this.lastTransition !== nullTransition) { - this.lastTransition.incrementRecursion(); - } - - // end of transition initialisation. begin Transition public methods - - // keep up the pace for the frequency time wheel for the last animation - this.advancePreviousFrequencyTimeWheel = function(deltaTime) { - var wheelAdvance = undefined; - - if (this.lastAnimation === avatar.selectedWalkBlend && - this.nextAnimation === avatar.selectedIdle) { - if (this.degreesRemaining <= 0) { - // stop continued motion - wheelAdvance = 0; - if (motion.isComingToHalt) { - if (this.lastFrequencyTimeWheelPos < QUARTER_CYCLE) { - this.lastFrequencyTimeWheelPos = 0; - } else { - this.lastFrequencyTimeWheelPos = HALF_CYCLE; - } - } - } else { - wheelAdvance = this.lastFrequencyTimeIncrement; - var distanceToTravel = avatar.calibration.strideLength * wheelAdvance / HALF_CYCLE; - if (this.degreesRemaining <= 0) { - distanceToTravel = 0; - this.degreesRemaining = 0; - } else { - this.degreesRemaining -= wheelAdvance; - } - } - } else { - wheelAdvance = this.lastFrequencyTimeIncrement; - } - - // advance the ft wheel - this.lastFrequencyTimeWheelPos += wheelAdvance; - if (this.lastFrequencyTimeWheelPos >= FULL_CYCLE) { - this.lastFrequencyTimeWheelPos = this.lastFrequencyTimeWheelPos % FULL_CYCLE; - } - - // advance ft wheel for the nested (previous) Transition - if (this.lastTransition !== nullTransition) { - this.lastTransition.advancePreviousFrequencyTimeWheel(deltaTime); - } - // update the lastElapsedFTDegrees for short stepping - this.lastElapsedFTDegrees += wheelAdvance; - this.degreesTurned += wheelAdvance; - }; - - this.updateProgress = function() { - const MILLISECONDS_CONVERT = 1000; - const ACCURACY_INCREASER = 1000; - var elapasedTime = (new Date().getTime() - this.startTime) / MILLISECONDS_CONVERT; - this.progress = elapasedTime / this.parameters.duration; - this.progress = Math.round(this.progress * ACCURACY_INCREASER) / ACCURACY_INCREASER; - - // updated nested transition/s - if (this.lastTransition !== nullTransition) { - if (this.lastTransition.updateProgress() === TRANSITION_COMPLETE) { - // the previous transition is now complete - this.lastTransition = nullTransition; - } - } - - // update any reachPoses - for (pose in this.liveReachPoses) { - // use independent timing for reachPoses - this.liveReachPoses[pose].progress += (motion.deltaTime / this.liveReachPoses[pose].reachPoseParameters.duration); - if (this.liveReachPoses[pose].progress >= 1) { - // time to kill off this reach pose - this.liveReachPoses.splice(pose, 1); - } - } - - // update transition progress - this.filteredProgress = filter.bezier(this.progress, this.parameters.easingLower, this.parameters.easingUpper); - return this.progress >= 1 ? TRANSITION_COMPLETE : false; - }; - - this.blendTranslations = function(frequencyTimeWheelPos, direction) { - var lastTranslations = {x:0, y:0, z:0}; - var nextTranslations = animationOperations.calculateTranslations(this.nextAnimation, - frequencyTimeWheelPos, - direction); - // are we blending with a previous, still live transition? - if (this.lastTransition !== nullTransition) { - lastTranslations = this.lastTransition.blendTranslations(this.lastFrequencyTimeWheelPos, - this.lastDirection); - } else { - lastTranslations = animationOperations.calculateTranslations(this.lastAnimation, - this.lastFrequencyTimeWheelPos, - this.lastDirection); - } - - // blend last / next translations - nextTranslations = Vec3.multiply(this.filteredProgress, nextTranslations); - lastTranslations = Vec3.multiply((1 - this.filteredProgress), lastTranslations); - nextTranslations = Vec3.sum(nextTranslations, lastTranslations); - - if (this.liveReachPoses.length > 0) { - for (pose in this.liveReachPoses) { - var reachPoseStrength = this.liveReachPoses[pose].currentStrength(); - var poseTranslations = animationOperations.calculateTranslations( - this.liveReachPoses[pose].reachPoseDataFile, - frequencyTimeWheelPos, - direction); - - // can't use Vec3 operations here, as if x,y or z is zero, the reachPose should have no influence at all - if (Math.abs(poseTranslations.x) > 0) { - nextTranslations.x = reachPoseStrength * poseTranslations.x + (1 - reachPoseStrength) * nextTranslations.x; - } - if (Math.abs(poseTranslations.y) > 0) { - nextTranslations.y = reachPoseStrength * poseTranslations.y + (1 - reachPoseStrength) * nextTranslations.y; - } - if (Math.abs(poseTranslations.z) > 0) { - nextTranslations.z = reachPoseStrength * poseTranslations.z + (1 - reachPoseStrength) * nextTranslations.z; - } - } - } - return nextTranslations; - }; - - this.blendRotations = function(jointName, frequencyTimeWheelPos, direction) { - var lastRotations = {x:0, y:0, z:0}; - var nextRotations = animationOperations.calculateRotations(jointName, - this.nextAnimation, - frequencyTimeWheelPos, - direction); - - // are we blending with a previous, still live transition? - if (this.lastTransition !== nullTransition) { - lastRotations = this.lastTransition.blendRotations(jointName, - this.lastFrequencyTimeWheelPos, - this.lastDirection); - } else { - lastRotations = animationOperations.calculateRotations(jointName, - this.lastAnimation, - this.lastFrequencyTimeWheelPos, - this.lastDirection); - } - // blend last / next translations - nextRotations = Vec3.multiply(this.filteredProgress, nextRotations); - lastRotations = Vec3.multiply((1 - this.filteredProgress), lastRotations); - nextRotations = Vec3.sum(nextRotations, lastRotations); - - // are there reachPoses defined for this transition? - if (this.liveReachPoses.length > 0) { - for (pose in this.liveReachPoses) { - var reachPoseStrength = this.liveReachPoses[pose].currentStrength(); - var poseRotations = animationOperations.calculateRotations(jointName, - this.liveReachPoses[pose].reachPoseDataFile, - frequencyTimeWheelPos, - direction); - - // don't use Vec3 operations here, as if x,y or z is zero, the reach pose should have no influence at all - if (Math.abs(poseRotations.x) > 0) { - nextRotations.x = reachPoseStrength * poseRotations.x + (1 - reachPoseStrength) * nextRotations.x; - } - if (Math.abs(poseRotations.y) > 0) { - nextRotations.y = reachPoseStrength * poseRotations.y + (1 - reachPoseStrength) * nextRotations.y; - } - if (Math.abs(poseRotations.z) > 0) { - nextRotations.z = reachPoseStrength * poseRotations.z + (1 - reachPoseStrength) * nextRotations.z; - } - } - } - return nextRotations; - }; -}; // end Transition constructor - -// individual joint modifiers -FrequencyMultipliers = function(joint, direction) { - // gather multipliers - this.pitchFrequencyMultiplier = 1; - this.yawFrequencyMultiplier = 1; - this.rollFrequencyMultiplier = 1; - this.swayFrequencyMultiplier = 1; - this.bobFrequencyMultiplier = 1; - this.thrustFrequencyMultiplier = 1; - - if (joint) { - if (joint.pitchFrequencyMultiplier) { - this.pitchFrequencyMultiplier = joint.pitchFrequencyMultiplier; - } - if (joint.yawFrequencyMultiplier) { - this.yawFrequencyMultiplier = joint.yawFrequencyMultiplier; - } - if (joint.rollFrequencyMultiplier) { - this.rollFrequencyMultiplier = joint.rollFrequencyMultiplier; - } - if (joint.swayFrequencyMultiplier) { - this.swayFrequencyMultiplier = joint.swayFrequencyMultiplier; - } - if (joint.bobFrequencyMultiplier) { - this.bobFrequencyMultiplier = joint.bobFrequencyMultiplier; - } - if (joint.thrustFrequencyMultiplier) { - this.thrustFrequencyMultiplier = joint.thrustFrequencyMultiplier; - } - } +// +// walkApi.js +// version 1.3 +// +// Created by David Wooldridge, June 2015 +// Copyright © 2014 - 2015 High Fidelity, Inc. +// +// Exposes API for use by walk.js version 1.2+. +// +// Editing tools for animation data files available here: https://github.com/DaveDubUK/walkTools +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// included here to ensure walkApi.js can be used as an API, separate from walk.js +Script.include("./libraries/walkConstants.js"); + +Avatar = function() { + // if Hydras are connected, the only way to enable use is to never set any arm joint rotation + this.hydraCheck = function() { + // function courtesy of Thijs Wenker (frisbee.js) + var numberOfButtons = Controller.getNumberOfButtons(); + var numberOfTriggers = Controller.getNumberOfTriggers(); + var numberOfSpatialControls = Controller.getNumberOfSpatialControls(); + const HYDRA_BUTTONS = 12; + const HYDRA_TRIGGERS = 2; + const HYDRA_CONTROLLERS_PER_TRIGGER = 2; + var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers; + if (numberOfButtons == HYDRA_BUTTONS && + numberOfTriggers == HYDRA_TRIGGERS && + controllersPerTrigger == HYDRA_CONTROLLERS_PER_TRIGGER) { + print('walk.js info: Razer Hydra detected. Setting arms free (not controlled by script)'); + return true; + } else { + print('walk.js info: Razer Hydra not detected. Arms will be controlled by script.'); + return false; + } + } + // settings + this.headFree = true; + this.armsFree = this.hydraCheck(); // automatically sets true to enable Hydra support - temporary fix + this.makesFootStepSounds = false; + this.blenderPreRotations = false; // temporary fix + this.animationSet = undefined; // currently just one animation set + this.setAnimationSet = function(animationSet) { + this.animationSet = animationSet; + switch (animationSet) { + case 'standardMale': + this.selectedIdle = walkAssets.getAnimationDataFile("MaleIdle"); + this.selectedWalk = walkAssets.getAnimationDataFile("MaleWalk"); + this.selectedWalkBackwards = walkAssets.getAnimationDataFile("MaleWalkBackwards"); + this.selectedSideStepLeft = walkAssets.getAnimationDataFile("MaleSideStepLeft"); + this.selectedSideStepRight = walkAssets.getAnimationDataFile("MaleSideStepRight"); + this.selectedWalkBlend = walkAssets.getAnimationDataFile("WalkBlend"); + this.selectedHover = walkAssets.getAnimationDataFile("MaleHover"); + this.selectedFly = walkAssets.getAnimationDataFile("MaleFly"); + this.selectedFlyBackwards = walkAssets.getAnimationDataFile("MaleFlyBackwards"); + this.selectedFlyDown = walkAssets.getAnimationDataFile("MaleFlyDown"); + this.selectedFlyUp = walkAssets.getAnimationDataFile("MaleFlyUp"); + this.selectedFlyBlend = walkAssets.getAnimationDataFile("FlyBlend"); + this.currentAnimation = this.selectedIdle; + return; + } + } + this.setAnimationSet('standardMale'); + + // calibration + this.calibration = { + hipsToFeet: 1, + strideLength: this.selectedWalk.calibration.strideLength + } + this.distanceFromSurface = 0; + this.calibrate = function() { + // Triple check: measurements are taken three times to ensure accuracy - the first result is often too large + const MAX_ATTEMPTS = 3; + var attempts = MAX_ATTEMPTS; + var extraAttempts = 0; + do { + for (joint in walkAssets.animationReference.joints) { + var IKChain = walkAssets.animationReference.joints[joint].IKChain; + + // only need to zero right leg IK chain and hips + if (IKChain === "RightLeg" || joint === "Hips" ) { + MyAvatar.setJointRotation(joint, Quat.fromPitchYawRollDegrees(0, 0, 0)); + } + } + this.calibration.hipsToFeet = MyAvatar.getJointPosition("Hips").y - MyAvatar.getJointPosition("RightToeBase").y; + + // maybe measuring before Blender pre-rotations have been applied? + if (this.calibration.hipsToFeet < 0 && this.blenderPreRotations) { + this.calibration.hipsToFeet *= -1; + } + + if (this.calibration.hipsToFeet === 0 && extraAttempts < 100) { + attempts++; + extraAttempts++;// Interface can sometimes report zero for hips to feet. if so, we try again. + } + } while (attempts-- > 1) + + // just in case + if (this.calibration.hipsToFeet <= 0 || isNaN(this.calibration.hipsToFeet)) { + this.calibration.hipsToFeet = 1; + print('walk.js error: Unable to get a non-zero measurement for the avatar hips to feet measure. Hips to feet set to default value ('+ + this.calibration.hipsToFeet.toFixed(3)+'m). This will cause some foot sliding. If your avatar has only just appeared, it is recommended that you re-load the walk script.'); + } else { + print('walk.js info: Hips to feet calibrated to '+this.calibration.hipsToFeet.toFixed(3)+'m'); + } + } + + // pose the fingers + this.poseFingers = function() { + for (knuckle in walkAssets.animationReference.leftHand) { + if (walkAssets.animationReference.leftHand[knuckle].IKChain === "LeftHandThumb") { + MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(0, 0, -4)); + } else { + MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(16, 0, 5)); + } + } + for (knuckle in walkAssets.animationReference.rightHand) { + if (walkAssets.animationReference.rightHand[knuckle].IKChain === "RightHandThumb") { + MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(0, 0, 4)); + } else { + MyAvatar.setJointRotation(knuckle, Quat.fromPitchYawRollDegrees(16, 0, -5)); + } + } + }; + this.calibrate(); + this.poseFingers(); + + // footsteps + this.nextStep = RIGHT; // the first step is right, because the waveforms say so + this.leftAudioInjector = null; + this.rightAudioInjector = null; + this.makeFootStepSound = function() { + // correlate footstep volume with avatar speed. place the audio source at the feet, not the hips + const SPEED_THRESHOLD = 0.4; + const VOLUME_ATTENUATION = 0.8; + const MIN_VOLUME = 0.5; + var volume = Vec3.length(motion.velocity) > SPEED_THRESHOLD ? + VOLUME_ATTENUATION * Vec3.length(motion.velocity) / MAX_WALK_SPEED : MIN_VOLUME; + volume = volume > 1 ? 1 : volume; // occurs when landing at speed - can walk faster than max walking speed + var options = { + position: Vec3.sum(MyAvatar.position, {x:0, y: -this.calibration.hipsToFeet, z:0}), + volume: volume + }; + if (this.nextStep === RIGHT) { + if (this.rightAudioInjector === null) { + this.rightAudioInjector = Audio.playSound(walkAssets.footsteps[0], options); + } else { + this.rightAudioInjector.setOptions(options); + this.rightAudioInjector.restart(); + } + this.nextStep = LEFT; + } else if (this.nextStep === LEFT) { + if (this.leftAudioInjector === null) { + this.leftAudioInjector = Audio.playSound(walkAssets.footsteps[1], options); + } else { + this.leftAudioInjector.setOptions(options); + this.leftAudioInjector.restart(); + } + this.nextStep = RIGHT; + } + } +}; + +// constructor for the Motion object +Motion = function() { + this.isLive = true; + // locomotion status + this.state = STATIC; + this.nextState = STATIC; + this.isMoving = false; + this.isWalkingSpeed = false; + this.isFlyingSpeed = false; + this.isAccelerating = false; + this.isDecelerating = false; + this.isDeceleratingFast = false; + this.isComingToHalt = false; + this.directedAcceleration = 0; + + // used to make sure at least one step has been taken when transitioning from a walk cycle + this.elapsedFTDegrees = 0; + + // the current transition (any layered transitions are nested within this transition) + this.currentTransition = null; + + // orientation, locomotion and timing + this.velocity = {x:0, y:0, z:0}; + this.acceleration = {x:0, y:0, z:0}; + this.yaw = Quat.safeEulerAngles(MyAvatar.orientation).y; + this.yawDelta = 0; + this.yawDeltaAcceleration = 0; + this.direction = FORWARDS; + this.deltaTime = 0; + + // historical orientation, locomotion and timing + this.lastDirection = FORWARDS; + this.lastVelocity = {x:0, y:0, z:0}; + this.lastYaw = Quat.safeEulerAngles(MyAvatar.orientation).y; + this.lastYawDelta = 0; + this.lastYawDeltaAcceleration = 0; + + // Quat.safeEulerAngles(MyAvatar.orientation).y tends to repeat values between frames, so values are filtered + var YAW_SMOOTHING = 22; + this.yawFilter = filter.createAveragingFilter(YAW_SMOOTHING); + this.deltaTimeFilter = filter.createAveragingFilter(YAW_SMOOTHING); + this.yawDeltaAccelerationFilter = filter.createAveragingFilter(YAW_SMOOTHING); + + // assess locomotion state + this.assess = function(deltaTime) { + // calculate avatar frame speed, velocity and acceleration + this.deltaTime = deltaTime; + this.velocity = Vec3.multiplyQbyV(Quat.inverse(MyAvatar.orientation), MyAvatar.getVelocity()); + var lateralVelocity = Math.sqrt(Math.pow(this.velocity.x, 2) + Math.pow(this.velocity.z, 2)); + + // MyAvatar.getAcceleration() currently not working. bug report submitted: https://worklist.net/20527 + var acceleration = {x:0, y:0, z:0}; + this.acceleration.x = (this.velocity.x - this.lastVelocity.x) / deltaTime; + this.acceleration.y = (this.velocity.y - this.lastVelocity.y) / deltaTime; + this.acceleration.z = (this.velocity.z - this.lastVelocity.z) / deltaTime; + + // MyAvatar.getAngularVelocity and MyAvatar.getAngularAcceleration currently not working. bug report submitted + this.yaw = Quat.safeEulerAngles(MyAvatar.orientation).y; + if (this.lastYaw < 0 && this.yaw > 0 || this.lastYaw > 0 && this.yaw < 0) { + this.lastYaw *= -1; + } + var timeDelta = this.deltaTimeFilter.process(deltaTime); + this.yawDelta = filter.degToRad(this.yawFilter.process(this.lastYaw - this.yaw)) / timeDelta; + this.yawDeltaAcceleration = this.yawDeltaAccelerationFilter.process(this.lastYawDelta - this.yawDelta) / timeDelta; + + // how far above the surface is the avatar? (for testing / validation purposes) + var pickRay = {origin: MyAvatar.position, direction: {x:0, y:-1, z:0}}; + var distanceFromSurface = Entities.findRayIntersectionBlocking(pickRay).distance; + avatar.distanceFromSurface = distanceFromSurface - avatar.calibration.hipsToFeet; + + // determine principle direction of locomotion + var FWD_BACK_BIAS = 100; // helps prevent false sidestep condition detection when banking hard + if (Math.abs(this.velocity.x) > Math.abs(this.velocity.y) && + Math.abs(this.velocity.x) > FWD_BACK_BIAS * Math.abs(this.velocity.z)) { + if (this.velocity.x < 0) { + this.directedAcceleration = -this.acceleration.x; + this.direction = LEFT; + } else if (this.velocity.x > 0){ + this.directedAcceleration = this.acceleration.x; + this.direction = RIGHT; + } + } else if (Math.abs(this.velocity.y) > Math.abs(this.velocity.x) && + Math.abs(this.velocity.y) > Math.abs(this.velocity.z)) { + if (this.velocity.y > 0) { + this.directedAcceleration = this.acceleration.y; + this.direction = UP; + } else if (this.velocity.y < 0) { + this.directedAcceleration = -this.acceleration.y; + this.direction = DOWN; + } + } else if (FWD_BACK_BIAS * Math.abs(this.velocity.z) > Math.abs(this.velocity.x) && + Math.abs(this.velocity.z) > Math.abs(this.velocity.y)) { + if (this.velocity.z < 0) { + this.direction = FORWARDS; + this.directedAcceleration = -this.acceleration.z; + } else if (this.velocity.z > 0) { + this.directedAcceleration = this.acceleration.z; + this.direction = BACKWARDS; + } + } else { + this.direction = NONE; + this.directedAcceleration = 0; + } + + // set speed flags + if (Vec3.length(this.velocity) < MOVE_THRESHOLD) { + this.isMoving = false; + this.isWalkingSpeed = false; + this.isFlyingSpeed = false; + this.isComingToHalt = false; + } else if (Vec3.length(this.velocity) < MAX_WALK_SPEED) { + this.isMoving = true; + this.isWalkingSpeed = true; + this.isFlyingSpeed = false; + } else { + this.isMoving = true; + this.isWalkingSpeed = false; + this.isFlyingSpeed = true; + } + + // set acceleration flags + if (this.directedAcceleration > ACCELERATION_THRESHOLD) { + this.isAccelerating = true; + this.isDecelerating = false; + this.isDeceleratingFast = false; + this.isComingToHalt = false; + } else if (this.directedAcceleration < DECELERATION_THRESHOLD) { + this.isAccelerating = false; + this.isDecelerating = true; + this.isDeceleratingFast = (this.directedAcceleration < FAST_DECELERATION_THRESHOLD); + } else { + this.isAccelerating = false; + this.isDecelerating = false; + this.isDeceleratingFast = false; + } + + // use the gathered information to build up some spatial awareness + var isOnSurface = (avatar.distanceFromSurface < ON_SURFACE_THRESHOLD); + var isUnderGravity = (avatar.distanceFromSurface < GRAVITY_THRESHOLD); + var isTakingOff = (isUnderGravity && this.velocity.y > OVERCOME_GRAVITY_SPEED); + var isComingInToLand = (isUnderGravity && this.velocity.y < -OVERCOME_GRAVITY_SPEED); + var aboutToLand = isComingInToLand && avatar.distanceFromSurface < LANDING_THRESHOLD; + var surfaceMotion = isOnSurface && this.isMoving; + var acceleratingAndAirborne = this.isAccelerating && !isOnSurface; + var goingTooFastToWalk = !this.isDecelerating && this.isFlyingSpeed; + var movingDirectlyUpOrDown = (this.direction === UP || this.direction === DOWN) + var maybeBouncing = Math.abs(this.acceleration.y > BOUNCE_ACCELERATION_THRESHOLD) ? true : false; + + // we now have enough information to set the appropriate locomotion mode + switch (this.state) { + case STATIC: + var staticToAirMotion = this.isMoving && (acceleratingAndAirborne || goingTooFastToWalk || + (movingDirectlyUpOrDown && !isOnSurface)); + var staticToSurfaceMotion = surfaceMotion && !motion.isComingToHalt && !movingDirectlyUpOrDown && + !this.isDecelerating && lateralVelocity > MOVE_THRESHOLD; + + if (staticToAirMotion) { + this.nextState = AIR_MOTION; + } else if (staticToSurfaceMotion) { + this.nextState = SURFACE_MOTION; + } else { + this.nextState = STATIC; + } + break; + + case SURFACE_MOTION: + var surfaceMotionToStatic = !this.isMoving || + (this.isDecelerating && motion.lastDirection !== DOWN && surfaceMotion && + !maybeBouncing && Vec3.length(this.velocity) < MAX_WALK_SPEED); + var surfaceMotionToAirMotion = (acceleratingAndAirborne || goingTooFastToWalk || movingDirectlyUpOrDown) && + (!surfaceMotion && isTakingOff) || + (!surfaceMotion && this.isMoving && !isComingInToLand); + + if (surfaceMotionToStatic) { + // working on the assumption that stopping is now inevitable + if (!motion.isComingToHalt && isOnSurface) { + motion.isComingToHalt = true; + } + this.nextState = STATIC; + } else if (surfaceMotionToAirMotion) { + this.nextState = AIR_MOTION; + } else { + this.nextState = SURFACE_MOTION; + } + break; + + case AIR_MOTION: + var airMotionToSurfaceMotion = (surfaceMotion || aboutToLand) && !movingDirectlyUpOrDown; + var airMotionToStatic = !this.isMoving && this.direction === this.lastDirection; + + if (airMotionToSurfaceMotion){ + this.nextState = SURFACE_MOTION; + } else if (airMotionToStatic) { + this.nextState = STATIC; + } else { + this.nextState = AIR_MOTION; + } + break; + } + } + + // frequency time wheel (foot / ground speed matching) + const DEFAULT_HIPS_TO_FEET = 1; + this.frequencyTimeWheelPos = 0; + this.frequencyTimeWheelRadius = DEFAULT_HIPS_TO_FEET / 2; + this.recentFrequencyTimeIncrements = []; + const FT_WHEEL_HISTORY_LENGTH = 8; + for (var i = 0; i < FT_WHEEL_HISTORY_LENGTH; i++) { + this.recentFrequencyTimeIncrements.push(0); + } + this.averageFrequencyTimeIncrement = 0; + + this.advanceFrequencyTimeWheel = function(angle){ + this.elapsedFTDegrees += angle; + // keep a running average of increments for use in transitions (used during transitioning) + this.recentFrequencyTimeIncrements.push(angle); + this.recentFrequencyTimeIncrements.shift(); + for (increment in this.recentFrequencyTimeIncrements) { + this.averageFrequencyTimeIncrement += this.recentFrequencyTimeIncrements[increment]; + } + this.averageFrequencyTimeIncrement /= this.recentFrequencyTimeIncrements.length; + this.frequencyTimeWheelPos += angle; + const FULL_CIRCLE = 360; + if (this.frequencyTimeWheelPos >= FULL_CIRCLE) { + this.frequencyTimeWheelPos = this.frequencyTimeWheelPos % FULL_CIRCLE; + } + } + + this.saveHistory = function() { + this.lastDirection = this.direction; + this.lastVelocity = this.velocity; + this.lastYaw = this.yaw; + this.lastYawDelta = this.yawDelta; + this.lastYawDeltaAcceleration = this.yawDeltaAcceleration; + } +}; // end Motion constructor + +// animation manipulation object +animationOperations = (function() { + + return { + + // helper function for renderMotion(). calculate joint translations based on animation file settings and frequency * time + calculateTranslations: function(animation, ft, direction) { + var jointName = "Hips"; + var joint = animation.joints[jointName]; + var jointTranslations = {x:0, y:0, z:0}; + + // gather modifiers and multipliers + modifiers = new FrequencyMultipliers(joint, direction); + + // calculate translations. Use synthesis filters where specified by the animation data file. + + // sway (oscillation on the x-axis) + if (animation.filters.hasOwnProperty(jointName) && 'swayFilter' in animation.filters[jointName]) { + jointTranslations.x = joint.sway * animation.filters[jointName].swayFilter.calculate + (filter.degToRad(modifiers.swayFrequencyMultiplier * ft + joint.swayPhase)) + joint.swayOffset; + } else { + jointTranslations.x = joint.sway * Math.sin + (filter.degToRad(modifiers.swayFrequencyMultiplier * ft + joint.swayPhase)) + joint.swayOffset; + } + // bob (oscillation on the y-axis) + if (animation.filters.hasOwnProperty(jointName) && 'bobFilter' in animation.filters[jointName]) { + jointTranslations.y = joint.bob * animation.filters[jointName].bobFilter.calculate + (filter.degToRad(modifiers.bobFrequencyMultiplier * ft + joint.bobPhase)) + joint.bobOffset; + } else { + jointTranslations.y = joint.bob * Math.sin + (filter.degToRad(modifiers.bobFrequencyMultiplier * ft + joint.bobPhase)) + joint.bobOffset; + + if (animation.filters.hasOwnProperty(jointName) && 'bobLPFilter' in animation.filters[jointName]) { + jointTranslations.y = filter.clipTrough(jointTranslations.y, joint, 2); + jointTranslations.y = animation.filters[jointName].bobLPFilter.process(jointTranslations.y); + } + } + // thrust (oscillation on the z-axis) + if (animation.filters.hasOwnProperty(jointName) && 'thrustFilter' in animation.filters[jointName]) { + jointTranslations.z = joint.thrust * animation.filters[jointName].thrustFilter.calculate + (filter.degToRad(modifiers.thrustFrequencyMultiplier * ft + joint.thrustPhase)) + joint.thrustOffset; + } else { + jointTranslations.z = joint.thrust * Math.sin + (filter.degToRad(modifiers.thrustFrequencyMultiplier * ft + joint.thrustPhase)) + joint.thrustOffset; + } + return jointTranslations; + }, + + // helper function for renderMotion(). calculate joint rotations based on animation file settings and frequency * time + calculateRotations: function(jointName, animation, ft, direction) { + var joint = animation.joints[jointName]; + var jointRotations = {x:0, y:0, z:0}; + + if (avatar.blenderPreRotations) { + jointRotations = Vec3.sum(jointRotations, walkAssets.blenderPreRotations.joints[jointName]); + } + + // gather frequency multipliers for this joint + modifiers = new FrequencyMultipliers(joint, direction); + + // calculate rotations. Use synthesis filters where specified by the animation data file. + + // calculate pitch + if (animation.filters.hasOwnProperty(jointName) && + 'pitchFilter' in animation.filters[jointName]) { + jointRotations.x += joint.pitch * animation.filters[jointName].pitchFilter.calculate + (filter.degToRad(ft * modifiers.pitchFrequencyMultiplier + joint.pitchPhase)) + joint.pitchOffset; + } else { + jointRotations.x += joint.pitch * Math.sin + (filter.degToRad(ft * modifiers.pitchFrequencyMultiplier + joint.pitchPhase)) + joint.pitchOffset; + } + // calculate yaw + if (animation.filters.hasOwnProperty(jointName) && + 'yawFilter' in animation.filters[jointName]) { + jointRotations.y += joint.yaw * animation.filters[jointName].yawFilter.calculate + (filter.degToRad(ft * modifiers.yawFrequencyMultiplier + joint.yawPhase)) + joint.yawOffset; + } else { + jointRotations.y += joint.yaw * Math.sin + (filter.degToRad(ft * modifiers.yawFrequencyMultiplier + joint.yawPhase)) + joint.yawOffset; + } + // calculate roll + if (animation.filters.hasOwnProperty(jointName) && + 'rollFilter' in animation.filters[jointName]) { + jointRotations.z += joint.roll * animation.filters[jointName].rollFilter.calculate + (filter.degToRad(ft * modifiers.rollFrequencyMultiplier + joint.rollPhase)) + joint.rollOffset; + } else { + jointRotations.z += joint.roll * Math.sin + (filter.degToRad(ft * modifiers.rollFrequencyMultiplier + joint.rollPhase)) + joint.rollOffset; + } + return jointRotations; + }, + + zeroAnimation: function(animation) { + for (i in animation.joints) { + for (j in animation.joints[i]) { + animation.joints[i][j] = 0; + } + } + }, + + blendAnimation: function(sourceAnimation, targetAnimation, percent) { + for (i in targetAnimation.joints) { + targetAnimation.joints[i].pitch += percent * sourceAnimation.joints[i].pitch; + targetAnimation.joints[i].yaw += percent * sourceAnimation.joints[i].yaw; + targetAnimation.joints[i].roll += percent * sourceAnimation.joints[i].roll; + targetAnimation.joints[i].pitchPhase += percent * sourceAnimation.joints[i].pitchPhase; + targetAnimation.joints[i].yawPhase += percent * sourceAnimation.joints[i].yawPhase; + targetAnimation.joints[i].rollPhase += percent * sourceAnimation.joints[i].rollPhase; + targetAnimation.joints[i].pitchOffset += percent * sourceAnimation.joints[i].pitchOffset; + targetAnimation.joints[i].yawOffset += percent * sourceAnimation.joints[i].yawOffset; + targetAnimation.joints[i].rollOffset += percent * sourceAnimation.joints[i].rollOffset; + if (i === "Hips") { + // Hips only + targetAnimation.joints[i].thrust += percent * sourceAnimation.joints[i].thrust; + targetAnimation.joints[i].sway += percent * sourceAnimation.joints[i].sway; + targetAnimation.joints[i].bob += percent * sourceAnimation.joints[i].bob; + targetAnimation.joints[i].thrustPhase += percent * sourceAnimation.joints[i].thrustPhase; + targetAnimation.joints[i].swayPhase += percent * sourceAnimation.joints[i].swayPhase; + targetAnimation.joints[i].bobPhase += percent * sourceAnimation.joints[i].bobPhase; + targetAnimation.joints[i].thrustOffset += percent * sourceAnimation.joints[i].thrustOffset; + targetAnimation.joints[i].swayOffset += percent * sourceAnimation.joints[i].swayOffset; + targetAnimation.joints[i].bobOffset += percent * sourceAnimation.joints[i].bobOffset; + } + } + }, + + deepCopy: function(sourceAnimation, targetAnimation) { + // calibration + targetAnimation.calibration = JSON.parse(JSON.stringify(sourceAnimation.calibration)); + + // harmonics + targetAnimation.harmonics = {}; + if (sourceAnimation.harmonics) { + targetAnimation.harmonics = JSON.parse(JSON.stringify(sourceAnimation.harmonics)); + } + + // filters + targetAnimation.filters = {}; + for (i in sourceAnimation.filters) { + // are any filters specified for this joint? + if (sourceAnimation.filters[i]) { + targetAnimation.filters[i] = sourceAnimation.filters[i]; + // wave shapers + if (sourceAnimation.filters[i].pitchFilter) { + targetAnimation.filters[i].pitchFilter = sourceAnimation.filters[i].pitchFilter; + } + if (sourceAnimation.filters[i].yawFilter) { + targetAnimation.filters[i].yawFilter = sourceAnimation.filters[i].yawFilter; + } + if (sourceAnimation.filters[i].rollFilter) { + targetAnimation.filters[i].rollFilter = sourceAnimation.filters[i].rollFilter; + } + // LP filters + if (sourceAnimation.filters[i].swayLPFilter) { + targetAnimation.filters[i].swayLPFilter = sourceAnimation.filters[i].swayLPFilter; + } + if (sourceAnimation.filters[i].bobLPFilter) { + targetAnimation.filters[i].bobLPFilter = sourceAnimation.filters[i].bobLPFilter; + } + if (sourceAnimation.filters[i].thrustLPFilter) { + targetAnimation.filters[i].thrustLPFilter = sourceAnimation.filters[i].thrustLPFilter; + } + } + } + // joints + targetAnimation.joints = JSON.parse(JSON.stringify(sourceAnimation.joints)); + } + } + +})(); // end animation object literal + +// ReachPose datafile wrapper object +ReachPose = function(reachPoseName) { + this.name = reachPoseName; + this.reachPoseParameters = walkAssets.getReachPoseParameters(reachPoseName); + this.reachPoseDataFile = walkAssets.getReachPoseDataFile(reachPoseName); + this.progress = 0; + this.smoothingFilter = filter.createAveragingFilter(this.reachPoseParameters.smoothing); + this.currentStrength = function() { + // apply optionally smoothed (D)ASDR envelope to reach pose's strength / influence whilst active + var segmentProgress = undefined; // progress through chosen segment + var segmentTimeDelta = undefined; // total change in time over chosen segment + var segmentStrengthDelta = undefined; // total change in strength over chosen segment + var lastStrength = undefined; // the last value the previous segment held + var currentStrength = undefined; // return value + + // select parameters based on segment (a segment being one of (D),A,S,D or R) + if (this.progress >= this.reachPoseParameters.sustain.timing) { + // release segment + segmentProgress = this.progress - this.reachPoseParameters.sustain.timing; + segmentTimeDelta = this.reachPoseParameters.release.timing - this.reachPoseParameters.sustain.timing; + segmentStrengthDelta = this.reachPoseParameters.release.strength - this.reachPoseParameters.sustain.strength; + lastStrength = this.reachPoseParameters.sustain.strength; + } else if (this.progress >= this.reachPoseParameters.decay.timing) { + // sustain phase + segmentProgress = this.progress - this.reachPoseParameters.decay.timing; + segmentTimeDelta = this.reachPoseParameters.sustain.timing - this.reachPoseParameters.decay.timing; + segmentStrengthDelta = this.reachPoseParameters.sustain.strength - this.reachPoseParameters.decay.strength; + lastStrength = this.reachPoseParameters.decay.strength; + } else if (this.progress >= this.reachPoseParameters.attack.timing) { + // decay phase + segmentProgress = this.progress - this.reachPoseParameters.attack.timing; + segmentTimeDelta = this.reachPoseParameters.decay.timing - this.reachPoseParameters.attack.timing; + segmentStrengthDelta = this.reachPoseParameters.decay.strength - this.reachPoseParameters.attack.strength; + lastStrength = this.reachPoseParameters.attack.strength; + } else if (this.progress >= this.reachPoseParameters.delay.timing) { + // attack phase + segmentProgress = this.progress - this.reachPoseParameters.delay.timing; + segmentTimeDelta = this.reachPoseParameters.attack.timing - this.reachPoseParameters.delay.timing; + segmentStrengthDelta = this.reachPoseParameters.attack.strength - this.reachPoseParameters.delay.strength; + lastStrength = 0; //this.delay.strength; + } else { + // delay phase + segmentProgress = this.progress; + segmentTimeDelta = this.reachPoseParameters.delay.timing; + segmentStrengthDelta = this.reachPoseParameters.delay.strength; + lastStrength = 0; + } + currentStrength = segmentTimeDelta > 0 ? lastStrength + segmentStrengthDelta * segmentProgress / segmentTimeDelta + : lastStrength; + // smooth off the response curve + currentStrength = this.smoothingFilter.process(currentStrength); + return currentStrength; + } +}; + +// constructor with default parameters +TransitionParameters = function() { + this.duration = 0.5; + this.easingLower = {x:0.25, y:0.75}; + this.easingUpper = {x:0.75, y:0.25}; + this.reachPoses = []; +} + +const QUARTER_CYCLE = 90; +const HALF_CYCLE = 180; +const THREE_QUARTER_CYCLE = 270; +const FULL_CYCLE = 360; + +// constructor for animation Transition +Transition = function(nextAnimation, lastAnimation, lastTransition, playTransitionReachPoses) { + + if (playTransitionReachPoses === undefined) { + playTransitionReachPoses = true; + } + + // record the current state of animation + this.nextAnimation = nextAnimation; + this.lastAnimation = lastAnimation; + this.lastTransition = lastTransition; + + // collect information about the currently playing animation + this.direction = motion.direction; + this.lastDirection = motion.lastDirection; + this.lastFrequencyTimeWheelPos = motion.frequencyTimeWheelPos; + this.lastFrequencyTimeIncrement = motion.averageFrequencyTimeIncrement; + this.lastFrequencyTimeWheelRadius = motion.frequencyTimeWheelRadius; + this.degreesToTurn = 0; // total degrees to turn the ft wheel before the avatar stops (walk only) + this.degreesRemaining = 0; // remaining degrees to turn the ft wheel before the avatar stops (walk only) + this.lastElapsedFTDegrees = motion.elapsedFTDegrees; // degrees elapsed since last transition start + motion.elapsedFTDegrees = 0; // reset ready for the next transition + motion.frequencyTimeWheelPos = 0; // start the next animation's frequency time wheel from zero + + // set parameters for the transition + this.parameters = new TransitionParameters(); + this.liveReachPoses = []; + if (walkAssets && lastAnimation && nextAnimation) { + // overwrite this.parameters with any transition parameters specified for this particular transition + walkAssets.getTransitionParameters(lastAnimation, nextAnimation, this.parameters); + // fire up any reach poses for this transition + if (playTransitionReachPoses) { + for (poseName in this.parameters.reachPoses) { + this.liveReachPoses.push(new ReachPose(this.parameters.reachPoses[poseName])); + } + } + } + this.startTime = new Date().getTime(); // Starting timestamp (seconds) + this.progress = 0; // how far are we through the transition? + this.filteredProgress = 0; + + // coming to a halt whilst walking? if so, will need a clean stopping point defined + if (motion.isComingToHalt) { + + const FULL_CYCLE_THRESHOLD = 320; + const HALF_CYCLE_THRESHOLD = 140; + const CYCLE_COMMIT_THRESHOLD = 5; + + // how many degrees do we need to turn the walk wheel to finish walking with both feet on the ground? + if (this.lastElapsedFTDegrees < CYCLE_COMMIT_THRESHOLD) { + // just stop the walk cycle right here and blend to idle + this.degreesToTurn = 0; + } else if (this.lastElapsedFTDegrees < HALF_CYCLE) { + // we have not taken a complete step yet, so we advance to the second stop angle + this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos; + } else if (this.lastFrequencyTimeWheelPos > 0 && this.lastFrequencyTimeWheelPos <= HALF_CYCLE_THRESHOLD) { + // complete the step and stop at 180 + this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos; + } else if (this.lastFrequencyTimeWheelPos > HALF_CYCLE_THRESHOLD && this.lastFrequencyTimeWheelPos <= HALF_CYCLE) { + // complete the step and next then stop at 0 + this.degreesToTurn = HALF_CYCLE - this.lastFrequencyTimeWheelPos + HALF_CYCLE; + } else if (this.lastFrequencyTimeWheelPos > HALF_CYCLE && this.lastFrequencyTimeWheelPos <= FULL_CYCLE_THRESHOLD) { + // complete the step and stop at 0 + this.degreesToTurn = FULL_CYCLE - this.lastFrequencyTimeWheelPos; + } else { + // complete the step and the next then stop at 180 + this.degreesToTurn = FULL_CYCLE - this.lastFrequencyTimeWheelPos + HALF_CYCLE; + } + + // transition length in this case should be directly proportional to the remaining degrees to turn + var MIN_FT_INCREMENT = 5.0; // degrees per frame + var MIN_TRANSITION_DURATION = 0.4; + const TWO_THIRDS = 0.6667; + this.lastFrequencyTimeIncrement *= TWO_THIRDS; // help ease the transition + var lastFrequencyTimeIncrement = this.lastFrequencyTimeIncrement > MIN_FT_INCREMENT ? + this.lastFrequencyTimeIncrement : MIN_FT_INCREMENT; + var timeToFinish = Math.max(motion.deltaTime * this.degreesToTurn / lastFrequencyTimeIncrement, + MIN_TRANSITION_DURATION); + this.parameters.duration = timeToFinish; + this.degreesRemaining = this.degreesToTurn; + } + + // deal with transition recursion (overlapping transitions) + this.recursionDepth = 0; + this.incrementRecursion = function() { + this.recursionDepth += 1; + + // cancel any continued motion + this.degreesToTurn = 0; + + // limit the number of layered / nested transitions + if (this.lastTransition !== nullTransition) { + this.lastTransition.incrementRecursion(); + if (this.lastTransition.recursionDepth > MAX_TRANSITION_RECURSION) { + this.lastTransition = nullTransition; + } + } + }; + if (this.lastTransition !== nullTransition) { + this.lastTransition.incrementRecursion(); + } + + // end of transition initialisation. begin Transition public methods + + // keep up the pace for the frequency time wheel for the last animation + this.advancePreviousFrequencyTimeWheel = function(deltaTime) { + var wheelAdvance = undefined; + + if (this.lastAnimation === avatar.selectedWalkBlend && + this.nextAnimation === avatar.selectedIdle) { + if (this.degreesRemaining <= 0) { + // stop continued motion + wheelAdvance = 0; + if (motion.isComingToHalt) { + if (this.lastFrequencyTimeWheelPos < QUARTER_CYCLE) { + this.lastFrequencyTimeWheelPos = 0; + } else { + this.lastFrequencyTimeWheelPos = HALF_CYCLE; + } + } + } else { + wheelAdvance = this.lastFrequencyTimeIncrement; + var distanceToTravel = avatar.calibration.strideLength * wheelAdvance / HALF_CYCLE; + if (this.degreesRemaining <= 0) { + distanceToTravel = 0; + this.degreesRemaining = 0; + } else { + this.degreesRemaining -= wheelAdvance; + } + } + } else { + wheelAdvance = this.lastFrequencyTimeIncrement; + } + + // advance the ft wheel + this.lastFrequencyTimeWheelPos += wheelAdvance; + if (this.lastFrequencyTimeWheelPos >= FULL_CYCLE) { + this.lastFrequencyTimeWheelPos = this.lastFrequencyTimeWheelPos % FULL_CYCLE; + } + + // advance ft wheel for the nested (previous) Transition + if (this.lastTransition !== nullTransition) { + this.lastTransition.advancePreviousFrequencyTimeWheel(deltaTime); + } + // update the lastElapsedFTDegrees for short stepping + this.lastElapsedFTDegrees += wheelAdvance; + this.degreesTurned += wheelAdvance; + }; + + this.updateProgress = function() { + const MILLISECONDS_CONVERT = 1000; + const ACCURACY_INCREASER = 1000; + var elapasedTime = (new Date().getTime() - this.startTime) / MILLISECONDS_CONVERT; + this.progress = elapasedTime / this.parameters.duration; + this.progress = Math.round(this.progress * ACCURACY_INCREASER) / ACCURACY_INCREASER; + + // updated nested transition/s + if (this.lastTransition !== nullTransition) { + if (this.lastTransition.updateProgress() === TRANSITION_COMPLETE) { + // the previous transition is now complete + this.lastTransition = nullTransition; + } + } + + // update any reachPoses + for (pose in this.liveReachPoses) { + // use independent timing for reachPoses + this.liveReachPoses[pose].progress += (motion.deltaTime / this.liveReachPoses[pose].reachPoseParameters.duration); + if (this.liveReachPoses[pose].progress >= 1) { + // time to kill off this reach pose + this.liveReachPoses.splice(pose, 1); + } + } + + // update transition progress + this.filteredProgress = filter.bezier(this.progress, this.parameters.easingLower, this.parameters.easingUpper); + return this.progress >= 1 ? TRANSITION_COMPLETE : false; + }; + + this.blendTranslations = function(frequencyTimeWheelPos, direction) { + var lastTranslations = {x:0, y:0, z:0}; + var nextTranslations = animationOperations.calculateTranslations(this.nextAnimation, + frequencyTimeWheelPos, + direction); + // are we blending with a previous, still live transition? + if (this.lastTransition !== nullTransition) { + lastTranslations = this.lastTransition.blendTranslations(this.lastFrequencyTimeWheelPos, + this.lastDirection); + } else { + lastTranslations = animationOperations.calculateTranslations(this.lastAnimation, + this.lastFrequencyTimeWheelPos, + this.lastDirection); + } + + // blend last / next translations + nextTranslations = Vec3.multiply(this.filteredProgress, nextTranslations); + lastTranslations = Vec3.multiply((1 - this.filteredProgress), lastTranslations); + nextTranslations = Vec3.sum(nextTranslations, lastTranslations); + + if (this.liveReachPoses.length > 0) { + for (pose in this.liveReachPoses) { + var reachPoseStrength = this.liveReachPoses[pose].currentStrength(); + var poseTranslations = animationOperations.calculateTranslations( + this.liveReachPoses[pose].reachPoseDataFile, + frequencyTimeWheelPos, + direction); + + // can't use Vec3 operations here, as if x,y or z is zero, the reachPose should have no influence at all + if (Math.abs(poseTranslations.x) > 0) { + nextTranslations.x = reachPoseStrength * poseTranslations.x + (1 - reachPoseStrength) * nextTranslations.x; + } + if (Math.abs(poseTranslations.y) > 0) { + nextTranslations.y = reachPoseStrength * poseTranslations.y + (1 - reachPoseStrength) * nextTranslations.y; + } + if (Math.abs(poseTranslations.z) > 0) { + nextTranslations.z = reachPoseStrength * poseTranslations.z + (1 - reachPoseStrength) * nextTranslations.z; + } + } + } + return nextTranslations; + }; + + this.blendRotations = function(jointName, frequencyTimeWheelPos, direction) { + var lastRotations = {x:0, y:0, z:0}; + var nextRotations = animationOperations.calculateRotations(jointName, + this.nextAnimation, + frequencyTimeWheelPos, + direction); + + // are we blending with a previous, still live transition? + if (this.lastTransition !== nullTransition) { + lastRotations = this.lastTransition.blendRotations(jointName, + this.lastFrequencyTimeWheelPos, + this.lastDirection); + } else { + lastRotations = animationOperations.calculateRotations(jointName, + this.lastAnimation, + this.lastFrequencyTimeWheelPos, + this.lastDirection); + } + // blend last / next translations + nextRotations = Vec3.multiply(this.filteredProgress, nextRotations); + lastRotations = Vec3.multiply((1 - this.filteredProgress), lastRotations); + nextRotations = Vec3.sum(nextRotations, lastRotations); + + // are there reachPoses defined for this transition? + if (this.liveReachPoses.length > 0) { + for (pose in this.liveReachPoses) { + var reachPoseStrength = this.liveReachPoses[pose].currentStrength(); + var poseRotations = animationOperations.calculateRotations(jointName, + this.liveReachPoses[pose].reachPoseDataFile, + frequencyTimeWheelPos, + direction); + + // don't use Vec3 operations here, as if x,y or z is zero, the reach pose should have no influence at all + if (Math.abs(poseRotations.x) > 0) { + nextRotations.x = reachPoseStrength * poseRotations.x + (1 - reachPoseStrength) * nextRotations.x; + } + if (Math.abs(poseRotations.y) > 0) { + nextRotations.y = reachPoseStrength * poseRotations.y + (1 - reachPoseStrength) * nextRotations.y; + } + if (Math.abs(poseRotations.z) > 0) { + nextRotations.z = reachPoseStrength * poseRotations.z + (1 - reachPoseStrength) * nextRotations.z; + } + } + } + return nextRotations; + }; +}; // end Transition constructor + +// individual joint modifiers +FrequencyMultipliers = function(joint, direction) { + // gather multipliers + this.pitchFrequencyMultiplier = 1; + this.yawFrequencyMultiplier = 1; + this.rollFrequencyMultiplier = 1; + this.swayFrequencyMultiplier = 1; + this.bobFrequencyMultiplier = 1; + this.thrustFrequencyMultiplier = 1; + + if (joint) { + if (joint.pitchFrequencyMultiplier) { + this.pitchFrequencyMultiplier = joint.pitchFrequencyMultiplier; + } + if (joint.yawFrequencyMultiplier) { + this.yawFrequencyMultiplier = joint.yawFrequencyMultiplier; + } + if (joint.rollFrequencyMultiplier) { + this.rollFrequencyMultiplier = joint.rollFrequencyMultiplier; + } + if (joint.swayFrequencyMultiplier) { + this.swayFrequencyMultiplier = joint.swayFrequencyMultiplier; + } + if (joint.bobFrequencyMultiplier) { + this.bobFrequencyMultiplier = joint.bobFrequencyMultiplier; + } + if (joint.thrustFrequencyMultiplier) { + this.thrustFrequencyMultiplier = joint.thrustFrequencyMultiplier; + } + } }; \ No newline at end of file diff --git a/examples/map.js~ b/examples/map.js~ new file mode 100644 index 0000000000..5a4e0f0f8c --- /dev/null +++ b/examples/map.js~ @@ -0,0 +1,323 @@ +Script.include("entityManager.js"); +Script.include("overlayManager.js"); + + +// Poll for nearby map data + +var entityManager = new EntityManager(); + +// From http://evanw.github.io/lightgl.js/docs/raytracer.html +function raySphereIntersection(origin, ray, center, radius) { + var offset = Vec3.subtract(origin, center); + var a = Vec3.dot(ray, ray); + // var a = ray.dot(ray); + var b = 2 * Vec3.dot(ray, offset); + // var b = 2 * ray.dot(offset); + var c = Vec3.dot(offset, offset) - radius * radius; + // var c = offset.dot(offset) - radius * radius; + var discriminant = b * b - 4 * a * c; + + if (discriminant > 0) { + return true; + } + + return null; +}; + + +Map = function(data) { + var visible = false; + + var ROOT_OFFSET = Vec3.multiply(0.3, Quat.getFront(MyAvatar.orientation)); + var ROOT_POSITION = Vec3.sum(MyAvatar.position, ROOT_OFFSET); + + var ROOT_SCALE = 0.0005; + + // Create object in objectManager + var rootObject = entityManager.addBare(); + var position = ROOT_POSITION; + rootObject.position = ROOT_POSITION; + rootObject.scale = ROOT_SCALE + Vec3.print("Position:", position); + + // Search for all nearby objects that have the userData "mapped" + // TODO Update to use the zone's bounds + var entities = Entities.findEntities(MyAvatar.position, 32000); + var mappedEntities = []; + var minCorner = { + x: 4294967295, + y: 4294967295, + z: 4294967295, + }; + var maxCorner = { + x: -4294967295, + y: -4294967295, + z: -4294967295, + }; + + for (var i = 0; i < entities.length; ++i) { + var entityID = entities[i]; + var properties = Entities.getEntityProperties(entityID); + if (properties.userData == "mapped" || properties.userData == "tracked") { + + print("Found: ", properties.name); + + minCorner.x = Math.min(minCorner.x, properties.position.x - (properties.dimensions.x / 2)); + minCorner.y = Math.min(minCorner.y, properties.position.y - (properties.dimensions.y / 2)); + minCorner.z = Math.min(minCorner.z, properties.position.z - (properties.dimensions.z / 2)); + + maxCorner.x = Math.max(maxCorner.x, properties.position.x - (properties.dimensions.x / 2)); + maxCorner.y = Math.max(maxCorner.y, properties.position.y - (properties.dimensions.y / 2)); + maxCorner.z = Math.max(maxCorner.z, properties.position.z - (properties.dimensions.z / 2)); + + } + // if (properties.userData == "mapped") { + // properties.visible = false; + // var entity = entityManager.add(properties.type, properties); + // mappedEntities.push(entity); + // } else if (properties.userData == "tracked") { + // // TODO implement tracking of objects + // } + } + + var dimensions = { + x: maxCorner.x - minCorner.x, + y: maxCorner.y - minCorner.y, + z: maxCorner.z - minCorner.z, + }; + Vec3.print("dims", dimensions); + + var center = { + x: minCorner.x + (dimensions.x / 2), + y: minCorner.y + (dimensions.y / 2), + z: minCorner.z + (dimensions.z / 2), + }; + Vec3.print("center", center); + + var trackedEntities = []; + var waypointEntities = []; + for (var i = 0; i < entities.length; ++i) { + var entityID = entities[i]; + var properties = Entities.getEntityProperties(entityID); + var mapData = null; + try { + var data = JSON.parse(properties.userData.replace(/(\r\n|\n|\r)/gm,"")); + mapData = data.mapData; + } catch (e) { + print("Caught: ", properties.name); + } + + if (mapData) { + print("Creating copy of", properties.name); + properties.name += " (COPY)"; + properties.userData = ""; + properties.visible = true; + var position = properties.position; + properties.position = Vec3.subtract(properties.position, center); + properties.position = Vec3.multiply(properties.position, ROOT_SCALE); + var extra = { }; + + if (mapData.track) { + extra.trackingEntityID= entityID; + trackedEntities.push(entity); + rootObject.addChild(entity); + } + if (mapData.waypoint) { + print("Waypoint: ", mapData.waypoint.name); + // properties.type = "Model"; + // properties.modelURL = "atp:ca49a13938376b3eb68d7b2b9189afb3f580c07b6950ea9e65b5260787204145.fbx"; + extra.waypoint = mapData.waypoint; + extra.waypoint.position = position; + } + + var entity = entityManager.add(properties.type, properties); + entity.__extra__ = extra; + if (mapData.waypoint) { + waypointEntities.push(entity); + } + mappedEntities.push(entity); + + rootObject.addChild(entity); + + } else { + // print("Not creating copy of", properties.name); + } + } + + var avatarArrowEntity = entityManager.add("Model", { + name: "You Are Here", + modelURL: "atp:ce4f0c4e491e40b73d28f2646da4f676fe9ea364cf5f1bf5615522ef6acfd80e.fbx", + position: Vec3.multiply(Vec3.subtract(MyAvatar.position, center), ROOT_SCALE), + dimensions: { x: 30, y: 100, z: 100 }, + }); + rootObject.addChild(avatarArrowEntity); + + this.isVisible = function() { + return visible; + } + + Controller.mousePressEvent.connect(mousePressEvent); + function mousePressEvent(event) { + // Entities.setZonesArePickable(false); + + var pickRay = Camera.computePickRay(event.x, event.y); + for (var i = 0; i < waypointEntities.length; ++i) { + var entity = waypointEntities[i]; + print("Checkit for hit", entity.__extra__.waypoint.name); + var result = raySphereIntersection(pickRay.origin, pickRay.direction, entity.worldPosition, 0.1);//entity.worldScale); + if (result) { + print("Pressed entity: ", entity.id); + print("Pressed waypoint: ", entity.__extra__.waypoint.name); + print("Teleporting..."); + MyAvatar.position = entity.__extra__.waypoint.position; + break; + } + } + // var result = Entities.findRayIntersection(pickRay, false); + // if (result.intersects) { + // var entity = entityManager.get(result.entityID); + // if (entity) { + // print("Pressed entity: ", entity.id); + // } + // if (entity && entity.__extra__.waypoint) { + // print("Pressed waypoint: ", entity.__extra__.waypoint.name); + // print("Teleporting..."); + // MyAvatar.position = entity.__extra__.waypoint.position; + // } + // } + + // Entities.setZonesArePickable(true); + }; + + var time = 0; + Script.update.connect(function(dt) { + time += dt; + // Update tracked entities + for (var i = 0; i < trackedEntities.length; ++i) { + entity = trackedEntities[i]; + var entityID = entity.__extra__.trackingEntityID; + var properties = Entities.getEntityProperties(entityID); + properties.position = Vec3.subtract(properties.position, center); + properties.position = Vec3.multiply(properties.position, ROOT_SCALE); + entity.position = properties.position; + } + + + var position = Vec3.subtract(MyAvatar.position, center) + position.y += 60 + (Math.sin(time) * 10); + position = Vec3.multiply(position, ROOT_SCALE); + avatarArrowEntity.position = position; + // Vec3.print("Position:", avatarArrowEntity.position); + + // rootObject.position = Vec3.sum(position, { x: 0, y: Math.sin(time) / 30, z: 0 }); + //var ROOT_OFFSET = Vec3.multiply(0.3, Quat.getFront(MyAvatar.orientation)); + //var ROOT_POSITION = Vec3.sum(MyAvatar.position, ROOT_OFFSET); + // position = ROOT_POSITION; + rootObject.position = ROOT_POSITION; + entityManager.update(); + + // Update waypoint highlights + var pickRay = Camera.computePickRay(event.x, event.y); + for (var i = 0; i < waypointEntities.length; ++i) { + var entity = waypointEntities[i]; + print("Checkit for hit", entity.__extra__.waypoint.name); + var result = raySphereIntersection(pickRay.origin, pickRay.direction, entity.worldPosition, 0.1);//entity.worldScale); + if (result) { + print("Pressed entity: ", entity.id); + print("Pressed waypoint: ", entity.__extra__.waypoint.name); + print("Teleporting..."); + MyAvatar.position = entity.__extra__.waypoint.position; + break; + } + } + }); + + function setVisible(newValue) { + if (visible != newValue) { + visible = newValue; + + if (visible) { + } else { + } + } + } + + this.show = function() { + setVisible(true); + } + + this.hide = function() { + setVisible(false); + } +}; + +var map = null; +map = Map(mapData); + +// On press key +Controller.keyPressEvent.connect(function(event) { + if (event.text == "m") { + if (!map) { + map = Map(mapData); + } + + map.show(); + print("MAP!"); + } +}); + + + + + +var mapData = { + config: { + // World dimensions that the minimap maps to + worldDimensions: { + x: 10.0, + y: 10.0, + z: 10.0, + }, + // The center of the map should map to this location in the center of the area + worldCenter: { + x: 5.0, + y: 5.0, + z: 5.0, + }, + // Map dimensions + mapDimensions: { + x: 10.0, + y: 10.0, + z: 10.0, + }, + + // Can this be automated? Tag entities that should be included? Store in UserData? + objects: [ + { + type: "Model", + modelURL: "https://hifi-public.s3.amazonaws.com/ozan/sets/huffman_set/huffman_set.fbx", + }, + ], + }, + waypoints: [ + { + name: "Forest's Edge", + position: { + }, + }, + ], +}; + + +// entityManager = new OverlayManager(); +// entityManager = new EntityManager(); +// +// var rootEntity = entityManager.addBare(); +// +// var time = 0; +// +// +// rootEntity.scale = 0.1; +// Script.include("sfData.js"); +// rootEntity.addChild(entity); +entityManager.update(); diff --git a/examples/pitching.js b/examples/pitching.js index 046113cbc3..b419e8935c 100644 --- a/examples/pitching.js +++ b/examples/pitching.js @@ -142,6 +142,7 @@ function createBaseball(position, velocity, ballScale) { var buildBaseballHitCallback = function(entityID) { var f = function(entityA, entityB, collision) { + print("Got baseball hit callback"); var properties = Entities.getEntityProperties(entityID, ['position', 'velocity']); var line = new InfiniteLine(properties.position, { red: 255, green: 128, blue: 89 }); var lastPosition = properties.position; @@ -154,11 +155,12 @@ var buildBaseballHitCallback = function(entityID) { lastPosition = properties.position; } }, 50); + var speed = Vec3.length(properties.velocity); Entities.editEntity(entityID, { - velocity: Vec3.multiply(3, properties.velocity), + velocity: Vec3.multiply(2, properties.velocity), gravity: { x: 0, - y: -2.8, + y: -9.8, z: 0 } }); diff --git a/examples/toys/flashlight/flashlight.js b/examples/toys/flashlight/flashlight.js index a775f185e3..912d542d6c 100644 --- a/examples/toys/flashlight/flashlight.js +++ b/examples/toys/flashlight/flashlight.js @@ -1,269 +1,269 @@ -// -// flashlight.js -// -// Script Type: Entity -// -// Created by Sam Gateau on 9/9/15. -// Additions by James B. Pollack @imgntn on 9/21/2015 -// Copyright 2015 High Fidelity, Inc. -// -// This is a toy script that can be added to the Flashlight model entity: -// "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx" -// that creates a spotlight attached with the flashlight model while the entity is grabbed -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// -/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ -(function() { - - Script.include("../../libraries/utils.js"); - - var ON_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_on.wav'; - var OFF_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_off.wav'; - - //we are creating lights that we don't want to get stranded so lets make sure that we can get rid of them - var startTime = Date.now(); - //if you're going to be using this in a dungeon or something and holding it for a long time, increase this lifetime value. - var LIFETIME = 25; - var MSEC_PER_SEC = 1000.0; - - // this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember - // our this object, so we can access it in cases where we're called without a this (like in the case of various global signals) - function Flashlight() { - return; - } - - //if the trigger value goes below this while held, the flashlight will turn off. if it goes above, it will - var DISABLE_LIGHT_THRESHOLD = 0.7; - - // These constants define the Spotlight position and orientation relative to the model - var MODEL_LIGHT_POSITION = { - x: 0, - y: -0.3, - z: 0 - }; - var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { - x: 1, - y: 0, - z: 0 - }); - - var GLOW_LIGHT_POSITION = { - x: 0, - y: -0.1, - z: 0 - }; - - // Evaluate the world light entity positions and orientations from the model ones - function evalLightWorldTransform(modelPos, modelRot) { - - return { - p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), - q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) - }; - } - - function glowLightWorldTransform(modelPos, modelRot) { - return { - p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)), - q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) - }; - } - - Flashlight.prototype = { - lightOn: false, - hand: null, - whichHand: null, - hasSpotlight: false, - spotlight: null, - setRightHand: function() { - this.hand = 'RIGHT'; - }, - - setLeftHand: function() { - this.hand = 'LEFT'; - }, - - startNearGrab: function() { - if (!this.hasSpotlight) { - - //this light casts the beam - this.spotlight = Entities.addEntity({ - type: "Light", - isSpotlight: true, - dimensions: { - x: 2, - y: 2, - z: 20 - }, - color: { - red: 255, - green: 255, - blue: 255 - }, - intensity: 2, - exponent: 0.3, - cutoff: 20, - lifetime: LIFETIME - }); - - //this light creates the effect of a bulb at the end of the flashlight - this.glowLight = Entities.addEntity({ - type: "Light", - dimensions: { - x: 0.25, - y: 0.25, - z: 0.25 - }, - isSpotlight: false, - color: { - red: 255, - green: 255, - blue: 255 - }, - exponent: 0, - cutoff: 90, // in degrees - lifetime: LIFETIME - }); - - this.hasSpotlight = true; - - } - - }, - - setWhichHand: function() { - this.whichHand = this.hand; - }, - - continueNearGrab: function() { - if (this.whichHand === null) { - //only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten - this.setWhichHand(); - } else { - this.updateLightPositions(); - this.changeLightWithTriggerPressure(this.whichHand); - } - }, - - releaseGrab: function() { - //delete the lights and reset state - if (this.hasSpotlight) { - Entities.deleteEntity(this.spotlight); - Entities.deleteEntity(this.glowLight); - this.hasSpotlight = false; - this.glowLight = null; - this.spotlight = null; - this.whichHand = null; - this.lightOn = false; - } - }, - - updateLightPositions: function() { - var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); - - //move the two lights along the vectors we set above - var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation); - var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation); - - //move them with the entity model - Entities.editEntity(this.spotlight, { - position: lightTransform.p, - rotation: lightTransform.q, - lifetime: (Date.now() - startTime) / MSEC_PER_SEC + LIFETIME - }); - - Entities.editEntity(this.glowLight, { - position: glowLightTransform.p, - rotation: glowLightTransform.q, - lifetime: (Date.now() - startTime) / MSEC_PER_SEC + LIFETIME - }); - - }, - - changeLightWithTriggerPressure: function(flashLightHand) { - var handClickString = flashLightHand + "_HAND_CLICK"; - - var handClick = Controller.findAction(handClickString); - - this.triggerValue = Controller.getActionValue(handClick); - - if (this.triggerValue < DISABLE_LIGHT_THRESHOLD && this.lightOn === true) { - this.turnLightOff(); - } else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) { - this.turnLightOn(); - } - return; - }, - - turnLightOff: function() { - this.playSoundAtCurrentPosition(false); - Entities.editEntity(this.spotlight, { - intensity: 0 - }); - Entities.editEntity(this.glowLight, { - intensity: 0 - }); - this.lightOn = false; - }, - - turnLightOn: function() { - this.playSoundAtCurrentPosition(true); - - Entities.editEntity(this.glowLight, { - intensity: 2 - }); - Entities.editEntity(this.spotlight, { - intensity: 2 - }); - this.lightOn = true; - }, - - playSoundAtCurrentPosition: function(playOnSound) { - var position = Entities.getEntityProperties(this.entityID, "position").position; - - var audioProperties = { - volume: 0.25, - position: position - }; - - if (playOnSound) { - Audio.playSound(this.ON_SOUND, audioProperties); - } else { - Audio.playSound(this.OFF_SOUND, audioProperties); - } - }, - - preload: function(entityID) { - - // preload() will be called when the entity has become visible (or known) to the interface - // it gives us a chance to set our local JavaScript object up. In this case it means: - // * remembering our entityID, so we can access it in cases where we're called without an entityID - // * preloading sounds - this.entityID = entityID; - this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); - this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); - - }, - - unload: function() { - // unload() will be called when our entity is no longer available. It may be because we were deleted, - // or because we've left the domain or quit the application. - if (this.hasSpotlight) { - Entities.deleteEntity(this.spotlight); - Entities.deleteEntity(this.glowLight); - this.hasSpotlight = false; - this.glowLight = null; - this.spotlight = null; - this.whichHand = null; - this.lightOn = false; - } - - }, - - }; - - // entity scripts always need to return a newly constructed object of our type - return new Flashlight(); +// +// flashlight.js +// +// Script Type: Entity +// +// Created by Sam Gateau on 9/9/15. +// Additions by James B. Pollack @imgntn on 9/21/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This is a toy script that can be added to the Flashlight model entity: +// "https://hifi-public.s3.amazonaws.com/models/props/flashlight.fbx" +// that creates a spotlight attached with the flashlight model while the entity is grabbed +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +/*global MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ +(function() { + + Script.include("../../libraries/utils.js"); + + var ON_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_on.wav'; + var OFF_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/Switches%20and%20sliders/flashlight_off.wav'; + + //we are creating lights that we don't want to get stranded so lets make sure that we can get rid of them + var startTime = Date.now(); + //if you're going to be using this in a dungeon or something and holding it for a long time, increase this lifetime value. + var LIFETIME = 25; + var MSEC_PER_SEC = 1000.0; + + // this is the "constructor" for the entity as a JS object we don't do much here, but we do want to remember + // our this object, so we can access it in cases where we're called without a this (like in the case of various global signals) + function Flashlight() { + return; + } + + //if the trigger value goes below this while held, the flashlight will turn off. if it goes above, it will + var DISABLE_LIGHT_THRESHOLD = 0.7; + + // These constants define the Spotlight position and orientation relative to the model + var MODEL_LIGHT_POSITION = { + x: 0, + y: -0.3, + z: 0 + }; + var MODEL_LIGHT_ROTATION = Quat.angleAxis(-90, { + x: 1, + y: 0, + z: 0 + }); + + var GLOW_LIGHT_POSITION = { + x: 0, + y: -0.1, + z: 0 + }; + + // Evaluate the world light entity positions and orientations from the model ones + function evalLightWorldTransform(modelPos, modelRot) { + + return { + p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, MODEL_LIGHT_POSITION)), + q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) + }; + } + + function glowLightWorldTransform(modelPos, modelRot) { + return { + p: Vec3.sum(modelPos, Vec3.multiplyQbyV(modelRot, GLOW_LIGHT_POSITION)), + q: Quat.multiply(modelRot, MODEL_LIGHT_ROTATION) + }; + } + + Flashlight.prototype = { + lightOn: false, + hand: null, + whichHand: null, + hasSpotlight: false, + spotlight: null, + setRightHand: function() { + this.hand = 'RIGHT'; + }, + + setLeftHand: function() { + this.hand = 'LEFT'; + }, + + startNearGrab: function() { + if (!this.hasSpotlight) { + + //this light casts the beam + this.spotlight = Entities.addEntity({ + type: "Light", + isSpotlight: true, + dimensions: { + x: 2, + y: 2, + z: 20 + }, + color: { + red: 255, + green: 255, + blue: 255 + }, + intensity: 2, + exponent: 0.3, + cutoff: 20, + lifetime: LIFETIME + }); + + //this light creates the effect of a bulb at the end of the flashlight + this.glowLight = Entities.addEntity({ + type: "Light", + dimensions: { + x: 0.25, + y: 0.25, + z: 0.25 + }, + isSpotlight: false, + color: { + red: 255, + green: 255, + blue: 255 + }, + exponent: 0, + cutoff: 90, // in degrees + lifetime: LIFETIME + }); + + this.hasSpotlight = true; + + } + + }, + + setWhichHand: function() { + this.whichHand = this.hand; + }, + + continueNearGrab: function() { + if (this.whichHand === null) { + //only set the active hand once -- if we always read the current hand, our 'holding' hand will get overwritten + this.setWhichHand(); + } else { + this.updateLightPositions(); + this.changeLightWithTriggerPressure(this.whichHand); + } + }, + + releaseGrab: function() { + //delete the lights and reset state + if (this.hasSpotlight) { + Entities.deleteEntity(this.spotlight); + Entities.deleteEntity(this.glowLight); + this.hasSpotlight = false; + this.glowLight = null; + this.spotlight = null; + this.whichHand = null; + this.lightOn = false; + } + }, + + updateLightPositions: function() { + var modelProperties = Entities.getEntityProperties(this.entityID, ['position', 'rotation']); + + //move the two lights along the vectors we set above + var lightTransform = evalLightWorldTransform(modelProperties.position, modelProperties.rotation); + var glowLightTransform = glowLightWorldTransform(modelProperties.position, modelProperties.rotation); + + //move them with the entity model + Entities.editEntity(this.spotlight, { + position: lightTransform.p, + rotation: lightTransform.q, + lifetime: (Date.now() - startTime) / MSEC_PER_SEC + LIFETIME + }); + + Entities.editEntity(this.glowLight, { + position: glowLightTransform.p, + rotation: glowLightTransform.q, + lifetime: (Date.now() - startTime) / MSEC_PER_SEC + LIFETIME + }); + + }, + + changeLightWithTriggerPressure: function(flashLightHand) { + var handClickString = flashLightHand + "_HAND_CLICK"; + + var handClick = Controller.findAction(handClickString); + + this.triggerValue = Controller.getActionValue(handClick); + + if (this.triggerValue < DISABLE_LIGHT_THRESHOLD && this.lightOn === true) { + this.turnLightOff(); + } else if (this.triggerValue >= DISABLE_LIGHT_THRESHOLD && this.lightOn === false) { + this.turnLightOn(); + } + return; + }, + + turnLightOff: function() { + this.playSoundAtCurrentPosition(false); + Entities.editEntity(this.spotlight, { + intensity: 0 + }); + Entities.editEntity(this.glowLight, { + intensity: 0 + }); + this.lightOn = false; + }, + + turnLightOn: function() { + this.playSoundAtCurrentPosition(true); + + Entities.editEntity(this.glowLight, { + intensity: 2 + }); + Entities.editEntity(this.spotlight, { + intensity: 2 + }); + this.lightOn = true; + }, + + playSoundAtCurrentPosition: function(playOnSound) { + var position = Entities.getEntityProperties(this.entityID, "position").position; + + var audioProperties = { + volume: 0.25, + position: position + }; + + if (playOnSound) { + Audio.playSound(this.ON_SOUND, audioProperties); + } else { + Audio.playSound(this.OFF_SOUND, audioProperties); + } + }, + + preload: function(entityID) { + + // preload() will be called when the entity has become visible (or known) to the interface + // it gives us a chance to set our local JavaScript object up. In this case it means: + // * remembering our entityID, so we can access it in cases where we're called without an entityID + // * preloading sounds + this.entityID = entityID; + this.ON_SOUND = SoundCache.getSound(ON_SOUND_URL); + this.OFF_SOUND = SoundCache.getSound(OFF_SOUND_URL); + + }, + + unload: function() { + // unload() will be called when our entity is no longer available. It may be because we were deleted, + // or because we've left the domain or quit the application. + if (this.hasSpotlight) { + Entities.deleteEntity(this.spotlight); + Entities.deleteEntity(this.glowLight); + this.hasSpotlight = false; + this.glowLight = null; + this.spotlight = null; + this.whichHand = null; + this.lightOn = false; + } + + }, + + }; + + // entity scripts always need to return a newly constructed object of our type + return new Flashlight(); }); \ No newline at end of file diff --git a/examples/walk.js b/examples/walk.js index 0b5bcab65a..63b5599cc2 100644 --- a/examples/walk.js +++ b/examples/walk.js @@ -1,454 +1,454 @@ -// -// walk.js -// version 1.25 -// -// Created by David Wooldridge, June 2015 -// Copyright © 2014 - 2015 High Fidelity, Inc. -// -// Animates an avatar using procedural animation techniques. -// -// Editing tools for animation data files available here: https://github.com/DaveDubUK/walkTools -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -// animations, reach poses, reach pose parameters, transitions, transition parameters, sounds, image/s and reference files -const HIFI_PUBLIC_BUCKET = "https://hifi-public.s3.amazonaws.com/"; -var pathToAssets = HIFI_PUBLIC_BUCKET + "procedural-animator/assets/"; - -Script.include([ - "./libraries/walkConstants.js", - "./libraries/walkFilters.js", - "./libraries/walkApi.js", - pathToAssets + "walkAssets.js" -]); - -// construct Avatar, Motion and (null) Transition -var avatar = new Avatar(); -var motion = new Motion(); -var nullTransition = new Transition(); -motion.currentTransition = nullTransition; - -// create settings (gets initial values from avatar) -Script.include("./libraries/walkSettings.js"); - -// Main loop -Script.update.connect(function(deltaTime) { - - if (motion.isLive) { - - // assess current locomotion state - motion.assess(deltaTime); - - // decide which animation should be playing - selectAnimation(); - - // advance the animation cycle/s by the correct amount/s - advanceAnimations(); - - // update the progress of any live transitions - updateTransitions(); - - // apply translation and rotations - renderMotion(); - - // save this frame's parameters - motion.saveHistory(); - } -}); - -// helper function for selectAnimation() -function setTransition(nextAnimation, playTransitionReachPoses) { - var lastTransition = motion.currentTransition; - var lastAnimation = avatar.currentAnimation; - - // if already transitioning from a blended walk need to maintain the previous walk's direction - if (lastAnimation.lastDirection) { - switch(lastAnimation.lastDirection) { - - case FORWARDS: - lastAnimation = avatar.selectedWalk; - break; - - case BACKWARDS: - lastAnimation = avatar.selectedWalkBackwards; - break; - - case LEFT: - lastAnimation = avatar.selectedSideStepLeft; - break; - - case RIGHT: - lastAnimation = avatar.selectedSideStepRight; - break; - } - } - - motion.currentTransition = new Transition(nextAnimation, lastAnimation, lastTransition, playTransitionReachPoses); - avatar.currentAnimation = nextAnimation; - - // reset default first footstep - if (nextAnimation === avatar.selectedWalkBlend && lastTransition === nullTransition) { - avatar.nextStep = RIGHT; - } -} - -// fly animation blending: smoothing / damping filters -const FLY_BLEND_DAMPING = 50; -var flyUpFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); -var flyDownFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); -var flyForwardFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); -var flyBackwardFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); - -// select / blend the appropriate animation for the current state of motion -function selectAnimation() { - var playTransitionReachPoses = true; - - // select appropriate animation. create transitions where appropriate - switch (motion.nextState) { - case STATIC: { - if (avatar.distanceFromSurface < ON_SURFACE_THRESHOLD && - avatar.currentAnimation !== avatar.selectedIdle) { - setTransition(avatar.selectedIdle, playTransitionReachPoses); - } else if (!(avatar.distanceFromSurface < ON_SURFACE_THRESHOLD) && - avatar.currentAnimation !== avatar.selectedHover) { - setTransition(avatar.selectedHover, playTransitionReachPoses); - } - motion.state = STATIC; - avatar.selectedWalkBlend.lastDirection = NONE; - break; - } - - case SURFACE_MOTION: { - // walk transition reach poses are currently only specified for starting to walk forwards - playTransitionReachPoses = (motion.direction === FORWARDS); - var isAlreadyWalking = (avatar.currentAnimation === avatar.selectedWalkBlend); - - switch (motion.direction) { - case FORWARDS: - if (avatar.selectedWalkBlend.lastDirection !== FORWARDS) { - animationOperations.deepCopy(avatar.selectedWalk, avatar.selectedWalkBlend); - avatar.calibration.strideLength = avatar.selectedWalk.calibration.strideLength; - } - avatar.selectedWalkBlend.lastDirection = FORWARDS; - break; - - case BACKWARDS: - if (avatar.selectedWalkBlend.lastDirection !== BACKWARDS) { - animationOperations.deepCopy(avatar.selectedWalkBackwards, avatar.selectedWalkBlend); - avatar.calibration.strideLength = avatar.selectedWalkBackwards.calibration.strideLength; - } - avatar.selectedWalkBlend.lastDirection = BACKWARDS; - break; - - case LEFT: - animationOperations.deepCopy(avatar.selectedSideStepLeft, avatar.selectedWalkBlend); - avatar.selectedWalkBlend.lastDirection = LEFT; - avatar.calibration.strideLength = avatar.selectedSideStepLeft.calibration.strideLength; - break - - case RIGHT: - animationOperations.deepCopy(avatar.selectedSideStepRight, avatar.selectedWalkBlend); - avatar.selectedWalkBlend.lastDirection = RIGHT; - avatar.calibration.strideLength = avatar.selectedSideStepRight.calibration.strideLength; - break; - - default: - // condition occurs when the avi goes through the floor due to collision hull errors - animationOperations.deepCopy(avatar.selectedWalk, avatar.selectedWalkBlend); - avatar.selectedWalkBlend.lastDirection = FORWARDS; - avatar.calibration.strideLength = avatar.selectedWalk.calibration.strideLength; - break; - } - - if (!isAlreadyWalking && !motion.isComingToHalt) { - setTransition(avatar.selectedWalkBlend, playTransitionReachPoses); - } - motion.state = SURFACE_MOTION; - break; - } - - case AIR_MOTION: { - // blend the up, down, forward and backward flying animations relative to motion speed and direction - animationOperations.zeroAnimation(avatar.selectedFlyBlend); - - // calculate influences based on velocity and direction - var velocityMagnitude = Vec3.length(motion.velocity); - var verticalProportion = motion.velocity.y / velocityMagnitude; - var thrustProportion = motion.velocity.z / velocityMagnitude / 2; - - // directional components - var upComponent = motion.velocity.y > 0 ? verticalProportion : 0; - var downComponent = motion.velocity.y < 0 ? -verticalProportion : 0; - var forwardComponent = motion.velocity.z < 0 ? -thrustProportion : 0; - var backwardComponent = motion.velocity.z > 0 ? thrustProportion : 0; - - // smooth / damp directional components to add visual 'weight' - upComponent = flyUpFilter.process(upComponent); - downComponent = flyDownFilter.process(downComponent); - forwardComponent = flyForwardFilter.process(forwardComponent); - backwardComponent = flyBackwardFilter.process(backwardComponent); - - // normalise directional components - var normaliser = upComponent + downComponent + forwardComponent + backwardComponent; - upComponent = upComponent / normaliser; - downComponent = downComponent / normaliser; - forwardComponent = forwardComponent / normaliser; - backwardComponent = backwardComponent / normaliser; - - // blend animations proportionally - if (upComponent > 0) { - animationOperations.blendAnimation(avatar.selectedFlyUp, - avatar.selectedFlyBlend, - upComponent); - } - if (downComponent > 0) { - animationOperations.blendAnimation(avatar.selectedFlyDown, - avatar.selectedFlyBlend, - downComponent); - } - if (forwardComponent > 0) { - animationOperations.blendAnimation(avatar.selectedFly, - avatar.selectedFlyBlend, - Math.abs(forwardComponent)); - } - if (backwardComponent > 0) { - animationOperations.blendAnimation(avatar.selectedFlyBackwards, - avatar.selectedFlyBlend, - Math.abs(backwardComponent)); - } - - if (avatar.currentAnimation !== avatar.selectedFlyBlend) { - setTransition(avatar.selectedFlyBlend, playTransitionReachPoses); - } - motion.state = AIR_MOTION; - avatar.selectedWalkBlend.lastDirection = NONE; - break; - } - } // end switch next state of motion -} - -// determine the length of stride. advance the frequency time wheels. advance frequency time wheels for any live transitions -function advanceAnimations() { - var wheelAdvance = 0; - - // turn the frequency time wheel - if (avatar.currentAnimation === avatar.selectedWalkBlend) { - // Using technique described here: http://www.gdcvault.com/play/1020583/Animation-Bootcamp-An-Indie-Approach - // wrap the stride length around a 'surveyor's wheel' twice and calculate the angular speed at the given (linear) speed - // omega = v / r , where r = circumference / 2 PI and circumference = 2 * stride length - var speed = Vec3.length(motion.velocity); - motion.frequencyTimeWheelRadius = avatar.calibration.strideLength / Math.PI; - var ftWheelAngularVelocity = speed / motion.frequencyTimeWheelRadius; - // calculate the degrees turned (at this angular speed) since last frame - wheelAdvance = filter.radToDeg(motion.deltaTime * ftWheelAngularVelocity); - } else { - // turn the frequency time wheel by the amount specified for this animation - wheelAdvance = filter.radToDeg(avatar.currentAnimation.calibration.frequency * motion.deltaTime); - } - - if (motion.currentTransition !== nullTransition) { - // the last animation is still playing so we turn it's frequency time wheel to maintain the animation - if (motion.currentTransition.lastAnimation === motion.selectedWalkBlend) { - // if at a stop angle (i.e. feet now under the avi) hold the wheel position for remainder of transition - var tolerance = motion.currentTransition.lastFrequencyTimeIncrement + 0.1; - if ((motion.currentTransition.lastFrequencyTimeWheelPos > - (motion.currentTransition.stopAngle - tolerance)) && - (motion.currentTransition.lastFrequencyTimeWheelPos < - (motion.currentTransition.stopAngle + tolerance))) { - motion.currentTransition.lastFrequencyTimeIncrement = 0; - } - } - motion.currentTransition.advancePreviousFrequencyTimeWheel(motion.deltaTime); - } - - // avoid unnaturally fast walking when landing at speed - simulates skimming / skidding - if (Math.abs(wheelAdvance) > MAX_FT_WHEEL_INCREMENT) { - wheelAdvance = 0; - } - - // advance the walk wheel the appropriate amount - motion.advanceFrequencyTimeWheel(wheelAdvance); - - // walking? then see if it's a good time to measure the stride length (needs to be at least 97% of max walking speed) - const ALMOST_ONE = 0.97; - if (avatar.currentAnimation === avatar.selectedWalkBlend && - (Vec3.length(motion.velocity) / MAX_WALK_SPEED > ALMOST_ONE)) { - - var strideMaxAt = avatar.currentAnimation.calibration.strideMaxAt; - const TOLERANCE = 1.0; - - if (motion.frequencyTimeWheelPos < (strideMaxAt + TOLERANCE) && - motion.frequencyTimeWheelPos > (strideMaxAt - TOLERANCE) && - motion.currentTransition === nullTransition) { - // measure and save stride length - var footRPos = MyAvatar.getJointPosition("RightFoot"); - var footLPos = MyAvatar.getJointPosition("LeftFoot"); - avatar.calibration.strideLength = Vec3.distance(footRPos, footLPos); - avatar.currentAnimation.calibration.strideLength = avatar.calibration.strideLength; - } else { - // use the previously saved value for stride length - avatar.calibration.strideLength = avatar.currentAnimation.calibration.strideLength; - } - } // end get walk stride length -} - -// initialise a new transition. update progress of a live transition -function updateTransitions() { - - if (motion.currentTransition !== nullTransition) { - // is this a new transition? - if (motion.currentTransition.progress === 0) { - // do we have overlapping transitions? - if (motion.currentTransition.lastTransition !== nullTransition) { - // is the last animation for the nested transition the same as the new animation? - if (motion.currentTransition.lastTransition.lastAnimation === avatar.currentAnimation) { - // then sync the nested transition's frequency time wheel for a smooth animation blend - motion.frequencyTimeWheelPos = motion.currentTransition.lastTransition.lastFrequencyTimeWheelPos; - } - } - } - if (motion.currentTransition.updateProgress() === TRANSITION_COMPLETE) { - motion.currentTransition = nullTransition; - } - } -} - -// helper function for renderMotion(). calculate the amount to lean forwards (or backwards) based on the avi's velocity -var leanPitchSmoothingFilter = filter.createButterworthFilter(); -function getLeanPitch() { - var leanProgress = 0; - - if (motion.direction === DOWN || - motion.direction === FORWARDS || - motion.direction === BACKWARDS) { - leanProgress = -motion.velocity.z / TOP_SPEED; - } - // use filters to shape the walking acceleration response - leanProgress = leanPitchSmoothingFilter.process(leanProgress); - return PITCH_MAX * leanProgress; -} - -// helper function for renderMotion(). calculate the angle at which to bank into corners whilst turning -var leanRollSmoothingFilter = filter.createButterworthFilter(); -function getLeanRoll() { - var leanRollProgress = 0; - var linearContribution = 0; - const LOG_SCALER = 8; - - if (Vec3.length(motion.velocity) > 0) { - linearContribution = (Math.log(Vec3.length(motion.velocity) / TOP_SPEED) + LOG_SCALER) / LOG_SCALER; - } - var angularContribution = Math.abs(motion.yawDelta) / DELTA_YAW_MAX; - leanRollProgress = linearContribution; - leanRollProgress *= angularContribution; - // shape the response curve - leanRollProgress = filter.bezier(leanRollProgress, {x: 1, y: 0}, {x: 1, y: 0}); - // which way to lean? - var turnSign = (motion.yawDelta >= 0) ? 1 : -1; - - if (motion.direction === BACKWARDS || - motion.direction === LEFT) { - turnSign *= -1; - } - // filter progress - leanRollProgress = leanRollSmoothingFilter.process(turnSign * leanRollProgress); - return ROLL_MAX * leanRollProgress; -} - -// animate the avatar using sine waves, geometric waveforms and harmonic generators -function renderMotion() { - // leaning in response to speed and acceleration - var leanPitch = motion.state === STATIC ? 0 : getLeanPitch(); - var leanRoll = motion.state === STATIC ? 0 : getLeanRoll(); - var lastDirection = motion.lastDirection; - // hips translations from currently playing animations - var hipsTranslations = {x:0, y:0, z:0}; - - if (motion.currentTransition !== nullTransition) { - // maintain previous direction when transitioning from a walk - if (motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { - motion.lastDirection = motion.currentTransition.lastDirection; - } - hipsTranslations = motion.currentTransition.blendTranslations(motion.frequencyTimeWheelPos, - motion.lastDirection); - } else { - hipsTranslations = animationOperations.calculateTranslations(avatar.currentAnimation, - motion.frequencyTimeWheelPos, - motion.direction); - } - // factor any leaning into the hips offset - hipsTranslations.z += avatar.calibration.hipsToFeet * Math.sin(filter.degToRad(leanPitch)); - hipsTranslations.x += avatar.calibration.hipsToFeet * Math.sin(filter.degToRad(leanRoll)); - - // ensure skeleton offsets are within the 1m limit - hipsTranslations.x = hipsTranslations.x > 1 ? 1 : hipsTranslations.x; - hipsTranslations.x = hipsTranslations.x < -1 ? -1 : hipsTranslations.x; - hipsTranslations.y = hipsTranslations.y > 1 ? 1 : hipsTranslations.y; - hipsTranslations.y = hipsTranslations.y < -1 ? -1 : hipsTranslations.y; - hipsTranslations.z = hipsTranslations.z > 1 ? 1 : hipsTranslations.z; - hipsTranslations.z = hipsTranslations.z < -1 ? -1 : hipsTranslations.z; - // apply translations - MyAvatar.setSkeletonOffset(hipsTranslations); - - // play footfall sound? - var producingFootstepSounds = (avatar.currentAnimation === avatar.selectedWalkBlend) && avatar.makesFootStepSounds; - - if (motion.currentTransition !== nullTransition && avatar.makesFootStepSounds) { - if (motion.currentTransition.nextAnimation === avatar.selectedWalkBlend || - motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { - producingFootstepSounds = true; - } - } - if (producingFootstepSounds) { - const QUARTER_CYCLE = 90; - const THREE_QUARTER_CYCLE = 270; - var ftWheelPosition = motion.frequencyTimeWheelPos; - - if (motion.currentTransition !== nullTransition && - motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { - ftWheelPosition = motion.currentTransition.lastFrequencyTimeWheelPos; - } - if (avatar.nextStep === LEFT && ftWheelPosition > THREE_QUARTER_CYCLE) { - avatar.makeFootStepSound(); - } else if (avatar.nextStep === RIGHT && (ftWheelPosition < THREE_QUARTER_CYCLE && ftWheelPosition > QUARTER_CYCLE)) { - avatar.makeFootStepSound(); - } - } - - // apply joint rotations - for (jointName in avatar.currentAnimation.joints) { - var joint = walkAssets.animationReference.joints[jointName]; - var jointRotations = undefined; - - // ignore arms / head rotations if options are selected in the settings - if (avatar.armsFree && (joint.IKChain === "LeftArm" || joint.IKChain === "RightArm")) { - continue; - } - if (avatar.headFree && joint.IKChain === "Head") { - continue; - } - - // if there's a live transition, blend the rotations with the last animation's rotations - if (motion.currentTransition !== nullTransition) { - jointRotations = motion.currentTransition.blendRotations(jointName, - motion.frequencyTimeWheelPos, - motion.lastDirection); - } else { - jointRotations = animationOperations.calculateRotations(jointName, - avatar.currentAnimation, - motion.frequencyTimeWheelPos, - motion.direction); - } - - // apply angular velocity and speed induced leaning - if (jointName === "Hips") { - jointRotations.x += leanPitch; - jointRotations.z += leanRoll; - } - - // apply rotations - MyAvatar.setJointRotation(jointName, Quat.fromVec3Degrees(jointRotations)); - } +// +// walk.js +// version 1.25 +// +// Created by David Wooldridge, June 2015 +// Copyright © 2014 - 2015 High Fidelity, Inc. +// +// Animates an avatar using procedural animation techniques. +// +// Editing tools for animation data files available here: https://github.com/DaveDubUK/walkTools +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// animations, reach poses, reach pose parameters, transitions, transition parameters, sounds, image/s and reference files +const HIFI_PUBLIC_BUCKET = "https://hifi-public.s3.amazonaws.com/"; +var pathToAssets = HIFI_PUBLIC_BUCKET + "procedural-animator/assets/"; + +Script.include([ + "./libraries/walkConstants.js", + "./libraries/walkFilters.js", + "./libraries/walkApi.js", + pathToAssets + "walkAssets.js" +]); + +// construct Avatar, Motion and (null) Transition +var avatar = new Avatar(); +var motion = new Motion(); +var nullTransition = new Transition(); +motion.currentTransition = nullTransition; + +// create settings (gets initial values from avatar) +Script.include("./libraries/walkSettings.js"); + +// Main loop +Script.update.connect(function(deltaTime) { + + if (motion.isLive) { + + // assess current locomotion state + motion.assess(deltaTime); + + // decide which animation should be playing + selectAnimation(); + + // advance the animation cycle/s by the correct amount/s + advanceAnimations(); + + // update the progress of any live transitions + updateTransitions(); + + // apply translation and rotations + renderMotion(); + + // save this frame's parameters + motion.saveHistory(); + } +}); + +// helper function for selectAnimation() +function setTransition(nextAnimation, playTransitionReachPoses) { + var lastTransition = motion.currentTransition; + var lastAnimation = avatar.currentAnimation; + + // if already transitioning from a blended walk need to maintain the previous walk's direction + if (lastAnimation.lastDirection) { + switch(lastAnimation.lastDirection) { + + case FORWARDS: + lastAnimation = avatar.selectedWalk; + break; + + case BACKWARDS: + lastAnimation = avatar.selectedWalkBackwards; + break; + + case LEFT: + lastAnimation = avatar.selectedSideStepLeft; + break; + + case RIGHT: + lastAnimation = avatar.selectedSideStepRight; + break; + } + } + + motion.currentTransition = new Transition(nextAnimation, lastAnimation, lastTransition, playTransitionReachPoses); + avatar.currentAnimation = nextAnimation; + + // reset default first footstep + if (nextAnimation === avatar.selectedWalkBlend && lastTransition === nullTransition) { + avatar.nextStep = RIGHT; + } +} + +// fly animation blending: smoothing / damping filters +const FLY_BLEND_DAMPING = 50; +var flyUpFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); +var flyDownFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); +var flyForwardFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); +var flyBackwardFilter = filter.createAveragingFilter(FLY_BLEND_DAMPING); + +// select / blend the appropriate animation for the current state of motion +function selectAnimation() { + var playTransitionReachPoses = true; + + // select appropriate animation. create transitions where appropriate + switch (motion.nextState) { + case STATIC: { + if (avatar.distanceFromSurface < ON_SURFACE_THRESHOLD && + avatar.currentAnimation !== avatar.selectedIdle) { + setTransition(avatar.selectedIdle, playTransitionReachPoses); + } else if (!(avatar.distanceFromSurface < ON_SURFACE_THRESHOLD) && + avatar.currentAnimation !== avatar.selectedHover) { + setTransition(avatar.selectedHover, playTransitionReachPoses); + } + motion.state = STATIC; + avatar.selectedWalkBlend.lastDirection = NONE; + break; + } + + case SURFACE_MOTION: { + // walk transition reach poses are currently only specified for starting to walk forwards + playTransitionReachPoses = (motion.direction === FORWARDS); + var isAlreadyWalking = (avatar.currentAnimation === avatar.selectedWalkBlend); + + switch (motion.direction) { + case FORWARDS: + if (avatar.selectedWalkBlend.lastDirection !== FORWARDS) { + animationOperations.deepCopy(avatar.selectedWalk, avatar.selectedWalkBlend); + avatar.calibration.strideLength = avatar.selectedWalk.calibration.strideLength; + } + avatar.selectedWalkBlend.lastDirection = FORWARDS; + break; + + case BACKWARDS: + if (avatar.selectedWalkBlend.lastDirection !== BACKWARDS) { + animationOperations.deepCopy(avatar.selectedWalkBackwards, avatar.selectedWalkBlend); + avatar.calibration.strideLength = avatar.selectedWalkBackwards.calibration.strideLength; + } + avatar.selectedWalkBlend.lastDirection = BACKWARDS; + break; + + case LEFT: + animationOperations.deepCopy(avatar.selectedSideStepLeft, avatar.selectedWalkBlend); + avatar.selectedWalkBlend.lastDirection = LEFT; + avatar.calibration.strideLength = avatar.selectedSideStepLeft.calibration.strideLength; + break + + case RIGHT: + animationOperations.deepCopy(avatar.selectedSideStepRight, avatar.selectedWalkBlend); + avatar.selectedWalkBlend.lastDirection = RIGHT; + avatar.calibration.strideLength = avatar.selectedSideStepRight.calibration.strideLength; + break; + + default: + // condition occurs when the avi goes through the floor due to collision hull errors + animationOperations.deepCopy(avatar.selectedWalk, avatar.selectedWalkBlend); + avatar.selectedWalkBlend.lastDirection = FORWARDS; + avatar.calibration.strideLength = avatar.selectedWalk.calibration.strideLength; + break; + } + + if (!isAlreadyWalking && !motion.isComingToHalt) { + setTransition(avatar.selectedWalkBlend, playTransitionReachPoses); + } + motion.state = SURFACE_MOTION; + break; + } + + case AIR_MOTION: { + // blend the up, down, forward and backward flying animations relative to motion speed and direction + animationOperations.zeroAnimation(avatar.selectedFlyBlend); + + // calculate influences based on velocity and direction + var velocityMagnitude = Vec3.length(motion.velocity); + var verticalProportion = motion.velocity.y / velocityMagnitude; + var thrustProportion = motion.velocity.z / velocityMagnitude / 2; + + // directional components + var upComponent = motion.velocity.y > 0 ? verticalProportion : 0; + var downComponent = motion.velocity.y < 0 ? -verticalProportion : 0; + var forwardComponent = motion.velocity.z < 0 ? -thrustProportion : 0; + var backwardComponent = motion.velocity.z > 0 ? thrustProportion : 0; + + // smooth / damp directional components to add visual 'weight' + upComponent = flyUpFilter.process(upComponent); + downComponent = flyDownFilter.process(downComponent); + forwardComponent = flyForwardFilter.process(forwardComponent); + backwardComponent = flyBackwardFilter.process(backwardComponent); + + // normalise directional components + var normaliser = upComponent + downComponent + forwardComponent + backwardComponent; + upComponent = upComponent / normaliser; + downComponent = downComponent / normaliser; + forwardComponent = forwardComponent / normaliser; + backwardComponent = backwardComponent / normaliser; + + // blend animations proportionally + if (upComponent > 0) { + animationOperations.blendAnimation(avatar.selectedFlyUp, + avatar.selectedFlyBlend, + upComponent); + } + if (downComponent > 0) { + animationOperations.blendAnimation(avatar.selectedFlyDown, + avatar.selectedFlyBlend, + downComponent); + } + if (forwardComponent > 0) { + animationOperations.blendAnimation(avatar.selectedFly, + avatar.selectedFlyBlend, + Math.abs(forwardComponent)); + } + if (backwardComponent > 0) { + animationOperations.blendAnimation(avatar.selectedFlyBackwards, + avatar.selectedFlyBlend, + Math.abs(backwardComponent)); + } + + if (avatar.currentAnimation !== avatar.selectedFlyBlend) { + setTransition(avatar.selectedFlyBlend, playTransitionReachPoses); + } + motion.state = AIR_MOTION; + avatar.selectedWalkBlend.lastDirection = NONE; + break; + } + } // end switch next state of motion +} + +// determine the length of stride. advance the frequency time wheels. advance frequency time wheels for any live transitions +function advanceAnimations() { + var wheelAdvance = 0; + + // turn the frequency time wheel + if (avatar.currentAnimation === avatar.selectedWalkBlend) { + // Using technique described here: http://www.gdcvault.com/play/1020583/Animation-Bootcamp-An-Indie-Approach + // wrap the stride length around a 'surveyor's wheel' twice and calculate the angular speed at the given (linear) speed + // omega = v / r , where r = circumference / 2 PI and circumference = 2 * stride length + var speed = Vec3.length(motion.velocity); + motion.frequencyTimeWheelRadius = avatar.calibration.strideLength / Math.PI; + var ftWheelAngularVelocity = speed / motion.frequencyTimeWheelRadius; + // calculate the degrees turned (at this angular speed) since last frame + wheelAdvance = filter.radToDeg(motion.deltaTime * ftWheelAngularVelocity); + } else { + // turn the frequency time wheel by the amount specified for this animation + wheelAdvance = filter.radToDeg(avatar.currentAnimation.calibration.frequency * motion.deltaTime); + } + + if (motion.currentTransition !== nullTransition) { + // the last animation is still playing so we turn it's frequency time wheel to maintain the animation + if (motion.currentTransition.lastAnimation === motion.selectedWalkBlend) { + // if at a stop angle (i.e. feet now under the avi) hold the wheel position for remainder of transition + var tolerance = motion.currentTransition.lastFrequencyTimeIncrement + 0.1; + if ((motion.currentTransition.lastFrequencyTimeWheelPos > + (motion.currentTransition.stopAngle - tolerance)) && + (motion.currentTransition.lastFrequencyTimeWheelPos < + (motion.currentTransition.stopAngle + tolerance))) { + motion.currentTransition.lastFrequencyTimeIncrement = 0; + } + } + motion.currentTransition.advancePreviousFrequencyTimeWheel(motion.deltaTime); + } + + // avoid unnaturally fast walking when landing at speed - simulates skimming / skidding + if (Math.abs(wheelAdvance) > MAX_FT_WHEEL_INCREMENT) { + wheelAdvance = 0; + } + + // advance the walk wheel the appropriate amount + motion.advanceFrequencyTimeWheel(wheelAdvance); + + // walking? then see if it's a good time to measure the stride length (needs to be at least 97% of max walking speed) + const ALMOST_ONE = 0.97; + if (avatar.currentAnimation === avatar.selectedWalkBlend && + (Vec3.length(motion.velocity) / MAX_WALK_SPEED > ALMOST_ONE)) { + + var strideMaxAt = avatar.currentAnimation.calibration.strideMaxAt; + const TOLERANCE = 1.0; + + if (motion.frequencyTimeWheelPos < (strideMaxAt + TOLERANCE) && + motion.frequencyTimeWheelPos > (strideMaxAt - TOLERANCE) && + motion.currentTransition === nullTransition) { + // measure and save stride length + var footRPos = MyAvatar.getJointPosition("RightFoot"); + var footLPos = MyAvatar.getJointPosition("LeftFoot"); + avatar.calibration.strideLength = Vec3.distance(footRPos, footLPos); + avatar.currentAnimation.calibration.strideLength = avatar.calibration.strideLength; + } else { + // use the previously saved value for stride length + avatar.calibration.strideLength = avatar.currentAnimation.calibration.strideLength; + } + } // end get walk stride length +} + +// initialise a new transition. update progress of a live transition +function updateTransitions() { + + if (motion.currentTransition !== nullTransition) { + // is this a new transition? + if (motion.currentTransition.progress === 0) { + // do we have overlapping transitions? + if (motion.currentTransition.lastTransition !== nullTransition) { + // is the last animation for the nested transition the same as the new animation? + if (motion.currentTransition.lastTransition.lastAnimation === avatar.currentAnimation) { + // then sync the nested transition's frequency time wheel for a smooth animation blend + motion.frequencyTimeWheelPos = motion.currentTransition.lastTransition.lastFrequencyTimeWheelPos; + } + } + } + if (motion.currentTransition.updateProgress() === TRANSITION_COMPLETE) { + motion.currentTransition = nullTransition; + } + } +} + +// helper function for renderMotion(). calculate the amount to lean forwards (or backwards) based on the avi's velocity +var leanPitchSmoothingFilter = filter.createButterworthFilter(); +function getLeanPitch() { + var leanProgress = 0; + + if (motion.direction === DOWN || + motion.direction === FORWARDS || + motion.direction === BACKWARDS) { + leanProgress = -motion.velocity.z / TOP_SPEED; + } + // use filters to shape the walking acceleration response + leanProgress = leanPitchSmoothingFilter.process(leanProgress); + return PITCH_MAX * leanProgress; +} + +// helper function for renderMotion(). calculate the angle at which to bank into corners whilst turning +var leanRollSmoothingFilter = filter.createButterworthFilter(); +function getLeanRoll() { + var leanRollProgress = 0; + var linearContribution = 0; + const LOG_SCALER = 8; + + if (Vec3.length(motion.velocity) > 0) { + linearContribution = (Math.log(Vec3.length(motion.velocity) / TOP_SPEED) + LOG_SCALER) / LOG_SCALER; + } + var angularContribution = Math.abs(motion.yawDelta) / DELTA_YAW_MAX; + leanRollProgress = linearContribution; + leanRollProgress *= angularContribution; + // shape the response curve + leanRollProgress = filter.bezier(leanRollProgress, {x: 1, y: 0}, {x: 1, y: 0}); + // which way to lean? + var turnSign = (motion.yawDelta >= 0) ? 1 : -1; + + if (motion.direction === BACKWARDS || + motion.direction === LEFT) { + turnSign *= -1; + } + // filter progress + leanRollProgress = leanRollSmoothingFilter.process(turnSign * leanRollProgress); + return ROLL_MAX * leanRollProgress; +} + +// animate the avatar using sine waves, geometric waveforms and harmonic generators +function renderMotion() { + // leaning in response to speed and acceleration + var leanPitch = motion.state === STATIC ? 0 : getLeanPitch(); + var leanRoll = motion.state === STATIC ? 0 : getLeanRoll(); + var lastDirection = motion.lastDirection; + // hips translations from currently playing animations + var hipsTranslations = {x:0, y:0, z:0}; + + if (motion.currentTransition !== nullTransition) { + // maintain previous direction when transitioning from a walk + if (motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { + motion.lastDirection = motion.currentTransition.lastDirection; + } + hipsTranslations = motion.currentTransition.blendTranslations(motion.frequencyTimeWheelPos, + motion.lastDirection); + } else { + hipsTranslations = animationOperations.calculateTranslations(avatar.currentAnimation, + motion.frequencyTimeWheelPos, + motion.direction); + } + // factor any leaning into the hips offset + hipsTranslations.z += avatar.calibration.hipsToFeet * Math.sin(filter.degToRad(leanPitch)); + hipsTranslations.x += avatar.calibration.hipsToFeet * Math.sin(filter.degToRad(leanRoll)); + + // ensure skeleton offsets are within the 1m limit + hipsTranslations.x = hipsTranslations.x > 1 ? 1 : hipsTranslations.x; + hipsTranslations.x = hipsTranslations.x < -1 ? -1 : hipsTranslations.x; + hipsTranslations.y = hipsTranslations.y > 1 ? 1 : hipsTranslations.y; + hipsTranslations.y = hipsTranslations.y < -1 ? -1 : hipsTranslations.y; + hipsTranslations.z = hipsTranslations.z > 1 ? 1 : hipsTranslations.z; + hipsTranslations.z = hipsTranslations.z < -1 ? -1 : hipsTranslations.z; + // apply translations + MyAvatar.setSkeletonOffset(hipsTranslations); + + // play footfall sound? + var producingFootstepSounds = (avatar.currentAnimation === avatar.selectedWalkBlend) && avatar.makesFootStepSounds; + + if (motion.currentTransition !== nullTransition && avatar.makesFootStepSounds) { + if (motion.currentTransition.nextAnimation === avatar.selectedWalkBlend || + motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { + producingFootstepSounds = true; + } + } + if (producingFootstepSounds) { + const QUARTER_CYCLE = 90; + const THREE_QUARTER_CYCLE = 270; + var ftWheelPosition = motion.frequencyTimeWheelPos; + + if (motion.currentTransition !== nullTransition && + motion.currentTransition.lastAnimation === avatar.selectedWalkBlend) { + ftWheelPosition = motion.currentTransition.lastFrequencyTimeWheelPos; + } + if (avatar.nextStep === LEFT && ftWheelPosition > THREE_QUARTER_CYCLE) { + avatar.makeFootStepSound(); + } else if (avatar.nextStep === RIGHT && (ftWheelPosition < THREE_QUARTER_CYCLE && ftWheelPosition > QUARTER_CYCLE)) { + avatar.makeFootStepSound(); + } + } + + // apply joint rotations + for (jointName in avatar.currentAnimation.joints) { + var joint = walkAssets.animationReference.joints[jointName]; + var jointRotations = undefined; + + // ignore arms / head rotations if options are selected in the settings + if (avatar.armsFree && (joint.IKChain === "LeftArm" || joint.IKChain === "RightArm")) { + continue; + } + if (avatar.headFree && joint.IKChain === "Head") { + continue; + } + + // if there's a live transition, blend the rotations with the last animation's rotations + if (motion.currentTransition !== nullTransition) { + jointRotations = motion.currentTransition.blendRotations(jointName, + motion.frequencyTimeWheelPos, + motion.lastDirection); + } else { + jointRotations = animationOperations.calculateRotations(jointName, + avatar.currentAnimation, + motion.frequencyTimeWheelPos, + motion.direction); + } + + // apply angular velocity and speed induced leaning + if (jointName === "Hips") { + jointRotations.x += leanPitch; + jointRotations.z += leanRoll; + } + + // apply rotations + MyAvatar.setJointRotation(jointName, Quat.fromVec3Degrees(jointRotations)); + } } \ No newline at end of file From 8f83870e527ab2d59eaf8237b5a1d297439016c3 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 21 Oct 2015 14:19:21 -0700 Subject: [PATCH 03/35] Update pitching.js to add audio --- examples/baseball/audio/assets.txt | 23 ++++ examples/baseball/audio/attribution.txt | 8 ++ examples/baseball/line.js | 165 ++++++++++++++++++++++++ examples/{ => baseball}/pitching.js | 0 4 files changed, 196 insertions(+) create mode 100644 examples/baseball/audio/assets.txt create mode 100644 examples/baseball/audio/attribution.txt create mode 100644 examples/baseball/line.js rename examples/{ => baseball}/pitching.js (100%) diff --git a/examples/baseball/audio/assets.txt b/examples/baseball/audio/assets.txt new file mode 100644 index 0000000000..225714cbe1 --- /dev/null +++ b/examples/baseball/audio/assets.txt @@ -0,0 +1,23 @@ +crowd-boos.wav +atp:c632c92b166ade60aa16b23ff1dfdf712856caeb83bd9311980b2d5edac821af.wav + +crowd-cheers-organ.wav +atp:b8044401a846ed29f881a0b9b80cf1ba41f26327180c28fc9c70d144f9b70045.wav + +crowd-medium.wav +atp:0821bf2ac60dd2f356dfdd948e8bb89c23984dc3584612f6c815765154f02cae.wav + +baseball-hitting-bat-1.wav +atp:6f0b691a0c9c9ece6557d97fe242b1faec4020fe26efc9c17327993b513c5fe5.wav + +baseball-hitting-bat-set-1.wav +atp:5be5806205158ebdc5c3623ceb7ae73315028b51ffeae24292aff7042e3fa6a9.wav + +baseball-hitting-bat-set-2.wav +atp:e68661374e2145c480809c26134782aad11e0de456c7802170c7abccc4028873.wav + +baseball-hitting-bat-set-3.wav +atp:787e3c9af17dd3929527787176ede83d6806260e63ddd5a4cef48cd22e32c6f7.wav + +baseball-hitting-bat-set-4.wav +atp:fc65383431a6238c7a4749f0f6f061f75a604ed5e17d775ab1b2955609e67ebb.wav \ No newline at end of file diff --git a/examples/baseball/audio/attribution.txt b/examples/baseball/audio/attribution.txt new file mode 100644 index 0000000000..760951447c --- /dev/null +++ b/examples/baseball/audio/attribution.txt @@ -0,0 +1,8 @@ +Baseball bat hitting sounds +https://www.freesound.org/people/SocializedArtist45/sounds/266595/ +https://www.freesound.org/people/CGEffex/sounds/93136/ + +Crowd Sounds +http://freesound.org/people/AshFox/sounds/191925/ +http://freesound.org/people/AshFox/sounds/191928/ +http://freesound.org/people/AshFox/sounds/191929/ \ No newline at end of file diff --git a/examples/baseball/line.js b/examples/baseball/line.js new file mode 100644 index 0000000000..c4fac784c4 --- /dev/null +++ b/examples/baseball/line.js @@ -0,0 +1,165 @@ +function info(message) { + print("[INFO] " + message); +} + +function error(message) { + print("[ERROR] " + message); +} + + +/****************************************************************************** + * PolyLine + *****************************************************************************/ +var LINE_DIMENSIONS = { x: 2000, y: 2000, z: 2000 }; +var MAX_LINE_LENGTH = 40; // This must be 2 or greater; +var PolyLine = function(position, color, defaultStrokeWidth) { + //info("Creating polyline"); + //Vec3.print("New line at", position); + this.position = position; + this.color = color; + this.defaultStrokeWidth = 0.10; + this.points = [ + { x: 0, y: 0, z: 0 }, + ]; + this.strokeWidths = [ + this.defaultStrokeWidth, + ] + this.normals = [ + { x: 1, y: 0, z: 0 }, + ] + this.entityID = Entities.addEntity({ + type: "PolyLine", + position: position, + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + dimensions: LINE_DIMENSIONS, + color: color, + lifetime: 20, + }); +}; + +PolyLine.prototype.enqueuePoint = function(position) { + if (this.isFull()) { + error("Hit max PolyLine size"); + return; + } + + //Vec3.print("pos", position); + //info("Number of points: " + this.points.length); + + position = Vec3.subtract(position, this.position); + this.points.push(position); + this.normals.push({ x: 1, y: 0, z: 0 }); + this.strokeWidths.push(this.defaultStrokeWidth); + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.dequeuePoint = function() { + if (this.points.length == 0) { + error("Hit min PolyLine size"); + return; + } + + this.points = this.points.slice(1); + this.normals = this.normals.slice(1); + this.strokeWidths = this.strokeWidths.slice(1); + + Entities.editEntity(this.entityID, { + linePoints: this.points, + normals: this.normals, + strokeWidths: this.strokeWidths, + }); +}; + +PolyLine.prototype.getFirstPoint = function() { + return Vec3.sum(this.position, this.points[0]); +}; + +PolyLine.prototype.getLastPoint = function() { + return Vec3.sum(this.position, this.points[this.points.length - 1]); +}; + +PolyLine.prototype.getSize = function() { + return this.points.length; +} + +PolyLine.prototype.isFull = function() { + return this.points.length >= MAX_LINE_LENGTH; +}; + +PolyLine.prototype.destroy = function() { + Entities.deleteEntity(this.entityID); + this.points = []; +}; + + +/****************************************************************************** + * InfiniteLine + *****************************************************************************/ +InfiniteLine = function(position, color) { + this.position = position; + this.color = color; + this.lines = [new PolyLine(position, color)]; + this.size = 0; +}; + +InfiniteLine.prototype.enqueuePoint = function(position) { + var currentLine; + + if (this.lines.length == 0) { + currentLine = new PolyLine(position, this.color); + this.lines.push(currentLine); + } else { + currentLine = this.lines[this.lines.length - 1]; + } + + if (currentLine.isFull()) { + //info("Current line is full, creating new line"); + //Vec3.print("Last line is", currentLine.getLastPoint()); + //Vec3.print("New line is", position); + var newLine = new PolyLine(currentLine.getLastPoint(), this.color); + this.lines.push(newLine); + currentLine = newLine; + } + + currentLine.enqueuePoint(position); + + ++this.size; +}; + +InfiniteLine.prototype.dequeuePoint = function() { + if (this.lines.length == 0) { + error("Trying to dequeue from InfiniteLine when no points are left"); + return; + } + + var lastLine = this.lines[0]; + lastLine.dequeuePoint(); + + if (lastLine.getSize() <= 1) { + this.lines = this.lines.slice(1); + } + + --this.size; +}; + +InfiniteLine.prototype.getFirstPoint = function() { + return this.lines.length > 0 ? this.lines[0].getFirstPoint() : null; +}; + +InfiniteLine.prototype.getLastPoint = function() { + return this.lines.length > 0 ? this.lines[lines.length - 1].getLastPoint() : null; +}; + +InfiniteLine.prototype.destroy = function() { + for (var i = 0; i < this.lines.length; ++i) { + this.lines[i].destroy(); + } + + this.size = 0; +}; diff --git a/examples/pitching.js b/examples/baseball/pitching.js similarity index 100% rename from examples/pitching.js rename to examples/baseball/pitching.js From 934575a78a1216c89c8636b3d7aa352d82e259d4 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 21 Oct 2015 16:46:07 -0700 Subject: [PATCH 04/35] Refactor pitching.js --- examples/baseball/pitching.js | 332 ++++++++++++++-------------------- 1 file changed, 139 insertions(+), 193 deletions(-) diff --git a/examples/baseball/pitching.js b/examples/baseball/pitching.js index b419e8935c..5cb29ac5d7 100644 --- a/examples/baseball/pitching.js +++ b/examples/baseball/pitching.js @@ -1,16 +1,25 @@ -//var PITCH_THUNK_SOUND_URL = "file:///C:/Users/Ryan/Downloads/323725__reitanna__thunk.wav"; -var PITCH_THUNK_SOUND_URL = "file:///C:/Users/Ryan/Downloads/thunk.wav"; +Script.include("line.js"); + +var AUDIO = { + crowdBoos: [ + SoundCache.getSound("atp:c632c92b166ade60aa16b23ff1dfdf712856caeb83bd9311980b2d5edac821af.wav", false) + ], + crowdCheers: [ + SoundCache.getSound("atp:0821bf2ac60dd2f356dfdd948e8bb89c23984dc3584612f6c815765154f02cae.wav", false), + SoundCache.getSound("atp:b8044401a846ed29f881a0b9b80cf1ba41f26327180c28fc9c70d144f9b70045.wav", false), + ], + batHit: [ + SoundCache.getSound("atp:6f0b691a0c9c9ece6557d97fe242b1faec4020fe26efc9c17327993b513c5fe5.wav", false), + SoundCache.getSound("atp:5be5806205158ebdc5c3623ceb7ae73315028b51ffeae24292aff7042e3fa6a9.wav", false), + SoundCache.getSound("atp:e68661374e2145c480809c26134782aad11e0de456c7802170c7abccc4028873.wav", false), + SoundCache.getSound("atp:787e3c9af17dd3929527787176ede83d6806260e63ddd5a4cef48cd22e32c6f7.wav", false), + SoundCache.getSound("atp:fc65383431a6238c7a4749f0f6f061f75a604ed5e17d775ab1b2955609e67ebb.wav", false), + ] +} + var PITCH_THUNK_SOUND_URL = "http://hifi-public.s3.amazonaws.com/sounds/ping_pong_gun/pong_sound.wav"; var pitchSound = SoundCache.getSound(PITCH_THUNK_SOUND_URL, false); -function info(message) { - print("[INFO] " + message); -} - -function error(message) { - print("[ERROR] " + message); -} - var PITCHING_MACHINE_URL = "atp:87d4879530b698741ecc45f6f31789aac11f7865a2c3bec5fe9b061a182c80d4.fbx"; var PITCHING_MACHINE_OUTPUT_OFFSET_PCT = { x: 0.0, @@ -23,7 +32,7 @@ var PITCHING_MACHINE_PROPERTIES = { position: { x: 0, y: 0.8, - z: -22.3, + z: -18.3, }, velocity: { x: 0, @@ -51,12 +60,13 @@ var PITCHING_MACHINE_PROPERTIES = { shapeType: "Box", }; PITCHING_MACHINE_PROPERTIES.dimensions = Vec3.multiply(2.5, PITCHING_MACHINE_PROPERTIES.dimensions); - +var DISTANCE_FROM_PLATE = PITCHING_MACHINE_PROPERTIES.position.z; var PITCH_RATE = 5000; var BASEBALL_MODEL_URL = "atp:7185099f1f650600ca187222573a88200aeb835454bd2f578f12c7fb4fd190fa.fbx"; -var BASEBALL_SPEED = 2.7; +var BASEBALL_MIN_SPEED = 2.7; +var BASEBALL_MAX_SPEED = 5.7; var BASEBALL_RADIUS = 0.07468; var BASEBALL_PROPERTIES = { name: "Baseball", @@ -78,13 +88,17 @@ var BASEBALL_PROPERTIES = { x: 17.0, y: 0, z: -8.0, + + x: 0.0, + y: 0, + z: 0.0, }, angularDamping: 0.0, damping: 0.0, restitution: 0.5, friction: 0.0, lifetime: 20, - collisionSoundURL: PITCH_THUNK_SOUND_URL, + //collisionSoundURL: PITCH_THUNK_SOUND_URL, gravity: { x: 0, y: 0,//-9.8, @@ -107,23 +121,44 @@ function shallowCopy(obj) { } function randomInt(low, high) { - return low + (Math.random() * (high - low)); + return Math.floor(randomFloat(low, high)); +} + +function randomFloat(low, high) { + if (high === undefined) { + high = low; + low = 0; + } + return low + Math.random() * (high - low); } var ACCELERATION_SPREAD = 10.15; -function createBaseball(position, velocity, ballScale) { +function Baseball(position, velocity, ballScale) { + var self = this; + + // Setup entity properties var properties = shallowCopy(BASEBALL_PROPERTIES); properties.position = position; properties.velocity = velocity; - properties.acceleration = { + /* + properties.gravity = { x: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), y: randomInt(-ACCELERATION_SPREAD, ACCELERATION_SPREAD), z: 0.0, }; + */ properties.dimensions = Vec3.multiply(ballScale, properties.dimensions); - var entityID = Entities.addEntity(properties); - Script.addEventHandler(entityID, "collisionWithEntity", buildBaseballHitCallback(entityID)); + + // Create entity + this.entityID = Entities.addEntity(properties); + this.trail = null; + this.onHit = function() { return true; }; + this.hasBeenHit = false; + + this.boundCollisionCallback = function(a, b, c) { self.collisionCallback.call(self, a, b, c); }; + Script.addEventHandler(this.entityID, "collisionWithEntity", this.boundCollisionCallback); + /* if (false && Math.random() < 0.5) { for (var i = 0; i < 50; i++) { Script.setTimeout(function() { @@ -137,26 +172,39 @@ function createBaseball(position, velocity, ballScale) { }, i * 100); } } - return entityID; + */ } -var buildBaseballHitCallback = function(entityID) { - var f = function(entityA, entityB, collision) { - print("Got baseball hit callback"); - var properties = Entities.getEntityProperties(entityID, ['position', 'velocity']); - var line = new InfiniteLine(properties.position, { red: 255, green: 128, blue: 89 }); +Baseball.prototype = { + collisionCallback: function(entityA, entityB, collision) { + var self = this; + + this.hasBeenHit = true; + var properties = Entities.getEntityProperties(this.entityID, ['position', 'velocity']); + this.trail = new InfiniteLine(properties.position, { red: 255, green: 128, blue: 89 }); var lastPosition = properties.position; - Vec3.print("Velocity", properties.velocity); - Vec3.print("VelocityChange", collision.velocityChange); - Script.setInterval(function() { - var properties = Entities.getEntityProperties(entityID, ['position']); + //Vec3.print("Velocity", properties.velocity); + //Vec3.print("VelocityChange", collision.velocityChange); + var speed = Vec3.length(properties.velocity); + playRandomSound(AUDIO.batHit, { + position: properties.position, + volume: 2.0 + }); + var sounds = null; + if (speed < 5.0) { + sounds = AUDIO.crowdBoos; + } else { + sounds = AUDIO.crowdCheers; + } + var self = this; + this.trailInterval = Script.setInterval(function() { + var properties = Entities.getEntityProperties(self.entityID, ['position']); if (Vec3.distance(properties.position, lastPosition)) { - line.enqueuePoint(properties.position); + self.trail.enqueuePoint(properties.position); lastPosition = properties.position; } }, 50); - var speed = Vec3.length(properties.velocity); - Entities.editEntity(entityID, { + Entities.editEntity(self.entityID, { velocity: Vec3.multiply(2, properties.velocity), gravity: { x: 0, @@ -164,12 +212,35 @@ var buildBaseballHitCallback = function(entityID) { z: 0 } }); - print("Baseball hit!"); - Script.removeEventHandler(entityID, "collisionWithEntity", f); - }; - return f; + + var removeHandler = this.onHit(entityB, collision); + if (removeHandler) { + Script.removeEventHandler(self.entityID, "collisionWithEntity", self.boundCollisionCallback); + } + }, + cleanupTrail: function() { + if (this.trail) { + Script.clearInterval(this.trailInterval); + this.trailInterval = null; + + this.trail.destroy(); + this.trail = null; + } + } } +function playRandomSound(sounds, options) { + if (options === undefined) { + options = { + volume: 1.0, + position: MyAvatar.position, + } + } + Audio.playSound(sounds[randomInt(sounds.length)], options); +} + +var lastTrail = null; + function vec3Mult(a, b) { return { @@ -179,8 +250,14 @@ function vec3Mult(a, b) { }; } +var lastBall = null; var injector = null; + function pitchBall() { + if (lastBall) { + lastBall.cleanupTrail(); + } + var machineProperties = Entities.getEntityProperties(pitchingMachineID, ["dimensions", "position", "rotation"]); var pitchFromPositionBase = machineProperties.position; var pitchFromOffset = vec3Mult(machineProperties.dimensions, PITCHING_MACHINE_OUTPUT_OFFSET_PCT); @@ -189,7 +266,32 @@ function pitchBall() { var pitchDirection = Quat.getFront(machineProperties.rotation); var ballScale = machineProperties.dimensions.x / PITCHING_MACHINE_PROPERTIES.dimensions.x; print("Creating baseball"); - var ballEntityID = createBaseball(pitchFromPosition, Vec3.multiply(BASEBALL_SPEED, pitchDirection), ballScale); + + var speed = randomFloat(BASEBALL_MIN_SPEED, BASEBALL_MAX_SPEED) + var timeToPassPlate = (DISTANCE_FROM_PLATE + 1.0) / speed; + + var baseball = new Baseball(pitchFromPosition, Vec3.multiply(speed, pitchDirection), ballScale); + lastBall = baseball; + + baseball.onHit = function(entityB, collision) { + var properties = Entities.getEntityProperties(entityB, ["name"]); + var name = properties.name; + print("Hit: " + name); + if (name == "backstop") { + print("STRIKE"); + } else if (name == "bat") { + print("HIT"); + Script.setTimeout(function() { + playRandomSound(sounds, { + position: { x: 0 ,y: 0, z: 0 }, + volume: 1.0, + }); + }, 500); + } + //Script.clearTimeout(strikeTimeout); + return true; + } + if (!injector) { injector = Audio.playSound(pitchSound, { position: pitchFromPosition, @@ -209,159 +311,3 @@ Script.setInterval(pitchBall, PITCH_RATE); -/****************************************************************************** - * PolyLine - *****************************************************************************/ -var LINE_DIMENSIONS = { x: 2000, y: 2000, z: 2000 }; -var MAX_LINE_LENGTH = 40; // This must be 2 or greater; -var PolyLine = function(position, color, defaultStrokeWidth) { - //info("Creating polyline"); - //Vec3.print("New line at", position); - this.position = position; - this.color = color; - this.defaultStrokeWidth = 0.10; - this.points = [ - { x: 0, y: 0, z: 0 }, - ]; - this.strokeWidths = [ - this.defaultStrokeWidth, - ] - this.normals = [ - { x: 1, y: 0, z: 0 }, - ] - this.entityID = Entities.addEntity({ - type: "PolyLine", - position: position, - linePoints: this.points, - normals: this.normals, - strokeWidths: this.strokeWidths, - dimensions: LINE_DIMENSIONS, - color: color, - lifetime: 20, - }); -}; - -PolyLine.prototype.enqueuePoint = function(position) { - if (this.isFull()) { - error("Hit max PolyLine size"); - return; - } - - //Vec3.print("pos", position); - //info("Number of points: " + this.points.length); - - position = Vec3.subtract(position, this.position); - this.points.push(position); - this.normals.push({ x: 1, y: 0, z: 0 }); - this.strokeWidths.push(this.defaultStrokeWidth); - Entities.editEntity(this.entityID, { - linePoints: this.points, - normals: this.normals, - strokeWidths: this.strokeWidths, - }); -}; - -PolyLine.prototype.dequeuePoint = function() { - if (this.points.length == 0) { - error("Hit min PolyLine size"); - return; - } - - this.points = this.points.slice(1); - this.normals = this.normals.slice(1); - this.strokeWidths = this.strokeWidths.slice(1); - - Entities.editEntity(this.entityID, { - linePoints: this.points, - normals: this.normals, - strokeWidths: this.strokeWidths, - }); -}; - -PolyLine.prototype.getFirstPoint = function() { - return Vec3.sum(this.position, this.points[0]); -}; - -PolyLine.prototype.getLastPoint = function() { - return Vec3.sum(this.position, this.points[this.points.length - 1]); -}; - -PolyLine.prototype.getSize = function() { - return this.points.length; -} - -PolyLine.prototype.isFull = function() { - return this.points.length >= MAX_LINE_LENGTH; -}; - -PolyLine.prototype.destroy = function() { - Entities.deleteEntity(this.entityID); - this.points = []; -}; - - -/****************************************************************************** - * InfiniteLine - *****************************************************************************/ -InfiniteLine = function(position, color) { - this.position = position; - this.color = color; - this.lines = [new PolyLine(position, color)]; - this.size = 0; -}; - -InfiniteLine.prototype.enqueuePoint = function(position) { - var currentLine; - - if (this.lines.length == 0) { - currentLine = new PolyLine(position, this.color); - this.lines.push(currentLine); - } else { - currentLine = this.lines[this.lines.length - 1]; - } - - if (currentLine.isFull()) { - //info("Current line is full, creating new line"); - //Vec3.print("Last line is", currentLine.getLastPoint()); - //Vec3.print("New line is", position); - var newLine = new PolyLine(currentLine.getLastPoint(), this.color); - this.lines.push(newLine); - currentLine = newLine; - } - - currentLine.enqueuePoint(position); - - ++this.size; -}; - -InfiniteLine.prototype.dequeuePoint = function() { - if (this.lines.length == 0) { - error("Trying to dequeue from InfiniteLine when no points are left"); - return; - } - - var lastLine = this.lines[0]; - lastLine.dequeuePoint(); - - if (lastLine.getSize() <= 1) { - this.lines = this.lines.slice(1); - } - - --this.size; -}; - -InfiniteLine.prototype.getFirstPoint = function() { - return this.lines.length > 0 ? this.lines[0].getFirstPoint() : null; -}; - -InfiniteLine.prototype.getLastPoint = function() { - return this.lines.length > 0 ? this.lines[lines.length - 1].getLastPoint() : null; -}; - -InfiniteLine.prototype.destroy = function() { - for (var i = 0; i < this.lines.length; ++i) { - this.lines[i].destroy(); - } - - this.size = 0; -}; From 001400908d3d27fc33ea9cb5c4004675d67c2588 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 23 Oct 2015 13:37:49 -0700 Subject: [PATCH 05/35] Add automatic CCD activation to the physics engine --- libraries/physics/src/ObjectMotionState.cpp | 18 ++++++++++++++++++ libraries/physics/src/ObjectMotionState.h | 1 + 2 files changed, 19 insertions(+) diff --git a/libraries/physics/src/ObjectMotionState.cpp b/libraries/physics/src/ObjectMotionState.cpp index 4f3d0396c6..b47c870216 100644 --- a/libraries/physics/src/ObjectMotionState.cpp +++ b/libraries/physics/src/ObjectMotionState.cpp @@ -115,6 +115,21 @@ void ObjectMotionState::setMotionType(MotionType motionType) { _motionType = motionType; } +void ObjectMotionState::updateCCDConfiguration() { + if (_body) { + if (_shape) { + btVector3 center; + btScalar radius; + _shape->getBoundingSphere(center, radius); + _body->setCcdMotionThreshold(radius * 2.0f); + _body->setCcdSweptSphereRadius(radius); + } else { + // Disable CCD + _body->setCcdMotionThreshold(0); + } + } +} + void ObjectMotionState::setRigidBody(btRigidBody* body) { // give the body a (void*) back-pointer to this ObjectMotionState if (_body != body) { @@ -125,6 +140,7 @@ void ObjectMotionState::setRigidBody(btRigidBody* body) { if (_body) { _body->setUserPointer(this); } + updateCCDConfiguration(); } } @@ -187,6 +203,8 @@ bool ObjectMotionState::handleHardAndEasyChanges(uint32_t flags, PhysicsEngine* if (_shape != newShape) { _shape = newShape; _body->setCollisionShape(_shape); + + updateCCDConfiguration(); } else { // huh... the shape didn't actually change, so we clear the DIRTY_SHAPE flag flags &= ~Simulation::DIRTY_SHAPE; diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index 450ac34a90..9945d07c33 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -137,6 +137,7 @@ protected: virtual bool isReadyToComputeShape() = 0; virtual btCollisionShape* computeNewShape() = 0; void setMotionType(MotionType motionType); + void updateCCDConfiguration(); // clearObjectBackPointer() overrrides should call the base method, then actually clear the object back pointer. virtual void clearObjectBackPointer() { _type = MOTIONSTATE_TYPE_INVALID; } From f5d12ee2c60a139cbde514d3a398cdbe098f60f3 Mon Sep 17 00:00:00 2001 From: black plastick Date: Mon, 26 Oct 2015 17:46:08 -0400 Subject: [PATCH 06/35] Added AvatarList script events for assigment client scripts: AvatarList.avatarAddedEvent(sessionUUID); AvatarList.avatarRemovedEvent(sessionUUID); --- assignment-client/src/avatars/AvatarMixer.cpp | 4 ++-- libraries/avatars/src/AvatarHashMap.cpp | 4 +++- libraries/avatars/src/AvatarHashMap.h | 4 ++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/assignment-client/src/avatars/AvatarMixer.cpp b/assignment-client/src/avatars/AvatarMixer.cpp index 833b53b729..ae6a8247c1 100644 --- a/assignment-client/src/avatars/AvatarMixer.cpp +++ b/assignment-client/src/avatars/AvatarMixer.cpp @@ -254,10 +254,10 @@ void AvatarMixer::broadcastAvatarData() { // potentially update the max full rate distance for this frame maxAvatarDistanceThisFrame = std::max(maxAvatarDistanceThisFrame, distanceToAvatar); - if (distanceToAvatar != 0.0f + if (distanceToAvatar != 0.0f && distribution(generator) > (nodeData->getFullRateDistance() / distanceToAvatar)) { return; - } + } AvatarDataSequenceNumber lastSeqToReceiver = nodeData->getLastBroadcastSequenceNumber(otherNode->getUUID()); AvatarDataSequenceNumber lastSeqFromSender = otherNodeData->getLastReceivedSequenceNumber(); diff --git a/libraries/avatars/src/AvatarHashMap.cpp b/libraries/avatars/src/AvatarHashMap.cpp index 520bb34887..c02fe98b19 100644 --- a/libraries/avatars/src/AvatarHashMap.cpp +++ b/libraries/avatars/src/AvatarHashMap.cpp @@ -44,7 +44,7 @@ AvatarSharedPointer AvatarHashMap::addAvatar(const QUuid& sessionUUID, const QWe avatar->setSessionUUID(sessionUUID); avatar->setOwningAvatarMixer(mixerWeakPointer); _avatarHash.insert(sessionUUID, avatar); - + emit avatarAddedEvent(sessionUUID); return avatar; } @@ -131,10 +131,12 @@ void AvatarHashMap::processKillAvatar(QSharedPointer packet, SharedNod // read the node id QUuid sessionUUID = QUuid::fromRfc4122(packet->readWithoutCopy(NUM_BYTES_RFC4122_UUID)); removeAvatar(sessionUUID); + } void AvatarHashMap::removeAvatar(const QUuid& sessionUUID) { _avatarHash.remove(sessionUUID); + emit avatarRemovedEvent(sessionUUID); } void AvatarHashMap::sessionUUIDChanged(const QUuid& sessionUUID, const QUuid& oldUUID) { diff --git a/libraries/avatars/src/AvatarHashMap.h b/libraries/avatars/src/AvatarHashMap.h index 804233b76a..f90c61ec4d 100644 --- a/libraries/avatars/src/AvatarHashMap.h +++ b/libraries/avatars/src/AvatarHashMap.h @@ -33,6 +33,10 @@ public: const AvatarHash& getAvatarHash() { return _avatarHash; } int size() { return _avatarHash.size(); } +signals: + void avatarAddedEvent(const QUuid& sessionUUID); + void avatarRemovedEvent(const QUuid& sessionUUID); + public slots: bool isAvatarInRange(const glm::vec3 & position, const float range); From d34fc35544fa38715d1990f0e09f3201c1f68679 Mon Sep 17 00:00:00 2001 From: black plastick Date: Tue, 27 Oct 2015 08:11:20 -0400 Subject: [PATCH 07/35] added AvatarList.avatarSessionChanged(sessionUUID, oldUUID) for AC scripts. --- assignment-client/src/avatars/AvatarMixer.cpp | 6 +++--- libraries/avatars/src/AvatarHashMap.cpp | 1 + libraries/avatars/src/AvatarHashMap.h | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/assignment-client/src/avatars/AvatarMixer.cpp b/assignment-client/src/avatars/AvatarMixer.cpp index ae6a8247c1..0a455891f9 100644 --- a/assignment-client/src/avatars/AvatarMixer.cpp +++ b/assignment-client/src/avatars/AvatarMixer.cpp @@ -254,10 +254,10 @@ void AvatarMixer::broadcastAvatarData() { // potentially update the max full rate distance for this frame maxAvatarDistanceThisFrame = std::max(maxAvatarDistanceThisFrame, distanceToAvatar); - if (distanceToAvatar != 0.0f + if (distanceToAvatar != 0.0f && distribution(generator) > (nodeData->getFullRateDistance() / distanceToAvatar)) { - return; - } + return; + } AvatarDataSequenceNumber lastSeqToReceiver = nodeData->getLastBroadcastSequenceNumber(otherNode->getUUID()); AvatarDataSequenceNumber lastSeqFromSender = otherNodeData->getLastReceivedSequenceNumber(); diff --git a/libraries/avatars/src/AvatarHashMap.cpp b/libraries/avatars/src/AvatarHashMap.cpp index c02fe98b19..ddc9160041 100644 --- a/libraries/avatars/src/AvatarHashMap.cpp +++ b/libraries/avatars/src/AvatarHashMap.cpp @@ -141,4 +141,5 @@ void AvatarHashMap::removeAvatar(const QUuid& sessionUUID) { void AvatarHashMap::sessionUUIDChanged(const QUuid& sessionUUID, const QUuid& oldUUID) { _lastOwnerSessionUUID = oldUUID; + emit avatarSessionChangedEvent(sessionUUID, oldUUID); } diff --git a/libraries/avatars/src/AvatarHashMap.h b/libraries/avatars/src/AvatarHashMap.h index f90c61ec4d..c0c511bc3a 100644 --- a/libraries/avatars/src/AvatarHashMap.h +++ b/libraries/avatars/src/AvatarHashMap.h @@ -36,6 +36,7 @@ public: signals: void avatarAddedEvent(const QUuid& sessionUUID); void avatarRemovedEvent(const QUuid& sessionUUID); + void avatarSessionChangedEvent(const QUuid& sessionUUID,const QUuid& oldUUID); public slots: bool isAvatarInRange(const glm::vec3 & position, const float range); From 8a85468254d5cdbbc79a38528ed4e78cf25453eb Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 10:39:44 -0700 Subject: [PATCH 08/35] changing model, repositioning --- .../whiteboard/whiteboardEntityScript.js | 2 +- .../painting/whiteboard/whiteboardSpawner.js | 241 ++++++++---------- 2 files changed, 107 insertions(+), 136 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardEntityScript.js b/examples/painting/whiteboard/whiteboardEntityScript.js index f38073f389..2d34fc8c40 100644 --- a/examples/painting/whiteboard/whiteboardEntityScript.js +++ b/examples/painting/whiteboard/whiteboardEntityScript.js @@ -239,7 +239,7 @@ unload: function() { Overlays.deleteOverlay(this.laserPointer); - // this.eraseBoard(); + this.eraseBoard(); } }; diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index cbc26da670..0804b992c7 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -16,13 +16,16 @@ Script.include("../../libraries/utils.js"); var scriptURL = Script.resolvePath("whiteboardEntityScript.js"); +//var modelURL = "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx"; +var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); -center.y += 0.4; + +var whiteboardDimensions, colorIndicator, eraseAllText +var colorBoxes = []; var colors = [ - hexToRgb("#2F8E84"), hexToRgb("#66CCB3"), hexToRgb("#A43C37"), hexToRgb("#491849"), @@ -30,21 +33,13 @@ var colors = [ hexToRgb("#993369"), hexToRgb("#9B47C2") ]; - -//WHITEBOARD -var whiteboardDimensions = { - x: 2, - y: 1.5, - z: 0.08 -}; var whiteboard = Entities.addEntity({ type: "Model", - modelURL: "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx", + modelURL: modelURL, name: "whiteboard", position: center, rotation: rotation, script: scriptURL, - dimensions: whiteboardDimensions, color: { red: 255, green: 255, @@ -52,144 +47,119 @@ var whiteboard = Entities.addEntity({ } }); +Script.setTimeout(function() { + whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; + setUp(); +}, 500) -// COLOR INDICATOR BOX -var colorIndicatorDimensions = { - x: whiteboardDimensions.x, - y: 0.05, - z: 0.02 -}; -scriptURL = Script.resolvePath("colorIndicatorEntityScript.js"); -var colorIndicatorPosition = Vec3.sum(center, { - x: 0, - y: whiteboardDimensions.y / 2 + colorIndicatorDimensions.y / 2, - z: 0 -}); -var colorIndicatorBox = Entities.addEntity({ - type: "Box", - name: "Color Indicator", - color: colors[0], - rotation: rotation, - position: colorIndicatorPosition, - dimensions: colorIndicatorDimensions, - script: scriptURL, - userData: JSON.stringify({ - whiteboard: whiteboard - }) -}); -Entities.editEntity(whiteboard, { - userData: JSON.stringify({ - color: { - currentColor: colors[0] - }, - colorIndicator: colorIndicatorBox - }) -}); - -//COLOR BOXES -var direction = Quat.getRight(rotation); -var colorBoxPosition = Vec3.subtract(center, Vec3.multiply(direction, whiteboardDimensions.x / 2)); -var colorBoxes = []; -var colorSquareDimensions = { - x: (whiteboardDimensions.x / 2) / (colors.length - 1), - y: 0.1, - z: 0.05 -}; -colorBoxPosition.y += whiteboardDimensions.y / 2 + colorIndicatorDimensions.y + colorSquareDimensions.y / 2; -var spaceBetweenColorBoxes = Vec3.multiply(direction, colorSquareDimensions.x * 2); -var scriptURL = Script.resolvePath("colorSelectorEntityScript.js"); -for (var i = 0; i < colors.length; i++) { - var colorBox = Entities.addEntity({ +function setUp() { + // COLOR INDICATOR BOX + var colorIndicatorDimensions = { + x: whiteboardDimensions.x, + y: 0.05, + z: 0.02 + }; + scriptURL = Script.resolvePath("colorIndicatorEntityScript.js"); + var colorIndicatorPosition = Vec3.sum(center, { + x: 0, + y: whiteboardDimensions.y / 2 + colorIndicatorDimensions.y / 2, + z: 0 + }); + colorIndicatorBox = Entities.addEntity({ type: "Box", - name: "Color Selector", - position: colorBoxPosition, - dimensions: colorSquareDimensions, + name: "Color Indicator", + color: colors[0], rotation: rotation, - color: colors[i], + position: colorIndicatorPosition, + dimensions: colorIndicatorDimensions, script: scriptURL, userData: JSON.stringify({ - whiteboard: whiteboard, + whiteboard: whiteboard + }) + }); + + Entities.editEntity(whiteboard, { + userData: JSON.stringify({ + color: { + currentColor: colors[0] + }, colorIndicator: colorIndicatorBox }) }); - colorBoxes.push(colorBox); - colorBoxPosition = Vec3.sum(colorBoxPosition, spaceBetweenColorBoxes); + + //COLOR BOXES + var direction = Quat.getRight(rotation); + var colorBoxPosition = Vec3.subtract(center, Vec3.multiply(direction, whiteboardDimensions.x / 2)); + var colorSquareDimensions = { + x: 0.1, + y: 0.1, + z: 0.002 + }; + colorBoxPosition.y += whiteboardDimensions.y / 2 + colorIndicatorDimensions.y + colorSquareDimensions.y / 2; + var spaceBetweenColorBoxes = Vec3.multiply(direction, colorSquareDimensions.x * 2); + var scriptURL = Script.resolvePath("colorSelectorEntityScript.js"); + for (var i = 0; i < colors.length; i++) { + var colorBox = Entities.addEntity({ + type: "Box", + name: "Color Selector", + position: colorBoxPosition, + dimensions: colorSquareDimensions, + rotation: rotation, + color: colors[i], + script: scriptURL, + userData: JSON.stringify({ + whiteboard: whiteboard, + colorIndicator: colorIndicatorBox + }) + }); + colorBoxes.push(colorBox); + colorBoxPosition = Vec3.sum(colorBoxPosition, spaceBetweenColorBoxes); + } + + + + var eraseBoxDimensions = { + x: 0.5, + y: 0.1, + z: 0.01 + }; + + + var eraseBoxPosition = Vec3.sum(center, Vec3.multiply(direction, whiteboardDimensions.x / 2 + eraseBoxDimensions.x / 2 + 0.01)); + eraseBoxPosition.y += 0.3; + scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); + eraseAllText = Entities.addEntity({ + type: "Text", + position: eraseBoxPosition, + name: "Eraser", + script: scriptURL, + rotation: rotation, + dimensions: eraseBoxDimensions, + backgroundColor: { + red: 0, + green: 60, + blue: 0 + }, + textColor: { + red: 255, + green: 10, + blue: 10 + }, + text: "ERASE BOARD", + lineHeight: 0.07, + userData: JSON.stringify({ + whiteboard: whiteboard + }) + }); + + + } - -// BLACK BOX -var blackBoxDimensions = { - x: 0.3, - y: 0.3, - z: 0.01 -}; - -colorBoxPosition = Vec3.subtract(center, Vec3.multiply(direction, whiteboardDimensions.x / 2 + blackBoxDimensions.x / 2 - 0.01)); -colorBoxPosition.y += 0.3; -var fragShaderURL = Script.resolvePath('blackInk.fs?v1' + Math.random()); -var blackBox = Entities.addEntity({ - type: 'Box', - name: "Black Color", - position: colorBoxPosition, - dimensions: blackBoxDimensions, - rotation: rotation, - color: { - red: 0, - green: 0, - blue: 0 - }, - script: scriptURL, - userData: JSON.stringify({ - whiteboard: whiteboard, - version: 2, - ProceduralEntity: { - shaderUrl: fragShaderURL - } - }) -}); - - -var eraseBoxDimensions = { - x: 0.5, - y: 0.1, - z: 0.01 -}; - - -var eraseBoxPosition = Vec3.sum(center, Vec3.multiply(direction, whiteboardDimensions.x / 2 + eraseBoxDimensions.x / 2 + 0.01)); -eraseBoxPosition.y += 0.3; -scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); -var eraseAllText = Entities.addEntity({ - type: "Text", - position: eraseBoxPosition, - name: "Eraser", - script: scriptURL, - rotation: rotation, - dimensions: eraseBoxDimensions, - backgroundColor: { - red: 0, - green: 60, - blue: 0 - }, - textColor: { - red: 255, - green: 10, - blue: 10 - }, - text: "ERASE BOARD", - lineHeight: 0.07, - userData: JSON.stringify({ - whiteboard: whiteboard - }) -}); - - - function cleanup() { Entities.deleteEntity(whiteboard); Entities.deleteEntity(eraseAllText); - Entities.deleteEntity(blackBox); Entities.deleteEntity(colorIndicatorBox); colorBoxes.forEach(function(colorBox) { Entities.deleteEntity(colorBox); @@ -197,5 +167,6 @@ function cleanup() { } + // Uncomment this line to delete whiteboard and all associated entity on script close Script.scriptEnding.connect(cleanup); \ No newline at end of file From fa22249f7a25a822791a20ae186829ef11630713 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 12:03:30 -0700 Subject: [PATCH 09/35] Improving model --- .../painting/whiteboard/whiteboardSpawner.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 0804b992c7..2de9821810 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -31,7 +31,7 @@ var colors = [ hexToRgb("#491849"), hexToRgb("#6AB03B"), hexToRgb("#993369"), - hexToRgb("#9B47C2") + hexToRgb("#000000") ]; var whiteboard = Entities.addEntity({ type: "Model", @@ -92,12 +92,19 @@ function setUp() { var direction = Quat.getRight(rotation); var colorBoxPosition = Vec3.subtract(center, Vec3.multiply(direction, whiteboardDimensions.x / 2)); var colorSquareDimensions = { - x: 0.1, - y: 0.1, + x: 0.13, + y: 0.13, z: 0.002 }; - colorBoxPosition.y += whiteboardDimensions.y / 2 + colorIndicatorDimensions.y + colorSquareDimensions.y / 2; - var spaceBetweenColorBoxes = Vec3.multiply(direction, colorSquareDimensions.x * 2); + + var palleteDepthOffset = -0.06; + var palleteHeightOffset = -0.28; + + colorBoxPosition = Vec3.sum(colorBoxPosition, Vec3.multiply(palleteDepthOffset, Quat.getFront(rotation))); + colorBoxPosition.y += palleteHeightOffset; + var spaceBetweenColorBoxes = Vec3.multiply(direction, colorSquareDimensions.x * 1.76); + var palleteXOffset = Vec3.multiply(direction, 0.43); + colorBoxPosition = Vec3.sum(colorBoxPosition, palleteXOffset); var scriptURL = Script.resolvePath("colorSelectorEntityScript.js"); for (var i = 0; i < colors.length; i++) { var colorBox = Entities.addEntity({ From f0a1335957aa0a80d61ecca1ddf98f03c4b03c4d Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 12:09:07 -0700 Subject: [PATCH 10/35] fixed paint initially showing up white --- examples/painting/whiteboard/whiteboardSpawner.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 2de9821810..6a4f800441 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -44,7 +44,12 @@ var whiteboard = Entities.addEntity({ red: 255, green: 255, blue: 255 - } + }, + userData: JSON.stringify({ + color: { + currentColor: colors[0] + } + }) }); Script.setTimeout(function() { From 8fcc07102699807e6eee5392c6765df9076e3ade Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 12:24:29 -0700 Subject: [PATCH 11/35] only can draw on whiteboard --- .../painting/whiteboard/whiteboardSpawner.js | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 6a4f800441..366aa2b73d 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -33,24 +33,32 @@ var colors = [ hexToRgb("#993369"), hexToRgb("#000000") ]; + var whiteboard = Entities.addEntity({ type: "Model", modelURL: modelURL, - name: "whiteboard", + name: "whiteboard base", position: center, rotation: rotation, +}); + +var surfaceCenter = Vec3.sum(center, Vec3.multiply(-0.1, Quat.getFront(rotation))); +surfaceCenter.y += 0.55; +var drawingSurface = Entities.addEntity({ + type: "Box", + color: {red: 255, green: 255, blue: 255}, + name: "whiteboard surface", + position: surfaceCenter, + dimensions: {x: 1.8, y: 1.4, z: 0.01}, script: scriptURL, - color: { - red: 255, - green: 255, - blue: 255 - }, - userData: JSON.stringify({ + rotation: rotation, + userData: JSON.stringify({ color: { currentColor: colors[0] } }) -}); + +}) Script.setTimeout(function() { whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; @@ -80,11 +88,11 @@ function setUp() { dimensions: colorIndicatorDimensions, script: scriptURL, userData: JSON.stringify({ - whiteboard: whiteboard + whiteboard: drawingSurface }) }); - Entities.editEntity(whiteboard, { + Entities.editEntity(drawingSurface, { userData: JSON.stringify({ color: { currentColor: colors[0] @@ -121,7 +129,7 @@ function setUp() { color: colors[i], script: scriptURL, userData: JSON.stringify({ - whiteboard: whiteboard, + whiteboard: drawingSurface, colorIndicator: colorIndicatorBox }) }); @@ -161,7 +169,7 @@ function setUp() { text: "ERASE BOARD", lineHeight: 0.07, userData: JSON.stringify({ - whiteboard: whiteboard + whiteboard: drawingSurface }) }); @@ -171,6 +179,7 @@ function setUp() { function cleanup() { Entities.deleteEntity(whiteboard); + Entities.deleteEntity(drawingSurface); Entities.deleteEntity(eraseAllText); Entities.deleteEntity(colorIndicatorBox); colorBoxes.forEach(function(colorBox) { From 25ded2bd9212f5c26a5285c64a79e8195fd18e16 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 13:03:44 -0700 Subject: [PATCH 12/35] added shapetype --- examples/painting/whiteboard/whiteboardSpawner.js | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 366aa2b73d..a7cfc0719a 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -36,6 +36,7 @@ var colors = [ var whiteboard = Entities.addEntity({ type: "Model", + shapeType: "box", modelURL: modelURL, name: "whiteboard base", position: center, From 0512da0e3971c21c57cb5c8e1233e3ba801004b2 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 13:07:11 -0700 Subject: [PATCH 13/35] updated url --- examples/painting/whiteboard/whiteboardSpawner.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index a7cfc0719a..16953129af 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -17,7 +17,8 @@ Script.include("../../libraries/utils.js"); var scriptURL = Script.resolvePath("whiteboardEntityScript.js"); //var modelURL = "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx"; -var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); +//var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); +var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); From ed6a866ff9dddc7b6cf1aee39bdaf9b3f52e1eda Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 13:43:13 -0700 Subject: [PATCH 14/35] ready for demo --- examples/painting/whiteboard/whiteboardEntityScript.js | 2 +- examples/painting/whiteboard/whiteboardSpawner.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardEntityScript.js b/examples/painting/whiteboard/whiteboardEntityScript.js index 2d34fc8c40..f38073f389 100644 --- a/examples/painting/whiteboard/whiteboardEntityScript.js +++ b/examples/painting/whiteboard/whiteboardEntityScript.js @@ -239,7 +239,7 @@ unload: function() { Overlays.deleteOverlay(this.laserPointer); - this.eraseBoard(); + // this.eraseBoard(); } }; diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 16953129af..82d7f63239 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -17,7 +17,7 @@ Script.include("../../libraries/utils.js"); var scriptURL = Script.resolvePath("whiteboardEntityScript.js"); //var modelURL = "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx"; -//var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); +// var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); @@ -192,4 +192,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -Script.scriptEnding.connect(cleanup); \ No newline at end of file +// Script.scriptEnding.connect(cleanup); \ No newline at end of file From d67f4958cdab2d60d2b7e08654e5b62667fbcb20 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 13:44:29 -0700 Subject: [PATCH 15/35] wait longer for dimensions --- examples/painting/whiteboard/whiteboardSpawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 82d7f63239..4303803266 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -65,7 +65,7 @@ var drawingSurface = Entities.addEntity({ Script.setTimeout(function() { whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; setUp(); -}, 500) +}, 1000) function setUp() { From 2f835b19fa1c181513412293964ebce6c1f5f30f Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 27 Oct 2015 17:17:19 -0700 Subject: [PATCH 16/35] surface is now a model --- .../painting/whiteboard/whiteboardSpawner.js | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 4303803266..037be61459 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -17,13 +17,17 @@ Script.include("../../libraries/utils.js"); var scriptURL = Script.resolvePath("whiteboardEntityScript.js"); //var modelURL = "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx"; -// var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); -var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; +var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); +// var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; + +var colorIndicatorBorderModelURL = "http://localhost:8080/colorIndicatorBorder.fbx?v1" + Math.random(); + +var surfaceModelURL = "http://localhost:8080/boardSurface.fbx?v1" + Math.random(); var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); -var whiteboardDimensions, colorIndicator, eraseAllText +var whiteboardDimensions, colorIndicatorBoxDimensions, colorIndicatorBox, eraseAllText var colorBoxes = []; var colors = [ @@ -44,14 +48,26 @@ var whiteboard = Entities.addEntity({ rotation: rotation, }); +var colorIndicatorPosition = {x: center.x, y: center.y, z: center.z}; +colorIndicatorPosition.y += 1.55; +colorIndicatorPosition = Vec3.sum(colorIndicatorPosition, Vec3.multiply(-0.1, Quat.getFront(rotation))); +var colorIndicatorBorder = Entities.addEntity({ + type: "Model", + position: colorIndicatorPosition, + modelURL: colorIndicatorBorderModelURL, + rotation: rotation, + shapeType: "box" +}); + var surfaceCenter = Vec3.sum(center, Vec3.multiply(-0.1, Quat.getFront(rotation))); -surfaceCenter.y += 0.55; +surfaceCenter.y += 0.6; var drawingSurface = Entities.addEntity({ - type: "Box", - color: {red: 255, green: 255, blue: 255}, + type: "Model", + modelURL: surfaceModelURL, + shapeType: "box", name: "whiteboard surface", position: surfaceCenter, - dimensions: {x: 1.8, y: 1.4, z: 0.01}, + // dimensions: {x: 1.7, y: 1.3, z: 0.01}, script: scriptURL, rotation: rotation, userData: JSON.stringify({ @@ -64,15 +80,18 @@ var drawingSurface = Entities.addEntity({ Script.setTimeout(function() { whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; + colorIndicatorDimensions = Entities.getEntityProperties(colorIndicatorBorder, "naturalDimensions").naturalDimensions; setUp(); }, 1000) function setUp() { // COLOR INDICATOR BOX + + var colorIndicatorDimensions = { x: whiteboardDimensions.x, - y: 0.05, + y: 0.5, z: 0.02 }; scriptURL = Script.resolvePath("colorIndicatorEntityScript.js"); @@ -112,7 +131,7 @@ function setUp() { z: 0.002 }; - var palleteDepthOffset = -0.06; + var palleteDepthOffset = -0.07; var palleteHeightOffset = -0.28; colorBoxPosition = Vec3.sum(colorBoxPosition, Vec3.multiply(palleteDepthOffset, Quat.getFront(rotation))); @@ -139,8 +158,6 @@ function setUp() { colorBoxPosition = Vec3.sum(colorBoxPosition, spaceBetweenColorBoxes); } - - var eraseBoxDimensions = { x: 0.5, y: 0.1, @@ -182,6 +199,7 @@ function setUp() { function cleanup() { Entities.deleteEntity(whiteboard); Entities.deleteEntity(drawingSurface); + Entities.deleteEntity(colorIndicatorBorder); Entities.deleteEntity(eraseAllText); Entities.deleteEntity(colorIndicatorBox); colorBoxes.forEach(function(colorBox) { @@ -192,4 +210,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -// Script.scriptEnding.connect(cleanup); \ No newline at end of file +Script.scriptEnding.connect(cleanup); \ No newline at end of file From 06897aa474535c49453fb66db832f256eef49a6a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 21 Oct 2015 11:53:38 -0700 Subject: [PATCH 17/35] don't force parenting of AssetRequest/AssetUpload --- libraries/networking/src/AssetClient.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/networking/src/AssetClient.cpp b/libraries/networking/src/AssetClient.cpp index b7f1205847..6a1b46340c 100644 --- a/libraries/networking/src/AssetClient.cpp +++ b/libraries/networking/src/AssetClient.cpp @@ -89,7 +89,6 @@ AssetRequest* AssetClient::createRequest(const QString& hash, const QString& ext // Move to the AssetClient thread in case we are not currently on that thread (which will usually be the case) request->moveToThread(thread()); - request->setParent(this); return request; } else { @@ -105,7 +104,6 @@ AssetUpload* AssetClient::createUpload(const QString& filename) { auto upload = new AssetUpload(filename); upload->moveToThread(thread()); - upload->setParent(this); return upload; } else { @@ -118,7 +116,6 @@ AssetUpload* AssetClient::createUpload(const QByteArray& data, const QString& ex auto upload = new AssetUpload(data, extension); upload->moveToThread(thread()); - upload->setParent(this); return upload; } else { From 40277823354d9ecd526628d799982356959d7e00 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 28 Oct 2015 13:18:57 -0700 Subject: [PATCH 18/35] Fixed issue with overlays disapearing on release grab --- .../whiteboard/whiteboardEntityScript.js | 4 + .../painting/whiteboard/whiteboardSpawner.js | 88 ++++++++----------- 2 files changed, 40 insertions(+), 52 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardEntityScript.js b/examples/painting/whiteboard/whiteboardEntityScript.js index f38073f389..fffaa78a8c 100644 --- a/examples/painting/whiteboard/whiteboardEntityScript.js +++ b/examples/painting/whiteboard/whiteboardEntityScript.js @@ -47,6 +47,7 @@ if (this.painting) { return; } + this.whichHand = this.hand; if (this.hand === RIGHT_HAND) { this.getHandPosition = MyAvatar.getRightPalmPosition; this.getHandRotation = MyAvatar.getRightPalmRotation; @@ -183,6 +184,9 @@ }, releaseGrab: function() { + if(this.hand !== this.whichHand) { + return; + } this.stopPainting(); }, diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 037be61459..c40a763bf6 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -22,12 +22,14 @@ var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); var colorIndicatorBorderModelURL = "http://localhost:8080/colorIndicatorBorder.fbx?v1" + Math.random(); +var eraseModelURL = "http://localhost:8080/eraser.fbx?v1" + Math.random(); + var surfaceModelURL = "http://localhost:8080/boardSurface.fbx?v1" + Math.random(); var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); -var whiteboardDimensions, colorIndicatorBoxDimensions, colorIndicatorBox, eraseAllText +var whiteboardDimensions, colorIndicatorBoxDimensions, colorIndicatorBox, eraser; var colorBoxes = []; var colors = [ @@ -48,7 +50,11 @@ var whiteboard = Entities.addEntity({ rotation: rotation, }); -var colorIndicatorPosition = {x: center.x, y: center.y, z: center.z}; +var colorIndicatorPosition = { + x: center.x, + y: center.y, + z: center.z +}; colorIndicatorPosition.y += 1.55; colorIndicatorPosition = Vec3.sum(colorIndicatorPosition, Vec3.multiply(-0.1, Quat.getFront(rotation))); var colorIndicatorBorder = Entities.addEntity({ @@ -60,27 +66,40 @@ var colorIndicatorBorder = Entities.addEntity({ }); var surfaceCenter = Vec3.sum(center, Vec3.multiply(-0.1, Quat.getFront(rotation))); -surfaceCenter.y += 0.6; +surfaceCenter.y += 0.6; var drawingSurface = Entities.addEntity({ type: "Model", modelURL: surfaceModelURL, shapeType: "box", name: "whiteboard surface", position: surfaceCenter, - // dimensions: {x: 1.7, y: 1.3, z: 0.01}, script: scriptURL, - rotation: rotation, - userData: JSON.stringify({ + rotation: rotation, + userData: JSON.stringify({ color: { currentColor: colors[0] } }) -}) +}); + +var eraseModelPosition = Vec3.sum(center, {x: 0, y: 2, z: 0 }); +scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); +var eraser = Entities.addEntity({ + type: "Model", + modelURL: eraseModelURL, + position: eraseModelPosition, + name: "Eraser", + script: scriptURL, + rotation: rotation, + userData: JSON.stringify({ + whiteboard: drawingSurface + }) +}); Script.setTimeout(function() { whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; - colorIndicatorDimensions = Entities.getEntityProperties(colorIndicatorBorder, "naturalDimensions").naturalDimensions; + colorIndicatorBorderDimensions = Entities.getEntityProperties(colorIndicatorBorder, "naturalDimensions").naturalDimensions; setUp(); }, 1000) @@ -89,24 +108,25 @@ function setUp() { // COLOR INDICATOR BOX - var colorIndicatorDimensions = { - x: whiteboardDimensions.x, - y: 0.5, - z: 0.02 - }; + var eraseModelDimensions = Entities.getEntityProperties(eraser, "naturalDimensions").naturalDimensions; + Entities.editEntity(eraser, {dimensions: eraseModelDimensions}); + Entities.editEntity(colorIndicatorBorder, {dimensions: colorIndicatorBorderDimensions}); + scriptURL = Script.resolvePath("colorIndicatorEntityScript.js"); var colorIndicatorPosition = Vec3.sum(center, { x: 0, - y: whiteboardDimensions.y / 2 + colorIndicatorDimensions.y / 2, + y: whiteboardDimensions.y / 2 + colorIndicatorBorderDimensions.y / 2, z: 0 }); + colorIndicatorPosition = Vec3.sum(colorIndicatorPosition, Vec3.multiply(-.1, Quat.getFront(rotation))); + var colorIndicatorBoxDimensions = Vec3.multiply(colorIndicatorBorderDimensions, 0.9); colorIndicatorBox = Entities.addEntity({ type: "Box", name: "Color Indicator", color: colors[0], rotation: rotation, position: colorIndicatorPosition, - dimensions: colorIndicatorDimensions, + dimensions: colorIndicatorBoxDimensions, script: scriptURL, userData: JSON.stringify({ whiteboard: drawingSurface @@ -158,49 +178,13 @@ function setUp() { colorBoxPosition = Vec3.sum(colorBoxPosition, spaceBetweenColorBoxes); } - var eraseBoxDimensions = { - x: 0.5, - y: 0.1, - z: 0.01 - }; - - - var eraseBoxPosition = Vec3.sum(center, Vec3.multiply(direction, whiteboardDimensions.x / 2 + eraseBoxDimensions.x / 2 + 0.01)); - eraseBoxPosition.y += 0.3; - scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); - eraseAllText = Entities.addEntity({ - type: "Text", - position: eraseBoxPosition, - name: "Eraser", - script: scriptURL, - rotation: rotation, - dimensions: eraseBoxDimensions, - backgroundColor: { - red: 0, - green: 60, - blue: 0 - }, - textColor: { - red: 255, - green: 10, - blue: 10 - }, - text: "ERASE BOARD", - lineHeight: 0.07, - userData: JSON.stringify({ - whiteboard: drawingSurface - }) - }); - - - } function cleanup() { Entities.deleteEntity(whiteboard); Entities.deleteEntity(drawingSurface); Entities.deleteEntity(colorIndicatorBorder); - Entities.deleteEntity(eraseAllText); + Entities.deleteEntity(eraser); Entities.deleteEntity(colorIndicatorBox); colorBoxes.forEach(function(colorBox) { Entities.deleteEntity(colorBox); From ac2effb33cce4e55c9c91e0b3263cd0177da4979 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 28 Oct 2015 13:32:10 -0700 Subject: [PATCH 19/35] Not erasing board, models hosted on s3 --- .../whiteboard/whiteboardEntityScript.js | 4 +--- .../painting/whiteboard/whiteboardSpawner.js | 21 +++++++------------ 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardEntityScript.js b/examples/painting/whiteboard/whiteboardEntityScript.js index fffaa78a8c..ce0e8a82b1 100644 --- a/examples/painting/whiteboard/whiteboardEntityScript.js +++ b/examples/painting/whiteboard/whiteboardEntityScript.js @@ -243,12 +243,10 @@ unload: function() { Overlays.deleteOverlay(this.laserPointer); - // this.eraseBoard(); + this.eraseBoard(); } - }; - // entity scripts always need to return a newly constructed object of our type return new Whiteboard(); }); \ No newline at end of file diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index c40a763bf6..573432b77a 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -16,15 +16,11 @@ Script.include("../../libraries/utils.js"); var scriptURL = Script.resolvePath("whiteboardEntityScript.js"); -//var modelURL = "https://hifi-public.s3.amazonaws.com/ozan/support/for_eric/whiteboard/whiteboard.fbx"; -var modelURL = "http://localhost:8080/whiteboard.fbx?v1" + Math.random(); -// var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; +var modelURL = "https://s3.amazonaws.com/hifi-public/eric/models/whiteboard.fbx"; -var colorIndicatorBorderModelURL = "http://localhost:8080/colorIndicatorBorder.fbx?v1" + Math.random(); - -var eraseModelURL = "http://localhost:8080/eraser.fbx?v1" + Math.random(); - -var surfaceModelURL = "http://localhost:8080/boardSurface.fbx?v1" + Math.random(); +var colorIndicatorBorderModelURL = "https://s3.amazonaws.com/hifi-public/eric/models/colorIndicatorBorder.fbx"; +var eraserModelURL = "https://s3.amazonaws.com/hifi-public/eric/models/eraser.fbx"; +var surfaceModelURL = "https://s3.amazonaws.com/hifi-public/eric/models/boardSurface.fbx"; var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); @@ -87,7 +83,7 @@ var eraseModelPosition = Vec3.sum(center, {x: 0, y: 2, z: 0 }); scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); var eraser = Entities.addEntity({ type: "Model", - modelURL: eraseModelURL, + modelURL: eraserModelURL, position: eraseModelPosition, name: "Eraser", script: scriptURL, @@ -101,13 +97,10 @@ Script.setTimeout(function() { whiteboardDimensions = Entities.getEntityProperties(whiteboard, "naturalDimensions").naturalDimensions; colorIndicatorBorderDimensions = Entities.getEntityProperties(colorIndicatorBorder, "naturalDimensions").naturalDimensions; setUp(); -}, 1000) +}, 2000) function setUp() { - // COLOR INDICATOR BOX - - var eraseModelDimensions = Entities.getEntityProperties(eraser, "naturalDimensions").naturalDimensions; Entities.editEntity(eraser, {dimensions: eraseModelDimensions}); Entities.editEntity(colorIndicatorBorder, {dimensions: colorIndicatorBorderDimensions}); @@ -194,4 +187,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -Script.scriptEnding.connect(cleanup); \ No newline at end of file +// Script.scriptEnding.connect(cleanup); \ No newline at end of file From e9bdb872bdd8d3c78a6c0f6e3f28b2b407c64bc7 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 28 Oct 2015 15:25:11 -0700 Subject: [PATCH 20/35] added light to whiteboard --- examples/painting/whiteboard/whiteboardSpawner.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 573432b77a..b08c9bd624 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -79,6 +79,14 @@ var drawingSurface = Entities.addEntity({ }); +var lightPosition = Vec3.sum(center, Vec3.multiply(-2, Quat.getFront(rotation))); +var light = Entities.addEntity({ + type: 'Light', + position: lightPosition, + dimensions: {x: 5, y: 5, z: 5}, + color: {red: 255, green: 255, blue: 255} +}); + var eraseModelPosition = Vec3.sum(center, {x: 0, y: 2, z: 0 }); scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); var eraser = Entities.addEntity({ @@ -179,6 +187,7 @@ function cleanup() { Entities.deleteEntity(colorIndicatorBorder); Entities.deleteEntity(eraser); Entities.deleteEntity(colorIndicatorBox); + Entities.deleteEntity(light); colorBoxes.forEach(function(colorBox) { Entities.deleteEntity(colorBox); }); From 1b1490bb75403a6f31040d234035c5af67092c76 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 28 Oct 2015 16:02:15 -0700 Subject: [PATCH 21/35] added blocker so user cant get so close to whiteboard --- .../painting/whiteboard/whiteboardSpawner.js | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index b08c9bd624..29183dcc76 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -25,7 +25,7 @@ var rotation = Quat.safeEulerAngles(Camera.getOrientation()); rotation = Quat.fromPitchYawRollDegrees(0, rotation.y, 0); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(rotation))); -var whiteboardDimensions, colorIndicatorBoxDimensions, colorIndicatorBox, eraser; +var whiteboardDimensions, colorIndicatorBoxDimensions, colorIndicatorBox, eraser, blocker; var colorBoxes = []; var colors = [ @@ -87,12 +87,13 @@ var light = Entities.addEntity({ color: {red: 255, green: 255, blue: 255} }); -var eraseModelPosition = Vec3.sum(center, {x: 0, y: 2, z: 0 }); +var eraserPosition = Vec3.sum(center, {x: 0, y: 2.05, z: 0 }); +eraserPosition = Vec3.sum(eraserPosition, Vec3.multiply(-0.1, rotation)); scriptURL = Script.resolvePath("eraseBoardEntityScript.js"); var eraser = Entities.addEntity({ type: "Model", modelURL: eraserModelURL, - position: eraseModelPosition, + position: eraserPosition, name: "Eraser", script: scriptURL, rotation: rotation, @@ -109,6 +110,17 @@ Script.setTimeout(function() { function setUp() { + var blockerPosition = Vec3.sum(center, {x: 0, y: -1, z: 0 }); + blockerPosition = Vec3.sum(blockerPosition, Vec3.multiply(-1, Quat.getFront(rotation))); + blocker = Entities.addEntity({ + type: "Box", + rotation: rotation, + position: blockerPosition, + dimensions: {x: whiteboardDimensions.x, y: 1, z: 0.1}, + shapeType: "box", + visible: false + }); + var eraseModelDimensions = Entities.getEntityProperties(eraser, "naturalDimensions").naturalDimensions; Entities.editEntity(eraser, {dimensions: eraseModelDimensions}); Entities.editEntity(colorIndicatorBorder, {dimensions: colorIndicatorBorderDimensions}); @@ -187,6 +199,7 @@ function cleanup() { Entities.deleteEntity(colorIndicatorBorder); Entities.deleteEntity(eraser); Entities.deleteEntity(colorIndicatorBox); + Entities.deleteEntity(blocker); Entities.deleteEntity(light); colorBoxes.forEach(function(colorBox) { Entities.deleteEntity(colorBox); @@ -196,4 +209,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -// Script.scriptEnding.connect(cleanup); \ No newline at end of file +Script.scriptEnding.connect(cleanup); \ No newline at end of file From 020566b76a996c931aece545e98782124f329397 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 29 Oct 2015 14:14:15 -0700 Subject: [PATCH 22/35] no deleting --- examples/painting/whiteboard/whiteboardEntityScript.js | 2 +- examples/painting/whiteboard/whiteboardSpawner.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardEntityScript.js b/examples/painting/whiteboard/whiteboardEntityScript.js index ce0e8a82b1..c10a8c23fe 100644 --- a/examples/painting/whiteboard/whiteboardEntityScript.js +++ b/examples/painting/whiteboard/whiteboardEntityScript.js @@ -243,7 +243,7 @@ unload: function() { Overlays.deleteOverlay(this.laserPointer); - this.eraseBoard(); + // this.eraseBoard(); } }; diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 29183dcc76..b2156dc898 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -209,4 +209,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -Script.scriptEnding.connect(cleanup); \ No newline at end of file +// Script.scriptEnding.connect(cleanup); \ No newline at end of file From 047614b2eb70fc7e60faef1486de3b515078344b Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Thu, 29 Oct 2015 16:18:15 -0700 Subject: [PATCH 23/35] New left and right grab animations. --- .../defaultAvatar_full/avatar-animation.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/interface/resources/meshes/defaultAvatar_full/avatar-animation.json b/interface/resources/meshes/defaultAvatar_full/avatar-animation.json index 682e0be1bf..515f6e96fe 100644 --- a/interface/resources/meshes/defaultAvatar_full/avatar-animation.json +++ b/interface/resources/meshes/defaultAvatar_full/avatar-animation.json @@ -197,7 +197,7 @@ "id": "rightHandOpen", "type": "clip", "data": { - "url": "http://hifi-public.s3.amazonaws.com/ozan/anim/hand_anims/point_right_hand.fbx", + "url": "https://hifi-public.s3.amazonaws.com/ozan/anim/grab/grab_right.fbx", "startFrame": 0.0, "endFrame": 0.0, "timeScale": 1.0, @@ -209,9 +209,9 @@ "id": "rightHandClose", "type": "clip", "data": { - "url": "http://hifi-public.s3.amazonaws.com/ozan/anim/squeeze_hands/right_hand_anim.fbx", - "startFrame": 15.0, - "endFrame": 15.0, + "url": "https://hifi-public.s3.amazonaws.com/ozan/anim/grab/grab_right.fbx", + "startFrame": 10.0, + "endFrame": 10.0, "timeScale": 1.0, "loopFlag": true }, @@ -346,7 +346,7 @@ "id": "leftHandOpen", "type": "clip", "data": { - "url": "http://hifi-public.s3.amazonaws.com/ozan/anim/hand_anims/point_left_hand.fbx", + "url": "https://hifi-public.s3.amazonaws.com/ozan/anim/grab/grab_left.fbx", "startFrame": 0.0, "endFrame": 0.0, "timeScale": 1.0, @@ -358,9 +358,9 @@ "id": "leftHandClose", "type": "clip", "data": { - "url": "http://hifi-public.s3.amazonaws.com/ozan/anim/squeeze_hands/left_hand_anim.fbx", - "startFrame": 15.0, - "endFrame": 15.0, + "url": "https://hifi-public.s3.amazonaws.com/ozan/anim/grab/grab_left.fbx", + "startFrame": 10.0, + "endFrame": 10.0, "timeScale": 1.0, "loopFlag": true }, From b6b71e7e7693aa92962ff080f562909e82238dec Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 29 Oct 2015 16:40:50 -0700 Subject: [PATCH 24/35] increased range and intensity of light --- examples/painting/whiteboard/whiteboardSpawner.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index b2156dc898..fa5040ed4b 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -79,11 +79,13 @@ var drawingSurface = Entities.addEntity({ }); -var lightPosition = Vec3.sum(center, Vec3.multiply(-2, Quat.getFront(rotation))); +var lightPosition = Vec3.sum(center, Vec3.multiply(-4, Quat.getFront(rotation))); var light = Entities.addEntity({ type: 'Light', + name: 'whiteboard light', position: lightPosition, - dimensions: {x: 5, y: 5, z: 5}, + dimensions: {x: 10, y: 10, z: 10}, + intensity: 2, color: {red: 255, green: 255, blue: 255} }); From 8b46ed7dff4e54bf9817bf0ee3df69ba62997bb2 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 29 Oct 2015 16:42:48 -0700 Subject: [PATCH 25/35] Check keyLight is defined --- examples/edit.js | 4 ++-- examples/html/entityProperties.html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/edit.js b/examples/edit.js index 447455e999..7a16030afc 100644 --- a/examples/edit.js +++ b/examples/edit.js @@ -1510,7 +1510,7 @@ PropertiesTool = function(opts) { if (entity.properties.rotation !== undefined) { entity.properties.rotation = Quat.safeEulerAngles(entity.properties.rotation); } - if (entity.properties.type === "Zone" && entity.properties.keyLight.direction !== undefined) { + if (entity.properties.keyLight !== undefined && entity.properties.keyLight.direction !== undefined) { entity.properties.keyLight.direction = Vec3.multiply(RADIANS_TO_DEGREES, Vec3.toPolar(entity.properties.keyLight.direction)); entity.properties.keyLight.direction.z = 0.0; } @@ -1541,7 +1541,7 @@ PropertiesTool = function(opts) { var rotation = data.properties.rotation; data.properties.rotation = Quat.fromPitchYawRollDegrees(rotation.x, rotation.y, rotation.z); } - if (entity.properties.type === "Zone" && data.properties.keyLight.direction !== undefined) { + if (data.properties.keyLight !== undefined && data.properties.keyLight.direction !== undefined) { data.properties.keyLight.direction = Vec3.fromPolar( data.properties.keyLight.direction.x * DEGREES_TO_RADIANS, data.properties.keyLight.direction.y * DEGREES_TO_RADIANS); } diff --git a/examples/html/entityProperties.html b/examples/html/entityProperties.html index fe40311b5f..412b413b2b 100644 --- a/examples/html/entityProperties.html +++ b/examples/html/entityProperties.html @@ -830,7 +830,7 @@ elZoneKeyLightColorRed.addEventListener('change', zoneKeyLightColorChangeFunction); elZoneKeyLightColorGreen.addEventListener('change', zoneKeyLightColorChangeFunction); elZoneKeyLightColorBlue.addEventListener('change', zoneKeyLightColorChangeFunction); - elZoneKeyLightIntensity.addEventListener('change', createEmitGroupNumberPropertyUpdateFunction('intensity','keyLight')); + elZoneKeyLightIntensity.addEventListener('change', createEmitGroupNumberPropertyUpdateFunction('keyLight','intensity')); elZoneKeyLightAmbientIntensity.addEventListener('change', createEmitGroupNumberPropertyUpdateFunction('keyLight','ambientIntensity')); var zoneKeyLightDirectionChangeFunction = createEmitGroupVec3PropertyUpdateFunction('keyLight','direction', elZoneKeyLightDirectionX, elZoneKeyLightDirectionY, elZoneKeyLightDirectionZ); elZoneKeyLightDirectionX.addEventListener('change', zoneKeyLightDirectionChangeFunction); From 6bdf66534f323d1710878bcc5f6d48f32e9a1992 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 29 Oct 2015 16:53:03 -0700 Subject: [PATCH 26/35] light tweak --- examples/painting/whiteboard/whiteboardSpawner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index fa5040ed4b..701112e1a8 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -79,7 +79,7 @@ var drawingSurface = Entities.addEntity({ }); -var lightPosition = Vec3.sum(center, Vec3.multiply(-4, Quat.getFront(rotation))); +var lightPosition = Vec3.sum(center, Vec3.multiply(-3, Quat.getFront(rotation))); var light = Entities.addEntity({ type: 'Light', name: 'whiteboard light', From 35e03d662d392af02b95969138b3d5cc84cdba37 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Fri, 30 Oct 2015 00:58:32 +0100 Subject: [PATCH 27/35] fix the constants of Vec3 , const glm::vec3& UNIT_***() weren't accessible in javascript. Solved it by making them properties that are CONSTANT. Also added function to multiply a vec3 by a vec3 called multiplyVbyV since overloading it over multiply gave me some problems --- libraries/script-engine/src/Vec3.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index b05e729a49..17a5afd09a 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -23,6 +23,25 @@ /// Scriptable interface a Vec3ernion helper class object. Used exclusively in the JavaScript API class Vec3 : public QObject { Q_OBJECT + Q_PROPERTY(glm::vec3 UNIT_X READ UNIT_X CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_Y READ UNIT_Y CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_Z READ UNIT_Z CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_NEG_X READ UNIT_NEG_X CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_NEG_Y READ UNIT_NEG_Y CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_NEG_Z READ UNIT_NEG_Z CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_XY READ UNIT_XY CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_XZ READ UNIT_XZ CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_YZ READ UNIT_YZ CONSTANT) + Q_PROPERTY(glm::vec3 UNIT_XYZ READ UNIT_XYZ CONSTANT) + Q_PROPERTY(glm::vec3 FLOAT_MAX READ FLOAT_MAX CONSTANT) + Q_PROPERTY(glm::vec3 FLOAT_MIN READ FLOAT_MIN CONSTANT) + Q_PROPERTY(glm::vec3 ZERO READ ZERO CONSTANT) + Q_PROPERTY(glm::vec3 ONE READ ONE CONSTANT) + Q_PROPERTY(glm::vec3 TWO READ TWO CONSTANT) + Q_PROPERTY(glm::vec3 HALF READ HALF CONSTANT) + Q_PROPERTY(glm::vec3 RIGHT READ RIGHT CONSTANT) + Q_PROPERTY(glm::vec3 UP READ UP CONSTANT) + Q_PROPERTY(glm::vec3 FRONT READ FRONT CONSTANT) public slots: glm::vec3 reflect(const glm::vec3& v1, const glm::vec3& v2) { return glm::reflect(v1, v2); } @@ -30,6 +49,7 @@ public slots: float dot(const glm::vec3& v1, const glm::vec3& v2) { return glm::dot(v1, v2); } glm::vec3 multiply(const glm::vec3& v1, float f) { return v1 * f; } glm::vec3 multiply(float f, const glm::vec3& v1) { return v1 * f; } + glm::vec3 multiplyVbyV(const glm::vec3& v1, const glm::vec3& v2) { return v1 * v2; } glm::vec3 multiplyQbyV(const glm::quat& q, const glm::vec3& v) { return q * v; } glm::vec3 sum(const glm::vec3& v1, const glm::vec3& v2) { return v1 + v2; } glm::vec3 subtract(const glm::vec3& v1, const glm::vec3& v2) { return v1 - v2; } @@ -45,6 +65,8 @@ public slots: glm::vec3 toPolar(const glm::vec3& v); glm::vec3 fromPolar(const glm::vec3& polar); glm::vec3 fromPolar(float elevation, float azimuth); + +private: const glm::vec3& UNIT_X() { return Vectors::UNIT_X; } const glm::vec3& UNIT_Y() { return Vectors::UNIT_Y; } const glm::vec3& UNIT_Z() { return Vectors::UNIT_Z; } From a149a2dd60bcf1abfeb85f80ee02dea3d220c635 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 30 Oct 2015 00:19:14 -0700 Subject: [PATCH 28/35] fix some warnings --- libraries/gl/src/gl/GlWindow.cpp | 3 +-- libraries/physics/src/CharacterController.cpp | 4 ++-- libraries/ui/src/VrMenu.cpp | 4 ++++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/gl/src/gl/GlWindow.cpp b/libraries/gl/src/gl/GlWindow.cpp index 7e43b29e73..40a5bedf7e 100644 --- a/libraries/gl/src/gl/GlWindow.cpp +++ b/libraries/gl/src/gl/GlWindow.cpp @@ -44,8 +44,7 @@ bool GlWindow::makeCurrent() { qDebug() << "GL Renderer: " << QString((const char*) glGetString(GL_RENDERER)); }); - QOpenGLContext * currentContext = QOpenGLContext::currentContext(); - Q_ASSERT(_context == currentContext); + Q_ASSERT(_context == QOpenGLContext::currentContext()); return makeCurrentResult; } diff --git a/libraries/physics/src/CharacterController.cpp b/libraries/physics/src/CharacterController.cpp index aadf9b16ea..8da9541387 100644 --- a/libraries/physics/src/CharacterController.cpp +++ b/libraries/physics/src/CharacterController.cpp @@ -14,11 +14,11 @@ #include "PhysicsCollisionGroups.h" bool CharacterController::needsRemoval() const { - return (bool)(_pendingFlags & PENDING_FLAG_REMOVE_FROM_SIMULATION); + return ((_pendingFlags & PENDING_FLAG_REMOVE_FROM_SIMULATION) == PENDING_FLAG_REMOVE_FROM_SIMULATION); } bool CharacterController::needsAddition() const { - return (bool)(_pendingFlags & PENDING_FLAG_ADD_TO_SIMULATION); + return ((_pendingFlags & PENDING_FLAG_ADD_TO_SIMULATION) == PENDING_FLAG_ADD_TO_SIMULATION); } void CharacterController::setDynamicsWorld(btDynamicsWorld* world) { diff --git a/libraries/ui/src/VrMenu.cpp b/libraries/ui/src/VrMenu.cpp index 57df4d5306..211e0e0f72 100644 --- a/libraries/ui/src/VrMenu.cpp +++ b/libraries/ui/src/VrMenu.cpp @@ -113,6 +113,7 @@ void VrMenu::addMenu(QMenu* menu) { Q_ARG(QVariant, QVariant::fromValue(qmlParent)), Q_ARG(QVariant, QVariant::fromValue(menu->title()))); Q_ASSERT(invokeResult); + Q_UNUSED(invokeResult); // FIXME - apparently we haven't upgraded the Qt on our unix Jenkins environments to 5.5.x QObject* result = returnedValue.value(); Q_ASSERT(result); @@ -153,6 +154,7 @@ void VrMenu::addAction(QMenu* menu, QAction* action) { Q_ARG(QVariant, QVariant::fromValue(menuQml)), Q_ARG(QVariant, QVariant::fromValue(action->text()))); Q_ASSERT(invokeResult); + Q_UNUSED(invokeResult); // FIXME - apparently we haven't upgraded the Qt on our unix Jenkins environments to 5.5.x QObject* result = returnedValue.value(); Q_ASSERT(result); // Bind the QML and Widget together @@ -174,6 +176,7 @@ void VrMenu::insertAction(QAction* before, QAction* action) { Q_ARG(QVariant, QVariant::fromValue(beforeQml)), Q_ARG(QVariant, QVariant::fromValue(action->text()))); Q_ASSERT(invokeResult); + Q_UNUSED(invokeResult); // FIXME - apparently we haven't upgraded the Qt on our unix Jenkins environments to 5.5.x QObject* result = returnedValue.value(); Q_ASSERT(result); bindActionToQmlAction(result, action); @@ -192,4 +195,5 @@ void VrMenu::removeAction(QAction* action) { Q_ARG(QVariant, QVariant::fromValue(menu)), Q_ARG(QVariant, QVariant::fromValue(item))); Q_ASSERT(invokeResult); + Q_UNUSED(invokeResult); // FIXME - apparently we haven't upgraded the Qt on our unix Jenkins environments to 5.5.x } From fcfab0fc513a96ae166318f613581cf083916e6e Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 29 Oct 2015 12:31:34 -0700 Subject: [PATCH 29/35] Moving SDL2, Sixense and Faceshift to external projects --- CMakeLists.txt | 25 ++++----- cmake/externals/faceshift/CMakeLists.txt | 30 +++++++++++ cmake/externals/sdl2/CMakeLists.txt | 30 ++++------- cmake/externals/sixense/CMakeLists.txt | 36 ++++--------- .../AddDependencyExternalProjects.cmake | 2 +- cmake/macros/TargetFaceshift.cmake | 14 +++++ cmake/macros/TargetSDL2.cmake | 14 +++++ cmake/macros/TargetSixense.cmake | 14 +++++ cmake/modules/FindFaceshift.cmake | 23 -------- cmake/modules/FindSixense.cmake | 45 +--------------- interface/CMakeLists.txt | 13 +++-- interface/external/faceshift/readme.txt | 26 --------- interface/external/sdl2/readme.txt | 13 ----- interface/external/sixense/readme.txt | 10 ---- libraries/input-plugins/CMakeLists.txt | 54 +------------------ 15 files changed, 119 insertions(+), 230 deletions(-) create mode 100644 cmake/externals/faceshift/CMakeLists.txt create mode 100644 cmake/macros/TargetFaceshift.cmake create mode 100644 cmake/macros/TargetSDL2.cmake create mode 100644 cmake/macros/TargetSixense.cmake delete mode 100644 interface/external/faceshift/readme.txt delete mode 100644 interface/external/sdl2/readme.txt delete mode 100644 interface/external/sixense/readme.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 79d8a0c0e0..d69b8c4c77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,21 +186,22 @@ set_property(DIRECTORY PROPERTY EP_PREFIX ${EXTERNAL_PROJECT_PREFIX}) setup_externals_binary_dir() # setup options to grab external project dependencies -option(GET_BULLET "Get Bullet library automatically as external project" 1) -option(GET_GLM "Get GLM library automatically as external project" 1) -option(GET_GVERB "Get Gverb library automatically as external project" 1) -option(GET_TBB "Get Threading Building Blocks library automatically as external project" 1) -option(GET_LIBOVR "Get LibOVR library automatically as external project" 1) -option(GET_VHACD "Get V-HACD library automatically as external project" 1) -option(GET_POLYVOX "Get polyvox library automatically as external project" 1) -option(GET_OPENVR "Get OpenVR library automatically as external project" 1) -option(GET_BOOSTCONFIG "Get Boost-config library automatically as external project" 1) -option(GET_OGLPLUS "Get OGLplus library automatically as external project" 1) -option(GET_GLEW "Get GLEW library automatically as external project" 1) +#option(GET_BULLET "Get Bullet library automatically as external project" 1) +#option(GET_GLM "Get GLM library automatically as external project" 1) +#option(GET_GVERB "Get Gverb library automatically as external project" 1) +#option(GET_TBB "Get Threading Building Blocks library automatically as external project" 1) +#option(GET_LIBOVR "Get LibOVR library automatically as external project" 1) +#option(GET_VHACD "Get V-HACD library automatically as external project" 1) +#option(GET_POLYVOX "Get polyvox library automatically as external project" 1) +#option(GET_OPENVR "Get OpenVR library automatically as external project" 1) +#option(GET_BOOSTCONFIG "Get Boost-config library automatically as external project" 1) +#option(GET_OGLPLUS "Get OGLplus library automatically as external project" 1) +#option(GET_GLEW "Get GLEW library automatically as external project" 1) +#option(GET_SDL2 "Get SDL2 library automatically as external project" 1) +#option(GET_SIXENSE "Get Sixense library automatically as external project" 1) option(USE_NSIGHT "Attempt to find the nSight libraries" 1) -option(GET_SDL2 "Get SDL2 library automatically as external project" 0) if (WIN32) add_paths_to_fixup_libs("${QT_DIR}/bin") diff --git a/cmake/externals/faceshift/CMakeLists.txt b/cmake/externals/faceshift/CMakeLists.txt new file mode 100644 index 0000000000..b18b861912 --- /dev/null +++ b/cmake/externals/faceshift/CMakeLists.txt @@ -0,0 +1,30 @@ +set(EXTERNAL_NAME faceshift) + +include(ExternalProject) +ExternalProject_Add( + ${EXTERNAL_NAME} + URL https://hifi-public.s3.amazonaws.com/dependencies/faceshift.zip + CMAKE_ARGS ${ANDROID_CMAKE_ARGS} -DCMAKE_INSTALL_PREFIX:PATH= + BINARY_DIR ${EXTERNAL_PROJECT_PREFIX}/build + LOG_DOWNLOAD 1 + LOG_CONFIGURE 1 + LOG_BUILD 1 +) + +# URL_MD5 1bdcb8a0b8d5b1ede434cc41efade41d + +# Hide this external target (for ide users) +set_target_properties(${EXTERNAL_NAME} PROPERTIES FOLDER "hidden/externals") + +ExternalProject_Get_Property(${EXTERNAL_NAME} INSTALL_DIR) + +string(TOUPPER ${EXTERNAL_NAME} EXTERNAL_NAME_UPPER) +set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIRS ${INSTALL_DIR}/include CACHE FILEPATH "Path to Faceshift include directory") + +if (WIN32) + set(${EXTERNAL_NAME_UPPER}_LIBRARY_DEBUG ${INSTALL_DIR}/lib/Debug/faceshift.lib CACHE FILEPATH "Faceshift libraries") + set(${EXTERNAL_NAME_UPPER}_LIBRARY_RELEASE ${INSTALL_DIR}/lib/Release/faceshift.lib CACHE FILEPATH "Faceshift libraries") +elseif (APPLE) + set(${EXTERNAL_NAME_UPPER}_LIBRARY_DEBUG ${INSTALL_DIR}/lib/Debug/libfaceshift.a CACHE FILEPATH "Faceshift libraries") + set(${EXTERNAL_NAME_UPPER}_LIBRARY_RELEASE ${INSTALL_DIR}/lib/Release/libfaceshift.a CACHE FILEPATH "Faceshift libraries") +endif() diff --git a/cmake/externals/sdl2/CMakeLists.txt b/cmake/externals/sdl2/CMakeLists.txt index e6c80cf6ef..0f44f28610 100644 --- a/cmake/externals/sdl2/CMakeLists.txt +++ b/cmake/externals/sdl2/CMakeLists.txt @@ -47,25 +47,13 @@ if (APPLE) ExternalProject_Get_Property(${EXTERNAL_NAME} SOURCE_DIR) set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIR ${SOURCE_DIR}/SDL2.framework/Headers CACHE PATH "Location of SDL2 include directory") set(${EXTERNAL_NAME_UPPER}_LIBRARY_TEMP ${SOURCE_DIR}/SDL2.framework/SDL2 CACHE STRING "Path to SDL2 library") +elseif (WIN32) + ExternalProject_Get_Property(${EXTERNAL_NAME} SOURCE_DIR) + set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIR ${SOURCE_DIR}/include CACHE PATH "Location of SDL2 include directory") + set(${EXTERNAL_NAME_UPPER}_LIBRARY_TEMP ${SOURCE_DIR}/lib/x86/SDL2.lib CACHE FILEPATH "Path to SDL2 library") + set(${EXTERNAL_NAME_UPPER}_DLL_PATH ${SOURCE_DIR}/lib/x86 CACHE PATH "Location of SDL2 DLL") else () - if (WIN32) - ExternalProject_Get_Property(${EXTERNAL_NAME} SOURCE_DIR) - set(_ROOT_DIR ${SOURCE_DIR}) - set(_INCLUDE_DIR ${_ROOT_DIR}/include) - set(_LIB_DIR "${SOURCE_DIR}/lib/x86") - set(_LIB_EXT "lib") - - set(${EXTERNAL_NAME_UPPER}_DLL_PATH ${_LIB_DIR} CACHE PATH "Location of SDL2 DLL") - else () - ExternalProject_Get_Property(${EXTERNAL_NAME} INSTALL_DIR) - set(_ROOT_DIR ${INSTALL_DIR}) - set(_INCLUDE_DIR ${_ROOT_DIR}/include/SDL2) - - set(_LIB_DIR ${INSTALL_DIR}/lib) - set(_LIB_EXT "so") - set(_LIB_PREFIX "lib") - endif () - - set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIR ${_INCLUDE_DIR} CACHE PATH "Location of SDL2 include directory") - set(${EXTERNAL_NAME_UPPER}_LIBRARY_TEMP ${_LIB_DIR}/${_LIB_PREFIX}SDL2.${_LIB_EXT} CACHE FILEPATH "Path to SDL2 library") -endif () \ No newline at end of file + ExternalProject_Get_Property(${EXTERNAL_NAME} INSTALL_DIR) + set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIR ${INSTALL_DIR}/include/SDL2 CACHE PATH "Location of SDL2 include directory") + set(${EXTERNAL_NAME_UPPER}_LIBRARY_TEMP ${INSTALL_DIR}/lib/libSDL2.so CACHE FILEPATH "Path to SDL2 library") +endif () diff --git a/cmake/externals/sixense/CMakeLists.txt b/cmake/externals/sixense/CMakeLists.txt index f6646e2272..72de4a7e15 100644 --- a/cmake/externals/sixense/CMakeLists.txt +++ b/cmake/externals/sixense/CMakeLists.txt @@ -1,30 +1,20 @@ include(ExternalProject) -include(SelectLibraryConfigurations) -set(EXTERNAL_NAME Sixense) +set(EXTERNAL_NAME sixense) string(TOUPPER ${EXTERNAL_NAME} EXTERNAL_NAME_UPPER) ExternalProject_Add( ${EXTERNAL_NAME} - URL ./SixenseSDK_062612.zip - URL_MD5 10cc8dc470d2ac1244a88cf04bc549cc + URL http://hifi-public.s3.amazonaws.com/dependencies/SixenseSDK_071615.zip + URL_MD5 752a3901f334124e9cffc2ba4136ef7d CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" LOG_DOWNLOAD 1 ) -if (APPLE) - find_library(SIXENSE_LIBRARY_RELEASE lib/osx_x64/release_dll/libsixense_x64.dylib HINTS ${SIXENSE_SEARCH_DIRS}) - find_library(SIXENSE_LIBRARY_DEBUG lib/osx_x64/debug_dll/libsixensed_x64.dylib HINTS ${SIXENSE_SEARCH_DIRS}) -elseif (UNIX) - find_library(SIXENSE_LIBRARY_RELEASE lib/linux_x64/release/libsixense_x64.so HINTS ${SIXENSE_SEARCH_DIRS}) - # find_library(SIXENSE_LIBRARY_DEBUG lib/linux_x64/debug/libsixensed_x64.so HINTS ${SIXENSE_SEARCH_DIRS}) -elseif (WIN32) -endif () - - +set_target_properties(${EXTERNAL_NAME} PROPERTIES FOLDER "hidden/externals") ExternalProject_Get_Property(${EXTERNAL_NAME} SOURCE_DIR) @@ -39,22 +29,18 @@ if (WIN32) set(ARCH_DIR "Win32") set(ARCH_SUFFIX "") endif() - - # FIXME need to account for different architectures - set(${EXTERNAL_NAME_UPPER}_LIBRARIES "${SOURCE_DIR}/lib/${ARCH_DIR}/release_dll/sixense${ARCH_SUFFIX}.lib" CACHE TYPE INTERNAL) - add_paths_to_fixup_libs(${SOURCE_DIR}/bin/win32) + + set(${EXTERNAL_NAME_UPPER}_LIBRARIES "${SOURCE_DIR}/lib/${ARCH_DIR}/VS2013/release_dll/sixense${ARCH_SUFFIX}.lib" CACHE TYPE INTERNAL) + add_paths_to_fixup_libs("${SOURCE_DIR}/bin/${ARCH_DIR}/VS2013/release_dll") elseif(APPLE) - # FIXME need to account for different architectures - set(${EXTERNAL_NAME_UPPER}_LIBRARIES ${SOURCE_DIR}/lib/osx_x64/release_dll/libsixense_x64.dylib CACHE TYPE INTERNAL) - add_paths_to_fixup_libs(${SOURCE_DIR}/bin/osx32) + set(${EXTERNAL_NAME_UPPER}_LIBRARIES ${SOURCE_DIR}/lib/osx_x64/release_dll/libsixense_x64.dylib CACHE TYPE INTERNAL) elseif(NOT ANDROID) - # FIXME need to account for different architectures - set(${EXTERNAL_NAME_UPPER}_LIBRARIES ${SOURCE_DIR}/lib/linux_x64/release/libsixense_x64.so CACHE TYPE INTERNAL) - add_paths_to_fixup_libs(${SOURCE_DIR}/bin/linux32) - + # FIXME need to account for different architectures + set(${EXTERNAL_NAME_UPPER}_LIBRARIES ${SOURCE_DIR}/lib/linux_x64/release/libsixense_x64.so CACHE TYPE INTERNAL) + endif() diff --git a/cmake/macros/AddDependencyExternalProjects.cmake b/cmake/macros/AddDependencyExternalProjects.cmake index e859ef2db5..e8292c3198 100644 --- a/cmake/macros/AddDependencyExternalProjects.cmake +++ b/cmake/macros/AddDependencyExternalProjects.cmake @@ -16,7 +16,7 @@ macro(ADD_DEPENDENCY_EXTERNAL_PROJECTS) string(TOUPPER ${_PROJ_NAME} _PROJ_NAME_UPPER) # has the user told us they specific don't want this as an external project? - if (GET_${_PROJ_NAME_UPPER}) + if (NOT DISABLE_${_PROJ_NAME_UPPER}) # have we already detected we can't have this as external project on this OS? if (NOT DEFINED ${_PROJ_NAME_UPPER}_EXTERNAL_PROJECT OR ${_PROJ_NAME_UPPER}_EXTERNAL_PROJECT) # have we already setup the target? diff --git a/cmake/macros/TargetFaceshift.cmake b/cmake/macros/TargetFaceshift.cmake new file mode 100644 index 0000000000..99f65d942a --- /dev/null +++ b/cmake/macros/TargetFaceshift.cmake @@ -0,0 +1,14 @@ +# +# Copyright 2015 High Fidelity, Inc. +# Created by Bradley Austin Davis on 2015/10/10 +# +# Distributed under the Apache License, Version 2.0. +# See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +# +macro(TARGET_FACESHIFT) + add_dependency_external_projects(faceshift) + find_package(Faceshift REQUIRED) + target_include_directories(${TARGET_NAME} PRIVATE ${FACESHIFT_INCLUDE_DIRS}) + target_link_libraries(${TARGET_NAME} ${FACESHIFT_LIBRARIES}) + add_definitions(-DHAVE_FACESHIFT) +endmacro() \ No newline at end of file diff --git a/cmake/macros/TargetSDL2.cmake b/cmake/macros/TargetSDL2.cmake new file mode 100644 index 0000000000..ee2328dfff --- /dev/null +++ b/cmake/macros/TargetSDL2.cmake @@ -0,0 +1,14 @@ +# +# Copyright 2015 High Fidelity, Inc. +# Created by Bradley Austin Davis on 2015/10/10 +# +# Distributed under the Apache License, Version 2.0. +# See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +# +macro(TARGET_SDL2) + add_dependency_external_projects(sdl2) + find_package(SDL2 REQUIRED) + target_include_directories(${TARGET_NAME} SYSTEM PRIVATE ${SDL2_INCLUDE_DIR}) + target_link_libraries(${TARGET_NAME} ${SDL2_LIBRARY}) + add_definitions(-DHAVE_SDL2) +endmacro() \ No newline at end of file diff --git a/cmake/macros/TargetSixense.cmake b/cmake/macros/TargetSixense.cmake new file mode 100644 index 0000000000..b52af9cdd2 --- /dev/null +++ b/cmake/macros/TargetSixense.cmake @@ -0,0 +1,14 @@ +# +# Copyright 2015 High Fidelity, Inc. +# Created by Bradley Austin Davis on 2015/10/10 +# +# Distributed under the Apache License, Version 2.0. +# See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +# +macro(TARGET_SIXENSE) + add_dependency_external_projects(sixense) + find_package(Sixense REQUIRED) + target_include_directories(${TARGET_NAME} PRIVATE ${SIXENSE_INCLUDE_DIRS}) + target_link_libraries(${TARGET_NAME} ${SIXENSE_LIBRARIES}) + add_definitions(-DHAVE_SIXENSE) +endmacro() \ No newline at end of file diff --git a/cmake/modules/FindFaceshift.cmake b/cmake/modules/FindFaceshift.cmake index 1f8b7b19fe..bd77951273 100644 --- a/cmake/modules/FindFaceshift.cmake +++ b/cmake/modules/FindFaceshift.cmake @@ -18,32 +18,9 @@ # See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html # -include("${MACRO_DIR}/HifiLibrarySearchHints.cmake") -hifi_library_search_hints("faceshift") - -find_path(FACESHIFT_INCLUDE_DIRS fsbinarystream.h PATH_SUFFIXES include HINTS ${FACESHIFT_SEARCH_DIRS}) - -if (APPLE) - set(ARCH_DIR "MacOS") -elseif (UNIX) - set(ARCH_DIR "UNIX") -elseif (WIN32) - if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") - set(ARCH_DIR "x64") - else() - set(ARCH_DIR "Win32") - endif() -endif () - -find_library(FACESHIFT_LIBRARY_RELEASE NAME faceshift PATH_SUFFIXES lib/${ARCH_DIR} HINTS ${FACESHIFT_SEARCH_DIRS}) -find_library(FACESHIFT_LIBRARY_DEBUG NAME faceshiftd PATH_SUFFIXES lib/${ARCH_DIR} HINTS ${FACESHIFT_SEARCH_DIRS}) - include(SelectLibraryConfigurations) select_library_configurations(FACESHIFT) -set(FACESHIFT_LIBRARIES ${FACESHIFT_LIBRARY}) - include(FindPackageHandleStandardArgs) find_package_handle_standard_args(Faceshift DEFAULT_MSG FACESHIFT_INCLUDE_DIRS FACESHIFT_LIBRARIES) - mark_as_advanced(FACESHIFT_INCLUDE_DIRS FACESHIFT_LIBRARIES FACESHIFT_SEARCH_DIRS) \ No newline at end of file diff --git a/cmake/modules/FindSixense.cmake b/cmake/modules/FindSixense.cmake index 98b37d5410..9abacac136 100644 --- a/cmake/modules/FindSixense.cmake +++ b/cmake/modules/FindSixense.cmake @@ -18,49 +18,6 @@ # See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html # -include("${MACRO_DIR}/HifiLibrarySearchHints.cmake") -hifi_library_search_hints("sixense") - -find_path(SIXENSE_INCLUDE_DIRS sixense.h PATH_SUFFIXES include HINTS ${SIXENSE_SEARCH_DIRS}) - -if (APPLE) - find_library(SIXENSE_LIBRARY_RELEASE lib/osx_x64/release_dll/libsixense_x64.dylib HINTS ${SIXENSE_SEARCH_DIRS}) - find_library(SIXENSE_LIBRARY_DEBUG lib/osx_x64/debug_dll/libsixensed_x64.dylib HINTS ${SIXENSE_SEARCH_DIRS}) -elseif (UNIX) - find_library(SIXENSE_LIBRARY_RELEASE lib/linux_x64/release/libsixense_x64.so HINTS ${SIXENSE_SEARCH_DIRS}) - # find_library(SIXENSE_LIBRARY_DEBUG lib/linux_x64/debug/libsixensed_x64.so HINTS ${SIXENSE_SEARCH_DIRS}) -elseif (WIN32) - if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") - set(ARCH_DIR "x64") - set(ARCH_SUFFIX "_x64") - else() - set(ARCH_DIR "Win32") - set(ARCH_SUFFIX "") - endif() - - find_library(SIXENSE_LIBRARY_RELEASE "lib/${ARCH_DIR}/release_dll/sixense${ARCH_SUFFIX}.lib" HINTS ${SIXENSE_SEARCH_DIRS}) - find_library(SIXENSE_LIBRARY_DEBUG "lib/${ARCH_DIR}/debug_dll/sixensed.lib" HINTS ${SIXENSE_SEARCH_DIRS}) - - find_path(SIXENSE_DEBUG_DLL_PATH "sixensed${ARCH_SUFFIX}.dll" PATH_SUFFIXES bin/${ARCH_DIR}/debug_dll HINTS ${SIXENSE_SEARCH_DIRS}) - find_path(SIXENSE_RELEASE_DLL_PATH "sixense${ARCH_SUFFIX}.dll" PATH_SUFFIXES bin/${ARCH_DIR}/release_dll HINTS ${SIXENSE_SEARCH_DIRS}) - find_path(SIXENSE_DEVICE_DLL_PATH DeviceDLL.dll PATH_SUFFIXES samples/${ARCH_DIR}/sixense_simple3d HINTS ${SIXENSE_SEARCH_DIRS}) -endif () - -include(SelectLibraryConfigurations) -select_library_configurations(SIXENSE) - -set(SIXENSE_REQUIREMENTS SIXENSE_INCLUDE_DIRS SIXENSE_LIBRARIES) -if (WIN32) - list(APPEND SIXENSE_REQUIREMENTS SIXENSE_DEBUG_DLL_PATH SIXENSE_RELEASE_DLL_PATH SIXENSE_DEVICE_DLL_PATH) -endif () - -set(SIXENSE_LIBRARIES "${SIXENSE_LIBRARY}") - include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Sixense DEFAULT_MSG ${SIXENSE_REQUIREMENTS}) - -if (WIN32) - add_paths_to_fixup_libs(${SIXENSE_DEBUG_DLL_PATH} ${SIXENSE_RELEASE_DLL_PATH} ${SIXENSE_DEVICE_DLL_PATH}) -endif () - +find_package_handle_standard_args(Sixense DEFAULT_MSG SIXENSE_INCLUDE_DIRS SIXENSE_LIBRARIES) mark_as_advanced(SIXENSE_LIBRARIES SIXENSE_INCLUDE_DIRS SIXENSE_SEARCH_DIRS) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index f9c92c59e7..f04fa88910 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -2,7 +2,12 @@ set(TARGET_NAME interface) project(${TARGET_NAME}) # set a default root dir for each of our optional externals if it was not passed -set(OPTIONAL_EXTERNALS "Faceshift" "LeapMotion" "RtMidi" "RSSDK" "3DConnexionClient" "iViewHMD") +set(OPTIONAL_EXTERNALS "LeapMotion" "RtMidi" "RSSDK" "iViewHMD") + +if(WIN32) + list(APPEND OPTIONAL_EXTERNALS "3DConnexionClient") +endif() + foreach(EXTERNAL ${OPTIONAL_EXTERNALS}) string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE) if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR) @@ -102,12 +107,14 @@ link_hifi_libraries(shared octree environment gpu gl procedural model render render-utils entities-renderer ui auto-updater plugins display-plugins input-plugins) +#fixme find a way to express faceshift as a plugin target_bullet() target_glew() target_opengl() - -add_dependency_external_projects(sdl2) +if (WIN32 OR APPLE) + target_faceshift() +endif() # perform standard include and linking for found externals foreach(EXTERNAL ${OPTIONAL_EXTERNALS}) diff --git a/interface/external/faceshift/readme.txt b/interface/external/faceshift/readme.txt deleted file mode 100644 index 4208711632..0000000000 --- a/interface/external/faceshift/readme.txt +++ /dev/null @@ -1,26 +0,0 @@ - -Instructions for adding the Faceshift library to Interface -Stephen Birarda, July 18th, 2014 - -OS X users: You can also use homebrew to get the Faceshift library by tapping our repo - highfidelity/homebrew-formulas -and then calling 'brew install highfidelity/formulas/faceshift'. - -You can download the Faceshift SDK from http://download.faceshift.com/faceshift-network.zip. - -Create a ‘faceshift’ folder under interface/externals. - -You may optionally choose to place this folder in a location outside the repository (so you can re-use with different checkouts and different projects). - -If so our CMake find module expects you to set the ENV variable 'HIFI_LIB_DIR' to a directory containing a subfolder ‘faceshift’ that contains the lib and include folders. - -1. Build a Faceshift static library from the fsbinarystream.cpp file. - Windows: Win32 console application; no precompiled header or SDL checks; no ATL or MFC headers; Project Properties, Configuration Type = Static Library (.lib). - -2. Copy the library files to the ‘lib’ folder in your Faceshift folder. - OSX: If you build a release version call it libfaceshift.a. The debug version should be called libfaceshiftd.a. - Windows: The release and debug versions should be called faceshift.lib and faceshiftd.lib, respectively. Copy them into a ‘Win32’ folder in your ‘lib’ folder. - -3. Copy the fsbinarystream.h header file from the Faceshift SDK into the ‘include’ folder in your Faceshift folder. - -4. Clear your build directory, run cmake and build, and you should be all set. - diff --git a/interface/external/sdl2/readme.txt b/interface/external/sdl2/readme.txt deleted file mode 100644 index 9f3bd40e15..0000000000 --- a/interface/external/sdl2/readme.txt +++ /dev/null @@ -1,13 +0,0 @@ - -Instructions for adding the SDL library (SDL2) to Interface -David Rowe, 11 Jan 2015 - -You can download the SDL development library from https://www.libsdl.org/. Interface has been tested with version 2.0.3. - -1. Copy the include and lib folders into the interface/externals/sdl2 folder. - This readme.txt should be there as well. - - You may optionally choose to copy the SDK folders to a location outside the repository (so you can re-use with different checkouts and different projects). - If so our CMake find module expects you to set the ENV variable 'HIFI_LIB_DIR' to a directory containing a subfolder 'sdl2' that contains the two folders mentioned above. - -2. Clear your build directory, run cmake and build, and you should be all set. diff --git a/interface/external/sixense/readme.txt b/interface/external/sixense/readme.txt deleted file mode 100644 index a4790caa5e..0000000000 --- a/interface/external/sixense/readme.txt +++ /dev/null @@ -1,10 +0,0 @@ - -Instructions for adding the Sixense driver to Interface -Andrzej Kapolka, November 18, 2013 - -1. Copy the Sixense sdk folders (bin, include, lib, and samples) into the interface/external/Sixense folder. This readme.txt should be there as well. - - You may optionally choose to copy the SDK folders to a location outside the repository (so you can re-use with different checkouts and different projects). - If so our CMake find module expects you to set the ENV variable 'HIFI_LIB_DIR' to a directory containing a subfolder 'sixense' that contains the folders mentioned above. - -3. Delete your build directory, run cmake and build, and you should be all set. diff --git a/libraries/input-plugins/CMakeLists.txt b/libraries/input-plugins/CMakeLists.txt index 094a697012..c26e14e756 100644 --- a/libraries/input-plugins/CMakeLists.txt +++ b/libraries/input-plugins/CMakeLists.txt @@ -11,55 +11,5 @@ if (WIN32) target_link_libraries(${TARGET_NAME} ${OPENVR_LIBRARIES}) endif() -#add_dependency_external_projects(Sixense) -#find_package(Sixense REQUIRED) -#target_include_directories(${TARGET_NAME} PRIVATE ${SIXENSE_INCLUDE_DIRS}) -#target_link_libraries(${TARGET_NAME} ${SIXENSE_LIBRARIES}) - -# set a default root dir for each of our optional externals if it was not passed -set(OPTIONAL_EXTERNALS "SDL2" "Sixense") -foreach(EXTERNAL ${OPTIONAL_EXTERNALS}) - string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE) - if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR) - string(TOLOWER ${EXTERNAL} ${EXTERNAL}_LOWERCASE) - set(${${EXTERNAL}_UPPERCASE}_ROOT_DIR "${CMAKE_SOURCE_DIR}/interface/external/${${EXTERNAL}_LOWERCASE}") - endif () -endforeach() - -# perform standard include and linking for found externals -foreach(EXTERNAL ${OPTIONAL_EXTERNALS}) - - if (${${EXTERNAL}_UPPERCASE}_REQUIRED) - find_package(${EXTERNAL} REQUIRED) - else () - find_package(${EXTERNAL}) - endif () - - if (${${EXTERNAL}_UPPERCASE}_FOUND AND NOT DISABLE_${${EXTERNAL}_UPPERCASE}) - add_definitions(-DHAVE_${${EXTERNAL}_UPPERCASE}) - - # include the library directories (ignoring warnings) - if (NOT ${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS) - set(${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIR}) - endif () - - include_directories(SYSTEM ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS}) - - # perform the system include hack for OS X to ignore warnings - if (APPLE) - foreach(EXTERNAL_INCLUDE_DIR ${${${EXTERNAL}_UPPERCASE}_INCLUDE_DIRS}) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${EXTERNAL_INCLUDE_DIR}") - endforeach() - endif () - - if (NOT ${${EXTERNAL}_UPPERCASE}_LIBRARIES) - set(${${EXTERNAL}_UPPERCASE}_LIBRARIES ${${${EXTERNAL}_UPPERCASE}_LIBRARY}) - endif () - - if (NOT APPLE OR NOT ${${EXTERNAL}_UPPERCASE} MATCHES "SIXENSE") - target_link_libraries(${TARGET_NAME} ${${${EXTERNAL}_UPPERCASE}_LIBRARIES}) - elseif (APPLE AND NOT INSTALLER_BUILD) - add_definitions(-DSIXENSE_LIB_FILENAME=\"${${${EXTERNAL}_UPPERCASE}_LIBRARY_RELEASE}\") - endif () - endif () -endforeach() +target_sdl2() +target_sixense() From e12ddf86f6b3fb90c3f51f3e63b322760ac5af84 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 30 Oct 2015 08:33:29 -0700 Subject: [PATCH 30/35] add back optional externals --- CMakeLists.txt | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d69b8c4c77..11ef4752f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,19 +186,19 @@ set_property(DIRECTORY PROPERTY EP_PREFIX ${EXTERNAL_PROJECT_PREFIX}) setup_externals_binary_dir() # setup options to grab external project dependencies -#option(GET_BULLET "Get Bullet library automatically as external project" 1) -#option(GET_GLM "Get GLM library automatically as external project" 1) -#option(GET_GVERB "Get Gverb library automatically as external project" 1) -#option(GET_TBB "Get Threading Building Blocks library automatically as external project" 1) -#option(GET_LIBOVR "Get LibOVR library automatically as external project" 1) -#option(GET_VHACD "Get V-HACD library automatically as external project" 1) -#option(GET_POLYVOX "Get polyvox library automatically as external project" 1) -#option(GET_OPENVR "Get OpenVR library automatically as external project" 1) -#option(GET_BOOSTCONFIG "Get Boost-config library automatically as external project" 1) -#option(GET_OGLPLUS "Get OGLplus library automatically as external project" 1) -#option(GET_GLEW "Get GLEW library automatically as external project" 1) -#option(GET_SDL2 "Get SDL2 library automatically as external project" 1) -#option(GET_SIXENSE "Get Sixense library automatically as external project" 1) +option(GET_BULLET "Get Bullet library automatically as external project" 1) +option(GET_GLM "Get GLM library automatically as external project" 1) +option(GET_GVERB "Get Gverb library automatically as external project" 1) +option(GET_TBB "Get Threading Building Blocks library automatically as external project" 1) +option(GET_LIBOVR "Get LibOVR library automatically as external project" 1) +option(GET_VHACD "Get V-HACD library automatically as external project" 1) +option(GET_POLYVOX "Get polyvox library automatically as external project" 1) +option(GET_OPENVR "Get OpenVR library automatically as external project" 1) +option(GET_BOOSTCONFIG "Get Boost-config library automatically as external project" 1) +option(GET_OGLPLUS "Get OGLplus library automatically as external project" 1) +option(GET_GLEW "Get GLEW library automatically as external project" 1) +option(GET_SDL2 "Get SDL2 library automatically as external project" 1) +option(GET_SIXENSE "Get Sixense library automatically as external project" 1) option(USE_NSIGHT "Attempt to find the nSight libraries" 1) From fb3bf163913a2fa7cb79a8c85354c22950cf0274 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Fri, 30 Oct 2015 09:01:42 -0700 Subject: [PATCH 31/35] improvement to cmake to explicitly use local external dependency --- CMakeLists.txt | 15 --------------- cmake/macros/AddDependencyExternalProjects.cmake | 2 +- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 11ef4752f5..efe99b550b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,21 +185,6 @@ set(EXTERNAL_PROJECT_PREFIX "project") set_property(DIRECTORY PROPERTY EP_PREFIX ${EXTERNAL_PROJECT_PREFIX}) setup_externals_binary_dir() -# setup options to grab external project dependencies -option(GET_BULLET "Get Bullet library automatically as external project" 1) -option(GET_GLM "Get GLM library automatically as external project" 1) -option(GET_GVERB "Get Gverb library automatically as external project" 1) -option(GET_TBB "Get Threading Building Blocks library automatically as external project" 1) -option(GET_LIBOVR "Get LibOVR library automatically as external project" 1) -option(GET_VHACD "Get V-HACD library automatically as external project" 1) -option(GET_POLYVOX "Get polyvox library automatically as external project" 1) -option(GET_OPENVR "Get OpenVR library automatically as external project" 1) -option(GET_BOOSTCONFIG "Get Boost-config library automatically as external project" 1) -option(GET_OGLPLUS "Get OGLplus library automatically as external project" 1) -option(GET_GLEW "Get GLEW library automatically as external project" 1) -option(GET_SDL2 "Get SDL2 library automatically as external project" 1) -option(GET_SIXENSE "Get Sixense library automatically as external project" 1) - option(USE_NSIGHT "Attempt to find the nSight libraries" 1) diff --git a/cmake/macros/AddDependencyExternalProjects.cmake b/cmake/macros/AddDependencyExternalProjects.cmake index e8292c3198..e35ca98959 100644 --- a/cmake/macros/AddDependencyExternalProjects.cmake +++ b/cmake/macros/AddDependencyExternalProjects.cmake @@ -16,7 +16,7 @@ macro(ADD_DEPENDENCY_EXTERNAL_PROJECTS) string(TOUPPER ${_PROJ_NAME} _PROJ_NAME_UPPER) # has the user told us they specific don't want this as an external project? - if (NOT DISABLE_${_PROJ_NAME_UPPER}) + if (NOT USE_LOCAL_${_PROJ_NAME_UPPER}) # have we already detected we can't have this as external project on this OS? if (NOT DEFINED ${_PROJ_NAME_UPPER}_EXTERNAL_PROJECT OR ${_PROJ_NAME_UPPER}_EXTERNAL_PROJECT) # have we already setup the target? From 7a4787a79cec2d33471ae86ebbb27f511705e2d8 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 30 Oct 2015 09:21:13 -0700 Subject: [PATCH 32/35] Rename stepSimulation to stepSimulationWithSubstepCallback to fix warning --- libraries/physics/src/PhysicsEngine.cpp | 7 ++++--- libraries/physics/src/ThreadSafeDynamicsWorld.cpp | 5 +++-- libraries/physics/src/ThreadSafeDynamicsWorld.h | 5 +++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 6773cecb14..2abe4317d7 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -228,7 +228,7 @@ void PhysicsEngine::removeContacts(ObjectMotionState* motionState) { void PhysicsEngine::stepSimulation() { CProfileManager::Reset(); - BT_PROFILE("stepSimulation"); + BT_PROFILE("stepSimulationWithSubstepCallback"); // NOTE: the grand order of operations is: // (1) pull incoming changes // (2) step simulation @@ -241,7 +241,7 @@ void PhysicsEngine::stepSimulation() { float timeStep = btMin(dt, MAX_TIMESTEP); if (_myAvatarController) { - // ADEBUG TODO: move this stuff outside and in front of stepSimulation, because + // ADEBUG TODO: move this stuff outside and in front of stepSimulationWithSubstepCallback, because // the updateShapeIfNecessary() call needs info from MyAvatar and should // be done on the main thread during the pre-simulation stuff if (_myAvatarController->needsRemoval()) { @@ -263,7 +263,8 @@ void PhysicsEngine::stepSimulation() { updateContactMap(); }; - int numSubsteps = _dynamicsWorld->stepSimulation(timeStep, PHYSICS_ENGINE_MAX_NUM_SUBSTEPS, PHYSICS_ENGINE_FIXED_SUBSTEP, onSubStep); + int numSubsteps = _dynamicsWorld->stepSimulationWithSubstepCallback(timeStep, PHYSICS_ENGINE_MAX_NUM_SUBSTEPS, + PHYSICS_ENGINE_FIXED_SUBSTEP, onSubStep); if (numSubsteps > 0) { BT_PROFILE("postSimulation"); _numSubsteps += (uint32_t)numSubsteps; diff --git a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp index 8bf7cdab20..94d6315705 100644 --- a/libraries/physics/src/ThreadSafeDynamicsWorld.cpp +++ b/libraries/physics/src/ThreadSafeDynamicsWorld.cpp @@ -27,8 +27,9 @@ ThreadSafeDynamicsWorld::ThreadSafeDynamicsWorld( : btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration) { } -int ThreadSafeDynamicsWorld::stepSimulation( btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep, SubStepCallback onSubStep) { - BT_PROFILE("stepSimulation"); +int ThreadSafeDynamicsWorld::stepSimulationWithSubstepCallback(btScalar timeStep, int maxSubSteps, + btScalar fixedTimeStep, SubStepCallback onSubStep) { + BT_PROFILE("stepSimulationWithSubstepCallback"); int subSteps = 0; if (maxSubSteps) { //fixed timestep with interpolation diff --git a/libraries/physics/src/ThreadSafeDynamicsWorld.h b/libraries/physics/src/ThreadSafeDynamicsWorld.h index 1f6ce09d0c..26954832cf 100644 --- a/libraries/physics/src/ThreadSafeDynamicsWorld.h +++ b/libraries/physics/src/ThreadSafeDynamicsWorld.h @@ -37,8 +37,9 @@ public: btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration); - // virtual overrides from btDiscreteDynamicsWorld - int stepSimulation( btScalar timeStep, int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.), SubStepCallback onSubStep = []() { }); + int stepSimulationWithSubstepCallback(btScalar timeStep, int maxSubSteps = 1, + btScalar fixedTimeStep = btScalar(1.)/btScalar(60.), + SubStepCallback onSubStep = []() { }); void synchronizeMotionStates(); // btDiscreteDynamicsWorld::m_localTime is the portion of real-time that has not yet been simulated From 84869c512b44ac1ca27b2e6cadd2616ac0ea865c Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 30 Oct 2015 09:30:22 -0700 Subject: [PATCH 33/35] Fix mis-renamed method names --- libraries/physics/src/PhysicsEngine.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 2abe4317d7..1e652b75c4 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -228,7 +228,7 @@ void PhysicsEngine::removeContacts(ObjectMotionState* motionState) { void PhysicsEngine::stepSimulation() { CProfileManager::Reset(); - BT_PROFILE("stepSimulationWithSubstepCallback"); + BT_PROFILE("stepSimulation"); // NOTE: the grand order of operations is: // (1) pull incoming changes // (2) step simulation @@ -241,7 +241,7 @@ void PhysicsEngine::stepSimulation() { float timeStep = btMin(dt, MAX_TIMESTEP); if (_myAvatarController) { - // ADEBUG TODO: move this stuff outside and in front of stepSimulationWithSubstepCallback, because + // ADEBUG TODO: move this stuff outside and in front of stepSimulation, because // the updateShapeIfNecessary() call needs info from MyAvatar and should // be done on the main thread during the pre-simulation stuff if (_myAvatarController->needsRemoval()) { From 93503d0a216019cff88a2b08441f88539881932f Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 30 Oct 2015 11:03:13 -0700 Subject: [PATCH 34/35] make kinematic grab set entities kinematic in bullet and update entity position --- examples/controllers/handControllerGrab.js | 24 ++++++++++++++++------ interface/src/avatar/AvatarActionHold.cpp | 5 +++++ libraries/physics/src/ObjectAction.cpp | 15 +++++++++++++- libraries/physics/src/ObjectMotionState.h | 9 ++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index cb445a0960..9ee9ed1e04 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -55,6 +55,7 @@ var NEAR_GRABBING_VELOCITY_SMOOTH_RATIO = 1.0; // adjust time-averaging of held var NEAR_PICK_MAX_DISTANCE = 0.3; // max length of pick-ray for close grabbing to be selected var RELEASE_VELOCITY_MULTIPLIER = 1.5; // affects throwing things var PICK_BACKOFF_DISTANCE = 0.2; // helps when hand is intersecting the grabble object +var NEAR_GRABBING_KINEMATIC = true; // force objects to be kinematic when near-grabbed ///////////////////////////////////////////////////////////////// // @@ -404,8 +405,9 @@ function MyController(hand, triggerAction) { var handControllerPosition = Controller.getSpatialControlPosition(this.palm); var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm)); - var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation", - "gravity", "ignoreForCollisions"]); + var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation", "gravity", + "ignoreForCollisions", + "collisionsWillMove"]); var now = Date.now(); // add the action and initialize some variables @@ -549,8 +551,14 @@ function MyController(hand, triggerAction) { this.lineOff(); var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, - ["position", "rotation", "gravity", "ignoreForCollisions"]); + ["position", "rotation", "gravity", + "ignoreForCollisions", "collisionsWillMove"]); this.activateEntity(this.grabbedEntity, grabbedProperties); + if (grabbedProperties.collisionsWillMove && NEAR_GRABBING_KINEMATIC) { + Entities.editEntity(this.grabbedEntity, { + collisionsWillMove: false + }); + } var handRotation = this.getHandRotation(); var handPosition = this.getHandPosition(); @@ -579,7 +587,8 @@ function MyController(hand, triggerAction) { timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, relativePosition: this.offsetPosition, relativeRotation: this.offsetRotation, - ttl: ACTION_TTL + ttl: ACTION_TTL, + kinematic: NEAR_GRABBING_KINEMATIC }); if (this.actionID === NULL_ACTION_ID) { this.actionID = null; @@ -631,7 +640,8 @@ function MyController(hand, triggerAction) { timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, relativePosition: this.offsetPosition, relativeRotation: this.offsetRotation, - ttl: ACTION_TTL + ttl: ACTION_TTL, + kinematic: NEAR_GRABBING_KINEMATIC }); this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); } @@ -828,6 +838,7 @@ function MyController(hand, triggerAction) { if (data["refCount"] == 1) { data["gravity"] = grabbedProperties.gravity; data["ignoreForCollisions"] = grabbedProperties.ignoreForCollisions; + data["collisionsWillMove"] = grabbedProperties.collisionsWillMove; var whileHeldProperties = {gravity: {x:0, y:0, z:0}}; if (invertSolidWhileHeld) { whileHeldProperties["ignoreForCollisions"] = ! grabbedProperties.ignoreForCollisions; @@ -845,7 +856,8 @@ function MyController(hand, triggerAction) { if (data["refCount"] < 1) { Entities.editEntity(entityID, { gravity: data["gravity"], - ignoreForCollisions: data["ignoreForCollisions"] + ignoreForCollisions: data["ignoreForCollisions"], + collisionsWillMove: data["collisionsWillMove"] }); data = null; } diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 238e48d2fd..59bfd88be6 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -119,6 +119,9 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { worldTrans.setRotation(glmToBullet(_rotationalTarget)); rigidBody->setWorldTransform(worldTrans); + ownerEntity->setPosition(_positionalTarget); + ownerEntity->setRotation(_rotationalTarget); + _previousPositionalTarget = _positionalTarget; _previousRotationalTarget = _rotationalTarget; _previousSet = true; @@ -224,6 +227,8 @@ QVariantMap AvatarActionHold::getArguments() { arguments["relativeRotation"] = glmToQMap(_relativeRotation); arguments["timeScale"] = _linearTimeScale; arguments["hand"] = _hand; + arguments["kinematic"] = _kinematic; + arguments["kinematicSetVelocity"] = _kinematicSetVelocity; }); return arguments; } diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index ff8382a143..d44ebc30b1 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -35,7 +35,9 @@ void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar delta if (ownerEntityExpired) { qDebug() << "warning -- action with no entity removing self from btCollisionWorld."; btDynamicsWorld* dynamicsWorld = static_cast(collisionWorld); - dynamicsWorld->removeAction(this); + if (dynamicsWorld) { + dynamicsWorld->removeAction(this); + } return; } @@ -120,6 +122,17 @@ QVariantMap ObjectAction::getArguments() { arguments["ttl"] = (float)(_expires - now) / (float)USECS_PER_SECOND; } arguments["tag"] = _tag; + + EntityItemPointer entity = _ownerEntity.lock(); + if (entity) { + ObjectMotionState* motionState = static_cast(entity->getPhysicsInfo()); + if (motionState) { + arguments["::active"] = motionState->isActive(); + arguments["::motion-type"] = motionTypeToString(motionState->getMotionType()); + } else { + arguments["::no-motion-state"] = true; + } + } }); return arguments; } diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index 450ac34a90..22f0e9dcb0 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -29,6 +29,15 @@ enum MotionType { MOTION_TYPE_KINEMATIC // keyframed motion }; +inline QString motionTypeToString(MotionType motionType) { + switch(motionType) { + case MOTION_TYPE_STATIC: return QString("static"); + case MOTION_TYPE_DYNAMIC: return QString("dynamic"); + case MOTION_TYPE_KINEMATIC: return QString("kinematic"); + } + return QString("unknown"); +} + enum MotionStateType { MOTIONSTATE_TYPE_INVALID, MOTIONSTATE_TYPE_ENTITY, From 31d92fd90ab37a482de3db966d77f419855df172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Brisset?= Date: Fri, 30 Oct 2015 12:51:30 -0700 Subject: [PATCH 35/35] Baseball test code --- interface/src/Application.cpp | 10 +++++-- interface/src/avatar/AvatarActionHold.cpp | 22 +++++++++++++-- .../input-plugins/ViveControllerManager.cpp | 28 +++++++++++++++---- .../src/input-plugins/ViveControllerManager.h | 8 ++++-- libraries/shared/src/PhysicsHelpers.h | 2 +- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index eebd26c3c3..8f9fc9b16e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -950,8 +950,8 @@ void Application::initializeGL() { checkFPStimer.start(1000); // call our idle function whenever we can - connect(&idleTimer, &QTimer::timeout, this, &Application::idle); - idleTimer.start(TARGET_SIM_FRAME_PERIOD_MS); + // connect(&idleTimer, &QTimer::timeout, this, &Application::idle); + // idleTimer.start(TARGET_SIM_FRAME_PERIOD_MS); _idleLoopStdev.reset(); // update before the first render @@ -1023,6 +1023,10 @@ void Application::paintGL() { if (_inPaint) { return; } + + // this is a good idea + idle(); + _inPaint = true; Finally clearFlagLambda([this] { _inPaint = false; }); @@ -2070,7 +2074,7 @@ void Application::idle() { float secondsSinceLastUpdate = timeSinceLastUpdateUs / USECS_PER_SECOND; if (isThrottled && (timeSinceLastUpdateUs / USECS_PER_MSEC) < THROTTLED_SIM_FRAME_PERIOD_MS) { - return; // bail early, we're throttled and not enough time has elapsed + //return; // bail early, we're throttled and not enough time has elapsed } _lastTimeUpdated.start(); diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 59bfd88be6..941e876075 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -35,7 +35,8 @@ AvatarActionHold::~AvatarActionHold() { qDebug() << "AvatarActionHold::~AvatarActionHold"; #endif } - +#include +#include void AvatarActionHold::updateActionWorker(float deltaTimeStep) { bool gotLock = false; glm::quat rotation; @@ -51,7 +52,24 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { glm::vec3 offset; glm::vec3 palmPosition; glm::quat palmRotation; - if (_hand == "right") { + + const auto& plugins = PluginManager::getInstance()->getInputPlugins(); + auto it = std::find_if(std::begin(plugins), std::end(plugins), [](const InputPluginPointer& plugin) { + return plugin->getName() == ViveControllerManager::NAME; + }); + + if (it != std::end(plugins)) { + const auto& vive = it->dynamicCast(); + auto index = (_hand == "right") ? 0 : 1; auto userInputMapper = DependencyManager::get(); + auto translation = extractTranslation(userInputMapper->getSensorToWorldMat()); + auto rotation = glm::quat_cast(userInputMapper->getSensorToWorldMat()); + + + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); + const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + palmPosition = translation + rotation * vive->getPosition(index); + palmRotation = rotation * vive->getRotation(index) * yFlip * quarterX; + } else if (_hand == "right") { palmPosition = holdingAvatar->getRightPalmPosition(); palmRotation = holdingAvatar->getRightPalmRotation(); } else { diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp index bb8267b616..28e001208c 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp @@ -217,6 +217,15 @@ void ViveControllerManager::renderHand(UserInputMapper::PoseValue pose, gpu::Bat batch.drawIndexed(gpu::TRIANGLES, mesh->getNumIndices(), 0); } +glm::vec3 ViveControllerManager::getPosition(int hand) const { + const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; + return extractTranslation(mat); +} +glm::quat ViveControllerManager::getRotation(int hand) const { + const mat4& mat = _trackedDevicePoseMat4[hand ? 3 : 4]; + return glm::quat_cast(mat); +} + void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { #ifdef Q_OS_WIN _poseStateMap.clear(); @@ -250,7 +259,7 @@ void ViveControllerManager::update(float deltaTime, bool jointsCaptured) { numTrackedControllers++; const mat4& mat = _trackedDevicePoseMat4[device]; - + if (!jointsCaptured) { handlePoseEvent(mat, numTrackedControllers - 1); } @@ -372,16 +381,23 @@ void ViveControllerManager::handlePoseEvent(const mat4& mat, int index) { // Q = (deltaQ * QOffset) * (yFlip * quarterTurnAboutX) // // Q = (deltaQ * inverse(deltaQForAlignedHand)) * (yFlip * quarterTurnAboutX) - + + float sign = (index == LEFT_HAND) ? -1.0f : 1.0f; + const glm::quat quarterX = glm::angleAxis(PI / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f)); const glm::quat yFlip = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); - float sign = (index == LEFT_HAND) ? -1.0f : 1.0f; const glm::quat signedQuaterZ = glm::angleAxis(sign * PI / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f)); const glm::quat eighthX = glm::angleAxis(PI / 4.0f, glm::vec3(1.0f, 0.0f, 0.0f)); - const glm::quat offset = glm::inverse(signedQuaterZ * eighthX); - rotation = rotation * offset * yFlip * quarterX; + + + const glm::quat rotationOffset = glm::inverse(signedQuaterZ * eighthX) * yFlip * quarterX; + const glm::vec3 translationOffset = glm::vec3(sign * CONTROLLER_LENGTH_OFFSET / 2.0f, + CONTROLLER_LENGTH_OFFSET / 2.0f, + 2.0f * CONTROLLER_LENGTH_OFFSET); - position += rotation * glm::vec3(0, 0, -CONTROLLER_LENGTH_OFFSET); + position += rotation * translationOffset; + rotation = rotation * rotationOffset; + //{quat, x = 0.653281, y = -0.270598, z = 0.653281, w = 0.270598}{vec3, x = 0.0381, y = -0.0381, z = -0.1524} _poseStateMap[makeInput(JointChannel(index)).getChannel()] = UserInputMapper::PoseValue(position, rotation); } diff --git a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h index 5cae8daaf4..f6220ca97a 100644 --- a/libraries/input-plugins/src/input-plugins/ViveControllerManager.h +++ b/libraries/input-plugins/src/input-plugins/ViveControllerManager.h @@ -27,6 +27,8 @@ class ViveControllerManager : public InputPlugin, public InputDevice { Q_OBJECT public: + static const QString NAME; + enum JoystickAxisChannel { AXIS_Y_POS = 1U << 1, AXIS_Y_NEG = 1U << 2, @@ -74,6 +76,10 @@ public: UserInputMapper::Input makeInput(unsigned int button, int index); UserInputMapper::Input makeInput(JoystickAxisChannel axis, int index); UserInputMapper::Input makeInput(JointChannel joint); + + int getNumDevices() const; + glm::vec3 getPosition(int device) const; + glm::quat getRotation(int device) const; private: void renderHand(UserInputMapper::PoseValue pose, gpu::Batch& batch, int index); @@ -92,8 +98,6 @@ private: int _rightHandRenderID; bool _renderControllers; - - static const QString NAME; }; #endif // hifi__ViveControllerManager diff --git a/libraries/shared/src/PhysicsHelpers.h b/libraries/shared/src/PhysicsHelpers.h index 7ceafea915..3b6fccdc99 100644 --- a/libraries/shared/src/PhysicsHelpers.h +++ b/libraries/shared/src/PhysicsHelpers.h @@ -16,7 +16,7 @@ #include const int PHYSICS_ENGINE_MAX_NUM_SUBSTEPS = 6; // Bullet will start to "lose time" at 10 FPS. -const float PHYSICS_ENGINE_FIXED_SUBSTEP = 1.0f / 60.0f; +const float PHYSICS_ENGINE_FIXED_SUBSTEP = 1.0f / 90.0f; // return incremental rotation (Bullet-style) caused by angularVelocity over timeStep glm::quat computeBulletRotationStep(const glm::vec3& angularVelocity, float timeStep);