Merge remote-tracking branch 'upstream/master'

This commit is contained in:
James Pollack 2015-10-01 15:46:51 -07:00
commit edbc2cc2c3
4 changed files with 223 additions and 24 deletions

View file

@ -0,0 +1,43 @@
// createPingPongGun.js
//
// Script Type: Entity Spawner
// Created by James B. Pollack on 9/30/2015
// Copyright 2015 High Fidelity, Inc.
//
// This script creates a gun that shoots ping pong balls when you pull the trigger on a hand controller.
// 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, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */
Script.include("../../utilities.js");
var scriptURL = Script.resolvePath('pingPongGun.js');
var MODEL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun.fbx'
var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/ping_pong_gun_collision_hull.obj';
var center = Vec3.sum(Vec3.sum(MyAvatar.position, {
x: 0,
y: 0.5,
z: 0
}), Vec3.multiply(0.5, Quat.getFront(Camera.getOrientation())));
var pingPongGun = Entities.addEntity({
type: "Model",
modelURL: MODEL_URL,
shapeType: 'compound',
compoundShapeURL: COLLISION_HULL_URL,
script: scriptURL,
position: center,
dimensions: {
x:0.67,
y: 0.14,
z: 0.09
},
collisionsWillMove: true,
});
function cleanUp() {
Entities.deleteEntity(pingPongGun);
}
Script.scriptEnding.connect(cleanUp);

View file

