浏览代码
Refactored subgraph to abstract subgraph + remap subgraph. This is groundwork to allow for real remap master nodes.
/main
Refactored subgraph to abstract subgraph + remap subgraph. This is groundwork to allow for real remap master nodes.
/main
Tim Cooper
7 年前
当前提交
76430e32
共有 22 个文件被更改,包括 457 次插入 和 689 次删除
-
7MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/NodeUtils.cs
-
1MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
-
18MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Interfaces/IGenerateProperties.cs
-
116MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraph.cs
-
75MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapInputNode.cs
-
15MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MaterialRemapAsset.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MaterialSubGraphAsset.cs
-
94MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs
-
290MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphOutputNode.cs
-
23MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporter.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporter.cs.meta
-
41MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporterEditor.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporterEditor.cs.meta
-
65MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraph.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraph.cs.meta
-
229MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs.meta
-
12MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/RemapMasterNode.cs.meta
-
140MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/RemapMasterNode.cs
|
|||
using System.IO; |
|||
using System.Text; |
|||
using UnityEditor.Experimental.AssetImporters; |
|||
using UnityEngine; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
[ScriptedImporter(1, "ShaderReampGraph")] |
|||
public class MasterRemapGraphImporter : ScriptedImporter |
|||
{ |
|||
public override void OnImportAsset(AssetImportContext ctx) |
|||
{ |
|||
var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8); |
|||
var graph = JsonUtility.FromJson<MasterRemapGraph>(textGraph); |
|||
|
|||
if (graph == null) |
|||
return; |
|||
|
|||
var graphAsset = ScriptableObject.CreateInstance<MasterRemapGraphAsset>(); |
|||
graphAsset.remapGraph = graph; |
|||
ctx.AddObjectToAsset("MainAsset", graphAsset); |
|||
ctx.SetMainObject(graphAsset); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 47e38a0256e24cec8792d9cfae7023b2 |
|||
timeCreated: 1506845544 |
|
|||
using UnityEditor; |
|||
using UnityEditor.Experimental.AssetImporters; |
|||
using UnityEditor.MaterialGraph.Drawing; |
|||
using UnityEngine; |
|||
using Debug = System.Diagnostics.Debug; |
|||
|
|||
[CustomEditor(typeof(MasterRemapGraphImporter))] |
|||
public class MasterRemapGraphImporterEditor : ScriptedImporterEditor |
|||
{ |
|||
public override void OnInspectorGUI() |
|||
{ |
|||
if (GUILayout.Button("Open Shader Editor")) |
|||
{ |
|||
AssetImporter importer = target as AssetImporter; |
|||
Debug.Assert(importer != null, "importer != null"); |
|||
ShowGraphEditWindow(importer.assetPath); |
|||
} |
|||
} |
|||
|
|||
private static void ShowGraphEditWindow(string path) |
|||
{ |
|||
var asset = AssetDatabase.LoadAssetAtPath<Object>(path); |
|||
var windows = Resources.FindObjectsOfTypeAll<MasterReampGraphEditWindow>(); |
|||
bool foundWindow = false; |
|||
foreach (var w in windows) |
|||
{ |
|||
if (w.selected == asset) |
|||
{ |
|||
foundWindow = true; |
|||
w.Focus(); |
|||
} |
|||
} |
|||
|
|||
if (!foundWindow) |
|||
{ |
|||
var window = CreateInstance<MasterReampGraphEditWindow>(); |
|||
window.Show(); |
|||
window.ChangeSelection(asset); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4dd321ee05034e7a967503617a02c1f4 |
|||
timeCreated: 1506845783 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Serializable] |
|||
public class AbstractSubGraph : AbstractMaterialGraph |
|||
, IGeneratesBodyCode |
|||
, IGeneratesFunction |
|||
{ |
|||
public virtual IEnumerable<AbstractMaterialNode> activeNodes { get; } |
|||
|
|||
public PreviewMode previewMode |
|||
{ |
|||
get { return activeNodes.Any(x => x.previewMode == PreviewMode.Preview3D) ? PreviewMode.Preview3D : PreviewMode.Preview2D; } |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
foreach (var node in activeNodes) |
|||
{ |
|||
if (node is IGeneratesBodyCode) |
|||
(node as IGeneratesBodyCode).GenerateNodeCode(visitor, generationMode); |
|||
} |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
foreach (var node in activeNodes) |
|||
{ |
|||
if (node is IGeneratesFunction) |
|||
(node as IGeneratesFunction).GenerateNodeFunction(visitor, generationMode); |
|||
} |
|||
} |
|||
|
|||
public override void CollectShaderProperties(PropertyCollector collector, GenerationMode generationMode) |
|||
{ |
|||
// if we are previewing the graph we need to
|
|||
// export 'exposed props' if we are 'for real'
|
|||
// then we are outputting the graph in the
|
|||
// nested context and the needed values will
|
|||
// be copied into scope.
|
|||
if (generationMode == GenerationMode.Preview) |
|||
{ |
|||
foreach (var prop in properties) |
|||
collector.AddShaderProperty(prop); |
|||
} |
|||
|
|||
foreach (var node in activeNodes) |
|||
{ |
|||
if (node is IGenerateProperties) |
|||
(node as IGenerateProperties).CollectShaderProperties(collector, generationMode); |
|||
} |
|||
} |
|||
|
|||
public IEnumerable<PreviewProperty> GetPreviewProperties() |
|||
{ |
|||
List<PreviewProperty> props = new List<PreviewProperty>(); |
|||
foreach (var node in activeNodes) |
|||
node.CollectPreviewMaterialProperties(props); |
|||
return props; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f2b67435729448498da7e7b90f052ebd |
|||
timeCreated: 1506844687 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
public abstract class AbstractSubGraphNode : AbstractMaterialNode |
|||
, IGeneratesFunction |
|||
, IOnAssetEnabled |
|||
, IMayRequireNormal |
|||
, IMayRequireTangent |
|||
, IMayRequireBitangent |
|||
, IMayRequireMeshUV |
|||
, IMayRequireScreenPosition |
|||
, IMayRequireViewDirection |
|||
, IMayRequirePosition |
|||
, IMayRequireVertexColor |
|||
, IMayRequireTime |
|||
{ |
|||
|
|||
protected virtual AbstractSubGraph subGraph { get; } |
|||
|
|||
public override bool hasPreview |
|||
{ |
|||
get { return subGraph != null; } |
|||
} |
|||
|
|||
public override PreviewMode previewMode |
|||
{ |
|||
get |
|||
{ |
|||
if (subGraph == null) |
|||
return PreviewMode.Preview2D; |
|||
|
|||
return PreviewMode.Preview3D; |
|||
} |
|||
} |
|||
|
|||
public virtual INode outputNode { get; } = null; |
|||
|
|||
public virtual void OnEnable() |
|||
{ |
|||
var validNames = new List<int>(); |
|||
if (subGraph == null) |
|||
{ |
|||
RemoveSlotsNameNotMatching(validNames); |
|||
return; |
|||
} |
|||
|
|||
var props = subGraph.properties; |
|||
foreach (var prop in props) |
|||
{ |
|||
var propType = prop.propertyType; |
|||
SlotValueType slotType; |
|||
|
|||
switch (propType) |
|||
{ |
|||
case PropertyType.Color: |
|||
slotType = SlotValueType.Vector4; |
|||
break; |
|||
case PropertyType.Texture: |
|||
slotType = SlotValueType.Texture2D; |
|||
break; |
|||
case PropertyType.Float: |
|||
slotType = SlotValueType.Vector1; |
|||
break; |
|||
case PropertyType.Vector2: |
|||
slotType = SlotValueType.Vector2; |
|||
break; |
|||
case PropertyType.Vector3: |
|||
slotType = SlotValueType.Vector3; |
|||
break; |
|||
case PropertyType.Vector4: |
|||
slotType = SlotValueType.Vector4; |
|||
break; |
|||
case PropertyType.Matrix2: |
|||
slotType = SlotValueType.Matrix2; |
|||
break; |
|||
case PropertyType.Matrix3: |
|||
slotType = SlotValueType.Matrix3; |
|||
break; |
|||
case PropertyType.Matrix4: |
|||
slotType = SlotValueType.Matrix4; |
|||
break; |
|||
default: |
|||
throw new ArgumentOutOfRangeException(); |
|||
} |
|||
|
|||
var id = prop.guid.GetHashCode(); |
|||
AddSlot(new MaterialSlot(id, prop.displayName, prop.referenceName, SlotType.Input, slotType, prop.defaultValue)); |
|||
validNames.Add(id); |
|||
} |
|||
|
|||
var subGraphOutputNode = outputNode; |
|||
if (outputNode != null) |
|||
{ |
|||
foreach (var slot in subGraphOutputNode.GetInputSlots<MaterialSlot>()) |
|||
{ |
|||
AddSlot(new MaterialSlot(slot.id, slot.displayName, slot.shaderOutputName, SlotType.Output, slot.valueType, slot.defaultValue)); |
|||
validNames.Add(slot.id); |
|||
} |
|||
} |
|||
|
|||
RemoveSlotsNameNotMatching(validNames); |
|||
} |
|||
|
|||
public override void CollectShaderProperties(PropertyCollector visitor, GenerationMode generationMode) |
|||
{ |
|||
base.CollectShaderProperties(visitor, generationMode); |
|||
|
|||
if (subGraph == null) |
|||
return; |
|||
|
|||
subGraph.CollectShaderProperties(visitor, GenerationMode.ForReals); |
|||
} |
|||
|
|||
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties) |
|||
{ |
|||
base.CollectPreviewMaterialProperties(properties); |
|||
|
|||
if (subGraph == null) |
|||
return; |
|||
|
|||
properties.AddRange(subGraph.GetPreviewProperties()); |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
if (subGraph == null) |
|||
return; |
|||
|
|||
subGraph.GenerateNodeFunction(visitor, GenerationMode.ForReals); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresNormal() |
|||
{ |
|||
if (subGraph == null) |
|||
return NeededCoordinateSpace.None; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => |
|||
{ |
|||
mask |= node.RequiresNormal(); |
|||
return mask; |
|||
}); |
|||
} |
|||
|
|||
public bool RequiresMeshUV(UVChannel channel) |
|||
{ |
|||
if (subGraph == null) |
|||
return false; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)); |
|||
} |
|||
|
|||
public bool RequiresScreenPosition() |
|||
{ |
|||
if (subGraph == null) |
|||
return false; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition()); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresViewDirection() |
|||
{ |
|||
if (subGraph == null) |
|||
return NeededCoordinateSpace.None; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => |
|||
{ |
|||
mask |= node.RequiresViewDirection(); |
|||
return mask; |
|||
}); |
|||
} |
|||
|
|||
|
|||
public NeededCoordinateSpace RequiresPosition() |
|||
{ |
|||
if (subGraph == null) |
|||
return NeededCoordinateSpace.None; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => |
|||
{ |
|||
mask |= node.RequiresPosition(); |
|||
return mask; |
|||
}); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresTangent() |
|||
{ |
|||
if (subGraph == null) |
|||
return NeededCoordinateSpace.None; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => |
|||
{ |
|||
mask |= node.RequiresTangent(); |
|||
return mask; |
|||
}); |
|||
} |
|||
|
|||
public bool RequiresTime() |
|||
{ |
|||
if (subGraph == null) |
|||
return false; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireTime>().Any(x => x.RequiresTime()); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresBitangent() |
|||
{ |
|||
if (subGraph == null) |
|||
return NeededCoordinateSpace.None; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => |
|||
{ |
|||
mask |= node.RequiresBitangent(); |
|||
return mask; |
|||
}); |
|||
} |
|||
|
|||
public bool RequiresVertexColor() |
|||
{ |
|||
if (subGraph == null) |
|||
return false; |
|||
|
|||
return subGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor()); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: bc4cf774a7a6480ba543eb6069cd1c50 |
|||
timeCreated: 1506847426 |
|
|||
fileFormatVersion: 2 |
|||
guid: 8f94baf06fa91ad41bc0184e6d14cb0f |
|||
timeCreated: 1481188026 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text.RegularExpressions; |
|||
#if UNITY_EDITOR
|
|||
using UnityEditor; |
|||
#endif
|
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
/* [Serializable] |
|||
[Title("Master/Remapper")] |
|||
public class RemapMasterNode : AbstractMasterNode |
|||
, IOnAssetEnabled |
|||
{ |
|||
[SerializeField] |
|||
private string m_SerialziedRemapGraph = string.Empty; |
|||
|
|||
[Serializable] |
|||
private class RemapGraphHelper |
|||
{ |
|||
public MaterialRemapAsset subGraph; |
|||
} |
|||
|
|||
public override bool allowedInRemapGraph |
|||
{ |
|||
get { return false; } |
|||
} |
|||
|
|||
public override string GetFullShader(GenerationMode mode, string name, out List<PropertyGenerator.TextureInfo> configuredTextures) |
|||
{ |
|||
var shaderTemplateLocation = ShaderGenerator.GetTemplatePath("shader.template"); |
|||
|
|||
if (remapAsset == null || !File.Exists(shaderTemplateLocation)) |
|||
{ |
|||
configuredTextures = new List<PropertyGenerator.TextureInfo>(); |
|||
return string.Empty; |
|||
} |
|||
|
|||
var shaderPropertiesVisitor = new PropertyGenerator(); |
|||
|
|||
// Step 1: Set this node as the remap target
|
|||
// Pass in the shader properties visitor here as
|
|||
// high level properties are shared
|
|||
// this is only used for the header
|
|||
var subShaders = remapAsset.masterRemapGraph.GetSubShadersFor(this, mode, shaderPropertiesVisitor); |
|||
|
|||
var templateText = File.ReadAllText(shaderTemplateLocation); |
|||
var resultShader = templateText.Replace("${ShaderName}", name); |
|||
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2)); |
|||
if (subShaders != null) |
|||
resultShader = resultShader.Replace("${SubShader}", subShaders.Aggregate(string.Empty, (i, j) => i + Environment.NewLine + j)); |
|||
else |
|||
resultShader = resultShader.Replace("${SubShader}", string.Empty); |
|||
|
|||
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres(); |
|||
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine); |
|||
} |
|||
|
|||
public override string GetSubShader(GenerationMode mode, PropertyGenerator shaderPropertiesVisitor) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties) |
|||
{ |
|||
base.CollectPreviewMaterialProperties(properties); |
|||
if (remapAsset == null) |
|||
return; |
|||
|
|||
remapAsset.masterRemapGraph.CollectPreviewMaterialProperties(properties); |
|||
} |
|||
|
|||
#if UNITY_EDITOR
|
|||
public MaterialRemapAsset remapAsset |
|||
{ |
|||
get |
|||
{ |
|||
if (string.IsNullOrEmpty(m_SerialziedRemapGraph)) |
|||
return null; |
|||
|
|||
var helper = new RemapGraphHelper(); |
|||
EditorJsonUtility.FromJsonOverwrite(m_SerialziedRemapGraph, helper); |
|||
return helper.subGraph; |
|||
} |
|||
set |
|||
{ |
|||
if (remapAsset == value) |
|||
return; |
|||
|
|||
var helper = new RemapGraphHelper(); |
|||
helper.subGraph = value; |
|||
m_SerialziedRemapGraph = EditorJsonUtility.ToJson(helper, true); |
|||
OnEnable(); |
|||
|
|||
if (onModified != null) |
|||
onModified(this, ModificationScope.Topological); |
|||
} |
|||
} |
|||
#else
|
|||
public MaterialSubGraphAsset subGraphAsset {get; set; } |
|||
#endif
|
|||
|
|||
public override PreviewMode previewMode |
|||
{ |
|||
get |
|||
{ |
|||
if (remapAsset == null) |
|||
return PreviewMode.Preview2D; |
|||
|
|||
return PreviewMode.Preview3D; |
|||
} |
|||
} |
|||
|
|||
public RemapMasterNode() |
|||
{ |
|||
name = "Remapper"; |
|||
} |
|||
|
|||
public void OnEnable() |
|||
{ |
|||
var validNames = new List<int>(); |
|||
if (remapAsset == null) |
|||
{ |
|||
RemoveSlotsNameNotMatching(validNames); |
|||
return; |
|||
} |
|||
|
|||
var inputNode = remapAsset.masterRemapGraph.inputNode; |
|||
foreach (var slot in inputNode.GetOutputSlots<MaterialSlot>()) |
|||
{ |
|||
AddSlot(new MaterialSlot(slot.id, slot.displayName, slot.shaderOutputName, SlotType.Input, slot.valueType, slot.defaultValue)); |
|||
validNames.Add(slot.id); |
|||
} |
|||
RemoveSlotsNameNotMatching(validNames); |
|||
} |
|||
}*/ |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue