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)
  • Use prefabs)

    Create a model from an asset

  • Create a script. -
    1. // Create a new entity and add it to the scene.var entity = new Entity();SceneSystem.SceneInstance.RootScene.Entities.Add(entity);// Add a model included in the game files.var modelComponent = entity.GetOrCreate<ModelComponent>();modelComponent.Model = Content.Load<Model>("MyFolder/MyModel");
    Tip
    Asset View, move the mouse over the model.
  • script component to any entity in the scene. It doesn’t matter which entity you use. For instructions, see Use a script.
  • Asset View, right-click the model you want to create at runtime and select Include in build as root asset. Manage assets.

    Create a procedural model

  • Create a script.
  • script component to any entity in the scene. It doesn’t matter which entity you use. For instructions, see Use a script. -
    1. // Create an entity and add it to the scene.var entity = new Entity();SceneSystem.SceneInstance.RootScene.Entities.Add(entity);// Create a model and assign it to the model component.var model = new Model();entity.GetOrCreate<ModelComponent>().Model = model;
    -
    1. // Add one or more meshes using geometric primitives (eg spheres or cubes).var meshDraw = GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw();var mesh = new Mesh { Draw = meshDraw }; model.Meshes.Add(mesh);
    Note
    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.mesh = new Mesh { Draw = new MeshDraw { /* Vertex buffer and index buffer setup */ } };model.Meshes.Add(mesh);
    Note
    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

  • 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.Material material = Content.Load<Material>("MyFolder/MyMaterial");model.Materials.Add(material);
  • Asset View, right-click every material asset your script uses and select Include in build as root asset. Manage assets.

    Option 2: Create new materials in code

    For example:
    1. // Create a material (eg with red diffuse color). var materialDescription = new MaterialDescriptor { Attributes = { DiffuseModel = new MaterialDiffuseLambertModelFeature(), Diffuse = new MaterialDiffuseMapFeature(new ComputeColor { Key = MaterialKeys.DiffuseValue }) } }; var material = Material.New(GraphicsDevice, materialDescription); material.Parameters.Set(MaterialKeys.DiffuseValue, Color.Red); model.Materials.Add(material);

    See also

  • Create a script
  • Use a script
  • Use prefabs