did anyone already face problems with glb importing as 3D Models?
I create my glb file in Python through pyvista/vtk/pygltflib. However, when I import it in Cesium Ion, the only option that seems to work is 3D Models (import as 3D tiles).
Also, when importing as 3D Models (import as 3D tiles) to Cesium Ion, it does not come with any orientation, and thus I cannot try it in SandCastle, because it simply does not work.
Because the model comes in an upward orientation instead of its original one (horizontal, as a map) as 3D Models (import as 3D tiles). And when we try to rotate it in Cesium Ion, it gets the wrong position. And positioning is crucial for our case since we are working with accurate georeferenced data.
Also, we are having trouble rotating the 3D Models (import as 3D tiles), which is a 3DTileset. The origin of the model (as depicted in the Cesium Ion Editor is not centered at the model but at its edge.
And we need to do that without Cesium Ion editor, through an automated process (lines of codes, therefore).
In an attempt to rotate this model, I adapted this example to adhere to rotation, not only translation.
The complete code is below.
var viewer = new Cesium.Viewer("cesiumContainer", {
shadows: true,
});
viewer.scene.globe.depthTestAgainstTerrain = true;
var viewModel = {
height: 0,
};
Cesium.knockout.track(viewModel);
var toolbar = document.getElementById("toolbar");
Cesium.knockout.applyBindings(viewModel, toolbar);
var tileset = new Cesium.Cesium3DTileset({
url: "../SampleData/Cesium3DTiles/Tilesets/Tileset/tileset.json",
});
tileset.readyPromise
.then(function (tileset) {
viewer.scene.primitives.add(tileset);
viewer.zoomTo(
tileset,
new Cesium.HeadingPitchRange(
0.0,
-0.5,
tileset.boundingSphere.radius * 2.0
)
);
})
.otherwise(function (error) {
console.log(error);
});
Cesium.knockout
.getObservable(viewModel, "height")
.subscribe(function (height) {
height = Number(height);
if (isNaN(height)) {
return;
}
var cartographic = Cesium.Cartographic.fromCartesian(
tileset.boundingSphere.center
);
var surface = Cesium.Cartesian3.fromRadians(
cartographic.longitude,
cartographic.latitude,
0.0
);
var offset = Cesium.Cartesian3.fromRadians(
cartographic.longitude,
cartographic.latitude,
height
);
var translation = Cesium.Cartesian3.subtract(
offset,
surface,
new Cesium.Cartesian3()
);
var rotation = Cesium.Matrix3.fromRotationY(
Cesium.Math.toRadians(1.0),
new Cesium.Matrix3()
);
// tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
tileset.modelMatrix = Cesium.Matrix4.fromRotationTranslation(rotation,translation);
});
I added var rotation = Cesium.Matrix3.fromRotationY - but could be X or Z: none worked for angle values different than 0.
I changed tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
for tileset.modelMatrix = Cesium.Matrix4.fromRotationTranslation(rotation,translation);
to make sense.
The easiest way to do this would be to upload your model using the REST API and pass in the longitude, latitude, and height for the origin of your 3D model. This will (1) make it appear in exactly the right location, and (2) when you rotate it with the Cesium ion tool it should rotate from the correct origin.
The REST API tutorial here describes how to set this up and has a link to a full example: https://cesium.com/docs/tutorials/rest-api/. The example uploads a CityGML, yours would be a type “3D_MODEL” which allows you to pass a “position”.
The method I suggested is done at upload time, not at runtime. So you’ll need to run it as a NodeJS script locally (or use anything to call the REST API, like Python etc)
we did uploaded our models thorugh the REST API. Thanks for the tip!
However, we could not place them correctly. They changed coordinates after importing. The coordinates in Cesium Ion Editor are different than the one we defined during the importing process. And we did use EPSG:4326 CRS. Any ideas?
I had a look to the model and it seems all the vertices are in UTM coordinates. Cesium ion expects models to be in local coordinates (https://cesium.com/docs/tutorials/import-photogrammetry/). So you’ll need to regenerate the models using local coords or unprojecting those positions.
To get the model to appear at those UTM positions if you were using Cesiumjs you would need to convert them to lat/lon/height. And then, to position something directly in the globe:
Cartesian3.fromDegrees(lat, lon, height)
Hope it helps.
P.S: While reviewing this we noticed that using local coordinates is not explicitely said in the BIM/CAD import documentation (https://cesium.com/docs/tutorials/import-3d-models/), so thank you for raising this issue, as it helps to improve our documentation.
thanks for the help. Could you please show me how did you notice this? Where/what are those coordinates values from my model? I have been a hard time trying to track in my code (with strong 3rd-party dependency) how these values are set.
I just converted to OBJ to be able to inspect visually the value of each vertex. But you can do the same by exporting to gltf and check min/max for position accessor or export to other text formats. Also you could import in blender to check where are the vertex positions.
@leomiquelutti you would need to center your model geometry around the (0, 0, 0), or just have it be close to that origin. You can confirm this by opening your model in a tool like Blender and see that it is close to the origin.
You can then pass in the lat/lon/height of where that (0, 0, 0) point is on the globe with the REST API.
For example, if you had a building model that looked like this:
You would want to place it using the REST API at a height of 9 meters above from the ground height at that location for it to appear correctly on the ground.
However, we need to apply heading = -90 to get the right orientation in the Cesium Ion Editor.
We tried to rotate it with the following code:
var center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
var heading = -Cesium.Math.PI_OVER_TWO;
var pitch = 0.0;
var roll = 0.0;
var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
var transform = Cesium.Transforms.headingPitchRollToFixedFrame(center, hpr);
The model goes out of the globe. Any ideas? Could you please help us?
If the origin of the model is not in the geometry center then rotating it will move it.
Have you tried rotating it before uploading it to Cesium ion? It looks like the ion REST API only supports a position, but the other way to handle this would be if you could pass a rotation to apply before tiling (which would be the same as rotating the model from its geometry origin before uploading it).