diff --git a/examples/edit.js b/examples/edit.js index 544f3c94b6..b33f47c2b7 100644 --- a/examples/edit.js +++ b/examples/edit.js @@ -1211,49 +1211,6 @@ PropertiesTool = function(opts) { webView.setVisible(visible); }; - vecToPolar = function(direction) { - var x = direction.x; - var y = direction.y; - var z = direction.z; - var pitch, yaw; - pitch = -Math.asin(y); - var c = Math.cos(-pitch); - if (Math.abs(pitch) > (Math.PI / 2.0 - epsilon)) { - //handle gymbal lock - if (pitch > 0) { - pitch = Math.PI / 2.0; - } else { - pitch = -Math.PI / 2.0; - } - yaw = 0.0; - } else { - if (z < 0) { - if(x > 0 && x < 1) { - yaw = Math.PI - Math.asin(x / c); - } else { - yaw = -Math.asin(x / c) - Math.PI; - } - } else { - yaw = Math.asin(x / c); - } - } - return { - x: pitch * RADIANS_TO_DEGREES, - y: yaw * RADIANS_TO_DEGREES, - z: 0.0 //discard roll component - }; - }; - - polarToVec = function(orientation) { - var pitch = orientation.x * DEGREES_TO_RADIANS; - var yaw = orientation.y * DEGREES_TO_RADIANS; - return { - x: Math.cos(pitch) * Math.sin(yaw), - y: Math.sin(-pitch), - z: Math.cos(pitch) * Math.cos(yaw) - }; - } - selectionManager.addEventListener(function() { data = { type: 'update', @@ -1267,7 +1224,8 @@ PropertiesTool = function(opts) { entity.properties.rotation = Quat.safeEulerAngles(entity.properties.rotation); } if (entity.properties.keyLightDirection !== undefined) { - entity.properties.keyLightDirection = vecToPolar(entity.properties.keyLightDirection); + entity.properties.keyLightDirection = Vec3.multiply(RADIANS_TO_DEGREES, Vec3.toPolar(entity.properties.keyLightDirection)); + entity.properties.keyLightDirection.z = 0.0; } selections.push(entity); } @@ -1297,7 +1255,8 @@ PropertiesTool = function(opts) { data.properties.rotation = Quat.fromPitchYawRollDegrees(rotation.x, rotation.y, rotation.z); } if (data.properties.keyLightDirection !== undefined) { - data.properties.keyLightDirection = polarToVec(data.properties.keyLightDirection); + data.properties.keyLightDirection = Vec3.fromPolar( + data.properties.keyLightDirection.x * DEGREES_TO_RADIANS, data.properties.keyLightDirection.y * DEGREES_TO_RADIANS); } Entities.editEntity(selectionManager.selections[0], data.properties); if (data.properties.name != undefined) { diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index 49a2ad2cbe..4ed16b2ef0 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -14,6 +14,7 @@ #include #include "ScriptEngineLogging.h" +#include "NumericalConstants.h" #include "Vec3.h" glm::vec3 Vec3::reflect(const glm::vec3& v1, const glm::vec3& v2) { @@ -73,3 +74,52 @@ void Vec3::print(const QString& lable, const glm::vec3& v) { bool Vec3::equal(const glm::vec3& v1, const glm::vec3& v2) { return v1 == v2; } + +glm::vec3 Vec3::toPolar(const glm::vec3& v) { + float radius = length(v); + if (glm::abs(radius) < EPSILON) { + return glm::vec3(0.0f, 0.0f, 0.0f); + } + + glm::vec3 u = v / radius; + + float elevation, azimuth; + + elevation = glm::asin(-u.y); + azimuth = atan2(v.x, v.z); + + // Round off small decimal values + if (glm::abs(elevation) < EPSILON) { + elevation = 0.0f; + } + if (glm::abs(azimuth) < EPSILON) { + azimuth = 0.0f; + } + + return glm::vec3(elevation, azimuth, radius); +} + +glm::vec3 Vec3::fromPolar(const glm::vec3& polar) { + float x = glm::cos(polar.x) * glm::sin(polar.y); + float y = glm::sin(-polar.x); + float z = glm::cos(polar.x) * glm::cos(polar.y); + + // Round small values to 0 + if (glm::abs(x) < EPSILON) { + x = 0.0f; + } + if (glm::abs(y) < EPSILON) { + y = 0.0f; + } + if (glm::abs(z) < EPSILON) { + z = 0.0f; + } + + return polar.z * glm::vec3(x, y, z); +} + +glm::vec3 Vec3::fromPolar(float elevation, float azimuth) { + glm::vec3 v = glm::vec3(elevation, azimuth, 1.0f); + return fromPolar(v); +} + diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 2a2e145e8e..82062ca80d 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -40,6 +40,9 @@ public slots: glm::vec3 mix(const glm::vec3& v1, const glm::vec3& v2, float m); void print(const QString& lable, const glm::vec3& v); bool equal(const glm::vec3& v1, const glm::vec3& v2); + glm::vec3 toPolar(const glm::vec3& v); + glm::vec3 fromPolar(const glm::vec3& polar); + glm::vec3 fromPolar(float elevation, float azimuth); };