using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using System.Linq;
using UnityEditor.ShaderGraph;
// Include material common properties names
using static UnityEngine.Rendering.HighDefinition.HDMaterialProperties;
namespace UnityEditor.Rendering.HighDefinition
{
///
/// The UI block that represents Transparency properties for materials.
///
public class TransparencyUIBlock : MaterialUIBlock
{
/// Transparency UI Block features. Use this to select which field you want to show.
[Flags]
public enum Features
{
/// Hides all the fields.
None = 0,
/// Displays the distortion fields.
Distortion = 1 << 0,
/// Displays the refraction fields.
Refraction = 1 << 1,
/// Displays all the fields.
All = ~0
}
internal class Styles
{
public const string header = "Transparency Inputs";
}
ExpandableBit m_ExpandableBit;
Features m_Features;
MaterialUIBlockList m_TransparencyBlocks;
///
/// Constructs a TransparencyUIBlock based on the parameters.
///
/// Bit index used to store the foldout state.
/// Features of the Transparency block.
public TransparencyUIBlock(ExpandableBit expandableBit, Features features = Features.All)
{
m_ExpandableBit = expandableBit;
m_Features = features;
m_TransparencyBlocks = new MaterialUIBlockList(parent);
if ((features & Features.Refraction) != 0)
m_TransparencyBlocks.Add(new RefractionUIBlock(1)); // This block will not be used in by a layered shader so we can safely set the layer count to 1
if ((features & Features.Distortion) != 0)
m_TransparencyBlocks.Add(new DistortionUIBlock());
}
///
/// Loads the material properties for the block.
///
public override void LoadMaterialProperties() {}
///
/// Renders the properties in the block.
///
public override void OnGUI()
{
// Disable the block if one of the materials is not transparent:
if (materials.Any(material => material.GetSurfaceType() != SurfaceType.Transparent))
return;
// If refraction model is not enabled in SG, we don't show the section
var shader = materials[0].shader;
if (shader.IsShaderGraph())
{
var defaultRefractionModel = shader.GetPropertyDefaultFloatValue(shader.FindPropertyIndex(kRefractionModel));
if (defaultRefractionModel == 0)
return;
}
using (var header = new MaterialHeaderScope(Styles.header, (uint)m_ExpandableBit, materialEditor))
{
if (header.expanded)
{
m_TransparencyBlocks.OnGUI(materialEditor, properties);
}
}
}
}
}