Adding Models dynamically so that they sit within the polygon and scale to the size of polygon (Cesium JS)

I have a shapefile that contains the polygons of an area. Now i am want to add the 3D models that will reside within the polygon and scale to the size of the polygons. Right now i am showing the models on centeroids of the polygon, I have also tried the Cesium.Transforms.eastNorthUpToFixedFrame(center); but it is not getting the job done. I am fetching the centroids via a query to mysql database. Here is a snippet of code.

var passedArray =  <?php echo json_encode($cords); ?>;
for(var i = 0; i < passedArray.length; i++){

 var heading = Cesium.Math.toRadians(100.0);

 //pitch describes the up and down dive of the model
 var pitch = Cesium.Math.toRadians(0.0);

 //roll describes the rotation of the model on its own axis
 var roll = Cesium.Math.toRadians(0.0);

 //position describes the long,lat and the height of the model from the ground
 //xcenter and ycenter are the coordinates point taken from the table which were calculated via arcmap mysql centroid was giving a false centroid.
 var position = Cesium.Cartesian3.fromDegrees(passedArray[i]['xcenter'],
 passedArray[i]['ycenter'], 0.0);

//orientation combines all the aspects above to get the right direction and location of 3d object
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, new
Cesium.HeadingPitchRoll(heading, pitch, roll));

//adding the 3d model to the map.
//scale: the scale of the object 1.0 defines the original scale.
//glb files are mostly suited for the 3d models
 entity[i] = viewer.entities.add({
 position : position,
 orientation : orientation,
 model : {
    uri : '<?php echo base_url();?>Cesium/Apps/SampleData/models/newmodel/Bambo_House.glb',
    scale : 50.0



Here is the picture of how the models are represented right now.

I am using CesiumJS 1.6 as a javascript library within Codeigniter(PHP) framework.

Is there a better way to do this. which can get me the desired results.

Is the issue that the 3D models are visually offset from the center?

The issue could be that the centroid computed and passed here is incorrect. You could try computing this on the client. I would first visualze the 4 corners of the rectangle in CesiumJS, to make sure they’re in the right place, and then compute the average of these values and place the model there.

You can also try to place a point as a marker just to verify that the computed centroid is correct, and to rule out that the model geometry itself is offset.

Sorry for the long delay. My question here is incomplete i asked the same question here and on stackoverflow so i forgot to paste a crucial part here apologies for that. I want the model to fit perfectly to the size of each polygon (which are shown via geojson) is there a way to do that. As some polygons are larger than the others.

My main issue is that i want to do what has been done on the featurepicker newyork sandcastle example. But the area that i have doesnot have any building and is undeveloped land i wish to add and replicate models according to the category. e.g if the plot is residential add a small house if commercial add a big building. How can this be achieved

Secondly is there a way to append the properties of the polygon after it has been displayed via geojson. For example i am showing plots as polygon for which i have created a geojson of the geom which has properties like area, plot_no,street_no and lane_no. Now after adding the geojson to the cesium viewer i want to add another property to the polygon that is owner_name is there a way to do that.

You should be able to iterate over the entities created by the GeoJSON, both to figure out what model to place there, how to scale it, and also alter their properties as you describe there. There’s an example of this when you click the “custom styling” button in this Sandcastle:

What kind of application are you working on?