浏览代码

[shader graph]Integtate new non modifiable settings into shader graph. Fix setting properties generation pipe to be simpler.

/main
Tim Cooper 9 年前
当前提交
6086c6d6
共有 10 个文件被更改,包括 162 次插入180 次删除
  1. 25
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs
  2. 24
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  3. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/ColorNode.cs
  4. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  5. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TextureNode.cs
  6. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector1Node.cs
  7. 213
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs
  8. 7
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector4Node.cs
  9. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/TextureProperty.cs
  10. 42
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs

25
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs


public void ExportShader(string path)
{
Dictionary<string, int> defaultTextures;
var shaderString = ShaderGenerator.GenerateSurfaceShader(this, name, false, out defaultTextures);
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = ShaderGenerator.GenerateSurfaceShader(this, name, false, out configuredTextures);
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader;
if (shader == null)
return;

var textureNames = new List<string>();
var textures = new List<Texture>();
foreach (var textureInfo in defaultTextures)
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable))
var texture = EditorUtility.InstanceIDToObject(textureInfo.Value) as Texture;
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
textureNames.Add(textureInfo.Key);
textureNames.Add(textureInfo.name);
textureNames.Clear();
textures.Clear();
foreach (var textureInfo in configuredTextures.Where(x => !x.modifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray());
shaderImporter.SaveAndReimport();
}
}

24
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs


if (previewMaterial.shader != defaultPreviewShader)
DestroyImmediate(previewMaterial.shader, true);
previewMaterial.shader = ShaderUtil.CreateShaderAsset(resultShader);
UpdatePreviewProperties();
previewMaterial.shader.hideFlags = HideFlags.DontSave;
}

/// </summary>
public Texture RenderPreview(Rect targetSize)
{
UpdatePreviewProperties();
if (s_Meshes[0] == null)
{
GameObject handleGo = (GameObject)EditorGUIUtility.LoadRequired("Previews/PreviewMaterials.fbx");

break;
}
}
public void ForwardPreviewMaterialPropertyUpdate()
{
var dependentNodes = CollectDependentNodes();
foreach (var node in dependentNodes)
{
if (node.hasPreview)
node.UpdatePreviewProperties();
}
}
protected virtual void CollectPreviewMaterialProperties (List<PreviewProperty> properties)
{
foreach (var s in inputSlots)

private static bool ImplicitConversionExists (ConcreteSlotValueType from, ConcreteSlotValueType to)
{
return (from >= to);
return from >= to || from == ConcreteSlotValueType.Vector1;
}
protected virtual ConcreteSlotValueType ConvertDynamicInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)

return;
var def = node.GetSlotDefaultValue(slot.name);
if (def != null && def.OnGUI())
{
node.UpdatePreviewProperties();
node.ForwardPreviewMaterialPropertyUpdate();
}
def.OnGUI();
}
}

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/ColorNode.cs


var boundProp = boundProperty as ColorProperty;
if (boundProp != null)
boundProp.defaultColor = m_Color;
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;

if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs


return false;
var shaderName = "Hidden/PreviewShader/" + name + "_" + Math.Abs(GetInstanceID());
Dictionary<string, int> defaultTextures;
List<PropertyGenerator.TextureInfo> defaultTextures;
var resultShader = ShaderGenerator.GenerateSurfaceShader(pixelGraph.owner, shaderName, true, out defaultTextures);
m_GeneratedShaderMode = PreviewMode.Preview3D;
InternalUpdatePreviewShader(resultShader);

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TextureNode.cs


if (HasBoundProperty())
return;
visitor.AddShaderProperty(new TexturePropertyChunk(GetPropertyName(), GetPropertyName(), m_DefaultTexture, m_TextureType, true));
visitor.AddShaderProperty(new TexturePropertyChunk(GetPropertyName(), GetPropertyName(), m_DefaultTexture, m_TextureType, false, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)

boundProp.defaultTexture = m_DefaultTexture;
boundProp.defaultTextureType = m_TextureType;
}
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;

}
if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector1Node.cs


