浏览代码

Gradient node

/main
MingWai 8 年前
当前提交
660338f9
共有 13 个文件被更改,包括 481 次插入0 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  2. 73
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs
  3. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs.meta
  4. 9
      MaterialGraphProject/Assets/_MingWai.meta
  5. 9
      MaterialGraphProject/Assets/_MingWai/Editor.meta
  6. 46
      MaterialGraphProject/Assets/_MingWai/Editor/GradientField.cs
  7. 12
      MaterialGraphProject/Assets/_MingWai/Editor/GradientField.cs.meta
  8. 147
      MaterialGraphProject/Assets/_MingWai/Editor/GradientWrapper.cs
  9. 12
      MaterialGraphProject/Assets/_MingWai/Editor/GradientWrapper.cs.meta
  10. 148
      MaterialGraphProject/Assets/_MingWai/GradientNode.cs
  11. 12
      MaterialGraphProject/Assets/_MingWai/GradientNode.cs.meta

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


{
typeMapper[typeof(AbstractMaterialNode)] = typeof(MaterialNodePresenter);
typeMapper[typeof(ColorNode)] = typeof(ColorNodePresenter);
typeMapper[typeof(GradientNode)] = typeof(GradientNodePresenter);
typeMapper[typeof(TextureNode)] = typeof(TextureNodePresenter);
typeMapper[typeof(TextureAssetNode)] = typeof(TextureAssetNodePresenter);
typeMapper[typeof(TextureLODNode)] = typeof(TextureLODNodePresenter);

73
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs


using System;
using System.Collections.Generic;
using RMGUI.GraphView;
using UnityEditor.Graphing.Drawing;
using UnityEngine;
namespace UnityEditor.MaterialGraph.Drawing
{
[Serializable]
class GradientContolPresenter : GraphControlPresenter
{
private GradientObject gradientobj;
private SerializedObject hserializedObject;
private SerializedProperty hcolorGradient;
private UnityEngine.MaterialGraph.GradientNode prevnode;
private string prevWindow = "";
public override void OnGUIHandler()
{
base.OnGUIHandler();
var cNode = node as UnityEngine.MaterialGraph.GradientNode;
if (cNode == null)
return;
if(gradientobj == null || prevnode != cNode)
{
prevnode = cNode;
gradientobj = new GradientObject();
hserializedObject = new SerializedObject(gradientobj);
hcolorGradient = hserializedObject.FindProperty("gradient");
}
EditorGUILayout.PropertyField(hcolorGradient, true, null);
hserializedObject.ApplyModifiedProperties();
cNode.gradient = gradientobj.gradient;
Event e = Event.current;
if (prevWindow != EditorWindow.focusedWindow.ToString() && EditorWindow.focusedWindow.ToString() != "(UnityEditor.GradientPicker)")
{
cNode.UpdateGradient();
prevWindow = EditorWindow.focusedWindow.ToString();
Debug.Log("Update Gradient Shader");
}
}
public override float GetHeight()
{
return EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing;
}
}
[Serializable]
public class GradientNodePresenter : MaterialNodePresenter
{
protected override IEnumerable<GraphElementPresenter> GetControlData()
{
var instance = CreateInstance<GradientContolPresenter>();
instance.Initialize(node);
return new List<GraphElementPresenter> { instance };
}
}
public class GradientObject : ScriptableObject
{
public Gradient gradient = new Gradient();
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs.meta


fileFormatVersion: 2
guid: 75dd92c51294c2f498d37158354fe0b1
timeCreated: 1476707367
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
MaterialGraphProject/Assets/_MingWai.meta


fileFormatVersion: 2
guid: 17f8fdfe553782d47be91ff2545455b4
folderAsset: yes
timeCreated: 1495460301
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
MaterialGraphProject/Assets/_MingWai/Editor.meta


fileFormatVersion: 2
guid: 520cd44e8917f774f9f275a1e6cc490b
folderAsset: yes
timeCreated: 1495462949
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

46
MaterialGraphProject/Assets/_MingWai/Editor/GradientField.cs


using UnityEngine;
using UnityEditor;
using System.Reflection;
using Type = System.Type;
public static class GUIGradientField
{
#region Initial Setup
private static MethodInfo s_miGradientField1;
private static MethodInfo s_miGradientField2;
static GUIGradientField()
{
// Get our grubby hands on hidden "GradientField" :)
Type tyEditorGUILayout = typeof(EditorGUILayout);
s_miGradientField1 = tyEditorGUILayout.GetMethod("GradientField", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string), GradientWrapper.s_tyGradient, typeof(GUILayoutOption[]) }, null);
s_miGradientField2 = tyEditorGUILayout.GetMethod("GradientField", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { GradientWrapper.s_tyGradient, typeof(GUILayoutOption[]) }, null);
}
#endregion
public static GradientWrapper GradientField(string label, GradientWrapper gradient, params GUILayoutOption[] options)
{
if (gradient == null)
gradient = new GradientWrapper();
gradient.GradientData = s_miGradientField1.Invoke(null, new object[] { label, gradient.GradientData, options });
return gradient;
}
public static GradientWrapper GradientField(GradientWrapper gradient, params GUILayoutOption[] options)
{
if (gradient == null)
gradient = new GradientWrapper();
gradient.GradientData = s_miGradientField1.Invoke(null, new object[] { gradient.GradientData, options });
return gradient;
}
}

12
MaterialGraphProject/Assets/_MingWai/Editor/GradientField.cs.meta


fileFormatVersion: 2
guid: d904de0858e72c44582885b5d11cbb9e
timeCreated: 1495462956
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

147
MaterialGraphProject/Assets/_MingWai/Editor/GradientWrapper.cs


using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Linq;
using Activator = System.Activator;
using Array = System.Array;
using Type = System.Type;
[System.Serializable]
public class GradientWrapper
{
/// <summary>
/// Wrapper for <c>GradientColorKey</c>.
/// </summary>
public struct ColorKey
{
public Color color;
public float time;
public ColorKey(Color color, float time)
{
this.color = color;
this.time = time;
}
}
/// <summary>
/// Wrapper for <c>GradientAlphaKey</c>.
/// </summary>
public struct AlphaKey
{
public float alpha;
public float time;
public AlphaKey(float alpha, float time)
{
this.alpha = alpha;
this.time = time;
}
}
#region Initial Setup
/// <summary>
/// Type of gradient.
/// </summary>
public static Type s_tyGradient;
#if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
private static MethodInfo s_miEvaluate;
private static PropertyInfo s_piColorKeys;
private static PropertyInfo s_piAlphaKeys;
private static Type s_tyGradientColorKey;
private static Type s_tyGradientAlphaKey;
#endif
/// <summary>
/// Perform one-off setup when class is accessed for first time.
/// </summary>
static GradientWrapper()
{
#if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
Assembly editorAssembly = typeof(Editor).Assembly;
s_tyGradientColorKey = editorAssembly.GetType("UnityEditor.GradientColorKey");
s_tyGradientAlphaKey = editorAssembly.GetType("UnityEditor.GradientAlphaKey");
// Note that `Gradient` is defined in the editor namespace in Unity 3.5.7!
s_tyGradient = editorAssembly.GetType("UnityEditor.Gradient");
s_miEvaluate = s_tyGradient.GetMethod("CalcColor", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(float) }, null);
s_piColorKeys = s_tyGradient.GetProperty("colorKeys", BindingFlags.Public | BindingFlags.Instance);
s_piAlphaKeys = s_tyGradient.GetProperty("alphaKeys", BindingFlags.Public | BindingFlags.Instance);
#else
// In Unity 4 this is easy :)
s_tyGradient = typeof(Gradient);
#endif
}
#endregion
#if (UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
#region Unity 3.5.7 Implementation
private object _gradient = Activator.CreateInstance(s_tyGradient);
public object GradientData {
get { return _gradient; }
set { _gradient = value; }
}
public Color Evaluate(float time) {
return (Color)s_miEvaluate.Invoke(_gradient, new object[] { time });
}
public void SetKeys(ColorKey[] colorKeys, AlphaKey[] alphaKeys) {
if (colorKeys != null) {
Array colorKeyParam = (Array)Activator.CreateInstance(s_tyGradientColorKey.MakeArrayType(), new object[] { colorKeys.Length });
for (int i = 0; i < colorKeys.Length; ++i)
colorKeyParam.SetValue(Activator.CreateInstance(s_tyGradientColorKey, colorKeys[i].color, colorKeys[i].time), i);
s_piColorKeys.SetValue(_gradient, colorKeyParam, null);
}
if (alphaKeys != null) {
Array alphaKeyParam = (Array)Activator.CreateInstance(s_tyGradientAlphaKey.MakeArrayType(), new object[] { alphaKeys.Length });
for (int i = 0; i < alphaKeys.Length; ++i)
alphaKeyParam.SetValue(Activator.CreateInstance(s_tyGradientAlphaKey, alphaKeys[i].alpha, alphaKeys[i].time), i);
s_piAlphaKeys.SetValue(_gradient, alphaKeyParam, null);
}
}
#endregion
#else
#region Unity 4.x Implementation
private Gradient _gradient = new Gradient();
public object GradientData
{
get { return _gradient; }
set { _gradient = value as Gradient; }
}
public Color Evaluate(float time)
{
return _gradient.Evaluate(time);
}
public void SetKeys(ColorKey[] colorKeys, AlphaKey[] alphaKeys)
{
GradientColorKey[] actualColorKeys = null;
GradientAlphaKey[] actualAlphaKeys = null;
if (colorKeys != null)
actualColorKeys = colorKeys.Select(key => new GradientColorKey(key.color, key.time)).ToArray();
if (alphaKeys != null)
actualAlphaKeys = alphaKeys.Select(key => new GradientAlphaKey(key.alpha, key.time)).ToArray();
_gradient.SetKeys(actualColorKeys, actualAlphaKeys);
}
#endregion
#endif
}

12
MaterialGraphProject/Assets/_MingWai/Editor/GradientWrapper.cs.meta


fileFormatVersion: 2
guid: aa9a37b7f8032ab4f95a380474ec7f17
timeCreated: 1495463020
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

148
MaterialGraphProject/Assets/_MingWai/GradientNode.cs


using System.Collections;
using UnityEngine;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Procedural/Gradient Editor")]
public class GradientNode : Function1Input, IGeneratesFunction
{
[SerializeField]
private Gradient m_gradient;
public Gradient gradient
{
get { return m_gradient; }
set
{
if (m_gradient == value)
{
return;
}
m_gradient = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public void UpdateGradient()
{
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
// Debug.Log("UPDATED GRAPH");
}
public GradientNode()
{
name = "Gradient";
UpdateNodeAfterDeserialization();
}
protected override string GetFunctionName()
{
return "unity_Gradient_" + precision;
}
protected override string GetInputSlotName()
{
return "Value";
}
protected override MaterialSlot GetInputSlot()
{
return new MaterialSlot(InputSlotId, GetInputSlotName(), kInputSlotShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector2.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector4, Vector2.zero);
}
private void GNF(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
GradientColorKey[] colorkeys = m_gradient.colorKeys;
GradientAlphaKey[] alphakeys = m_gradient.alphaKeys;
//Start
outputString.AddShaderChunk(GetFunctionPrototype("v"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
//Color
Color c;
float cp;
for (int i = 0; i < colorkeys.Length; i++)
{
c = colorkeys[i].color;
cp = colorkeys[i].time;
outputString.AddShaderChunk("float3 color" + i + "=float3(" + c.r + "," + c.g + "," + c.b + ");", false);
outputString.AddShaderChunk("float colorp" + i + "=" + cp + ";", false);
}
outputString.AddShaderChunk("float3 gradcolor = color0;", false);
for (int i = 0; i < colorkeys.Length - 1; i++)
{
int j = i + 1;
outputString.AddShaderChunk("float colorLerpPosition" + i + "=smoothstep(colorp" + i + ",colorp" + j + ",v);", false);
outputString.AddShaderChunk("gradcolor = lerp(gradcolor,color" + j + ",colorLerpPosition" + i + ");", false);
}
//Alpha
float a;
float ap;
for (int i = 0; i < alphakeys.Length; i++)
{
a = alphakeys[i].alpha;
ap = alphakeys[i].time;
outputString.AddShaderChunk("float alpha" + i + "=" + a + ";", false);
outputString.AddShaderChunk("float alphap" + i + "=" + ap + ";", false);
}
outputString.AddShaderChunk("float gradalpha = alpha0;", false);
for (int i = 0; i < alphakeys.Length - 1; i++)
{
int j = i + 1;
outputString.AddShaderChunk("float alphaLerpPosition" + i + "=smoothstep(alphap" + i + ",alphap" + j + ",v);", false);
outputString.AddShaderChunk("gradalpha = lerp(gradalpha,alpha" + j + ",alphaLerpPosition" + i + ");", false);
}
//Result
outputString.AddShaderChunk("return float4(gradcolor,gradalpha);", false);
//End
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
//yield return null;
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
GNF(visitor, generationMode);
}
}
}

12
MaterialGraphProject/Assets/_MingWai/GradientNode.cs.meta


fileFormatVersion: 2
guid: 9d0c2acd47052ae4497fe393345ad533
timeCreated: 1495627084
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存