@ -0,0 +1,160 @@
// pingPongGun.js
//
// Script Type: Entity
// Created by James B. Pollack @imgntn on 9/21/2015
// Copyright 2015 High Fidelity, Inc.
//
// This script shoots a ping pong ball.
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
/*global print, 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 SHOOTING_SOUND_URL = 'http://hifi-public.s3.amazonaws.com/sounds/ping_pong_gun/pong_sound.wav';
function PingPongGun() {
return;
}
//if the trigger value goes below this value, reload the gun.
var RELOAD_THRESHOLD = 0.95;
var GUN_TIP_FWD_OFFSET = 0.45;
var GUN_TIP_UP_OFFSET = 0.040;
var GUN_FORCE = 15;
var BALL_RESTITUTION = 0.6;
var BALL_LINEAR_DAMPING = 0.4;
var BALL_GRAVITY = {
x: 0,
y: -9.8,
z: 0
};
var BALL_DIMENSIONS = {
x: 0.04,
y: 0.04,
z: 0.04
}
var BALL_COLOR = {
red: 255,
green: 255,
blue: 255
}
PingPongGun.prototype = {
hand: null,
whichHand: null,
gunTipPosition: null,
canShoot: false,
canShootTimeout: null,
setRightHand: function() {
this.hand = 'RIGHT';
},
setLeftHand: function() {
this.hand = 'LEFT';
},
startNearGrab: function() {
this.setWhichHand();
},
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 {
if (this.canShootTimeout !== null) {
Script.clearTimeout(this.canShootTimeout);
}
this.checkTriggerPressure(this.whichHand);
}
},
releaseGrab: function() {
var _t = this;
this.canShootTimeout = Script.setTimeout(function() {
_t.canShoot = false;
}, 250)
},
checkTriggerPressure: function(gunHand) {
var handClickString = gunHand + "_HAND_CLICK";
var handClick = Controller.findAction(handClickString);
this.triggerValue = Controller.getActionValue(handClick);
if (this.triggerValue < RELOAD_THRESHOLD) {
// print('RELOAD');
this.canShoot = true;
} else if (this.triggerValue >= RELOAD_THRESHOLD && this.canShoot === true) {
var gunProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]);
this.shootBall(gunProperties);
this.canShoot = false;
}
return;
},
shootBall: function(gunProperties) {
var forwardVec = Quat.getFront(Quat.multiply(gunProperties.rotation, Quat.fromPitchYawRollDegrees(0, -90, 0)));
forwardVec = Vec3.normalize(forwardVec);
forwardVec = Vec3.multiply(forwardVec, GUN_FORCE);
var properties = {
type: 'Sphere',
color: BALL_COLOR,
dimensions: BALL_DIMENSIONS,
linearDamping: BALL_LINEAR_DAMPING,
gravity: BALL_GRAVITY,
restitution: BALL_RESTITUTION,
collisionsWillMove: true,
rotation: gunProperties.rotation,
position: this.getGunTipPosition(gunProperties),
velocity: forwardVec,
lifetime: 10
};
Entities.addEntity(properties);
this.playSoundAtCurrentPosition(gunProperties.position);
},
playSoundAtCurrentPosition: function(position) {
var audioProperties = {
volume: 0.1,
position: position
};
Audio.playSound(this.SHOOTING_SOUND, audioProperties);
},
getGunTipPosition: function(properties) {
//the tip of the gun is going to be in a different place than the center, so we move in space relative to the model to find that position
var frontVector = Quat.getRight(properties.rotation);
var frontOffset = Vec3.multiply(frontVector, GUN_TIP_FWD_OFFSET);
var upVector = Quat.getRight(properties.rotation);
var upOffset = Vec3.multiply(upVector, GUN_TIP_UP_OFFSET);
var gunTipPosition = Vec3.sum(properties.position, frontOffset);
gunTipPosition = Vec3.sum(gunTipPosition, upOffset);
return gunTipPosition;
},
preload: function(entityID) {
this.entityID = entityID;
this.SHOOTING_SOUND = SoundCache.getSound(SHOOTING_SOUND_URL);
}
};
// entity scripts always need to return a newly constructed object of our type
return new PingPongGun();
});

View file

@ -144,13 +144,12 @@ void FBXReader::consolidateFBXMaterials() {
// FIXME: Do not use the Specular Factor yet as some FBX models have it set to 0
// metallic *= material.specularFactor;
material._material->setMetallic(metallic);
material._material->setGloss(material.shininess);
material._material->setGloss(material.shininess);
if (material.opacity <= 0.0f) {
material._material->setOpacity(1.0f);
material._material->setOpacity(1.0f);
} else {
material._material->setOpacity(material.opacity);
material._material->setOpacity(material.opacity);
}
}
}

View file

@ -539,14 +539,16 @@ FBXGeometry* OBJReader::readOBJ(QByteArray& model, const QVariantHash& mapping,
foreach (QString materialID, materials.keys()) {
OBJMaterial& objMaterial = materials[materialID];
geometry.materials[materialID] = FBXMaterial(objMaterial.diffuseColor, // glm::vec3(1.0f, 1.0f, 1.0f)
objMaterial.specularColor, // glm::vec3(1.0f)
glm::vec3(), // glm::vec3()
glm::vec2(0.f, 1.0f), // glm::vec2(0.f, 1.0f)
objMaterial.shininess, // 96.0f
objMaterial.opacity); // 1.0f
FBXMaterial& material = geometry.materials[materialID];
material._material = std::make_shared<model::Material>();
geometry.materials[materialID] = FBXMaterial(objMaterial.diffuseColor,
objMaterial.specularColor,
glm::vec3(0.0f),
glm::vec2(0.0f, 1.0f),
objMaterial.shininess,
objMaterial.opacity);
FBXMaterial& fbxMaterial = geometry.materials[materialID];
fbxMaterial.materialID = materialID;
fbxMaterial._material = std::make_shared<model::Material>();
model::MaterialPointer modelMaterial = fbxMaterial._material;
if (!objMaterial.diffuseTextureFilename.isEmpty()) {
FBXTexture texture;
@ -554,21 +556,16 @@ FBXGeometry* OBJReader::readOBJ(QByteArray& model, const QVariantHash& mapping,
// TODO -- something to get textures working again
}
material._material->setEmissive(material.emissiveColor);
if (glm::all(glm::equal(material.diffuseColor, glm::vec3(0.0f)))) {
material._material->setDiffuse(material.diffuseColor);
} else {
material._material->setDiffuse(material.diffuseColor);
}
material._material->setMetallic(glm::length(material.specularColor));
material._material->setGloss(material.shininess);
modelMaterial->setEmissive(fbxMaterial.emissiveColor);
modelMaterial->setDiffuse(fbxMaterial.diffuseColor);
modelMaterial->setMetallic(glm::length(fbxMaterial.specularColor));
modelMaterial->setGloss(fbxMaterial.shininess);
if (material.opacity <= 0.0f) {
material._material->setOpacity(1.0f);
if (fbxMaterial.opacity <= 0.0f) {
modelMaterial->setOpacity(1.0f);
} else {
material._material->setOpacity(material.opacity);
modelMaterial->setOpacity(fbxMaterial.opacity);
}
}
return geometryPtr;