{
var boundProp = boundProperty as FloatProperty;
if (boundProp != null)
{
}
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;

if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()

213
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector2Node.cs


using System;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
[Title("Generate/Vector 2 Node")]
class Vector2Node : PropertyNode, IGeneratesBodyCode
{
private const string kOutputSlotName = "Value";
[SerializeField]
private Vector2 m_Value;
public override void OnCreate()
{
base.OnCreate();
name = "V2Node";
}
public override void OnEnable()
{
base.OnEnable();
AddSlot(new MaterialGraphSlot(new Slot(SlotType.OutputSlot, kOutputSlotName), SlotValueType.Vector2));
}
public override PropertyType propertyType
{
get { return PropertyType.Vector2; }
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{}
public override string GetOutputVariableNameForSlot(Slot s, GenerationMode generationMode)
{
return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;
visitor.AddShaderChunk("float2 " + GetPropertyName() + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (HasBoundProperty() || generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "2 " + GetPropertyName() + " = " + precision + "2 (" + m_Value.x + ", " + m_Value.y + ");", true);
}
public override float GetNodeUIHeight(float width)
{
return 2*EditorGUIUtility.singleLineHeight;
}
public override bool NodeUI(Rect drawArea)
{
base.NodeUI(drawArea);
EditorGUI.BeginChangeCheck();
m_Value = EditorGUI.Vector2Field(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), "Value", m_Value);
if (EditorGUI.EndChangeCheck())
{
var boundProp = boundProperty as VectorProperty;
if (boundProp != null)
{
boundProp.defaultVector = m_Value;
}
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;
}
public override void BindProperty(ShaderProperty property, bool rebuildShaders)
{
base.BindProperty(property, rebuildShaders);
var vectorProp = property as VectorProperty;
if (vectorProp)
{
m_Value = vectorProp.defaultVector;
}
if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = GetPropertyName(),
m_PropType = PropertyType.Vector2,
m_Vector4 = m_Value
};
}
}
}
using System;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
[Title("Generate/Vector 2 Node")]
class Vector2Node : PropertyNode, IGeneratesBodyCode
{
private const string kOutputSlotName = "Value";
[SerializeField]
private Vector2 m_Value;
public override void OnCreate()
{
base.OnCreate();
name = "V2Node";
}
public override void OnEnable()
{
base.OnEnable();
AddSlot(new MaterialGraphSlot(new Slot(SlotType.OutputSlot, kOutputSlotName), SlotValueType.Vector2));
}
public override PropertyType propertyType
{
get { return PropertyType.Vector2; }
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{}
public override string GetOutputVariableNameForSlot(Slot s, GenerationMode generationMode)
{
return GetPropertyName();
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)
{
if (HasBoundProperty() || !generationMode.IsPreview())
return;
visitor.AddShaderChunk("float2 " + GetPropertyName() + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (HasBoundProperty() || generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "2 " + GetPropertyName() + " = " + precision + "2 (" + m_Value.x + ", " + m_Value.y + ");", true);
}
public override float GetNodeUIHeight(float width)
{
return 2*EditorGUIUtility.singleLineHeight;
}
public override bool NodeUI(Rect drawArea)
{
base.NodeUI(drawArea);
EditorGUI.BeginChangeCheck();
m_Value = EditorGUI.Vector2Field(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), "Value", m_Value);
if (EditorGUI.EndChangeCheck())
{
var boundProp = boundProperty as VectorProperty;
if (boundProp != null)
{
boundProp.defaultVector = m_Value;
}
return true;
}
return false;
}
public override void BindProperty(ShaderProperty property, bool rebuildShaders)
{
base.BindProperty(property, rebuildShaders);
var vectorProp = property as VectorProperty;
if (vectorProp)
{
m_Value = vectorProp.defaultVector;
}
if (rebuildShaders)
RegeneratePreviewShaders();
}
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = GetPropertyName(),
m_PropType = PropertyType.Vector2,
m_Vector4 = m_Value
};
}
}
}

7
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector4Node.cs


{
boundProp.defaultVector = m_Value;
}
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
return true;
}
return false;

if (rebuildShaders)
RegeneratePreviewShaders();
else
{
UpdatePreviewProperties();
ForwardPreviewMaterialPropertyUpdate();
}
}
public override PreviewProperty GetPreviewProperty()

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Prop/TextureProperty.cs


public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderProperty(new TexturePropertyChunk(name, m_PropertyDescription, m_DefaultTexture, m_DefaultTextureType, false));
visitor.AddShaderProperty(new TexturePropertyChunk(name, m_PropertyDescription, m_DefaultTexture, m_DefaultTextureType, false, true));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType slotValueType)

42
UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs


{
private readonly Texture m_DefaultTexture;
private readonly TextureType m_DefaultTextureType;
private readonly bool m_Modifiable;
public TexturePropertyChunk(string propertyName, string propertyDescription, Texture defaultTexture, TextureType defaultTextureType, bool hidden)
public TexturePropertyChunk(string propertyName, string propertyDescription, Texture defaultTexture, TextureType defaultTextureType, bool hidden, bool modifiable)
m_Modifiable = modifiable;
}
public override string GetPropertyString()

result.Append("[HideInInspector] ");
if (!m_Modifiable)
result.Append("[NonModifiableTextureData] ");
result.Append(m_PropertyName);
result.Append("(\"");

{
get
{
int i = 0;
++i;
}
}
public bool modifiable
{
get
{
return m_Modifiable;
}
}
}

public class PropertyGenerator
{
public struct TextureInfo
{
public string name;
public int textureId;
public bool modifiable;
}
private readonly List<PropertyChunk> m_Properties = new List<PropertyChunk>();
public void AddShaderProperty(PropertyChunk chunk)

return sb.ToString();
}
public Dictionary<string, int> GetDefaultTexutres()
public List<TextureInfo> GetConfiguredTexutres()
var result = new Dictionary<string, int>();
var result = new List<TextureInfo>();
var tex = prop.defaultTexture.GetInstanceID();
result.Add(prop.propertyName, tex);
var textureInfo = new TextureInfo
{
name = prop.propertyName,
textureId = prop.defaultTexture.GetInstanceID(),
modifiable = prop.modifiable
};
result.Add(textureInfo);
}
}
return result;

return template;
}
public static string GenerateSurfaceShader(MaterialGraph graph, string shaderName, bool isPreview, out Dictionary<string, int> defaultTextures)
public static string GenerateSurfaceShader(MaterialGraph graph, string shaderName, bool isPreview, out List<PropertyGenerator.TextureInfo> configuredTxtures)
defaultTextures = new Dictionary<string, int>();
configuredTxtures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}

MaterialWindow.DebugMaterialGraph("----------Shader-----------");
MaterialWindow.DebugMaterialGraph(resultShader);
defaultTextures = shaderPropertiesVisitor.GetDefaultTexutres();
configuredTxtures = shaderPropertiesVisitor.GetConfiguredTexutres();
return resultShader;
}

正在加载...
取消
保存