using UnityEngine;
using UnityEngine.Rendering;
namespace jeanf.universalplayer
{
///
/// Picks the material authored for the ACTIVE render pipeline. Shader Graphs and
/// hand-written shaders are pipeline-specific (an HDRP graph is magenta under URP and
/// vice versa), so components that ship a look for both pipelines hold one material
/// per pipeline and resolve it here at runtime — no project-side wiring.
///
public static class PipelineMaterials
{
/// The material for the active pipeline: under HDRP, otherwise . Falls back to whichever is assigned.
public static Material Pick(Material urpOrBuiltIn, Material hdrp) => Pick(urpOrBuiltIn, hdrp, ActivePipelineTypeName());
/// Testable core: is the active RenderPipelineAsset's type name (null/empty = Built-in).
public static Material Pick(Material urpOrBuiltIn, Material hdrp, string pipelineTypeName)
{
var isHdrp = !string.IsNullOrEmpty(pipelineTypeName) && pipelineTypeName.Contains("HDRenderPipelineAsset");
var preferred = isHdrp ? hdrp : urpOrBuiltIn;
var other = isHdrp ? urpOrBuiltIn : hdrp;
return preferred != null ? preferred : other;
}
/// Type name of the pipeline asset actually rendering (quality override first, then the graphics default); null under Built-in.
public static string ActivePipelineTypeName()
{
var asset = GraphicsSettings.currentRenderPipeline != null ? GraphicsSettings.currentRenderPipeline : GraphicsSettings.defaultRenderPipeline;
return asset != null ? asset.GetType().ToString() : null;
}
/// Assigns to every slot of every renderer under (inactive included) and turns their shadows off.
public static void ApplyToRenderers(GameObject root, Material material)
{
if (root == null || material == null) return;
foreach (var renderer in root.GetComponentsInChildren(true))
{
var materials = new Material[Mathf.Max(1, renderer.sharedMaterials.Length)];
for (var i = 0; i < materials.Length; i++) materials[i] = material;
renderer.sharedMaterials = materials;
renderer.shadowCastingMode = ShadowCastingMode.Off;
}
}
}
}