How to Create a Model from Code
Models we use in our projects are usually pregenerated by 3D graphics software (like 3ds Max or Blender). However, some projects will demand that we create our own models (and meshes as well). This article exemplifies the creation of a new model by code.
Single mesh model
Note
The creation of custom meshes (With their vertex and index information) is covered in this article.
For a single mesh Model, Evergine offers an easy way creating it, just passing it as a parameter in its constructor:
public Model(string name, Mesh mesh)
Parameter | Description |
---|---|
name | Name of the new model. |
mesh | Mesh used for the new model. |
As a result a new Model will be generated with only one node, containing the mesh passed as parameter.
Sample Code:
var assetsService = Application.Current.Container.Resolve<AssetsService>();
// We can create a mesh
Mesh mesh = this.CreateMesh(this.graphicsContext);
// We simply pass the mesh as constructor parameter with the name.
Model model = new Model("CustomModel", mesh);
// We convert to an entity as a normal model.
Entity entity = model.InstantiateModelHierarchy("MyModel", assetsService);
this.Managers.EntityManager.Add(entity);
Model with multiple meshes
However, if we want to create more complex models (With multiple meshes, textures, nodes, etc.) we have to manually create the Model instance. In this case, we just use the base Model constructor and creates the hierarchy from zero.
Sample Code
The next code will create a model with two meshes within the same node. These meshes are the one created in the Meshes manual page
// Create two meshes (covered in Meshes article).
var mesh1 = this.CreateMesh1();
var mesh2 = this.CreateMesh2();
// Create MeshContainer, that
var meshContainer = new MeshContainer()
{
Name = "MultiplesMeshes",
Meshes = new List<Mesh>() { mesh1, mesh2 }
};
// Create RootNode, containing the previously defindes mesh container. It also has no children
var rootNode = new NodeContent()
{
Name = "MultiplesMeshes",
Mesh = meshContainer,
Children = new NodeContent[0],
ChildIndices = new int[0],
};
// Create material.
// In the Material section of this manual, you can find additional information on this topic.
var material = new StandardMaterial(assetsService.Load<Effect>(DefaultResourcesIDsStandardEffectID))
{
VertexColorEnabled = true, // Because the sample mesh uses color by vertex.
IBLEnabled = false, // It also doesn't have normal information, so any
LightingEnabled = false, // lighting calculations won't work.
};
// Create material List
var materialCollection = new List<(string, System.Guid)>()
{
("Default", material.Id), // We assign material's id as the default material.
};
// Finally we define the model with the nodes, material list, mesh containers and the indices of
// the root nodes (in this case only one).
var model = new Model()
{
MeshContainers = new[] { meshContainer },
Materials = materialCollection,
AllNodes = new[] { rootNode },
RootNodes = new[] { 0 },
};
// We convert to an entity as a normal model.
Entity entity = model.InstantiateModelHierarchy("MyModel", assetsService);
this.Managers.EntityManager.Add(entity);