Hello!
I am migrating a project to DOTS. The project has a construction system that adds a translucent material with a blending animation from the original materials to a single preview material. Blending occurs through a custom simple shader that accepts two materials and outputs an intermediate material each frame. In monobehaviour mode, this shader works correctly.
When switching to DOTS, there is a problem with displaying parts of the mesh that overlap each other, that is, a problem with sorting in rendering when adding a translucent material. I am testing on the example of a simple house model that has several materials. The fence can be visible through the translucent roof in one frame, but disappear in the next, then reappear in the nex. When the animation ends, I apply a final translucent preview material to all entities, and when it is applied, all mesh elements are rendered. I do not understand why the problem occurs specifically when animating through a blending shader. Update: I changed the animation code a little, now during the entire animation the display of objects is constant without flickering, I just can’t see the fence through the translucent roof.
Below is the code where the blend shader material is configured. Also a screenshot with the shader settings. In the Unity examples I found a scene with translucent cubes, the project notes that entities should have the Unity.Rendering.DepthSorted_Tag component. I checked that the baked entities have this component, but just in case I still check in the code. The tag component is there, but the problem is not solved.
for (int i = 0; i < linkedEntities.Length; i++)
{
Entity childEntity = linkedEntities[i].Value;
if (EntityManager.HasComponent<RenderMeshArray>(childEntity))
{
var originalRenderMeshArray = EntityManager.GetSharedComponentManaged<RenderMeshArray>(childEntity);
var newMaterials = new Material[originalRenderMeshArray.Materials.Length];
for (int j = 0; j < newMaterials.Length; j++)
{
Material blendMaterial = new Material(previewComponent.PreviewBlendShaderRef.Value);
blendMaterial.SetColor("_BaseColor", originalRenderMeshArray.Materials[j].GetColor("_BaseColor"));
blendMaterial.SetFloat("_Smoothness", 0f);
blendMaterial.renderQueue = 3000;
blendMaterial.SetInt("_RenderQueueOffset", 50);
blendMaterial.SetColor("_PreviewColor", targetMaterial.color);
blendMaterial.SetFloat("_Blend", previewComponent.BlendValue);
newMaterials[j] = blendMaterial;
}
var newRenderMeshArray = new RenderMeshArray(newMaterials, originalRenderMeshArray.Meshes);
ecb.SetSharedComponentManaged(childEntity, newRenderMeshArray);
if (!EntityManager.HasComponent<Unity.Rendering.DepthSorted_Tag>(childEntity))
{
ecb.AddComponent<Unity.Rendering.DepthSorted_Tag>(childEntity);
Debug.Log($"Added DepthSorted_Tag to child entity {childEntity.Index} for blending");
}
}
}
