Model definition data structure

Modified on Thu, 02 Dec 2021 at 09:35 AM

A Model Definition is a scriptable object that contains a list of voxel definitions and colors that form some kind of building, tree or any other placeable structure.


Contains the following data:


- sizeX / sizeY / sizeZ: specify the size of the model in integer units (ie. 30x50x40)

- offsetX / offsetY / offsetZ: optional location offset when the model is placed (by default, 0,0,0)

- buildDuration: when placing the model, the duration of that operation. Voxels will be placed iteratively for that duration. A value of 0 will place the model instantly.

- exclusiveTree: when this boolean is true, no other tree will be placed on the same chunk after this model has been placed. This is useful if you have some "big" tree or construction that you don't want to be overwritten partially by other trees in the biome.

- fitToTerrain: when this boolean is true, the model base will be extended down filling any hole/gap until the terrain surface.

- bits: this is an array of ModelBit values which contains the actual voxel definitions, indices and colors (see below).

- torches: an array with optional torches that are included with the model.


A ModelBit is defined as:


[Serializable]
public struct ModelBit {
    public int voxelIndex;
    public VoxelDefinition voxelDefinition;
    public bool isEmpty;
    public Color32 color;
    public float rotation;
}

Notes:

  • The voxelIndex is the index of the entry in the model definition volume. The voxelindex is calculated using this formula: y * (sizeX * sizeZ) + z * sizeX + x.
  • The isEmpty bool specifies if this position should not be filled by any other voxel. This prevents the terrain generator from filling the interior of buildings with terrain voxels if the building is partially placed under the surface. You can also use the ModelFillInside(model) method to mark empty voxels inside a building model definition (this method locates the roof and iterates down, marking any entry that doesn't have a voxel definition as empty).
  • If one position in the Model Definition has  no ModelBit associated, that position won't be altered in the world when the model definition is placed.
  • The rotation field must be 0, 90, 180 or 270. This rotation will be applied to the side textures of the voxel only.



Creating Model Definitions at runtime


To create a Model Definition at runtime, just instantiate a new object of that type:


ModelDefinition myModel = ScriptableObject.CreateInstace<ModelDefinition>();

and now you can populate the above fields to complete your model.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article