thanks for the reply, I’m trying to do it in edit mode, because I want to see the results while editing.
I’ve changed my approach to use a BakingSystem but now the entities only render when the SubScene is closed.
When the SubScene is open, I can see that the entities and components are created, but they don’t render which is odd.
here is the general idea with the Grid authoring,
I use GridCell to designate which entities created by the baker need to be operated on further in the baking system (because I cannot access RenderMeshUtility.AddComponents in a Baker)
[TemporaryBakingType]
public struct GridCell : IComponentData
{
public float3 Value;
public UnityObjectRef<Mesh> mesh;
public UnityObjectRef<Material> material;
}
public class GridAuthoringBaker : Baker<GridAuthoring>
{
public override void Bake(GridAuthoring authoring)
{
var aabb = authoring.aabb;
var entity = GetEntity(authoring, TransformUsageFlags.WorldSpace);
// was trying to pass this in.. but cannot instantiate during Baking due to duplicate GUID issue
//var cellPrefab = GetEntity(authoring.prefab, TransformUsageFlags.Renderable);
var grid = new Grid { aabb = aabb, dimension = (int3)(aabb.Max - aabb.Min) };
AddComponent(entity, grid );
// create baking entities... ????
for (int i = 0; i < grid.Length; i++)
{
var e = CreateAdditionalEntity(TransformUsageFlags.WorldSpace, false, $"{authoring.name}_{i.ToString()}");
var pos = Util.ConvertIndexToWorldPosition(i, grid.dimension, grid.aabb);
Debug.DrawRay(pos,Vector3.up,Color.cyan,1f);
AddComponent(e, new GridCell { Value = pos, mesh = authoring.mesh, material = authoring.material});
}
}
}
And here is the baking system:
[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
public partial struct GridBakeSystem : ISystem
{
private EntityQuery _gridCellQuery;
public void OnCreate(ref SystemState state)
{
_gridCellQuery = new EntityQueryBuilder(Allocator.Temp)
.WithAll<GridCell>()
.WithOptions(EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabledEntities)
.Build(ref state);
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var cells = _gridCellQuery.ToComponentDataArray<GridCell>(Allocator.TempJob);
var entities = _gridCellQuery.ToEntityArray(Allocator.TempJob);
for (var i = 0; i < cells.Length; i++)
{
var entity = entities[i];
//var entity = state.EntityManager.CreateEntity();
var cell = cells[i];
var desc = new RenderMeshDescription(shadowCastingMode: ShadowCastingMode.Off, receiveShadows: false);
var renderMeshArray = new RenderMeshArray(new [] { cell.material.Value }, new [] { cell.mesh.Value });
RenderMeshUtility.AddComponents(
entity,
state.EntityManager,
desc,
renderMeshArray,
MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0));
state.EntityManager.AddComponentData(entity, new LocalToWorld{Value = float4x4.TRS(cell.Value,quaternion.identity,new float3(1f,1f,1f) * Util.CELL_RADIUS)});
state.EntityManager.AddComponentData(entity, new CellInfluence{delta = 1f,value = 0f});
state.EntityManager.AddBuffer<CellNeighbors>(entity);
state.EntityManager.AddBuffer<PropagationValue>(entity);
}
entities.Dispose(state.Dependency);
cells.Dispose(state.Dependency);
}
}