From aac73c36650f329271c155aba554aae1c5e8034f Mon Sep 17 00:00:00 2001 From: Mika Impola Date: Thu, 15 May 2014 21:44:45 +0300 Subject: [PATCH 1/5] Script to make your avatar sit down. --- examples/sit.js | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 examples/sit.js diff --git a/examples/sit.js b/examples/sit.js new file mode 100644 index 0000000000..d67f6f6bb5 --- /dev/null +++ b/examples/sit.js @@ -0,0 +1,141 @@ +// +// sit.js +// examples +// +// Created by Mika Impola on February 8, 2014 +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +var buttonImageUrl = "https://worklist-prod.s3.amazonaws.com/attachment/0aca88e1-9bd8-5c1d.svg"; + +var windowDimensions = Controller.getViewportDimensions(); + +var buttonWidth = 37; +var buttonHeight = 45; +var buttonPadding = 10; + +var buttonPositionX = windowDimensions.x - buttonPadding - buttonWidth; +var buttonPositionY = (windowDimensions.y - buttonHeight) / 2 ; + +var sitDownButton = Overlays.addOverlay("image", { + x: buttonPositionX, y: buttonPositionY, width: buttonWidth, height: buttonHeight, + subImage: { x: 0, y: buttonHeight, width: buttonWidth, height: buttonHeight}, + imageURL: buttonImageUrl, + visible: true, + alpha: 1.0 + }); +var standUpButton = Overlays.addOverlay("image", { + x: buttonPositionX, y: buttonPositionY, width: buttonWidth, height: buttonHeight, + subImage: { x: buttonWidth, y: buttonHeight, width: buttonWidth, height: buttonHeight}, + imageURL: buttonImageUrl, + visible: false, + alpha: 1.0 + }); + +var passedTime = 0.0; +var startPosition = null; +var animationLenght = 2.0; + +// This is the pose we would like to end up +var pose = [ + {joint:"RightUpLeg", rotation: {x:100.0, y:15.0, z:0.0}}, + {joint:"RightLeg", rotation: {x:-130.0, y:15.0, z:0.0}}, + {joint:"RightFoot", rotation: {x:30, y:15.0, z:0.0}}, + {joint:"LeftUpLeg", rotation: {x:100.0, y:-15.0, z:0.0}}, + {joint:"LeftLeg", rotation: {x:-130.0, y:-15.0, z:0.0}}, + {joint:"LeftFoot", rotation: {x:30, y:15.0, z:0.0}}, + + {joint:"Spine2", rotation: {x:20, y:0.0, z:0.0}}, + + {joint:"RightShoulder", rotation: {x:0.0, y:40.0, z:0.0}}, + {joint:"LeftShoulder", rotation: {x:0.0, y:-40.0, z:0.0}} + +]; + +var startPoseAndTransition = []; + +function storeStartPoseAndTransition() { + for (var i = 0; i < pose.length; i++){ + var startRotation = Quat.safeEulerAngles(MyAvatar.getJointRotation(pose[i].joint)); + var transitionVector = Vec3.subtract( pose[i].rotation, startRotation ); + startPoseAndTransition.push({joint: pose[i].joint, start: startRotation, transition: transitionVector}); + } +} + +function updateJoints(factor){ + for (var i = 0; i < startPoseAndTransition.length; i++){ + var scaledTransition = Vec3.multiply(startPoseAndTransition[i].transition, factor); + var rotation = Vec3.sum(startPoseAndTransition[i].start, scaledTransition); + MyAvatar.setJointData(startPoseAndTransition[i].joint, Quat.fromVec3Degrees( rotation )); + } +} + +var sittingDownAnimation = function(deltaTime) { + + passedTime += deltaTime; + var factor = passedTime/animationLenght; + + if ( passedTime <= animationLenght ) { + updateJoints(factor); + + var pos = { x: startPosition.x - 0.3 * factor, y: startPosition.y - 0.5 * factor, z: startPosition.z}; + MyAvatar.position = pos; + } +} + +var standingUpAnimation = function(deltaTime){ + + passedTime += deltaTime; + var factor = 1 - passedTime/animationLenght; + + if ( passedTime <= animationLenght ) { + + updateJoints(factor); + + var pos = { x: startPosition.x + 0.3 * (passedTime/animationLenght), y: startPosition.y + 0.5 * (passedTime/animationLenght), z: startPosition.z}; + MyAvatar.position = pos; + } +} + +Controller.mousePressEvent.connect(function(event){ + + var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); + + if (clickedOverlay == sitDownButton) { + passedTime = 0.0; + startPosition = MyAvatar.position; + storeStartPoseAndTransition(); + try{ + Script.update.disconnect(standingUpAnimation); + } catch(e){ + // no need to handle. if it wasn't connected no harm done + } + Script.update.connect(sittingDownAnimation); + Overlays.editOverlay(sitDownButton, { visible: false }); + Overlays.editOverlay(standUpButton, { visible: true }); + } else if (clickedOverlay == standUpButton) { + passedTime = 0.0; + startPosition = MyAvatar.position; + try{ + Script.update.disconnect(sittingDownAnimation); + } catch (e){} + Script.update.connect(standingUpAnimation); + Overlays.editOverlay(standUpButton, { visible: false }); + Overlays.editOverlay(sitDownButton, { visible: true }); + } +}) + + +Script.scriptEnding.connect(function() { + + for (var i = 0; i < pose.length; i++){ + MyAvatar.clearJointData(pose[i][0]); + } + + Overlays.deleteOverlay(sitDownButton); + Overlays.deleteOverlay(standUpButton); +}); From fd3e871242126eef2473401f8468f3f9dffcd853 Mon Sep 17 00:00:00 2001 From: Mika Impola Date: Sat, 17 May 2014 21:04:27 +0300 Subject: [PATCH 2/5] Updating button position when window is resized. --- examples/sit.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/sit.js b/examples/sit.js index d67f6f6bb5..f503438792 100644 --- a/examples/sit.js +++ b/examples/sit.js @@ -129,6 +129,18 @@ Controller.mousePressEvent.connect(function(event){ } }) +function update(deltaTime){ + var newWindowDimensions = Controller.getViewportDimensions(); + if( newWindowDimensions.x != windowDimensions.x || newWindowDimensions.y != windowDimensions.y ){ + windowDimensions = newWindowDimensions; + var newX = windowDimensions.x - buttonPadding - buttonWidth; + var newY = (windowDimensions.y - buttonHeight) / 2 ; + Overlays.editOverlay( standUpButton, {x: newX, y: newY} ); + Overlays.editOverlay( sitDownButton, {x: newX, y: newY} ); + } +} + +Script.update.connect(update); Script.scriptEnding.connect(function() { From cc1bd5d63bd061744a262645baa8f9d49ceab9d9 Mon Sep 17 00:00:00 2001 From: Mika Impola Date: Sat, 17 May 2014 21:24:29 +0300 Subject: [PATCH 3/5] Fixed cropping of the image. --- examples/sit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sit.js b/examples/sit.js index f503438792..1df877dba6 100644 --- a/examples/sit.js +++ b/examples/sit.js @@ -15,7 +15,7 @@ var buttonImageUrl = "https://worklist-prod.s3.amazonaws.com/attachment/0aca88e1 var windowDimensions = Controller.getViewportDimensions(); var buttonWidth = 37; -var buttonHeight = 45; +var buttonHeight = 46; var buttonPadding = 10; var buttonPositionX = windowDimensions.x - buttonPadding - buttonWidth; From 4f4b4c08bb32fa2bbe74a57450187a730f117373 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 19 May 2014 12:29:49 -0700 Subject: [PATCH 4/5] Restore specular lighting--but, per the OpenGL spec, shut off specular contribution if the surface isn't facing the light. --- interface/resources/shaders/model.frag | 7 +++++-- interface/resources/shaders/model_normal_map.frag | 9 ++++++--- .../resources/shaders/model_normal_specular_map.frag | 9 ++++++--- interface/resources/shaders/model_specular_map.frag | 9 ++++++--- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/interface/resources/shaders/model.frag b/interface/resources/shaders/model.frag index 3964bd5b97..a9d93f2f6a 100644 --- a/interface/resources/shaders/model.frag +++ b/interface/resources/shaders/model.frag @@ -20,11 +20,14 @@ varying vec4 normal; void main(void) { // compute the base color based on OpenGL lighting model vec4 normalizedNormal = normalize(normal); + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[0].position))); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = max(0.0, dot(gl_LightSource[0].position, normalizedNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + normalizedNormal)); // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + diff --git a/interface/resources/shaders/model_normal_map.frag b/interface/resources/shaders/model_normal_map.frag index a4f7a887c5..392be1f1cf 100644 --- a/interface/resources/shaders/model_normal_map.frag +++ b/interface/resources/shaders/model_normal_map.frag @@ -32,12 +32,15 @@ void main(void) { // compute the base color based on OpenGL lighting model vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + float diffuse = dot(viewNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(viewNormal, gl_LightSource[0].position))); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = max(0.0, dot(gl_LightSource[0].position, viewNormal)); - + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + viewNormal)); + // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + vec4(pow(specular, gl_FrontMaterial.shininess) * gl_FrontLightProduct[0].specular.rgb, 0.0); diff --git a/interface/resources/shaders/model_normal_specular_map.frag b/interface/resources/shaders/model_normal_specular_map.frag index f5b9d2b06b..dbbb343c62 100644 --- a/interface/resources/shaders/model_normal_specular_map.frag +++ b/interface/resources/shaders/model_normal_specular_map.frag @@ -35,12 +35,15 @@ void main(void) { // compute the base color based on OpenGL lighting model vec4 viewNormal = vec4(normalizedTangent * localNormal.x + normalizedBitangent * localNormal.y + normalizedNormal * localNormal.z, 0.0); + float diffuse = dot(viewNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(viewNormal, gl_LightSource[0].position))); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = max(0.0, dot(gl_LightSource[0].position, viewNormal)); - + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + viewNormal)); + // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + vec4(pow(specular, gl_FrontMaterial.shininess) * gl_FrontLightProduct[0].specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, 0.0); diff --git a/interface/resources/shaders/model_specular_map.frag b/interface/resources/shaders/model_specular_map.frag index 4e2f3d0c98..b955b5cfa6 100644 --- a/interface/resources/shaders/model_specular_map.frag +++ b/interface/resources/shaders/model_specular_map.frag @@ -23,12 +23,15 @@ varying vec4 normal; void main(void) { // compute the base color based on OpenGL lighting model vec4 normalizedNormal = normalize(normal); + float diffuse = dot(normalizedNormal, gl_LightSource[0].position); + float facingLight = step(0.0, diffuse); vec4 base = gl_Color * (gl_FrontLightModelProduct.sceneColor + gl_FrontLightProduct[0].ambient + - gl_FrontLightProduct[0].diffuse * max(0.0, dot(normalizedNormal, gl_LightSource[0].position))); + gl_FrontLightProduct[0].diffuse * (diffuse * facingLight)); // compute the specular component (sans exponent) - float specular = max(0.0, dot(gl_LightSource[0].position, normalizedNormal)); - + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position + vec4(0.0, 0.0, 1.0, 0.0)), + normalizedNormal)); + // modulate texture by base color and add specular contribution gl_FragColor = base * texture2D(diffuseMap, gl_TexCoord[0].st) + vec4(pow(specular, gl_FrontMaterial.shininess) * gl_FrontLightProduct[0].specular.rgb * texture2D(specularMap, gl_TexCoord[0].st).rgb, 0.0); From e066d552a0b699c1dc0066369bca618945212ce6 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 19 May 2014 12:48:26 -0700 Subject: [PATCH 5/5] Added missing unlock that was causing a hang. --- interface/src/ui/ModelsBrowser.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/ui/ModelsBrowser.cpp b/interface/src/ui/ModelsBrowser.cpp index f65829a8ac..4296a096a0 100644 --- a/interface/src/ui/ModelsBrowser.cpp +++ b/interface/src/ui/ModelsBrowser.cpp @@ -351,6 +351,7 @@ bool ModelHandler::parseHeaders(QNetworkReply* reply) { QList items = _model.findItems(QFileInfo(reply->url().toString()).baseName()); if (items.isEmpty() || items.first()->text() == DO_NOT_MODIFY_TAG) { + _lock.unlock(); return false; }