#Adding rendering component from Baker

1 messages · Page 1 of 1 (latest)

topaz patio
#

I've been trying to bake a entity with the necessary redering component using RenderMeshUtility. I managed to make it work in the start() of a Monobehavior but I have been unsuccessful doing it at baking.
Here is the relevant part of my code so far:

public override void Bake(SpriteAuthoring authoring)
{
    Entity entity = GetEntity(TransformUsageFlags.Renderable);
    var world = World.DefaultGameObjectInjectionWorld;
    var entityManager = world.EntityManager;

    var spriteMeshAsset = authoring.spriteMeshAsset;
    var SpriteTexture = authoring.SpriteTexture;
    var spriteMaterial = authoring.spriteMaterial;

    var spriteMesh = new Mesh
    {
        vertices = (Vector3[])spriteMeshAsset.vertices.Clone(),
        triangles = (int[])spriteMeshAsset.triangles.Clone(),
        normals = (Vector3[])spriteMeshAsset.normals.Clone(),
        uv = (Vector2[])spriteMeshAsset.uv.Clone()
    };
    spriteMesh.RecalculateBounds();

    Material materialInstance = new Material(spriteMaterial);

    var renderMeshArray = new RenderMeshArray(
        new Material[] { materialInstance },
        new Mesh[] { spriteMesh }
    );

    var desc = new RenderMeshDescription(
        shadowCastingMode: ShadowCastingMode.Off,
        receiveShadows: false);

    RenderMeshUtility.AddComponents(
        entity,
        entityManager,
        desc,
        renderMeshArray,
        MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
    );
    entityManager.AddComponentData(entity, new LocalToWorld());

    entityManager.AddComponentData(entity, new LocalToWorld { Value = float4x4.Translate(float3.zero) });
    var spriteTransfrom = LocalTransform.FromPosition(0, 0, 0);
    spriteTransfrom.Rotation = Quaternion.Euler(0, 0, 0);
    entityManager.AddComponentData(entity, spriteTransfrom);
}
topaz patio
#

I guess you're not supposed to use RenderMeshUtility in a Baker and I coudn't manage to add all the component manually so I just solved this by using a Quad and a Mesh Renderer on the entity for conversion.

errant blaze
#

You're not supposed to use world or entity manager in a baker, because that's the wrong world

#

There should be a baking version of the api

#

Not home do can't check

celest dragon
#

if there is a version for bakers specifically it isnt mentioned in the manual. ive been using class baking components to store mesh and material info in bakers, and then using a baking system to use the RenderMeshUtility.AddComponents to add everything