youbeQ - Moving from Google Earth to Cesium
by André Santos
This is a guest post by André Santos on his experience porting the awesome youbeQ app from the Google Earth Plugin to Cesium. - Patrick
youbeQ first appeared at the end of 2011, we wanted to build a 3D platform where people could explore the Earth, talk and share content along the way, and the Google Earth Plugin seemed the right fit for our needs. It provided the Earth full of 3D buildings and a good representation of the terrain, but it also had a lot of limitations and performance inconsistencies that gave me a lot of headaches, but I kept on working with it trying to build something awesome.
youbeQ kept on evolving and changing and is now a driving and flight simulator, where you can explore the Earth with different vehicles.
The year of 2013 came, bringing the hope of a bright new future with the showcase of the new Google Maps with its awesome 3D mode in WebGL, but it also marked the beginning of the end for the NPAPI in Google Chrome, which the Google Earth Plugin uses.
Since our users use mostly Google Chrome this presented a big big problem and we needed an alternative that could give the same or similar functionality as the GE Plugin. Unfortunately there was nothing available at the time.
From time to time we would go for a look around the web to see how the virtual globes were doing, and I checked Cesium a couple of times but it lacked the ability to add and manipulate 3D models, something crucial for youbeQ. But one time, I chanced upon the cool post about 3D models, and after losing any hope of the new 3D layer in Google Maps being available any time soon I decided (a month ago) it was time to give Cesium a try. This marked the beginning of the challenge of changing youbeQ from the Google Earth Plugin to Cesium.
Changing to Cesium
The first thing I had to get used to, was working with Cartesian values instead of degrees (latitude, longitude), thankfully there is always a
fromDegrees function to help you. Also, manipulating matrices for setting the position, orientation and scale of the different elements was something that took awhile to get used to. Another small detail was the order of the [latitude, longitude] when passing it into functions in Cesium, here it is [longitude, latitude].
So in order to migrate youbeQ, I started by thinking of all the different parts I had and needed to find a way to implement in Cesium:
- Load 3D models and manipulate them in real-time
- Manipulate the Camera in real-time
- Create GroundOverlays and manipulate them in real-time
- Create Placemarks and manipulate them in real-time
Thanks to Sandcastle and the huge help of different members from the Cesium team, I was able to find solutions to almost every part. Their team was really fast in answering questions on the forum, which allowed me to reach a working version in a couple of weeks. youbeQ is running stable using Cesium since the beginning of this month (October 2014). Finally youbeQ is plugin free, is faster and the overall user experience improved.
How I did it
So let's go over each of those parts, and hopefully these tips/solutions can help people coming from the GE world get a better feel of how to do it in Cesium.
This proved to be a big challenge that I faced during this migration.
In GE, loading a 3D model is pretty straightforward and the same applies to Cesium. But if you have multiple 3D models, where some are positioned with another as a reference but with custom behaviors, for example the flaps of a plane, this requires some math to get things in the right place, which isn't as straightforward in either GE or Cesium.
So let's say you have the main body and other bodies attached to it, I was doing some fancy quaternion construction given the Euler angles of the main body, multiplying that with the quarternions for each axis of the components, finding the resulting Euler angles and setting their position and orientation. This worked pretty well in GE, but then trying to accomplish the same in Cesium proved to be difficult (you can check the forum post to see a lot of code snippets of the mess I was in).
But then Dan Bagnell came to the rescue, and pointed me to having my model as a hierarchy of meshes, which takes care of positioning the components correctly and you only have to worry about the orientation. He also showed me that you can access each node of the model by its name and change its matrix, this allows you to change the node's position, orientation and even its scale:
UPDATE: Starting with Cesium 1.6, the above code can be simplified to:
This was a really important part of the youbeQ experience, and with which I spent many hours banging my head against the wall, but with the help of Dan Bagnell, I finally got it to work as I wanted. In GE we can control the camera by setting its position [latitude, longitude, altitude] and orientation [heading, tilt, roll]:
In Cesium, it is easy to place it where you want, but it requires some tricks to get it to look at the place you want. First you have to get the Cartesian of your [longitude, latitude, altitude], find the transform for the camera given that position, find the direction we want the camera to look at, apply the heading and tilt in world coordinates than do the twist (roll), simple enough. Here is some code:
Once again I have to thank Dan Bagnell, without his help I would still be driving a vehicle either looking up at the stars or looking everywhere but the vehicle.
UPDATE: Starting with Cesium 1.6, setting the heading, pitch, and roll in the above code can be simplified to:
This is something pretty simple to accomplish in GE, and allows me to place "shadows" beneath the vehicles in youbeQ. You can set the image of the overlay, its location and rotation through a
This proved to be a difficult element to recreate, at the moment, in Cesium. Thanks to the help of Mark Erikson on the forum, I found out that I could use the
SingleTileImageryProvider to load an image that drapes over the terrain, but you can't change its position after loading, which prevents me from using this solution for my use case, but if you don't need to change its position after loading it should give pretty much the same result as a GroundOverlay:
According to Patrick Cozzi, the
RectanglePrimitive will support draping over terrain, but this a feature that is not yet being developed.
In GE a placemark may contain a name and an icon, you can change its style, and easily set its position on the globe by setting its geometry. To change its position you can just update its geometry coordinates:
To accomplish the same in Cesium, I had to use Labels and Billboards. The label allows us to write text on the globe while the billboard allows us to set images. You have to create a
LabelCollection and/or a
BillboardCollection and add it to the scene. Then you can add your label and/or billboard elements to the corresponding collection:
In order to update its position I change the position attribute of each element in the collection according to the [longitude, latitude, altitude].
Since we are dealing with an HTML
canvas element we can overlay anything on top of it, something that was not possible with the GE Plugin (except for Mac OSX), so ScreenOverlays became HTML elements.
So that is it for the big parts required for the transition. Almost everything was possible to implement, and the remaining elements will be possible to replicate in the future, so I believe I made the right choice in switching to Cesium.
Cesium is a great platform, it provides a lot of elements that allow you to build any kind of application, it has better performance than GE, but there is a big element missing that people coming from the GE world love, the 3D buildings layer, which is something that affects greatly youbeQ.
There will eventually be some kind of 3D buildings layer in Cesium but until then we, in youbeQ, will probably have to add our own models in some key locations or else the world seems too deserted.
Apart from that, we are now focused on improving the experience and realism of the vehicles, we are also thinking of re-introducing a global chat, allowing for direct audio and video calls between travelers and also group calls. But we will see what makes more sense to introduce into youbeQ.
PS: This is something that I use a lot in GE: finding the altitude of the terrain at a given position. For anyone interested in knowing how to find that in Cesium:
Beware that altitude can be