Search code examples
cesiumjsczml

How to correctly use unitQuaternion czml property in Cesium


I use the gltf branch of Cesium, and I want to display 3d model of planes. To do that I create czmlDataSource that I load and add to dataSources.

The problem is that I can't figure out how to calculate orientation quaternion to have planes parallel to the ground at a given lat,lon,alt heading north by default (and then impact their heading, eventually pitch and roll).

here is what I do to compute my actual quaternions, but the 3d models are not correctly oriented (and I don't know how to change heading, pitch, roll) :

    var geoPosition = new Cesium.Cartographic(Cesium.Math.toRadians(inputPosition.lon), Cesium.Math.toRadians(inputPosition.lat), inputPosition.alt);
    var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(geoPosition);

    var euler = [cartesianPosition.x, cartesianPosition.y, cartesianPosition.z];
    var qx = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_X, euler[0]);
    var qy = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Y, euler[1]);
    var qz = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, euler[2]);
    var qt = Cesium.Quaternion.multiply(qz, qy);
    var q = Cesium.Quaternion.multiply(qt, qx);
    Cesium.Quaternion.normalize(q, q);

var czmlSrc = [{
       "orientation": {
            "epoch": "2012-08-04T16:00:00Z",
            "interpolationAlgorithm": "LINEAR",
            "interpolationDegree": 1,
            "unitQuaternion": [0, q.x,q.y,q.z,q.w,
                            3600, q.x,q.y,q.z,q.w]
        }
   }];

Solution

  • CZML currently has the orientation "backwards" compared to Cesium's convention. So if you are working with CZML you actually want the conjugate of the orientation. We plan on fixing this in a major CZML update within a few months, but didn't want to break all of the existing documents out there with the current format. When we do update, the plan is to try and do it in a backwards compatible way, so existing CZML will still work.