您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

网格Meshes

网格资源Mesh resource

网格是不可变的 共享资源,只能通过 模型转换来创建。Meshes are an immutable shared resource, that can only be created through model conversion. 网格包含一个或多个 submeshes ,以及 raycast 查询的物理表示形式。Meshes contain one or multiple submeshes along with a physics representation for raycast queries. 每个子网格都引用一种默认情况下应呈现的 材料Each submesh references a material with which it should be rendered by default. 若要在三维空间中放置网格,请将 MeshComponent 添加到 实体To place a mesh in 3D space, add a MeshComponent to an Entity.

网格资源属性Mesh resource properties

Mesh类属性为:The Mesh class properties are:

  • 材料: 材料的数组。Materials: An array of materials. 每个材料由不同的子网格使用。Each material is used by a different submesh. 数组中的多个条目可能引用相同的 材料Multiple entries in the array may reference the same material. 此数据不能在运行时修改。This data cannot be modified at runtime.

  • 边界: 网格顶点 (AABB) 的本地空间轴对齐的边界框。Bounds: A local-space axis-aligned bounding box (AABB) of the mesh vertices.

MeshComponentMeshComponent

MeshComponent类用于放置网格资源的实例。The MeshComponent class is used to place an instance of a mesh resource. 每个 MeshComponent 都引用单个网格。Each MeshComponent references a single mesh. 它可以重写用于呈现每个子网格的材料。It may override which materials are used to render each submesh.

MeshComponent 属性MeshComponent properties

  • 网格: 此组件使用的网格资源。Mesh: The mesh resource that is used by this component.

  • 材料: 在网格组件上指定的材料的数组。Materials: The array of materials specified on the mesh component itself. 该数组的长度始终与网格资源上的 材料 数组的长度相同。The array will always have the same length as the Materials array on the mesh resource. 不应从网格默认值覆盖的材料在此数组中设置为 nullMaterials that shall not be overridden from the mesh default, are set to null in this array.

  • UsedMaterials: 每个子网格的实际使用材料的数组。UsedMaterials: The array of actually used materials for each submesh. 对于非 null 值,将与 材料 数组中的数据相同。Will be identical to the data in the Materials array, for non-null values. 否则,它包含来自网格实例中的 材料 阵列的值。Otherwise it contains the value from the Materials array in the mesh instance.

网格的共享Sharing of meshes

Mesh可以在网格组件的多个实例之间共享资源。A Mesh resource can be shared across multiple instances of mesh components. 此外, Mesh 分配给网格组件的资源可以随时以编程方式进行更改。Furthermore, the Mesh resource that is assigned to a mesh component can be changed programmatically at any time. 下面的代码演示如何克隆网格:The code below demonstrates how to clone a mesh:

Entity CloneEntityWithModel(RenderingConnection api, Entity sourceEntity)
{
    MeshComponent meshComp = sourceEntity.FindComponentOfType<MeshComponent>();
    if (meshComp != null)
    {
        Entity newEntity = api.CreateEntity();
        MeshComponent newMeshComp = api.CreateComponent(ObjectType.MeshComponent, newEntity) as MeshComponent;
        newMeshComp.Mesh = meshComp.Mesh; // share the mesh
        return newEntity;
    }
    return null;
}
ApiHandle<Entity> CloneEntityWithModel(ApiHandle<RenderingConnection> api, ApiHandle<Entity> sourceEntity)
{
    if (ApiHandle<MeshComponent> meshComp = sourceEntity->FindComponentOfType<MeshComponent>())
    {
        ApiHandle<Entity> newEntity = *api->CreateEntity();
        ApiHandle<MeshComponent> newMeshComp = api->CreateComponent(ObjectType::MeshComponent, newEntity)->as<RemoteRendering::MeshComponent>();
        newMeshComp->SetMesh(meshComp->GetMesh()); // share the mesh
        return newEntity;
    }
    return nullptr;
}

API 文档API documentation

后续步骤Next steps