浏览代码

[material graph]Working on texture preview / regeneration of drawing layer workflow.

/main
Tim Cooper 9 年前
当前提交
c522adcc
共有 20 个文件被更改,包括 160 次插入543 次删除
  1. 90
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableNode.cs
  2. 1
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphDataSource.cs
  3. 4
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/GUIModificationType.cs
  4. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/CombineNodeUI.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbsoluteNode.cs
  6. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ScreenPosNode.cs
  7. 123
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
  8. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SwizzleNode.cs
  9. 67
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs
  10. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TimeNode.cs
  11. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UVNode.cs
  12. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ViewDirectionNode.cs
  13. 63
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/TextureNodeUI.cs
  14. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/TextureNodeUI.cs.meta
  15. 96
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/KaleidoscopeNode.cs
  16. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/KaleidoscopeNode.cs.meta
  17. 101
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MandelbrotNode.cs
  18. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MandelbrotNode.cs.meta
  19. 105
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PerlinNode.cs
  20. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PerlinNode.cs.meta

90
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawableNode.cs


if (m_Ui != null)
{
var modificationType = m_Ui.Render(m_CustomUiRect);
if (modificationType == GUIModificationType.Repaint)
{
// if we were changed, we need to redraw all the
// dependent nodes.
RepaintDependentNodes(m_Node);
}
else if (modificationType == GUIModificationType.ModelChanged)
if (modificationType == GUIModificationType.ModelChanged)
ValidateDependentNodes(m_Node);
if (modificationType == GUIModificationType.DataChanged)
{
ValidateDependentNodes(m_Node);
RepaintDependentNodes(m_Node);
}
else if (modificationType == GUIModificationType.Repaint)
{
// if we were changed, we need to redraw all the
// dependent nodes.
RepaintDependentNodes(m_Node);
}
private void RepaintDependentNodes(INode theNode)
{
var dependentNodes = new List<INode>();

}
}
private void ValidateDependentNodes(INode theNode)
{
var dependentNodes = new List<INode>();
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, theNode);
foreach (var node in dependentNodes)
node.ValidateNode();
}
/*
public static void OnGUI(List<CanvasElement> selection)
{

}
}*/
/* public virtual GUIModificationType NodeUI(Rect drawArea)
{
return GUIModificationType.None;
}
/* public virtual GUIModificationType NodeUI(Rect drawArea)
{
return GUIModificationType.None;
}
public virtual bool OnGUI()
{
GUILayout.Label("MaterialSlot Defaults", EditorStyles.boldLabel);
var modified = false;
foreach (var slot in inputSlots)
{
if (!owner.GetEdges(GetSlotReference(slot.name)).Any())
modified |= DoSlotUI(this, slot);
}
public virtual bool OnGUI()
{
GUILayout.Label("MaterialSlot Defaults", EditorStyles.boldLabel);
var modified = false;
foreach (var slot in inputSlots)
{
if (!owner.GetEdges(GetSlotReference(slot.name)).Any())
modified |= DoSlotUI(this, slot);
}
return modified;
}
return modified;
}
public static bool DoSlotUI(SerializableNode node, ISlot slot)
{
GUILayout.BeginHorizontal( /*EditorStyles.inspectorBig*);
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("MaterialSlot " + slot.name, EditorStyles.largeLabel);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
public static bool DoSlotUI(SerializableNode node, ISlot slot)
{
GUILayout.BeginHorizontal( /*EditorStyles.inspectorBig*);
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("MaterialSlot " + slot.name, EditorStyles.largeLabel);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
//TODO: fix this
return false;
//return slot.OnGUI();
}*/
//TODO: fix this
return false;
//return slot.OnGUI();
}*/
}
}

1
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphDataSource.cs


