Create a model from code

BeginnerProgrammer

You can create models in scripts at runtime. You can do this in several different ways, including:

  • creating a model from an asset

  • creating a procedural model using built-in geometric primitives (eg a sphere or cube)

  • instantiating a prefab that contains a model (see Use prefabs)

Create a model from an asset

  • Create a new, empty synchronous script. For full instructions, see Create a script.

Create a script

  • In the script, load the model using its asset URL. For example:
  1. // Create a new entity and add it to the scene.
  2. var entity = new Entity();
  3. SceneSystem.SceneInstance.RootScene.Entities.Add(entity);
  4. // Add a model included in the game files.
  5. var modelComponent = entity.GetOrCreate<ModelComponent>();
  6. modelComponent.Model = Content.Load<Model>("MyFolder/MyModel");
Tip

To find the model's asset URL, in the Asset View, move the mouse over the model.(Get asset URL

  • Add the script as a script component to any entity in the scene. It doesn't matter which entity you use. For instructions, see Use a script.

Add script component to entity

  • In the Asset View, right-click the model you want to create at runtime and select Include in build as root asset.

Include in build as root asset

This makes sure the asset is available for the script to use at runtime. For more information, see Manage assets.

Create a procedural model

  • Create a new, empty synchronous script. For full instructions, see Create a script.

Add new script

  • Add the script as a script component to any entity in the scene. It doesn't matter which entity you use. For instructions, see Use a script.

Add script component to entity

  • In your script, instantiate an empty entity and an empty model. For example:
  1. // Create an entity and add it to the scene.
  2. var entity = new Entity();
  3. SceneSystem.SceneInstance.RootScene.Entities.Add(entity);
  4. // Create a model and assign it to the model component.
  5. var model = new Model();
  6. entity.GetOrCreate<ModelComponent>().Model = model;
  • In your script, create a procedural model using built-in geometric primitives (eg a sphere or cube). For example:
  1. // Add one or more meshes using geometric primitives (eg spheres or cubes).
  2. var meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();
  3. var mesh = new Mesh { Draw = meshDraw };
  4. model.Meshes.Add(mesh);
Note

To use the code above, make sure you add using Xenko.Extensions to the top of your script.

Alternatively, create a mesh using your own vertex and index buffers. For example:

  1. // Create a mesh using your own vertex and index buffers.
  2. mesh = new Mesh { Draw = new MeshDraw { /* Vertex buffer and index buffer setup */ } };
  3. model.Meshes.Add(mesh);
Note

For information about how to set up vertex and index buffers, see Drawing vertices.

Finally, you need to give the model one or more materials. There are two ways to do this.

Option 1: load a material in code

  • In your code, load one or more materials and add them to the model. Because models can use multiple materials (one for each mesh in the model), use Mesh.MaterialIndex to specify which materials in the list are used for which mesh.

    For example:

  1. // Add one or more materials. Because models might expect multiple materials (one per mesh), Mesh.MaterialIndex specifies which material in the list is used for which mesh.
  2. Material material = Content.Load<Material>("MyFolder/MyMaterial");
  3. model.Materials.Add(material);
  • In the Asset View, right-click every material asset your script uses and select Include in build as root asset.

Include in build as root asset

This makes sure the asset is available for the script to use at runtime. For more information, see Manage assets.

Option 2: Create new materials in code

For example:

  1. // Create a material (eg with red diffuse color).
  2. var materialDescription = new MaterialDescriptor
  3. {
  4. Attributes =
  5. {
  6. DiffuseModel = new MaterialDiffuseLambertModelFeature(),
  7. Diffuse = new MaterialDiffuseMapFeature(new ComputeColor { Key = MaterialKeys.DiffuseValue })
  8. }
  9. };
  10. var material = Material.New(GraphicsDevice, materialDescription);
  11. material.Parameters.Set(MaterialKeys.DiffuseValue, Color.Red);
  12. model.Materials.Add(material);

See also