/// <summary> /// Clones a <see cref="ModelComponent"/>s <see cref="Material"/> if required; /// </summary> /// <param name="modelComponent"></param> /// <param name="materialIndex"></param> /// <returns></returns> /// <exception cref="ArgumentNullException">If <paramref name="modelComponent"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="materialIndex"/> is less than 0 or greater than <see cref="ModelComponent.GetMaterialCount"/> and not in <see cref="ModelComponent.Materials"/>.</exception> private static Material GetMaterialCopy(this ModelComponent modelComponent, int materialIndex) { if (modelComponent == null) { throw new ArgumentNullException(nameof(modelComponent)); } if (!IsValidMaterialIndex(modelComponent, materialIndex)) { throw new ArgumentOutOfRangeException(nameof(materialIndex)); } var material = modelComponent.GetMaterial(materialIndex); if (material is ModelComponentMaterialCopy copy && copy.ModelComponent == modelComponent) { return(material); } var materialCopy = new ModelComponentMaterialCopy() { ModelComponent = modelComponent, }; MaterialExtensions.CopyProperties(material, materialCopy); modelComponent.Materials[materialIndex] = materialCopy; return(materialCopy); }