/// <summary> /// This method was introduced as a workaround for too aggressive submesh draw call batching, /// leading to incorrect draw order when 3+ materials are used at submeshes in alternating order. /// Otherwise, e.g. when using Lightweight Render Pipeline, deliberately separated draw calls /// "A1 B A2" are reordered to "A1A2 B", regardless of batching-related project settings. /// </summary> private void SetMaterialSettingsToFixDrawOrder() { if (reusedPropertyBlock == null) { reusedPropertyBlock = new MaterialPropertyBlock(); } bool hasPerRendererBlock = meshRenderer.HasPropertyBlock(); if (hasPerRendererBlock) { meshRenderer.GetPropertyBlock(reusedPropertyBlock); } for (int i = 0; i < meshRenderer.sharedMaterials.Length; ++i) { if (!meshRenderer.sharedMaterials[i]) { continue; } if (!hasPerRendererBlock) { meshRenderer.GetPropertyBlock(reusedPropertyBlock, i); } // Note: this parameter shall not exist at any shader, then Unity will create separate // material instances (not in terms of memory cost or leakage). reusedPropertyBlock.SetFloat(SUBMESH_DUMMY_PARAM_ID, i); meshRenderer.SetPropertyBlock(reusedPropertyBlock, i); meshRenderer.sharedMaterials[i].enableInstancing = false; } }