浏览代码

Merge pull request #173 from Unity-Technologies/CreateNodeOnSpace

Fixes #159, fixes #88, fixes #157, fixes #162
/main
GitHub 7 年前
当前提交
16ddad23
共有 15 个文件被更改,包括 99 次插入56 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  2. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableCubemap.cs
  3. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  8. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/NodeUtils.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs
  10. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector2Node.cs
  11. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector3Node.cs
  12. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector4Node.cs
  13. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  14. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
var channelCount = SlotValueHelper.GetChannelCount(concreteValueType);
var values = value.x.ToString();
string values = NodeUtils.FloatToShaderValue(value.x);
if (channelCount == 1)
return values;
for (var i = 1; i < channelCount; i++)

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableCubemap.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SerializableCubemap : ISerializationCallbackReceiver
public class SerializableCubemap
{
[SerializeField]
private string m_SerializedCubemap;

public Cubemap cubemap;
}
Cubemap m_Cubemap;
if (m_Cubemap == null && !string.IsNullOrEmpty(m_SerializedCubemap))
{
var cube = new CubemapHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedCubemap, cube);
m_Cubemap = cube.cubemap;
m_SerializedCubemap = null;
}
return m_Cubemap;
}
set { m_Cubemap = value; }
}
if (string.IsNullOrEmpty(m_SerializedCubemap))
return null;
public void OnBeforeSerialize()
{
var cube = new CubemapHelper { cubemap = cubemap };
m_SerializedCubemap = EditorJsonUtility.ToJson(cube, true);
}
var cube = new CubemapHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedCubemap, cube);
return cube.cubemap;
}
set
{
if(cubemap == value)
return;
public void OnAfterDeserialize()
{
var cubemapHelper = new CubemapHelper();
cubemapHelper.cubemap = value;
m_SerializedCubemap = EditorJsonUtility.ToJson(cubemapHelper, true);
}
}
}
}

35
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SerializableTexture : ISerializationCallbackReceiver
public class SerializableTexture
{
[SerializeField]
private string m_SerializedTexture;

public Texture texture;
}
Texture m_Texture;
if (m_Texture == null && !string.IsNullOrEmpty(m_SerializedTexture))
{
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
m_Texture = tex.texture;
m_SerializedTexture = null;
}
return m_Texture;
if (string.IsNullOrEmpty(m_SerializedTexture))
return null;
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
set { m_Texture = value; }
}
set
{
if (texture == value)
return;
public void OnBeforeSerialize()
{
var tex = new TextureHelper { texture = texture };
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
}
public void OnAfterDeserialize()
{
var textureHelper = new TextureHelper();
textureHelper.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(textureHelper, true);
}
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return value.ToString();
return NodeUtils.FloatToShaderValue(value);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2 (" + value.x + "," + value.y + ")";
return precision + "2 (" + NodeUtils.FloatToShaderValue(value.x) + "," + NodeUtils.FloatToShaderValue(value.y) + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3 (" + value.x + "," + value.y + "," + value.z + ")";
return precision + "3 (" + NodeUtils.FloatToShaderValue(value.x) + "," + NodeUtils.FloatToShaderValue(value.y) + "," + NodeUtils.FloatToShaderValue(value.z) + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
return precision + "4 (" + NodeUtils.FloatToShaderValue(value.x) + "," + NodeUtils.FloatToShaderValue(value.y) + "," + NodeUtils.FloatToShaderValue(value.z) + "," + NodeUtils.FloatToShaderValue(value.w) + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)

20
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Implementation/NodeUtils.cs


using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
namespace UnityEditor.Graphing
{

char[] arr = input.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (Char.IsLetterOrDigit(c))));
return new string(arr);
}
public static string FloatToShaderValue(float value)
{
if (Single.IsPositiveInfinity(value))
return "1.#INF";
else if (Single.IsNegativeInfinity(value))
return "-1.#INF";
else if (Single.IsNaN(value))
return "NAN";
else
{
decimal amount;
if( decimal.TryParse(value.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out amount) )
return amount.ToString();
else
return value.ToString();
}
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs


if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_Value + ";", true);
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + NodeUtils.FloatToShaderValue(m_Value) + ";", true);
}
public override string GetVariableNameForSlot(int slotId)

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector2Node.cs


