- Home /
Unity3D Entity Component System Change RenderMesh Color Property
I have my entities initialized in my bootstrap class as follows:
EntityManager entityManager = World.Active.EntityManager;
EntityArchetype entityArchetype = entityManager.CreateArchetype(
typeof(PersonComponent),
typeof(Translation),
typeof(RenderMesh),
typeof(LocalToWorld),
typeof(Rotation),
typeof(Scale)
);
NativeArray<Entity> entityArray = new NativeArray<Entity>(persons.Count, Allocator.Temp);
entityManager.CreateEntity(entityArchetype, entityArray);
Now, I managed to access, change, set and edit all properties through either a job system or a component system, i.e. (Entities.ForEach(x => {..})) however, I also noticed that this seems to be possible only with components which aren't shared components. This is how I initialize my entities in the bootstrap script:
foreach (var entity in entityArray.Zip(persons, Tuple.Create))
{
entityManager.SetComponentData(entity.Item1, new PersonComponent {
UserId = entity.Item2.UserId
});
entityManager.SetComponentData(entity.Item1, new Translation {
Value = new float3(entity.Item2.UnityLocation.X, entity.Item2.UnityLocation.Y, entity.Item2.UnityLocation.Z)
});
entityManager.SetComponentData(entity.Item1, new Rotation {
Value = Quaternion.Euler(new Vector3(90, 0, 0))
});
entityManager.SetSharedComponentData(entity.Item1, new RenderMesh {
mesh = Bootstrap.Mesh_LOD1,
material = Bootstrap.Material_LOD1,
});
entityManager.SetComponentData(entity.Item1, new Scale {
Value = 0.5f
});
I have a requirement to change the color of the RenderMesh component for each entity dynamically and independently, however, I can't access the color property. This makes sense when I think about it, given that it is a "shared component" i.e. the same for all entities but how do I work around this then?
Example of how I intended to use it: 
Answer by unity_10D6FmlpqgbpAw · Sep 28, 2020 at 10:57 AM
Technically, you aren't changing the color of the mesh, you're changing the color of the material property in the mesh.
The other concern is that RenderMesh is likely going to be read-only when passed into your system, so you'll also likely have to create a new rendermesh while calling SharedComponentData on the current entity. This requires running the WithStructuralChanges() function on your ForEach, which is a performance hit.
Rather than doing this... you may want to consider passing in a _Color value to a shader, and have the shader do the work. I've ran into a situation like this, and the first half works up to about 500+ or so entities before becoming non-performant, and I'm researching the shader method next.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Must I attach every script to a gameobject in order to work ? 2 Answers
Sprite Fade In/Out Not Working 1 Answer
After compiling game lags 0 Answers