initial commit

This commit is contained in:
Jo 2025-01-07 02:06:59 +01:00
parent 6715289efe
commit 788c3389af
37645 changed files with 2526849 additions and 80 deletions

View file

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public interface BaseMaterialField
{
public int GetShaderPropIdx();
public abstract void UpdateMaterialProperty(MaterialProperty boundProp);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: da9d9cf7353fd9e42b955b549e395b8b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,133 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class EmissionToggleField : Toggle, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
bool isIntField = false;
string keyword;
public delegate void BeforeChangeEvent(ChangeEvent<bool> evt);
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx, string keyword, bool isIntField, bool noStyle = false)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.isIntField = isIntField;
this.keyword = keyword;
this.RegisterValueChangedCallback(OnChangedEvent);
bool state = false;
if (isIntField)
{
state = materialProperty.intValue != 0 ? true : false;
}
else
{
state = materialProperty.floatValue > 0 ? true : false;
}
this.SetValueWithoutNotify(state);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
this.showMixedValue = true;
}
if (!noStyle)
{
label = materialProperty.displayName;
SetFullLineStyle();
}
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<bool> evt)
{
if (isIntField)
{
materialProperty.intValue = evt.newValue ? 1 : 0;
}
else
{
materialProperty.floatValue = evt.newValue ? 1 : 0;
}
Object[] targets = materialProperty.targets;
int numMats = targets.Length;
// Setting the value through the materialProperty already recorded an undo, append to that
Undo.RecordObjects(targets, Undo.GetCurrentGroupName());
SetKeywordOnTargets(evt.newValue);
SetGIFlagsOnTargets(evt.newValue);
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
Undo.IncrementCurrentGroup();
this.showMixedValue = false;
}
void SetKeywordOnTargets(bool value)
{
if (keyword != null)
{
Object[] materials = materialProperty.targets;
int numMaterials = materials.Length;
Shader s = (materials[0] as Material).shader;
LocalKeyword kw = new LocalKeyword(s, keyword);
for (int i = 0; i < numMaterials; i++)
{
(materials[0] as Material).SetKeyword(kw, value);
}
}
}
void SetGIFlagsOnTargets(bool state)
{
Object[] materials = materialProperty.targets;
int numMaterials = materials.Length;
for (int i = 0; i < numMaterials; i++)
{
(materials[0] as Material).globalIlluminationFlags = state ? MaterialGlobalIlluminationFlags.BakedEmissive : MaterialGlobalIlluminationFlags.EmissiveIsBlack;
}
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
bool state = false;
if (isIntField)
{
state = materialProperty.intValue != 0 ? true : false;
}
else
{
state = materialProperty.floatValue > 0 ? true : false;
}
if (value != state)
{
this.SetValueWithoutNotify(state);
}
this.showMixedValue = materialProperty.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 08421d46ab7f7614eadd68e4e7aca77c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using static UnityEngine.Rendering.DebugUI.MessageBox;
namespace UnityEditor.UIElements
{
public class GIFlagsPopup : PopupField<int>
{
static Dictionary<int, string> flagLabels = new Dictionary<int, string>()
{
{0, "None" },
{1, "Realtime" },
{2, "Baked" },
};
public GIFlagsPopup(SerializedObject serializedMaterial)
{
List<int> flags = new List<int>() { 0, 1, 2 };
this.choices = flags;
this.index = 2;
this.formatSelectedValueCallback = GetCurrentFlagName;
this.formatListItemCallback = GetValidFlagName;
this.bindingPath = "m_LightmapFlags";
this.label = "Global Illumination";
VisualElement label = ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement dropdown = ElementAt(1);
dropdown.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
style.marginRight = 3;
}
static string GetCurrentFlagName(int flag)
{
string label;
if (flagLabels.TryGetValue(flag, out label))
{
return label;
}
else
{
return "-";
}
}
static string GetValidFlagName(int flag)
{
return flagLabels[flag];
}
class RenderQueueIntField : IntegerField
{
public int defaultQueue = 2000;
protected override string ValueToString(int v)
{
if (v == -1)
{
return defaultQueue.ToString();
}
return v.ToString(base.formatString, CultureInfo.InvariantCulture.NumberFormat);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6a738a0b3f474af46bd9cf980c7b0627
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,65 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SLZMaterialUI;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialColorField : ColorField, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx, bool isPartOfTexField)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify(materialProperty.colorValue);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
if (isPartOfTexField)
{
style.alignSelf = Align.Center;
style.flexGrow = 1;
style.flexShrink = 1;
}
else
{
label = materialProperty.displayName;
SetFullLineStyle();
}
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<Color> evt)
{
materialProperty.colorValue = evt.newValue;
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
if (value != boundProp.colorValue)
{
this.SetValueWithoutNotify(boundProp.colorValue);
}
this.showMixedValue = boundProp.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b97e041999f2d4a43ad1f83584415ce1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialDummyField : INotifyValueChanged<float>, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
float m_value;
public float value
{
get { return m_value; }
set {
if (m_value != value)
{
m_value = value;
materialProperty.floatValue = m_value;
}
}
}
public MaterialDummyField(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
SetValueWithoutNotify(materialProperty.floatValue);
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.floatValue)
{
this.SetValueWithoutNotify(boundProp.floatValue);
}
}
public void SetValueWithoutNotify(float value)
{
m_value = value;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a5fccb07f24f1146b93d089e493e007
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,92 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialEmissionFlagsField : VisualElement
{
static EnumFieldUtils.EnumData s_EmissionFlags;
static EnumFieldUtils.EnumData emissionFlags
{
get
{
if (s_EmissionFlags == null)
{
s_EmissionFlags = new EnumFieldUtils.EnumData()
{
values = new Enum[] { MaterialGlobalIlluminationFlags.EmissiveIsBlack, MaterialGlobalIlluminationFlags.RealtimeEmissive, MaterialGlobalIlluminationFlags.BakedEmissive },
flagValues = new int[] { 0, 1, 2 },
displayNames = new string[] { "None", "Realtime", "Baked" },
names = new string[] { "EmissiveIsBlack", "RealtimeEmissive", "BakedEmissive" },
tooltip = new string[] { "Emission will not emit light during light baking", "Emission will be treated as a real-time source when baking realtime GI", "Emission will emit light when baking lighting" },
flags = true,
underlyingType = typeof(MaterialGlobalIlluminationFlags),
unsigned = false,
serializable = true,
};
}
return s_EmissionFlags;
}
}
static object s_BoxedEnumData;
static object boxedEnumData
{
get
{
if (s_BoxedEnumData == null)
{
s_BoxedEnumData = EnumFieldUtils.GetBoxedEnumData(emissionFlags);
}
return s_BoxedEnumData;
}
}
static Action<EnumField, Enum> s_UpdateValueLabelAction;
static Action<EnumField, Enum> UpdateValueLabelAction
{
get
{
if (s_UpdateValueLabelAction == null)
{
MethodInfo updateValueInfo = typeof(EnumField).GetMethod("UpdateValueLabel", BindingFlags.Instance | BindingFlags.NonPublic);
s_UpdateValueLabelAction = (Action<EnumField, Enum>) updateValueInfo.CreateDelegate(typeof(Action<EnumField, Enum>));
}
return s_UpdateValueLabelAction;
}
}
public EnumField internalField;
public MaterialEmissionFlagsField(string label, MaterialGlobalIlluminationFlags defaultValue)
{
internalField = new EnumField(label, defaultValue);
SetEnumData(internalField);
EnumFieldUtils.UpdateValueLabelAction(internalField, defaultValue);
VisualElement labelElement = internalField.ElementAt(0);
labelElement.AddToClassList("materialGUILeftBox");
labelElement.style.overflow = Overflow.Hidden;
labelElement.style.minWidth = 0;
VisualElement dropdownElement = internalField.ElementAt(1);
dropdownElement.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
Add(internalField);
}
public void SetEnumData(EnumField field)
{
EnumFieldUtils.enumDataField.SetValue(field, boxedEnumData);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1d6b65ddfa2dcdb459c9ebc448986435
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,55 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialFloatField : FloatField, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify(materialProperty.floatValue);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
label = materialProperty.displayName;
SetFullLineStyle();
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<float> evt)
{
materialProperty.floatValue = evt.newValue;
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.floatValue)
{
this.SetValueWithoutNotify(boundProp.floatValue);
}
this.showMixedValue = boundProp.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8d3da44cdff624d4ebb0c6b43ad59f8a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,55 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialIntField : IntegerField, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify(materialProperty.intValue);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
label = materialProperty.displayName;
SetFullLineStyle();
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<int> evt)
{
materialProperty.intValue = evt.newValue;
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.intValue)
{
this.SetValueWithoutNotify(boundProp.intValue);
}
this.showMixedValue = boundProp.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7fa327141ed044147b9a2efea174b00c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,82 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialIntPopup : PopupField<int>, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
Dictionary<int, string> choiceLabels;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx, List<int> choices, Dictionary<int, string> choiceLabels)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.choices = choices;
this.choiceLabels = choiceLabels;
this.formatSelectedValueCallback = GetCurrentFlagName;
this.formatListItemCallback = GetValidFlagName;
VisualElement label = ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement dropdown = ElementAt(1);
dropdown.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
style.marginRight = 3;
RegisterCallback<ChangeEvent<int>>(evt =>
{
materialProperty.floatValue = (float)evt.newValue;
}
);
this.SetValueWithoutNotify((int)materialProperty.floatValue);
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
int newVal = (int)boundProp.floatValue;
if (this.value != newVal)
{
this.SetValueWithoutNotify(newVal);
}
this.showMixedValue = boundProp.hasMixedValue;
}
string GetCurrentFlagName(int type)
{
string label;
if (choiceLabels.TryGetValue(type, out label))
{
return label;
}
else
{
return "-";
}
}
string GetValidFlagName(int type)
{
return choiceLabels[type];
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a265e5d4ddf6f44c9f8c719875f1428
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialIntRangeField : SliderInt, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify((int)materialProperty.floatValue);
this.lowValue = (int)materialProperty.rangeLimits.x;
this.highValue = (int)materialProperty.rangeLimits.y;
this.showInputField = true;
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
label = materialProperty.displayName;
SetFullLineStyle();
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<int> evt)
{
materialProperty.floatValue = evt.newValue; // Int ranges are still floats...
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.floatValue)
{
this.SetValueWithoutNotify((int)boundProp.floatValue);
this.lowValue = (int)boundProp.rangeLimits.x;
this.highValue = (int)boundProp.rangeLimits.y;
}
this.showMixedValue = boundProp.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d75b03261072c5d458af914c4ed8d432
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialRangeField : Slider, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify(materialProperty.floatValue);
this.lowValue = materialProperty.rangeLimits.x;
this.highValue = materialProperty.rangeLimits.y;
this.showInputField = true;
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
label = materialProperty.displayName;
SetFullLineStyle();
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<float> evt)
{
materialProperty.floatValue = evt.newValue;
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.floatValue)
{
this.SetValueWithoutNotify(boundProp.floatValue);
this.lowValue = boundProp.rangeLimits.x;
this.highValue = boundProp.rangeLimits.y;
}
this.showMixedValue = boundProp.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8774bd78aca1ca1438a7328c4e7691a0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SLZMaterialUI;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialScaleOffsetField : VisualElement, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
Vector2Field tilingField;
Vector2Field offsetField;
public Vector4 value;
public MaterialScaleOffsetField(MaterialProperty boundProp, int shaderPropertyIdx)
{
this.materialProperty = boundProp;
this.shaderPropertyIdx = shaderPropertyIdx;
value = boundProp.textureScaleAndOffset;
tilingField = new Vector2Field();
tilingField.style.marginRight = 4;
tilingField.label = "Tiling";
VisualElement tilingLabel = tilingField.ElementAt(0);
VisualElement tilingInput = tilingField.ElementAt(1);
tilingInput.RemoveAt(2);
tilingLabel.AddToClassList("materialGUILeftBox");
tilingInput.AddToClassList("materialGUIRightBox");
tilingField.SetValueWithoutNotify(new Vector2(value.x, value.y));
tilingField.RegisterValueChangedCallback(OnChangedEventTiling);
offsetField = new Vector2Field();
offsetField.style.marginRight = 4;
offsetField.label = "Offset";
VisualElement offsetLabel = offsetField.ElementAt(0);
VisualElement offsetInput = offsetField.ElementAt(1);
offsetInput.RemoveAt(2);
offsetLabel.AddToClassList("materialGUILeftBox");
offsetInput.AddToClassList("materialGUIRightBox");
offsetField.SetValueWithoutNotify(new Vector2(value.z, value.w));
offsetField.RegisterValueChangedCallback(OnChangedEventOffset);
Add(tilingField);
Add(offsetField);
}
public void OnChangedEventTiling(ChangeEvent<Vector2> evt)
{
Vector4 scaleOffset = new Vector4(evt.newValue.x, evt.newValue.y, value.z, value.w);
value = scaleOffset;
materialProperty.textureScaleAndOffset = scaleOffset;
tilingField.showMixedValue = false;
}
public void OnChangedEventOffset(ChangeEvent<Vector2> evt)
{
Vector4 scaleOffset = new Vector4(value.x, value.y, evt.newValue.x, evt.newValue.y);
value = scaleOffset;
materialProperty.textureScaleAndOffset = scaleOffset;
tilingField.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (value != boundProp.textureScaleAndOffset)
{
value = boundProp.textureScaleAndOffset;
Vector2 scale = new Vector2(value.x, value.y);
Vector2 offset = new Vector2(value.z, value.w);
tilingField.SetValueWithoutNotify(scale);
offsetField.SetValueWithoutNotify(offset);
}
tilingField.showMixedValue = materialProperty.hasMixedValue;
offsetField.showMixedValue = materialProperty.hasMixedValue;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f7b4173acb2fca4999ffa4a8b1ad615
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,125 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialToggleField : Toggle, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
bool isIntField = false;
string keyword;
public delegate void BeforeChangeEvent(ChangeEvent<bool> evt);
//public BeforeChangeEvent BeforeChange;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx, string keyword, bool isIntField, bool noStyle = false)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.isIntField = isIntField;
this.keyword = keyword;
this.RegisterValueChangedCallback(OnChangedEvent);
bool state = false;
if (isIntField)
{
state = materialProperty.intValue != 0 ? true : false;
}
else
{
state = materialProperty.floatValue > 0 ? true : false;
}
this.SetValueWithoutNotify(state);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
this.showMixedValue = true;
}
if (!noStyle)
{
label = materialProperty.displayName;
SetFullLineStyle();
}
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<bool> evt)
{
//BeforeChange.Invoke(evt);
if (isIntField)
{
materialProperty.intValue = evt.newValue ? 1 : 0;
}
else
{
materialProperty.floatValue = evt.newValue ? 1 : 0;
}
if (keyword != null)
{
Object[] targets = materialProperty.targets;
int numMats = targets.Length;
// Setting the value through the materialProperty already recorded an undo, append to that
Undo.RecordObjects(targets, Undo.GetCurrentGroupName());
}
SetKeywordOnTargets(evt.newValue);
if (keyword != null)
{
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
Undo.IncrementCurrentGroup();
}
this.showMixedValue = false;
}
void SetKeywordOnTargets(bool value)
{
if (keyword != null)
{
Object[] materials = materialProperty.targets;
int numMaterials = materials.Length;
Shader s = (materials[0] as Material).shader;
LocalKeyword kw = new LocalKeyword(s, keyword);
for (int i = 0; i < numMaterials; i++)
{
(materials[0] as Material).SetKeyword(kw, value);
}
}
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
bool state = false;
if (isIntField)
{
state = materialProperty.intValue != 0 ? true : false;
}
else
{
state = materialProperty.floatValue > 0 ? true : false;
}
if (this.value != state)
{
this.SetValueWithoutNotify(state);
}
this.showMixedValue = materialProperty.hasMixedValue;
//MarkDirtyRepaint();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fbe8648b2f113f641a3c1a49f43dcfcd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class MaterialVectorField : Vector4Field, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx)
{
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.RegisterValueChangedCallback(OnChangedEvent);
this.SetValueWithoutNotify(materialProperty.vectorValue);
style.marginRight = 3;
if (materialProperty.hasMixedValue)
{
//this.SetValueWithoutNotify(Color.gray);
this.showMixedValue = true;
}
label = materialProperty.displayName;
SetFullLineStyle();
}
public void SetFullLineStyle()
{
VisualElement label = this.ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement color = this.ElementAt(1);
color.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
}
public void OnChangedEvent(ChangeEvent<Vector4> evt)
{
materialProperty.vectorValue = evt.newValue;
this.showMixedValue = false;
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
if (this.value != boundProp.vectorValue)
{
this.SetValueWithoutNotify(boundProp.vectorValue);
}
this.showMixedValue = boundProp.hasMixedValue;
//MarkDirtyRepaint();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ba3f18a26aea39a48851c4f24b173f72
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,125 @@
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace UnityEditor.UIElements
{
public class RenderQueueDropdown : VisualElement, INotifyValueChanged<int>
{
public PopupField<int> renderQueuePresets;
RenderQueueIntField renderQueueCustom;
int m_value;
public int value
{
get { return m_value; }
set {
if (value == defaultRenderQueue)
SetValue(-1);
else
SetValue(value);
}
}
int defaultRenderQueue;
Dictionary<int, string> queueLabels = new Dictionary<int, string>()
{
{-1, " From Shader " },
{1000, " Background " },
{2000, " Geometry " },
{2450, " Alpha Test " },
{3000, " Transparent " }
};
public RenderQueueDropdown(SerializedObject serializedMaterial, Shader shader)
{
this.style.flexDirection = FlexDirection.Row;
//this.style.
this.style.alignSelf = Align.Stretch;
this.style.alignItems = Align.Stretch;
this.style.flexWrap = Wrap.NoWrap;
this.style.justifyContent = Justify.SpaceBetween;
this.defaultRenderQueue = shader.renderQueue;
this.AddToClassList("unity-base-field");
List<int> queues = new List<int>() { -1, 1000, 2000, 2450, 3000 };
//SerializedProperty renderQueueProp = serializedMaterial.FindProperty("m_CustomRenderQueue");
renderQueuePresets = new PopupField<int>(queues, -1, GetCurrentQueueName, GetValidQueueName);
//renderQueuePresets.style.minWidth = 9 * 12;
renderQueuePresets.bindingPath = "m_CustomRenderQueue";
//renderQueuePresets.Bind(serializedMaterial);
renderQueuePresets.style.width = 100;
renderQueueCustom = new RenderQueueIntField();
renderQueueCustom.defaultQueue = shader.renderQueue;
renderQueueCustom.style.width = 48;
renderQueueCustom.bindingPath = "m_CustomRenderQueue";
//renderQueueCustom.Bind(serializedMaterial);
renderQueueCustom.style.unityTextAlign = TextAnchor.MiddleRight;
VisualElement renderQueueGroup = new VisualElement();
renderQueueGroup.style.flexDirection = FlexDirection.Row;
renderQueueGroup.style.flexShrink = 0;
renderQueueGroup.style.marginRight = 4;
renderQueueGroup.Add(renderQueuePresets);
renderQueueGroup.Add(renderQueueCustom);
Label label = new Label("Render Queue");
label.style.alignSelf = Align.Center;
label.style.textOverflow = TextOverflow.Ellipsis;
label.style.flexShrink = 1;
Add(label);
Add(renderQueueGroup);
}
string GetCurrentQueueName(int queue)
{
string label;
if (queueLabels.TryGetValue(queue, out label))
{
return label;
}
else
{
return "Custom";
}
}
string GetValidQueueName(int queue)
{
return queueLabels[queue];
}
class RenderQueueIntField : IntegerField
{
public int defaultQueue = 2000;
protected override string ValueToString(int v)
{
if (v == -1)
{
return defaultQueue.ToString();
}
return v.ToString(base.formatString, CultureInfo.InvariantCulture.NumberFormat);
}
}
public void SetValue(int value)
{
if (value == defaultRenderQueue) value = -1;
m_value = value;
renderQueuePresets.value = value;
renderQueueCustom.SetValueWithoutNotify(value);
}
public void SetValueWithoutNotify(int value)
{
if (value == defaultRenderQueue) value = -1;
m_value = value;
renderQueuePresets.SetValueWithoutNotify(value);
renderQueueCustom.SetValueWithoutNotify(value);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a93dcfdc27a47f54c8409794026ed59a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,138 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.SLZMaterialUI
{
public class SurfaceTypeField : PopupField<int>, BaseMaterialField
{
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
public MaterialProperty materialProperty;
MaterialProperty MaterialProperty { get { return materialProperty; } }
public INotifyValueChanged<float> blendSrc;
public INotifyValueChanged<float> blendDst;
public INotifyValueChanged<float> zWrite;
public INotifyValueChanged<int> renderQueue;
enum SurfaceTypes
{
Opaque = 0,
Transparent = 1,
Fade = 2,
}
static Dictionary<int, string> surfaceLabels = new Dictionary<int, string>()
{
{0, "Opaque" },
{1, "Transparent" },
{2, "Fade" },
};
static Span<int> SurfaceQueue => new int[] { 2000, 3000, 3000 };
static Span<int> SurfaceBlendSrc => new int[] { (int)BlendMode.One, (int)BlendMode.One, (int)BlendMode.SrcAlpha };
static Span<int> SurfaceBlendDst => new int[] { (int)BlendMode.Zero, (int)BlendMode.OneMinusSrcAlpha, (int)BlendMode.OneMinusSrcAlpha };
static Span<int> SurfaceZWrite => new int[] { 1, 0, 0 };
public void Initialize(MaterialProperty materialProperty, int shaderPropertyIdx,
INotifyValueChanged<float> blendSrc,
INotifyValueChanged<float> blendDst,
INotifyValueChanged<float> zWrite,
INotifyValueChanged<int> queueField)
{
this.formatSelectedValueCallback = GetCurrentFlagName;
this.formatListItemCallback = GetValidFlagName;
this.materialProperty = materialProperty;
this.shaderPropertyIdx = shaderPropertyIdx;
this.blendSrc = blendSrc;
this.blendDst = blendDst;
this.zWrite = zWrite;
this.renderQueue = queueField;
this.label = "Surface Type";
this.choices = new List<int>() { 0, 1, 2 };
VisualElement label = ElementAt(0);
label.AddToClassList("materialGUILeftBox");
label.style.overflow = Overflow.Hidden;
label.style.minWidth = 0;
VisualElement dropdown = ElementAt(1);
dropdown.AddToClassList("materialGUIRightBox");
style.justifyContent = Justify.FlexStart;
style.marginRight = 3;
RegisterCallback<ChangeEvent<int>>(evt =>
{
int newVal = evt.newValue;
Debug.Log("Trying to set surface to " + newVal);
UnityEngine.Object[] targets = MaterialProperty.targets;
//MaterialProperty.floatValue = newVal;
int numTargets = targets.Length;
Undo.IncrementCurrentGroup();
Undo.RecordObjects(targets, "Set Surface Type");
Shader s = ((Material)targets[0]).shader;
int surfIdx = s.FindPropertyIndex("_Surface");
int blendSrcIdx = s.FindPropertyIndex("_BlendSrc");
int blendDstIdx = s.FindPropertyIndex("_BlendDst");
int zWriteIdx = s.FindPropertyIndex("_ZWrite");
Debug.Log(string.Format("Num Targets: {4}, _Surface: {0}, _BlendSrc:{1}, _BlendDst:{2}, _ZWrite:{3} ", surfIdx, blendSrcIdx, blendDstIdx, zWriteIdx, numTargets));
int queue = SurfaceQueue[newVal];
queue = queue == s.renderQueue ? -1 : queue;
for (int i = 0; i < numTargets; i++)
{
Material mat = (Material)targets[i];
mat.SetFloat("_Surface", newVal);
mat.SetFloat("_BlendSrc", SurfaceBlendSrc[newVal]);
mat.SetFloat("_BlendDst", SurfaceBlendDst[newVal]);
mat.SetFloat("_ZWrite", SurfaceZWrite[newVal]);
mat.renderQueue = queue;
}
queueField.SetValueWithoutNotify(SurfaceQueue[newVal]);
Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
}
);
this.SetValueWithoutNotify((int)materialProperty.floatValue);
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
materialProperty = boundProp;
int newVal = (int)boundProp.floatValue;
if (this.value != newVal)
{
this.SetValueWithoutNotify(newVal);
}
this.showMixedValue = boundProp.hasMixedValue;
}
static string GetCurrentFlagName(int type)
{
string label;
if (surfaceLabels.TryGetValue(type, out label))
{
return label;
}
else
{
return "-";
}
}
static string GetValidFlagName(int type)
{
return surfaceLabels[type];
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6e24e45408716b14ca04c507eff19c04
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,365 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using static UnityEngine.UI.InputField;
using Object = UnityEngine.Object;
using System;
using SLZ.SLZEditorTools;
using UnityEngine.Rendering;
namespace UnityEditor.SLZMaterialUI
{
public class TextureField : VisualElement, BaseMaterialField
{
public MaterialProperty textureProperty;
public VisualElement leftAlignBox { get; private set; }
public VisualElement rightAlignBox { get; private set; }
public string tooltip2 { get { return texObjField.tooltip; } set { texObjField.tooltip = value; } }
public Texture defaultTexture;
public UnityEditor.UIElements.ObjectField texObjField;
Texture currentValue;
Image thumbnail;
bool isNormalMap;
RenderTexture thumbnailRT;
UnityEngine.Rendering.TextureDimension textureType;
public int shaderPropertyIdx;
public int GetShaderPropIdx() { return shaderPropertyIdx; }
static Action<ObjectField> s_updateObjDelegate;
static Action<ObjectField> UpdateObjDelegate
{
get {
if (s_updateObjDelegate == null)
{
MethodInfo m = typeof(UnityEditor.UIElements.ObjectField).GetMethod("UpdateDisplay", BindingFlags.Instance | BindingFlags.NonPublic);
if (m == null)
{
Debug.LogError("Missing UpdateDisplay method");
}
s_updateObjDelegate = (Action<ObjectField>)m.CreateDelegate(typeof(Action<ObjectField>));
}
return s_updateObjDelegate;
}
}
public TextureField(MaterialProperty textureProperty, int texturePropertyIdx, bool isNormalMap, Texture defaultTexture = null)
{
this.textureProperty = textureProperty;
this.currentValue = textureProperty.textureValue;
this.shaderPropertyIdx = texturePropertyIdx;
this.isNormalMap = isNormalMap;
this.defaultTexture = defaultTexture;
RegisterCallback<DetachFromPanelEvent>(evt => Dispose());
leftAlignBox = new VisualElement();
leftAlignBox.AddToClassList("materialGUILeftBox");
rightAlignBox = new VisualElement();
rightAlignBox.AddToClassList("materialGUIRightBox");
style.flexDirection = FlexDirection.Row;
texObjField = new UnityEditor.UIElements.ObjectField();
//List<SearchProvider> providers = new List<SearchProvider>() { new SearchProvider("sfklahlkjhsa", "hello") };
//textureField.searchContext = new SearchContext(providers, "t:Texture2D", SearchFlags.Default);
//textureField.bindingPath = "m_SavedProperties.m_TexEnvs.Array.data[0].second.m_Texture";
textureType = textureProperty.textureDimension;
switch (textureProperty.textureDimension)
{
case (UnityEngine.Rendering.TextureDimension.Tex2D):
texObjField.objectType = typeof(Texture2D);
break;
case (UnityEngine.Rendering.TextureDimension.Tex3D):
texObjField.objectType = typeof(Texture3D);
break;
case (UnityEngine.Rendering.TextureDimension.Tex2DArray):
texObjField.objectType = typeof(Texture2DArray);
break;
case (UnityEngine.Rendering.TextureDimension.Cube):
texObjField.objectType = typeof(Cubemap);
break;
case (UnityEngine.Rendering.TextureDimension.CubeArray):
texObjField.objectType = typeof(CubemapArray);
break;
}
VisualElement background = texObjField.ElementAt(0);
background.style.backgroundColor = StyleKeyword.None;
background.style.borderBottomColor = StyleKeyword.None;
background.style.borderLeftColor = StyleKeyword.None;
background.style.borderRightColor = StyleKeyword.None;
background.style.borderTopColor = StyleKeyword.None;
background.style.borderBottomWidth = StyleKeyword.None;
background.style.borderLeftWidth = StyleKeyword.None;
background.style.borderRightWidth = StyleKeyword.None;
background.style.borderTopWidth = StyleKeyword.None;
background.style.borderTopLeftRadius = StyleKeyword.None;
background.style.borderTopRightRadius = StyleKeyword.None;
background.style.borderBottomLeftRadius = StyleKeyword.None;
background.style.borderBottomRightRadius = StyleKeyword.None;
background.style.justifyContent = Justify.FlexStart;
background.style.flexWrap = Wrap.NoWrap;
background.style.overflow = Overflow.Hidden;
VisualElement contents = background.ElementAt(0);
contents.style.flexShrink = 0;
contents.style.flexGrow = 1;
contents.style.overflow = Overflow.Hidden;
contents.style.flexBasis = StyleKeyword.Auto;
VisualElement oldlabel = contents.ElementAt(1);
oldlabel.style.display = DisplayStyle.None;
VisualElement searchButton = background.ElementAt(1);
searchButton.style.width = StyleKeyword.Auto;
searchButton.style.backgroundImage = StyleKeyword.None;
searchButton.style.flexDirection = FlexDirection.Row;
searchButton.style.overflow = Overflow.Hidden;
searchButton.style.flexBasis = StyleKeyword.Auto;
VisualElement fakeRadial = new VisualElement();
fakeRadial.AddToClassList("unity-object-field__selector");
fakeRadial.pickingMode = PickingMode.Ignore;
fakeRadial.style.backgroundColor = Color.clear;
Label label = new Label(textureProperty.displayName);
label.pickingMode = PickingMode.Ignore;
label.style.textOverflow = TextOverflow.Ellipsis;
searchButton.Add(fakeRadial);
searchButton.Add(label);
Image oldThumbnail = contents.ElementAt(0) as Image;
oldThumbnail.style.display = DisplayStyle.None;
thumbnail = new Image();
thumbnail.AddToClassList("textureFieldThumb");
thumbnail.AddToClassList("unity-object-field-display__icon");
thumbnail.pickingMode = PickingMode.Ignore;
thumbnail.scaleMode = ScaleMode.StretchToFill;
thumbnail.tintColor = Color.white;
thumbnailRT = new RenderTexture((int)(32.0f * EditorGUIUtility.pixelsPerPoint), (int)(32.0f * EditorGUIUtility.pixelsPerPoint), 1, RenderTextureFormat.ARGB32, 1);
thumbnailRT.depth = 0;
thumbnailRT.name = textureProperty.name + "_icon";
thumbnailRT.Create();
thumbnail.image = thumbnailRT;
contents.Insert(0, thumbnail);
texObjField.RegisterValueChangedCallback(OnObjectFieldChanged);
if (textureProperty.hasMixedValue)
{
currentValue = null;
texObjField.showMixedValue = true;
}
else
{
//if (textureProperty.textureValue == null && defaultTexture != null)
//{
// textureProperty.textureValue = defaultTexture;
// currentValue = defaultTexture;
//}
texObjField.showMixedValue = false;
}
texObjField.SetValueWithoutNotify(currentValue);
UpdateThumbnail();
leftAlignBox.Add(texObjField);
Add(leftAlignBox);
Add(rightAlignBox);
}
void OnObjectFieldChanged(ChangeEvent<Object> evt)
{
value = evt.newValue;
}
public void SetValueWithoutNotify(Object newValue)
{
if (newValue == null || newValue is Texture)
{
currentValue = (Texture) newValue;
if (currentValue == null && defaultTexture != null)
{
currentValue = defaultTexture;
}
UpdateThumbnail();
textureProperty.textureValue = currentValue;
texObjField.SetValueWithoutNotify(currentValue);
UpdateObjDelegate.Invoke(texObjField);
texObjField.showMixedValue = newValue == null;
}
else throw new System.ArgumentException($"Expected object of type {typeof(Texture2D)}");
}
public Object value
{
get => currentValue;
set
{
if (value == currentValue)
return;
Object previous = currentValue;
SetValueWithoutNotify(value);
}
}
void UpdateThumbnail()
{
BlitTextureIcon(thumbnailRT, currentValue, textureType);
}
public void UpdateMaterialProperty(MaterialProperty boundProp)
{
bool valueChanged = currentValue != boundProp.textureValue;
textureProperty = boundProp;
if (valueChanged)
{
currentValue = boundProp.textureValue;
texObjField.SetValueWithoutNotify(currentValue);
UpdateThumbnail();
}
texObjField.showMixedValue = boundProp.hasMixedValue;
}
void Dispose()
{
if (thumbnailRT != null)
{
thumbnailRT.Clear();
}
}
static Material s_blitMaterial;
static LocalKeyword[] dimKeywords;
static LocalKeyword normalMapKeyword;
static int prop_Blit2D = Shader.PropertyToID("_Blit2D");
static int prop_Blit2DArray = Shader.PropertyToID("_Blit2DArray");
static int prop_BlitCube = Shader.PropertyToID("_BlitCube");
static int prop_BlitCubeArray = Shader.PropertyToID("_BlitCubeArray");
static int prop_Blit3D = Shader.PropertyToID("_Blit3D");
static int prop_BlitDim = Shader.PropertyToID("_BlitDim");
static void UpdateKeywords()
{
Shader blitShader = s_blitMaterial.shader;
dimKeywords[(int)TextureDimension.Tex2D - 2] = new LocalKeyword(blitShader, "DIM_2D");
dimKeywords[(int)TextureDimension.Tex2DArray - 2] = new LocalKeyword(blitShader, "DIM_2DARRAY");
dimKeywords[(int)TextureDimension.Cube - 2] = new LocalKeyword(blitShader, "DIM_CUBE");
dimKeywords[(int)TextureDimension.CubeArray - 2] = new LocalKeyword(blitShader, "DIM_CUBEARRAY");
dimKeywords[(int)TextureDimension.Tex3D - 2] = new LocalKeyword(blitShader, "DIM_3D");
normalMapKeyword = new LocalKeyword(blitShader, "NORMAL_MAP");
}
void BlitTextureIcon(RenderTexture icon, Texture tex, TextureDimension texType)
{
if (icon == null) return;
RenderTexture active = RenderTexture.active;
if (tex == null)
{
RenderTexture.active = icon;
GL.Clear(false, true, Color.clear, 0);
RenderTexture.active = active;
return;
}
if (s_blitMaterial == null || s_blitMaterial.shader == null || dimKeywords == null)
{
Shader blitShader = Shader.Find("Hidden/ShaderGUITextureIconBlit");
if (blitShader == null)
{
Debug.LogError("Could not find Blit_ShaderGUI.shader (Hidden/ShaderGUITextureIconBlit), cannot generate material thumbnails");
return;
}
s_blitMaterial = new Material(blitShader);
dimKeywords = new LocalKeyword[5];
}
UpdateKeywords();
int offsetDimEnum = (int)texType - 2;
for (int i = 0; i < 5; i++)
{
if (i == offsetDimEnum)
s_blitMaterial.EnableKeyword(dimKeywords[i]);
else
s_blitMaterial.DisableKeyword(dimKeywords[i]);
}
switch (texType)
{
case TextureDimension.Tex2D:
s_blitMaterial.SetTexture(prop_Blit2D, tex);
break;
case TextureDimension.Tex2DArray:
s_blitMaterial.SetTexture(prop_Blit2DArray, tex);
break;
case TextureDimension.Cube:
s_blitMaterial.SetTexture(prop_BlitCube, tex);
s_blitMaterial.SetVector(prop_BlitDim, new Vector4(tex.width, tex.height, 0, 0));
break;
case TextureDimension.CubeArray:
s_blitMaterial.SetTexture(prop_BlitCubeArray, tex);
s_blitMaterial.SetVector(prop_BlitDim, new Vector4(tex.width, tex.height, 0, 0));
break;
case TextureDimension.Tex3D:
s_blitMaterial.SetTexture(prop_Blit3D, tex);
s_blitMaterial.SetVector(prop_BlitDim, new Vector4(tex.width, tex.height, 0, 0));
break;
default:
Debug.LogError("Shader GUI Icon Blitter: Unknown texture dimension " + texType);
return;
}
if (isNormalMap)
{
s_blitMaterial.EnableKeyword(normalMapKeyword);
}
Graphics.Blit(tex, icon, s_blitMaterial);
RenderTexture.active = active;
if (isNormalMap)
{
s_blitMaterial.DisableKeyword(normalMapKeyword);
}
switch (texType)
{
case TextureDimension.Tex2D:
s_blitMaterial.SetTexture(prop_Blit2D, null);
break;
case TextureDimension.Tex2DArray:
s_blitMaterial.SetTexture(prop_Blit2DArray, null);
break;
case TextureDimension.Cube:
s_blitMaterial.SetTexture(prop_BlitCube, null);
break;
case TextureDimension.CubeArray:
s_blitMaterial.SetTexture(prop_BlitCubeArray, null);
break;
case TextureDimension.Tex3D:
s_blitMaterial.SetTexture(prop_Blit3D, null);
break;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 38c312bdff088324b93a4ff922600f48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: