using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;
// Include material common properties names
using static UnityEngine.Rendering.HighDefinition.HDMaterialProperties;
namespace UnityEditor.Rendering.HighDefinition
{
// A Material can be authored from the shader graph or by hand. When written by hand we need to provide an inspector.
// Such a Material will share some properties between it various variant (shader graph variant or hand authored variant).
// HDShaderGUI is here to provide a support for setup material keyword and pass function. It will allow the GUI
// to setup the material properties needed for rendering when switching shaders on a material. For the GUI part
// of the material you must use Material UI Blocks, examples of doing so can be found in the classes UnlitGUI,
// LitGUI or LayeredLitGUI.
///
/// Use this class to build your custom Shader GUI for HDRP.
/// You can use a class that inherits from HDShaderGUI in the Shader Graph Custom EditorGUI field.
///
public abstract class HDShaderGUI : ShaderGUI
{
///
/// Sets up the keywords and passes for the material you pass in as a parameter.
///
/// Target material.
[Obsolete("SetupMaterialKeywordsAndPass has been renamed ValidateMaterial", false)]
protected virtual void SetupMaterialKeywordsAndPass(Material material)
{
ValidateMaterial(material);
}
///
/// Unity calls this function when it displays the GUI. This method is sealed so you cannot override it. To implement your custom GUI, use OnMaterialGUI instead.
///
/// Material editor instance.
/// The list of properties in the inspected material(s).
public sealed override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
{
if (!(RenderPipelineManager.currentPipeline is HDRenderPipeline))
{
CoreEditorUtils.DrawFixMeBox("Editing HDRP materials is only supported when an HDRP asset is assigned in the Graphics Settings", MessageType.Warning, "Open",
() => SettingsService.OpenProjectSettings("Project/Graphics"));
}
else
{
OnMaterialGUI(materialEditor, props);
}
}
///
/// Implement your custom GUI in this function. To display a UI similar to HDRP shaders, use a MaterialUIBlockList.
///
/// The current material editor.
/// The list of properties in the inspected material(s).
protected abstract void OnMaterialGUI(MaterialEditor materialEditor, MaterialProperty[] props);
///
/// Override the material preview GUI.
///
/// The current material editor.
/// Preview rect.
/// Style for the background.
public override void OnMaterialPreviewGUI(MaterialEditor materialEditor, Rect r, GUIStyle background)
{
using (ListPool.Get(out var overrides))
{
Material material = materialEditor.target as Material;
foreach (var nameID in HDMaterial.GetShaderDiffusionProfileProperties(material.shader))
{
if (!material.HasProperty(nameID))
continue;
var diffusionProfile = HDMaterial.GetDiffusionProfileAsset(material, nameID);
if (diffusionProfile != null)
overrides.Add(diffusionProfile);
if (overrides.Count >= DiffusionProfileConstants.DIFFUSION_PROFILE_COUNT - 1)
break;
}
EditorGraphicsSettings.TryGetRenderPipelineSettingsForPipeline(out var settings);
if (settings == null || settings.volumeProfile == null)
{
EditorGUI.HelpBox(r, $"The current {nameof(VolumeProfile)} is null, please assign one on Graphics Settings > HDRP", MessageType.Error);
}
else
{
var diffusionProfileList = VolumeUtils.GetOrCreateDiffusionProfileList(settings.volumeProfile);
var profiles = diffusionProfileList.ToArray();
diffusionProfileList.ReplaceWithArray(overrides.ToArray());
materialEditor.DefaultPreviewGUI(r, background);
diffusionProfileList.ReplaceWithArray(profiles);
}
}
}
///
/// Override the material interactive preview GUI.
///
/// The current material editor.
/// Preview rect.
/// Style for the background.
public override void OnMaterialInteractivePreviewGUI(MaterialEditor materialEditor, Rect r, GUIStyle background)
{
OnMaterialPreviewGUI(materialEditor, r, background);
}
}
}