if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "2 " + GetVariableNameForNode() + " = " + precision + "2" + m_Value + ";", true);
visitor.AddShaderChunk(string.Format("{0}2 {1} = {0}2({2},{3});", precision, GetVariableNameForNode(), NodeUtils.FloatToShaderValue(m_Value.x), NodeUtils.FloatToShaderValue(m_Value.y)), true);
}
public override string GetVariableNameForSlot(int slotId)

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector3Node.cs


if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "3 " + GetVariableNameForNode() + " = " + precision + "3" + m_Value + ";", true);
var s = string.Format("{0}3 {1} = {0}3({2},{3},{4});",
precision,
GetVariableNameForNode(),
NodeUtils.FloatToShaderValue(m_Value.x),
NodeUtils.FloatToShaderValue(m_Value.y),
NodeUtils.FloatToShaderValue(m_Value.z));
visitor.AddShaderChunk(s, true);
}
public override string GetVariableNameForSlot(int slotId)

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector4Node.cs


if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + precision + "4" + m_Value + ";", true);
var s = string.Format("{0}4 {1} = {0}4({2},{3},{4},{5});",
precision,
GetVariableNameForNode(),
NodeUtils.FloatToShaderValue(m_Value.x),
NodeUtils.FloatToShaderValue(m_Value.y),
NodeUtils.FloatToShaderValue(m_Value.z),
NodeUtils.FloatToShaderValue(m_Value.w));
visitor.AddShaderChunk(s, true);
}
public override string GetVariableNameForSlot(int slotId)

17
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


[NonSerialized]
bool m_HasError;
[NonSerialized]
public bool forceRedrawPreviews = false;
GraphEditorView m_GraphEditorView;
GraphEditorView graphEditorView

m_GraphEditorView.RemoveFromHierarchy();
m_GraphEditorView.Dispose();
}
m_GraphEditorView = value;
if (m_GraphEditorView != null)
{

graphEditorView = new GraphEditorView(this, materialGraph, asset.name) { persistenceKey = selectedGuid };
}
if (forceRedrawPreviews)
{
// Redraw all previews
foreach (INode node in m_GraphObject.graph.GetNodes<INode>())
node.Dirty(ModificationScope.Node);
forceRedrawPreviews = false;
}
graphEditorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}

Undo.ClearUndo(graphObject);
DestroyImmediate(graphObject);
}
graphEditorView = null;
}

var inputSlotRef = new SlotReference(remappedInputNodeGuid, inputSlot.slotId);
subGraph.Connect(outputSlotRef, inputSlotRef);
}
// one edge needs to go to outside world
else if (outputSlotExistsInSubgraph)
{

var graph = graphObject.graph as IShaderGraph;
if (graph == null)
return;
List<PropertyCollector.TextureInfo> configuredTextures;
graph.GetShader(Path.GetFileNameWithoutExtension(path), GenerationMode.ForReals, out configuredTextures);
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderGraphImporter;
if (shaderImporter == null)

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs


using System.Text;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing;
[ScriptedImporter(4, ShaderGraphImporter.ShaderGraphExtension)]
public class ShaderGraphImporter : ScriptedImporter

var text = GetShaderText<MaterialGraph>(ctx.assetPath, out configuredTextures);
if (text == null)
text = errorShader;
var name = Path.GetFileNameWithoutExtension(ctx.assetPath);
string shaderName = string.Format("graphs/{0}", name);
text = text.Replace("Hidden/GraphErrorShader2", shaderName);
var shader = ShaderUtil.CreateShaderAsset(text);

static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
MaterialGraphEditWindow[] windows = Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>();
foreach (var matGraphEditWindow in windows)
{
matGraphEditWindow.forceRedrawPreviews = true;
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


}
MaterialGraphView {
background-color: rgb(20, 21, 21);
background-color: #202020;
}
MaterialNodeView.graphElement.node.MaterialNode {

正在加载...
取消
保存