using UnityEditor.Experimental.Graph;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngineInternal;
namespace UnityEditor.Graphing.Drawing
{

4
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/GUIModificationType.cs


public enum GUIModificationType
{
None = 0,
// just repaint this node and it's dependencies
// something structurally changed, rebuild the graph from scratch!
// some data internally to the node was modified
// that dependent nodes may use.
DataChanged = 1 << 2
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/CombineNodeUI.cs


[CustomNodeUI(typeof(CombineNode))]
public class CombineNodeUI : AbstractMaterialNodeUI
{
public float GetNodeUiHeight(float width)
public override float GetNodeUiHeight(float width)
{
return base.GetNodeUiHeight(width) + EditorGUIUtility.singleLineHeight;
}

var toReturn = GUIModificationType.None;
if (EditorGUI.EndChangeCheck())
{
toReturn = GUIModificationType.Repaint;
toReturn = GUIModificationType.DataChanged;
area.y += 2 * EditorGUIUtility.singleLineHeight;
area.height -= 2 * EditorGUIUtility.singleLineHeight;
area.y += EditorGUIUtility.singleLineHeight;
area.height -= EditorGUIUtility.singleLineHeight;
toReturn |= base.Render(area);
return toReturn;
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbsoluteNode.cs


{
name = "AbsoluteNode";
}
protected override string GetFunctionName() {return "unity_absolute_" + precision; }
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ScreenPosNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(kOutputSlotName, kOutputSlotName, SlotType.Output, 0, SlotValueType.Vector4, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { kOutputSlotName });
}

123
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs


namespace UnityEngine.MaterialGraph
{
/* public static class SlotExtensions
{
public static bool Editable (this MaterialSlot slot)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return false;
var properties = node[slot];
if (properties != null)
return properties.editable;
return false;
}
public static bool Removable (this MaterialSlot slot)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return false;
var properties = node[slot];
if (properties != null)
return properties.removable;
return false;
}
public static bool SupportsDefault (this MaterialSlot slot)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return true;
var properties = node[slot];
if (properties != null)
return properties.supportsDefault;
return true;
}
public static ShaderProperty GetDefaultValue (this MaterialSlot slot)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return null;
var properties = node[slot];
if (properties != null && properties.supportsDefault)
return properties.value;
return null;
}
public static void SetDefaultValue (this MaterialSlot slot, ShaderProperty value)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return;
var properties = node[slot];
if (properties != null && properties.supportsDefault)
slot.SetDefaultValueForSlot (value);
}
public static SlotProperties GetSlotProperties (this MaterialSlot slot)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return null;
int index = node.m_SlotPropertiesIndexes.FindIndex (x => x == slot.name);
if (index > -1)
return node.m_SlotProperties[index];
return null;
}
public static void SetDefaultValueForSlot (this MaterialSlot slot, ShaderProperty value)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return;
var properties = node[slot];
if (properties != null && properties.supportsDefault) {
Object.DestroyImmediate (properties.value, true);
properties.value = value;
} else {
properties = new SlotProperties() {value = value};
slot.SetPropertiesForSlot (properties);
}
}
public static void SetPropertiesForSlot (this MaterialSlot slot, SlotProperties property)
{
var node = slot.node as AbstractMaterialNode;
if (node == null)
return;
int index = node.m_SlotPropertiesIndexes.FindIndex (x => x == slot.name);
var oldValue = index > -1 ? node.m_SlotProperties[index] : null;
if (oldValue != null && oldValue.value != null)
Object.DestroyImmediate (oldValue.value, true);
if (property == null && index > -1)
{
if (oldValue != null)
oldValue.value = null;
node.m_SlotPropertiesIndexes.RemoveAt (index);
node.m_SlotProperties.RemoveAt (index);
}
else
{
if (index > -1) {
node.m_SlotProperties[index] = property;
if (node is IPropertyNode)
((IPropertyNode)node).UpdateProperty ();
}
else
{
node.m_SlotPropertiesIndexes.Add (slot.name);
node.m_SlotProperties.Add (property);
if (node is IPropertyNode)
((IPropertyNode)node).UpdateProperty ();
}
}
}
public static void ClearDefaultValue (this MaterialSlot slot)
{
slot.SetDefaultValueForSlot (null);
}
}*/
[Serializable]
public enum SlotValueType
{

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SwizzleNode.cs


using UnityEditor;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph

67
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs


protected const string kUVSlotName = "UV";
[SerializeField]
public Texture2D m_DefaultTexture;
private Texture2D m_DefaultTexture;
public TextureType m_TextureType;
private TextureType m_TextureType;
public override bool hasPreview { get { return true; } }
private List<string> m_TextureTypeNames;
public Texture2D defaultTexture
{
get { return m_DefaultTexture; }
set { m_DefaultTexture = value; }
}
public override bool hasPreview { get { return true; } }
public TextureType textureType
{
get { return m_TextureType; }
set { m_TextureType = value; }
}
LoadTextureTypes();
UpdateNodeAfterDeserialization();
public override void UpdateNodeAfterDeserialization()
public sealed override void UpdateNodeAfterDeserialization()
LoadTextureTypes();
AddSlot(new MaterialSlot(kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, 0, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot(kOutputSlotRName, kOutputSlotRName, SlotType.Output, 1, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(kOutputSlotGName, kOutputSlotGName, SlotType.Output, 2, SlotValueType.Vector1, Vector4.zero));

RemoveSlotsNameNotMatching(validSlots);
}
}
private void LoadTextureTypes()
{
if (m_TextureTypeNames == null)
m_TextureTypeNames = new List<string>(Enum.GetNames(typeof(TextureType)));
}
// Node generations

return;
var edges = owner.GetEdges(GetSlotReference(uvSlot.name));
if (edges.Any())
if (!edges.Any())
UVNode.StaticGenerateVertexToFragmentBlock(visitor, generationMode);
}

return;
var edges = owner.GetEdges(GetSlotReference(uvSlot.name));
if (edges.Any())
if (!edges.Any())
UVNode.GenerateVertexShaderBlock(visitor);
}

visitor.AddShaderChunk("sampler2D " + propertyName + ";", true);
}
/*
public override float GetNodeUIHeight(float width)
{
return EditorGUIUtility.singleLineHeight * 2;
}
public override GUIModificationType NodeUI(Rect drawArea)
{
LoadTextureTypes();
base.NodeUI(drawArea);
EditorGUI.BeginChangeCheck();
m_DefaultTexture = EditorGUI.MiniThumbnailObjectField( new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), new GUIContent("Texture"), m_DefaultTexture, typeof(Texture2D), null) as Texture2D;
var texureChanged = EditorGUI.EndChangeCheck();
drawArea.y += EditorGUIUtility.singleLineHeight;
EditorGUI.BeginChangeCheck();
m_TextureType = (TextureType) EditorGUI.Popup(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), (int) m_TextureType, m_TextureTypeNames.ToArray(), EditorStyles.popup);
var typeChanged = EditorGUI.EndChangeCheck();
if (typeChanged)
{
pixelGraph.RevalidateGraph();
return GUIModificationType.Repaint;
}
return texureChanged ? GUIModificationType.Repaint : GUIModificationType.None;
}
/*
public override bool DrawSlotDefaultInput(Rect rect, Slot inputSlot)
{
var uvSlot = FindInputSlot(kUVSlotName);

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TimeNode.cs


public TimeNode(IGraph theOwner) : base(theOwner)
{
name = "Time";
UpdateNodeAfterDeserialization();
public override void UpdateNodeAfterDeserialization()
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(kOutputSlotName, kOutputSlotName, SlotType.Output, 0, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot(kOutputSlotNameX, kOutputSlotNameX, SlotType.Output, 1, SlotValueType.Vector1, Vector4.zero));

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UVNode.cs


public UVNode(IGraph theOwner) : base(theOwner)
{
name = "UV";
UpdateNodeAfterDeserialization();
}
public override void UpdateNodeAfterDeserialization()

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ViewDirectionNode.cs


public ViewDirectionNode(IGraph theOwner) : base(theOwner)
{
name = "View Direction";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()

63
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/TextureNodeUI.cs


using System;
using System.Collections.Generic;
using UnityEditor.Graphing;
using UnityEditor.Graphing.Drawing;
using UnityEngine;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph
{
[CustomNodeUI(typeof(TextureNode))]
public class TextureNodeUI : AbstractMaterialNodeUI
{
public override float GetNodeUiHeight(float width)
{
return base.GetNodeUiHeight(width) + EditorGUIUtility.singleLineHeight * 2;
}
private string[] m_TextureTypeNames;
private string[] textureTypeNames
{
get
{
if (m_TextureTypeNames == null)
m_TextureTypeNames = Enum.GetNames(typeof(TextureType));
return m_TextureTypeNames;
}
}
public override GUIModificationType Render(Rect area)
{
var node = m_Node as TextureNode;
if (node == null)
return base.Render(area);
if (m_Node == null)
return GUIModificationType.None;
EditorGUI.BeginChangeCheck();
node.defaultTexture = EditorGUI.MiniThumbnailObjectField(new Rect(area.x, area.y, area.width, EditorGUIUtility.singleLineHeight), new GUIContent("Texture"), node.defaultTexture, typeof(Texture2D), null) as Texture2D;
var texureChanged = EditorGUI.EndChangeCheck();
area.y += EditorGUIUtility.singleLineHeight;
area.height -= EditorGUIUtility.singleLineHeight;
EditorGUI.BeginChangeCheck();
node.textureType = (TextureType)EditorGUI.Popup(new Rect(area.x, area.y, area.width, EditorGUIUtility.singleLineHeight), (int)node.textureType, textureTypeNames, EditorStyles.popup);
var typeChanged = EditorGUI.EndChangeCheck();
var toReturn = GUIModificationType.None;
if (typeChanged)
{
toReturn |= GUIModificationType.DataChanged;
}
if (texureChanged)
toReturn |= GUIModificationType.Repaint;
area.y += EditorGUIUtility.singleLineHeight;
area.height -= EditorGUIUtility.singleLineHeight;
toReturn |= base.Render(area);
return toReturn;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/TextureNodeUI.cs.meta


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

96
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/KaleidoscopeNode.cs


using System;
using System.Linq;
using UnityEngine;
/*
namespace UnityEditor.MaterialGraph
{
[Title("Fractal/Kaleidoscope Node")]
class KaleidoscopeNode : BaseMaterialNode, IGeneratesFunction, IGeneratesBodyCode
{
[SerializeField]
private int m_Iterations = 6;
private const string kPointInputName = "Point";
private const string kConstant1InputName = "Constant1";
private const string kConstant2InputName = "Constant2";
private const string kOutputSlotName = "Output";
public override bool hasPreview { get { return true; } }
public override void OnCreate()
{
name = "KaleidoscopeNode";
base.OnCreate();
// AddSlot(new Slot(SlotType.InputSlot, kPointInputName));
//AddSlot(new Slot(SlotType.InputSlot, kConstant1InputName));
//AddSlot(new Slot(SlotType.InputSlot, kConstant2InputName));
//AddSlot(new Slot(SlotType.OutputSlot, kOutputSlotName));
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
foreach (var precision in m_PrecisionNames)
{
outputString.AddShaderChunk("inline " + precision + "4 unity_kaleidoscope_" + precision + " (" + precision + "2 p, " + precision + "4 c1, " + precision + "4 c2, int iterations)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("c1 = c1 * 2 - 1;", false);
outputString.AddShaderChunk("c2 = c2 * 2 - 1;", false);
outputString.AddShaderChunk("for (int n = 0; n < iterations; n++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("p = c1.xy + p * c1.zw + " + precision + "2(dot(p, c2.xy), dot(p, c2.zw));", false);
outputString.AddShaderChunk("if(p.x < p.y) p.xy = -p.yx;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.AddShaderChunk("return " + precision + "4(p, 1, 1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
}
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputSlot = FindOutputSlot(kOutputSlotName);
var pointInput = FindInputSlot(kPointInputName);
var constant1Input = FindInputSlot(kConstant1InputName);
var constant2Input = FindInputSlot(kConstant2InputName);
if (outputSlot == null || pointInput == null || constant1Input == null || constant2Input == null)
{
Debug.LogError("Invalid slot configuration on node: " + name);
return;
}
string pointInputValue = GetSlotValue(pointInput, generationMode);
string constant1InputValue = GetSlotValue(constant1Input, generationMode);
string constant2InputValue = GetSlotValue(constant2Input, generationMode);
string outName = GetOutputVariableNameForSlot(outputSlot, generationMode);
visitor.AddShaderChunk(precision + "4 " + outName + " = unity_kaleidoscope_" + precision + "(" + pointInputValue + ".xy, " + constant1InputValue + ", " + constant2InputValue + ", " + m_Iterations + ");", false);
}
static float Slider(string title, float value, float from, float to)
{
GUILayout.BeginHorizontal();
GUILayout.Label(title);
value = GUILayout.HorizontalSlider(value, from, to, GUILayout.Width(64));
GUILayout.EndHorizontal();
return value;
}
/*public override void NodeUI()
{
base.NodeUI();
EditorGUI.BeginChangeCheck();
m_Iterations = (int)Slider("Iterations", (float)m_Iterations, 1, 50);
if (EditorGUI.EndChangeCheck())
RegeneratePreviewShaders();
}*
}
}
*/

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/KaleidoscopeNode.cs.meta


fileFormatVersion: 2
guid: dfc00afcda39bfa4fbd9bd6176f53477
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

101
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MandelbrotNode.cs


using System;
using System.Linq;
using UnityEngine;
/*
namespace UnityEditor.MaterialGraph
{
[Title("Fractal/Mandelbrot Node")]
class MandelbrotNode : BaseMaterialNode, IGeneratesFunction, IGeneratesBodyCode
{
[SerializeField]
private int m_Iterations = 6;
private const string kPointInputName = "Point";
private const string kConstantInputName = "Constant";
private const string kOutputSlotName = "Output";
public override bool hasPreview { get { return true; } }
public override void OnCreate()
{
name = "MandelbrotNode";
base.OnCreate();
//AddSlot(new Slot(SlotType.InputSlot, kPointInputName));
//AddSlot(new Slot(SlotType.InputSlot, kConstantInputName));
//AddSlot(new Slot(SlotType.OutputSlot, kOutputSlotName));
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
foreach (var precision in m_PrecisionNames)
{
outputString.AddShaderChunk("inline " + precision + "4 unity_mandelbrot_" + precision + " (" + precision + "2 p, " + precision + "2 c, float limit, float scale, int iterations)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + " zr = p.x * 4 - 2, zi = p.y * 4 - 2, dzr = 1, dzi = 0, r = 0, len2;", false);
outputString.AddShaderChunk("for (int n = 0; n < iterations; n++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + " tmp1 = 2 * zr * zi + c.y; zr = zr * zr - zi * zi + c.x; zi = tmp1;", false);
outputString.AddShaderChunk(precision + " tmp2 = 2 * (dzr * zi + dzi * zr); dzr = 2 * (dzr * zr - dzi * zi) + 1; dzi = tmp2;", false);
outputString.AddShaderChunk("len2 = zr * zr + zi * zi;", false);
outputString.AddShaderChunk("if (len2 > 1000000 * limit) { r = n; break; }", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.AddShaderChunk("return scale * sqrt(len2 / (dzr * dzr + dzi * dzi)) * log(len2);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
}
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var validSlots = ListPool<Slot>.Get();
GetValidInputSlots(validSlots);
var outputSlot = outputSlots.FirstOrDefault(x => x.name == kOutputSlotName);
var pointInput = validSlots.FirstOrDefault(x => x.name == kPointInputName);
var constantInput = validSlots.FirstOrDefault(x => x.name == kConstantInputName);
ListPool<Slot>.Release(validSlots);
if (outputSlot == null || pointInput == null || constantInput == null)
{
Debug.LogError("Invalid slot configuration on node: " + name);
return;
}
//TODO: This will break if there is NO input connected, use default in that case
var pointProvider = pointInput.edges[0].fromSlot.node as BaseMaterialNode;
var pointName = pointProvider.GetOutputVariableNameForSlot(pointInput.edges[0].fromSlot, generationMode);
var constantProvider = constantInput.edges[0].fromSlot.node as BaseMaterialNode;
var constantName = constantProvider.GetOutputVariableNameForSlot(constantInput.edges[0].fromSlot, generationMode);
string outName = GetOutputVariableNameForSlot(outputSlot, generationMode);
visitor.AddShaderChunk(precision + "4 " + outName + " = unity_mandelbrot_" + precision + "(" + pointName + ".xy, " + constantName + ".xy, " + constantName + ".z, " + constantName + ".w, " + m_Iterations + ");", false);
}
static float Slider(string title, float value, float from, float to)
{
GUILayout.BeginHorizontal();
GUILayout.Label(title);
value = GUILayout.HorizontalSlider(value, from, to, GUILayout.Width(64));
GUILayout.EndHorizontal();
return value;
}
/*public override void NodeUI()
{
base.NodeUI();
EditorGUI.BeginChangeCheck();
m_Iterations = (int)Slider("Iterations", (float)m_Iterations, 1, 50);
if (EditorGUI.EndChangeCheck())
RegeneratePreviewShaders();
}*
}
}*/

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MandelbrotNode.cs.meta


fileFormatVersion: 2
guid: 45896ba83118f8548bd9d2f9263bcde5
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

105
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PerlinNode.cs


using UnityEngine;
/*
namespace UnityEditor.MaterialGraph
{
[Title("Fractal/Perlin Node")]
class PerlinNode : TextureNode, IGeneratesFunction, IGeneratesBodyCode
{
public override bool hasPreview { get { return true; } }
[SerializeField]
private int m_Iterations = 4;
[SerializeField]
private float m_Decay = 0.5f;
[SerializeField]
private float m_Frequency = 2.0f;
public override void OnCreate()
{
name = "Perlin";
base.OnCreate();
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
foreach (var precision in m_PrecisionNames)
{
outputString.AddShaderChunk("inline " + precision + "4 unity_perlin_" + precision + " ("
+ "sampler2D textureID, "
+ "int iterations, "
+ precision + " decay, "
+ precision + " frequency, "
+ precision + "2 p"
+ ")", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + "4 sum = " + precision + "4(0, 0, 0, 0);", false);
outputString.AddShaderChunk(precision + " amp = 0.5;", false);
outputString.AddShaderChunk("for(int n = 0; n < iterations; n++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("sum += amp * tex2D (textureID, p);", false);
outputString.AddShaderChunk("p *= frequency;", false);
outputString.AddShaderChunk("amp *= decay;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.AddShaderChunk("return sum;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
}
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
/* public override void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputSlot = FindOutputSlot(kOutputSlotName);
if (outputSlot == null)
return;
var uvSlot = FindInputSlot(kUVSlotName);
if (uvSlot == null)
return;
var uvName = "IN.meshUV0";
if (uvSlot.edges.Count > 0)
{
var fromNode = uvSlot.edges[0].fromSlot.node as BaseMaterialNode;
uvName = fromNode.GetOutputVariableNameForSlot(uvSlot.edges[0].fromSlot, generationMode);
}
string body = "unity_perlin_" + precision + "(" + GetPropertyName() + ", " + m_Iterations + ", " + m_Decay + ", " + m_Frequency + ", " + uvName + ".xy)";
visitor.AddShaderChunk("float4 " + GetOutputVariableNameForSlot(outputSlot, generationMode) + " = " + body + ";", true);
}
*
static float Slider(string title, float value, float from, float to)
{
GUILayout.BeginHorizontal();
GUILayout.Label(title);
value = GUILayout.HorizontalSlider(value, from, to, GUILayout.Width(64));
GUILayout.EndHorizontal();
return value;
}
/*
public override float GetNodeUIHeight()
{
return EditorGUIUtility.singleLineHeight;
}
public override bool NodeUI(Rect drawArea)
{
base.NodeUI(drawArea);
EditorGUI.BeginChangeCheck();
m_Iterations = (int)Slider("Iterations", (float)m_Iterations, 1, 8);
m_Decay = Slider("Decay", m_Decay, -1f, 1f);
m_Frequency = Slider("Frequency", m_Frequency, 0f, 5f);
if (EditorGUI.EndChangeCheck())
RegeneratePreviewShaders();
}*
}
}*/

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PerlinNode.cs.meta


fileFormatVersion: 2
guid: 6388ec78711fae240b4d5c60bd70d36d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
正在加载...
取消
保存