浏览代码

[Mat Graph] Master Node Inversion refactor

/main
Tim Cooper 8 年前
当前提交
313777e5
共有 43 个文件被更改,包括 845 次插入733 次删除
  1. 2
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Styles/SerializableGraph.uss.imported.asset
  2. 2
      MaterialGraphProject/Assets/NewUI/Editor/Demo/Views/NodalView.uss.imported.asset
  3. 2
      MaterialGraphProject/Assets/NewUI/Editor/Demo/Views/SimpleContentView.uss.imported.asset
  4. 2
      MaterialGraphProject/Assets/NewUI/Editor/Views/GraphView.uss.imported.asset
  5. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/DrawData/NodePreviewDrawData.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphView.cs
  7. 412
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Styles/MaterialGraph.uss.imported.asset
  8. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Styles/MaterialGraph.uss.imported.asset.meta
  9. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/shader.template
  10. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/PropertyNodeTests.cs
  11. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs
  12. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function1InputTests.cs
  13. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function2InputTests.cs
  14. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function3InputTests.cs
  15. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  16. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs
  17. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs
  18. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs
  19. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Interfaces/Interfaces.cs
  20. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/NormalNode.cs
  21. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ScreenPosNode.cs
  22. 24
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs
  23. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UVNode.cs
  24. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ViewDirectionNode.cs
  25. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldPosNode.cs
  26. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs
  27. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs
  28. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  29. 230
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  30. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/COPIED/SelectionDragger.cs.meta
  31. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawData/HeaderDrawData.cs.meta
  32. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Drawer/HeaderDrawer.cs.meta
  33. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMasterNode.cs
  34. 200
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractSurfaceMasterNode.cs
  35. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractSurfaceMasterNode.cs.meta
  36. 57
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MetallicMasterNode.cs
  37. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MetallicMasterNode.cs.meta
  38. 57
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SpecularMasterNode.cs
  39. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SpecularMasterNode.cs.meta
  40. 41
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/3DPreview.template
  41. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/3DPreview.template.meta
  42. 201
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs
  43. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMasterNode.cs.meta

2
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Styles/SerializableGraph.uss.imported.asset


m_Enabled: 1
m_EditorHideFlags: 1
m_Script: {fileID: 11997, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_Name: SerializableGraph.uss.imported
m_EditorClassIdentifier:
matchers:
- specificity: 2

2
MaterialGraphProject/Assets/NewUI/Editor/Demo/Views/NodalView.uss.imported.asset


m_Enabled: 1
m_EditorHideFlags: 1
m_Script: {fileID: 11997, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_Name: NodalView.uss.imported
m_EditorClassIdentifier:
matchers:
- specificity: 102

2
MaterialGraphProject/Assets/NewUI/Editor/Demo/Views/SimpleContentView.uss.imported.asset


m_Enabled: 1
m_EditorHideFlags: 1
m_Script: {fileID: 11997, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_Name: SimpleContentView.uss.imported
m_EditorClassIdentifier:
matchers:
- specificity: 2

2
MaterialGraphProject/Assets/NewUI/Editor/Views/GraphView.uss.imported.asset


m_Enabled: 1
m_EditorHideFlags: 1
m_Script: {fileID: 11997, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_Name: GraphView.uss.imported
m_EditorClassIdentifier:
matchers:
- specificity: 11

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/DrawData/NodePreviewDrawData.cs


var shaderName = "Hidden/PreviewShader/" + localNode.GetVariableNameForNode();
List<PropertyGenerator.TextureInfo> defaultTextures;
//TODO: Need to get the real options somehow
var resultShader = ShaderGenerator.GenerateSurfaceShader(localNode, new MaterialOptions(), shaderName, true, out defaultTextures);
var resultShader = ((AbstractMasterNode) m_Node).GetShader(new MaterialOptions(), GenerationMode.Preview, out defaultTextures);
return ShaderGenerator.GeneratePreviewShader(m_Node, out m_GeneratedShaderMode);
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphView.cs


return null;
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = ShaderGenerator.GenerateSurfaceShader(materialGraph.masterNode, new MaterialOptions(), materialGraph.name, false, out configuredTextures);
var shaderString = materialGraph.masterNode.GetShader(new MaterialOptions(), GenerationMode.ForReals, out configuredTextures);
File.WriteAllText(path, shaderString);
AssetDatabase.Refresh(); // Investigate if this is optimal

412
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Styles/MaterialGraph.uss.imported.asset


depth: 2147483647
previousIndex: -1
nextIndex: -1
- specificity: 102
- specificity: 202
selectorValues:
- MaterialNodeDrawer
selectorTypes: 02000000

nextIndex: 2
- specificity: 102
- specificity: 202
- slots
- header
nextIndex: -1
nextIndex: 3
- title
selectorTypes: 06000000
ruleIndex: 1
depth: 2147483647
previousIndex: 2
nextIndex: -1
- specificity: 102
selectorValues:
nextIndex: 4
- specificity: 202
selectorValues:
- slots
selectorTypes: 06000000
ruleIndex: 2
depth: 2147483647
previousIndex: 3
- specificity: 202
- specificity: 102
- input
- slots
selectorTypes: 06000000
ruleIndex: 2
depth: 2147483647

nextIndex: 8
- specificity: 202
selectorValues:
- output
- input
- specificity: 2
- specificity: 202
- NodeAnchor
- MaterialNodeDrawer
nextIndex: -1
- specificity: 102
nextIndex: 10
- specificity: 202
- NodeAnchor
selectorTypes: 02000000
ruleIndex: 5
- slots
selectorTypes: 06000000
ruleIndex: 4
previousIndex: -1
previousIndex: 9
- specificity: 102
- specificity: 202
- connector
- output
ruleIndex: 5
ruleIndex: 4
- specificity: 112
- specificity: 103
- NodeAnchor
- MaterialNodeDrawer
ruleIndex: 6
ruleIndex: 5
- specificity: 112
- specificity: 103
- connector
- anchorHighlight
selectorTypes: 0600000003000000
ruleIndex: 6
- slots
selectorTypes: 06000000
ruleIndex: 5
nextIndex: -1
- specificity: 102
nextIndex: 14
- specificity: 103
ruleIndex: 7
ruleIndex: 5
previousIndex: -1
nextIndex: 15
- specificity: 102
selectorValues:
- type
selectorTypes: 06000000
ruleIndex: 7
depth: 2147483647
previousIndex: 14
previousIndex: 13
ruleIndex: 8
ruleIndex: 6
nextIndex: 17
nextIndex: 16
ruleIndex: 8
ruleIndex: 6
previousIndex: 16
nextIndex: 18
previousIndex: 15
nextIndex: 17
ruleIndex: 8
ruleIndex: 6
previousIndex: 17
nextIndex: 19
previousIndex: 16
nextIndex: 18
ruleIndex: 8
ruleIndex: 6
previousIndex: 18
previousIndex: 17
ruleIndex: 9
ruleIndex: 7
nextIndex: 21
nextIndex: 20
ruleIndex: 9
ruleIndex: 7
previousIndex: 20
nextIndex: 22
previousIndex: 19
nextIndex: 21
ruleIndex: 9
ruleIndex: 7
previousIndex: 21
nextIndex: 23
previousIndex: 20
nextIndex: 22
ruleIndex: 7
depth: 2147483647
previousIndex: 21
nextIndex: -1
- specificity: 102
selectorValues:
- NodeAnchor
selectorTypes: 02000000
ruleIndex: 8
depth: 2147483647
previousIndex: -1
nextIndex: 24
- specificity: 102
selectorValues:
- connector
selectorTypes: 06000000
ruleIndex: 8
depth: 2147483647
previousIndex: 23
nextIndex: -1
- specificity: 112
selectorValues:
- NodeAnchor
selectorTypes: 02000000
previousIndex: 22
previousIndex: -1
nextIndex: 26
- specificity: 112
selectorValues:
- connector
- anchorHighlight
selectorTypes: 0600000003000000
ruleIndex: 9
depth: 2147483647
previousIndex: 25
nextIndex: -1
- specificity: 203
selectorValues:

depth: 2147483647
previousIndex: -1
nextIndex: 25
nextIndex: 28
- output
- slots
previousIndex: 24
nextIndex: 26
previousIndex: 27
nextIndex: 29
- specificity: 203
selectorValues:
- NodeAnchor

previousIndex: 25
nextIndex: 27
previousIndex: 28
nextIndex: 30
- specificity: 203
selectorValues:
- type

previousIndex: 26
previousIndex: 29
- specificity: 113
- specificity: 303
- Node
- horizontal
selectorTypes: 0200000003000000
- MaterialNodeDrawer
selectorTypes: 02000000
nextIndex: 29
- specificity: 113
nextIndex: 32
- specificity: 303
selectorValues:
- slots
selectorTypes: 06000000
ruleIndex: 11
depth: 2147483647
previousIndex: 31
nextIndex: 33
- specificity: 303
selectorValues:
- input
selectorTypes: 06000000
ruleIndex: 11
depth: 2147483647
previousIndex: 32
nextIndex: 34
- specificity: 303
previousIndex: 28
nextIndex: 30
- specificity: 113
previousIndex: 33
nextIndex: 35
- specificity: 303
- connector
- type
previousIndex: 29
previousIndex: 34
- specificity: 102
- specificity: 303
selectorValues:
- MaterialNodeDrawer
selectorTypes: 02000000

nextIndex: 32
nextIndex: 37
- specificity: 303
selectorValues:
- slots
selectorTypes: 06000000
ruleIndex: 12
depth: 2147483647
previousIndex: 36
nextIndex: 38
- specificity: 303
selectorValues:
- output
selectorTypes: 06000000
ruleIndex: 12
depth: 2147483647
previousIndex: 37
nextIndex: 39
- specificity: 303
selectorValues:
- NodeAnchor
selectorTypes: 02000000
ruleIndex: 12
depth: 2147483647
previousIndex: 38
nextIndex: 40
- specificity: 303
selectorValues:
- type
selectorTypes: 06000000
ruleIndex: 12
depth: 2147483647
previousIndex: 39
nextIndex: -1
- specificity: 102
selectorValues:
- MaterialNodeDrawer
selectorTypes: 02000000
ruleIndex: 13
depth: 2147483647
previousIndex: -1
nextIndex: 42
ruleIndex: 12
ruleIndex: 13
previousIndex: 31
previousIndex: 41
ruleIndex: 13
ruleIndex: 14
nextIndex: 34
nextIndex: 44
ruleIndex: 13
ruleIndex: 14
previousIndex: 33
nextIndex: 35
previousIndex: 43
nextIndex: 45
ruleIndex: 13
ruleIndex: 14
previousIndex: 34
previousIndex: 44
ruleIndex: 14
ruleIndex: 15
nextIndex: 37
nextIndex: 47
ruleIndex: 14
ruleIndex: 15
previousIndex: 36
previousIndex: 46
ruleIndex: 15
ruleIndex: 16
nextIndex: 39
nextIndex: 49
ruleIndex: 15
ruleIndex: 16
previousIndex: 38
nextIndex: 40
previousIndex: 48
nextIndex: 50
ruleIndex: 15
ruleIndex: 16
previousIndex: 39
previousIndex: 49
ruleIndex: 16
ruleIndex: 17
depth: 2147483647
previousIndex: -1
nextIndex: -1

customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
- type: 3
propertyID: 40
valueIndex: 0
- type: 0
propertyID: 17
valueIndex: 0
- type: 1
propertyID: 32
valueIndex: 1
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
valueIndex: 0
valueIndex: 1
valueIndex: 1
valueIndex: 2
valueIndex: 0
- type: 0
propertyID: 23
- type: 0
propertyID: 17
valueIndex: 2
valueIndex: 2
valueIndex: 3
valueIndex: 3
valueIndex: 4
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
propertyID: 28
valueIndex: 2
propertyID: 31
valueIndex: 3
- type: 0
propertyID: 1
- type: 1
propertyID: 26
- type: 0
propertyID: 3
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
- type: 1
propertyID: 26
valueIndex: 5
customPropertyNames: []
customPropertyHandles: []

valueIndex: 1
valueIndex: 2
valueIndex: 2
valueIndex: 3
valueIndex: 5
- type: 0
propertyID: 0
- type: 0
propertyID: 2
valueIndex: 7
valueIndex: 3
valueIndex: 4
valueIndex: 4
valueIndex: 5
valueIndex: 7
valueIndex: 8
propertyID: 26
valueIndex: 3
propertyID: 32
valueIndex: 6
propertyID: 26
valueIndex: 4
propertyID: 32
valueIndex: 7
propertyID: 32
valueIndex: 5
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
- type: 0
propertyID: 0
propertyID: 26
propertyID: 2
propertyID: 7
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles:
- type: 1
propertyID: 26
- type: 3
propertyID: 41
- type: 0
propertyID: 7
valueIndex: 10
valueIndex: 7
valueIndex: 9
valueIndex: 8
valueIndex: 10
- type: 3
propertyID: 41
valueIndex: 7
valueIndex: 11
valueIndex: 10
valueIndex: 12
valueIndex: 11
customPropertyNames: []
customPropertyHandles: []
- builtinPropertyHandles: []

customPropertyHandles:
- type: 3
propertyID: 43
valueIndex: 5
valueIndex: 8
valueIndex: 6
valueIndex: 9
valueIndex: 7
valueIndex: 10
valueIndex: 9
valueIndex: 11
valueIndex: 10
valueIndex: 12
- 200
- 1
- 200
- 5
- 5
- 1
- 1
ints: 0000000002000000020000000200000003000000050000000000000000000000000000004b0000000a000000
ints: 00000000040000000200000002000000020000000300000003000000050000000000000000000000000000004b0000000a000000
- {r: 1, g: 1, b: 1, a: 1}
- {r: 0.26666668, g: 0.26666668, b: 0.26666668, a: 1}
- {r: 0.26666668, g: 0.26666668, b: 0.26666668, a: 1}
- {r: 0.2509804, g: 0.2784314, b: 0.28627452, a: 1}
- {r: 0.1254902, g: 0.16078432, b: 0.24705882, a: 1}
- {r: 0.011764706, g: 0.047058824, b: 0.13333334, a: 1}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Styles/MaterialGraph.uss.imported.asset.meta


fileFormatVersion: 2
guid: 13814e9314253674e8d4966bba14d963
timeCreated: 1478162579
timeCreated: 1478262941
licenseType: Pro
NativeFormatImporter:
userData:

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/shader.template


#pragma glsl
#pragma debug
${LightingFunction}
${ShaderFunctions}
${ShaderPropertyUsages}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/PropertyNodeTests.cs


m_TextureNode.textureType = TextureType.Bump;
m_TextureNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.ForReals);
var expected1 = "[NonModifiableTextureData] "
+ m_TextureNode.propertyName

+ Environment.NewLine;
m_TextureNode.exposedState = PropertyNode.ExposedState.Exposed;
generator = new PropertyGenerator();
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected2, generator.GetShaderString(0));
}

m_TextureNode.defaultTexture = null;
m_TextureNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.ForReals);
var expected = "sampler2D "
+ m_TextureNode.propertyName
+ ";"

m_TextureNode.exposedState = PropertyNode.ExposedState.Exposed;
generator = new ShaderGenerator();
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs


// Generate the shader
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = ShaderGenerator.GenerateSurfaceShader(materialGraph.masterNode, graphAsset.options, materialGraph.name, false, out configuredTextures);
var shaderString = materialGraph.masterNode.GetShader(graphAsset.options, GenerationMode.ForReals, out configuredTextures);
m_Shader = ShaderUtil.CreateShaderAsset(shaderString);
m_Shader.hideFlags = HideFlags.HideAndDontSave;
Assert.IsNotNull(m_Shader, "Shader Generation Failed");

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function1InputTests.cs


);
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeCode(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeCode(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}

+ "}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function2InputTests.cs


);
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeCode(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeCode(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}

+ "}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}

+ "\t}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(1));
}
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function3InputTests.cs


, Environment.NewLine);
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeCode(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeCode(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}

+ "}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.SurfaceShader);
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs


string expected = string.Format("{0}", m_NodeA.GetVariableNameForSlot(TestNode.V1In));
var slot = m_NodeA.GetInputSlots<MaterialSlot>().FirstOrDefault();
var result = m_NodeA.GetSlotValue(slot.id, GenerationMode.Preview2D);
var result = m_NodeA.GetSlotValue(slot.id, GenerationMode.Preview);
Assert.AreEqual(expected, result);
}

string expected = string.Format("{0} {1};{2}", AbstractMaterialNode.OutputPrecision.@fixed, m_NodeA.GetVariableNameForSlot(TestNode.V1In), Environment.NewLine);
var visitor = new ShaderGenerator();
m_NodeA.precision = AbstractMaterialNode.OutputPrecision.@fixed;
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview2D);
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview);
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview2D);
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview);
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview2D);
m_NodeA.GeneratePropertyUsages(visitor, GenerationMode.Preview);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
}

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs


var slot = m_NodeA.slot;
var visitor = new ShaderGenerator();
slot.GeneratePropertyUsages(visitor, GenerationMode.Preview2D);
slot.GeneratePropertyUsages(visitor, GenerationMode.Preview);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}

string expected = string.Format("{0}", m_NodeA.GetVariableNameForSlot(TestNode.V1In));
var slot = m_NodeA.slot;
var result = slot.GetDefaultValue(GenerationMode.Preview2D);
var result = slot.GetDefaultValue(GenerationMode.Preview);
result = slot.GetDefaultValue(GenerationMode.SurfaceShader);
result = slot.GetDefaultValue(GenerationMode.ForReals);
result = slot.GetDefaultValue(GenerationMode.SurfaceShader);
result = slot.GetDefaultValue(GenerationMode.ForReals);
result = slot.GetDefaultValue(GenerationMode.SurfaceShader);
result = slot.GetDefaultValue(GenerationMode.ForReals);
result = slot.GetDefaultValue(GenerationMode.SurfaceShader);
result = slot.GetDefaultValue(GenerationMode.ForReals);
Assert.AreEqual("half4 (6,6,6,1)", result);
}

var slot = new MaterialSlot(0, string.Empty, string.Empty, SlotType.Input, SlotValueType.Vector1, Vector4.zero);
Assert.Throws<Exception>(() => slot.GeneratePropertyUsages(new ShaderGenerator(), GenerationMode.Preview2D));
Assert.Throws<Exception>(() => slot.GetDefaultValue(GenerationMode.Preview2D));
Assert.Throws<Exception>(() => slot.GeneratePropertyUsages(new ShaderGenerator(), GenerationMode.Preview));
Assert.Throws<Exception>(() => slot.GetDefaultValue(GenerationMode.Preview));
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs


m_Graph.Connect(m_Abs.GetSlotReference(Function1Input.OutputSlotId), m_PixelNode.GetSlotReference(AbstractSurfaceMasterNode.AlbedoSlotId));
}
[Test]
public void TestNodeGenerateSurfaceOutputProperly()
{
var generator = new ShaderGenerator();
m_PixelNode.GenerateSurfaceOutput(generator);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
Assert.AreEqual(MetallicMasterNode.SurfaceOutputStructureName, generator.GetPragmaString());
}
[Test]
public void TestNodeGeneratesCorrectNodeCode()

, m_Abs.GetVariableNameForSlot(Function1Input.OutputSlotId));
var generator = new ShaderGenerator();
m_PixelNode.GenerateNodeCode(generator, GenerationMode.SurfaceShader);
m_PixelNode.GenerateNodeCode(generator, GenerationMode.ForReals);
Console.WriteLine(generator.GetShaderString(0));

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs


m_Vector1Node.value = 0.6f;
m_Vector1Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector1Node.propertyName

+ Environment.NewLine;
m_Vector1Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector1Node.value = 0.6f;
m_Vector1Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector1Node.precision

+ Environment.NewLine;
m_Vector1Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector2Node.value = value;
m_Vector2Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector2Node.propertyName

+ Environment.NewLine;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector2Node.value = value;
m_Vector2Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector2Node.precision

+ Environment.NewLine;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector3Node.value = value;
m_Vector3Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector3Node.propertyName

+ Environment.NewLine;
m_Vector3Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector3Node.value = value;
m_Vector3Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector3Node.precision

+ Environment.NewLine;
m_Vector3Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector4Node.value = value;
m_Vector4Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector4Node.propertyName

+ Environment.NewLine;
m_Vector4Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_Vector4Node.value = value;
m_Vector4Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector4Node.precision

+ Environment.NewLine;
m_Vector4Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_ColorNode.color = value;
m_ColorNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_ColorNode.propertyName

+ Environment.NewLine;
m_ColorNode.exposedState = PropertyNode.ExposedState.Exposed;
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

m_ColorNode.color = value;
m_ColorNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_ColorNode.precision

+ Environment.NewLine;
m_ColorNode.exposedState = PropertyNode.ExposedState.Exposed;
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.ForReals);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
}

21
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Interfaces/Interfaces.cs


public enum GenerationMode
{
Preview2D,
Preview3D,
SurfaceShader
Preview,
ForReals
public static bool IsPreview(this GenerationMode mode) { return mode == GenerationMode.Preview2D || mode == GenerationMode.Preview3D; }
public static bool Is2DPreview(this GenerationMode mode) { return mode == GenerationMode.Preview2D; }
public static bool Is3DPreview(this GenerationMode mode) { return mode == GenerationMode.Preview3D; }
public static bool IsPreview(this GenerationMode mode) { return mode == GenerationMode.Preview; }
}
public interface IGeneratesBodyCode

public interface IGeneratesVertexToFragmentBlock
{
void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode);
}
public interface IGeneratesVertexShaderBlock
{
void GenerateVertexShaderBlock(ShaderGenerator visitor, GenerationMode generationMode);
}
public interface IGenerateProperties
{
void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode);

11
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/NormalNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Input/Normal Node")]
public class NormalNode : AbstractMaterialNode
public interface IRequiresNormal
{ }
[Title("Input/World Normal Node")]
public class NormalNode : AbstractMaterialNode, IRequiresNormal
{
private const int kOutputSlotId = 0;
private const string kOutputSlotName = "Normal";

name = "Normal";
name = "World Normal";
UpdateNodeAfterDeserialization();
}

public override string GetVariableNameForSlot(int slotId)
{
return "o.Normal";
return "IN.worldNormal";
}
}
}

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


namespace UnityEngine.MaterialGraph
{
public interface IRequiresScreenPosition
{ }
public class ScreenPosNode : AbstractMaterialNode, IGeneratesVertexToFragmentBlock
public class ScreenPosNode : AbstractMaterialNode, IRequiresScreenPosition
{
public ScreenPosNode()
{

public override bool hasPreview { get { return true; } }
public override PreviewMode previewMode
{
get { return PreviewMode.Preview3D; }
get { return PreviewMode.Preview2D; }
}

public override string GetVariableNameForSlot(int slotId)
{
return "IN.screenPos";
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
string temp = precision + "4 screenPos";
if (generationMode == GenerationMode.Preview2D)
temp += " : TEXCOORD1";
temp += ";";
visitor.AddShaderChunk(temp, true);
}
}
}

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


namespace UnityEngine.MaterialGraph
{
[Title("Input/Texture Node")]
public class TextureNode : PropertyNode, IGeneratesBodyCode, IGeneratesVertexShaderBlock, IGeneratesVertexToFragmentBlock
public class TextureNode : PropertyNode, IGeneratesBodyCode, IRequiresMeshUV
{
protected const string kUVSlotName = "UV";
protected const string kOutputSlotRGBAName = "RGBA";

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(GetPreviewProperty());
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return;
var edges = owner.GetEdges(uvSlot.slotReference);
if (!edges.Any())
UVNode.StaticGenerateVertexToFragmentBlock(visitor, generationMode);
}
public void GenerateVertexShaderBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return;
var edges = owner.GetEdges(uvSlot.slotReference);
if (!edges.Any())
UVNode.GenerateVertexShaderBlock(visitor);
}
// Properties

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


namespace UnityEngine.MaterialGraph
{
interface IRequiresMeshUV
{ }
public class UVNode : AbstractMaterialNode, IGeneratesVertexToFragmentBlock, IGeneratesVertexShaderBlock, IGeneratesBodyCode
public class UVNode : AbstractMaterialNode, IGeneratesBodyCode
{
public const int OutputSlotId = 0;
private const string kOutputSlotName = "UV";

{
AddSlot(new MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
public static void StaticGenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
string temp = "half4 meshUV0";
if (generationMode == GenerationMode.Preview2D)
temp += " : TEXCOORD0";
temp += ";";
visitor.AddShaderChunk(temp, true);
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
StaticGenerateVertexToFragmentBlock(visitor, generationMode);
}
public static void GenerateVertexShaderBlock(ShaderGenerator visitor)
{
visitor.AddShaderChunk("o.meshUV0 = v.texcoord;", true);
}
public void GenerateVertexShaderBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
GenerateVertexShaderBlock(visitor);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

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


namespace UnityEngine.MaterialGraph
{
interface IRequiresViewDirection
{}
public class ViewDirectionNode : AbstractMaterialNode, IGeneratesVertexToFragmentBlock
public class ViewDirectionNode : AbstractMaterialNode, IRequiresViewDirection
{
private const int kOutputSlotId = 0;
private const string kOutputSlotName = "ViewDirection";

public override string GetVariableNameForSlot(int slotId)
{
return "IN.viewDir";
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode == GenerationMode.Preview2D)
throw new InvalidEnumArgumentException(string.Format("Trying to generate 2D preview on {0}. This is not supported!", this));
visitor.AddShaderChunk("float3 viewDir;", true);
}
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldPosNode.cs


namespace UnityEngine.MaterialGraph
{
interface IRequiresWorldPosition
{ }
public class WorldPosNode : AbstractMaterialNode, IGeneratesVertexToFragmentBlock
public class WorldPosNode : AbstractMaterialNode, IRequiresWorldPosition
{
private const int kOutputSlotId = 0;
private const string kOutputSlotName = "WorldPos";

public override string GetVariableNameForSlot(int slotId)
{
return "IN.worldPos";
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk(precision + "3 worldPos;", true);
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs


, IGeneratesBodyCode
, IGeneratesFunction
, IGenerateProperties
, IGeneratesVertexShaderBlock
, IGeneratesVertexToFragmentBlock
{
[NonSerialized]
private SubGraphInputNode m_InputNode;

{
foreach (var node in usedNodes)
{
if (node is IGeneratesVertexShaderBlock)
(node as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(visitor, generationMode);
//TODO: Fix
//if (node is IGeneratesVertexShaderBlock)
// (node as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(visitor, generationMode);
}
}

{
if (node is IGeneratesVertexToFragmentBlock)
(node as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(visitor, generationMode);
//TODO: Fix
//if (node is IGeneratesVertexToFragmentBlock)
// (node as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(visitor, generationMode);
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphInputNode.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode == GenerationMode.SurfaceShader)
if (generationMode == GenerationMode.ForReals)
return;
foreach (var slot in GetOutputSlots<MaterialSlot>())

18
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs


public class SubGraphNode : AbstractMaterialNode
, IGeneratesBodyCode
, IGeneratesFunction
, IGeneratesVertexShaderBlock
, IGeneratesVertexToFragmentBlock
, IOnAssetEnabled
{
[SerializeField]

foreach (var slot in GetInputSlots<MaterialSlot>())
{
var varName = subGraphInputNode.GetVariableNameForSlot(slot.id);
var varValue = GetSlotValue(slot.id, GenerationMode.SurfaceShader);
var varValue = GetSlotValue(slot.id, GenerationMode.ForReals);
var outDimension = ConvertConcreteSlotValueTypeToString(slot.concreteValueType);
outputString.AddShaderChunk(

// Step 4...
// Using the inputs we can now generate the shader body :)
var bodyGenerator = new ShaderGenerator();
subGraph.GenerateNodeCode(bodyGenerator, GenerationMode.SurfaceShader);
subGraph.GenerateNodeCode(bodyGenerator, GenerationMode.ForReals);
var subGraphOutputNode = subGraphAsset.subGraph.outputNode;
outputString.AddShaderChunk(bodyGenerator.GetShaderString(0), false);

{
var inputValue = subGraphOutputNode.GetSlotValue(slot.id, GenerationMode.SurfaceShader);
var inputValue = subGraphOutputNode.GetSlotValue(slot.id, GenerationMode.ForReals);
outputString.AddShaderChunk(
GetVariableNameForSlot(slot.id)

if (subGraph == null)
return;
subGraph.GeneratePropertyBlock(visitor, GenerationMode.SurfaceShader);
subGraph.GeneratePropertyBlock(visitor, GenerationMode.ForReals);
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)

if (subGraph == null)
return;
subGraph.GeneratePropertyUsages(visitor, GenerationMode.SurfaceShader);
subGraph.GeneratePropertyUsages(visitor, GenerationMode.ForReals);
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)

if (subGraph == null)
return;
subGraph.GenerateNodeFunction(visitor, GenerationMode.SurfaceShader);
subGraph.GenerateNodeFunction(visitor, GenerationMode.ForReals);
}
public void GenerateVertexShaderBlock(ShaderGenerator visitor, GenerationMode generationMode)

subGraph.GenerateVertexShaderBlock(visitor, GenerationMode.SurfaceShader);
subGraph.GenerateVertexShaderBlock(visitor, GenerationMode.ForReals);
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)

subGraph.GenerateVertexToFragmentBlock(visitor, GenerationMode.SurfaceShader);
subGraph.GenerateVertexToFragmentBlock(visitor, GenerationMode.ForReals);
}
}
}

230
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


private readonly int m_IndentLevel;
private readonly string m_ShaderChunkString;
public int chunkIndentLevel { get { return m_IndentLevel; } }
public string chunkString { get { return m_ShaderChunkString; } }
public int chunkIndentLevel
{
get { return m_IndentLevel; }
}
public string chunkString
{
get { return m_ShaderChunkString; }
}
}
private readonly List<ShaderChunk> m_ShaderChunks = new List<ShaderChunk>();

m_ShaderChunks.Add(new ShaderChunk(m_IndentLevel, s));
}
public void Indent() { m_IndentLevel++; }
public void Deindent() { m_IndentLevel = Math.Max(0, m_IndentLevel - 1); }
public void Indent()
{
m_IndentLevel++;
}
public void Deindent()
{
m_IndentLevel = Math.Max(0, m_IndentLevel - 1);
}
public string GetShaderString(int baseIndentLevel)
{

return sb.ToString();
}
private static string GetTemplatePath(string templateName)
internal static string GetTemplatePath(string templateName)
{
var path = new List<string>
{

}
private const string kErrorString = @"ERROR!";
public static string AdaptNodeOutput(AbstractMaterialNode node, int outputSlotId, ConcreteSlotValueType convertToType, bool textureSampleUVHack = false)
{
var outputSlot = node.FindOutputSlot<MaterialSlot>(outputSlotId);

switch (convertFromType)
{
case ConcreteSlotValueType.Vector2:
return string.Format("half4({0}.x, {0}.y, 0.0, 0.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, 0.0, 1.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, {0}.z, 0.0)", rawOutput);
return string.Format("half4({0}.x, {0}.y, {0}.z, 1.0)", rawOutput);
default:
return kErrorString;
}

}
public int numberOfChunks
{
get { return m_ShaderChunks.Count; }
}
public static string GeneratePreviewShader(AbstractMaterialNode node, out PreviewMode generatedShaderMode)
{
if (!node.GetOutputSlots<MaterialSlot>().Any())

// figure out what kind of preview we want!
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
var generationMode = GenerationMode.Preview2D;
{
generationMode = GenerationMode.Preview3D;
}
string templateLocation = GetTemplatePath(generationMode == GenerationMode.Preview2D ? "2DPreview.template" : "3DPreview.template");
string templateLocation = GetTemplatePath("2DPreview.template");
if (!File.Exists(templateLocation))
return null;

var shaderInputVisitor = new ShaderGenerator();
var shaderName = "Hidden/PreviewShader/" + node.GetVariableNameForSlot(node.GetOutputSlots<MaterialSlot>().First().id);
var shaderInputVisitor = new ShaderGenerator();
var shaderName = "Hidden/PreviewShader/" + node.GetVariableNameForSlot(node.GetOutputSlots<MaterialSlot>().First().id);
// always add color because why not.
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true);
vertexShaderBlock.AddShaderChunk("float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;", true);
vertexShaderBlock.AddShaderChunk("float3 viewDir = UnityWorldSpaceViewDir(worldPos);", true);
vertexShaderBlock.AddShaderChunk("float4 screenPos = ComputeScreenPos(UnityObjectToClipPos(v.vertex));", true);
vertexShaderBlock.AddShaderChunk("float3 worldNormal = UnityObjectToWorldNormal(v.normal);", true);
if (activeNodeList.Any(x => x is IRequiresWorldPosition))
{
shaderInputVisitor.AddShaderChunk("float3 worldPos : TEXCOORD2;", true);
vertexShaderBlock.AddShaderChunk("o.worldPos = worldPos;", true);
}
if (activeNodeList.Any(x => x is IRequiresNormal))
{
shaderInputVisitor.AddShaderChunk("float3 worldNormal : TEXCOORD3;", true);
vertexShaderBlock.AddShaderChunk("o.worldNormal = worldNormal;", true);
}
if (activeNodeList.Any(x => x is IRequiresMeshUV))
{
shaderInputVisitor.AddShaderChunk("half4 meshUV0 : TEXCOORD0;", true);
vertexShaderBlock.AddShaderChunk("o.meshUV0 = v.texcoord;", true);
}
if (activeNodeList.Any(x => x is IRequiresViewDirection))
{
shaderInputVisitor.AddShaderChunk("float3 viewDir : TEXCOORD1;", true);
vertexShaderBlock.AddShaderChunk("o.viewDir = viewDir;", true);
}
if (activeNodeList.Any(x => x is IRequiresScreenPosition))
{
shaderInputVisitor.AddShaderChunk("float4 screenPos : TEXCOORD3;", true);
vertexShaderBlock.AddShaderChunk("o.screenPos = screenPos;", true);
}
var generationMode = GenerationMode.Preview;
if (activeNode is IGeneratesVertexToFragmentBlock)
(activeNode as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(shaderInputVisitor, generationMode);
if (activeNode is IGeneratesVertexShaderBlock)
(activeNode as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(vertexShaderBlock, generationMode);
if (shaderInputVisitor.numberOfChunks == 0)
{
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true);
}
if (generationMode == GenerationMode.Preview2D)
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutputForPreview(node, node.GetOutputSlots<MaterialSlot>().First().id, ConcreteSlotValueType.Vector4) + ";", true);
else
shaderBodyVisitor.AddShaderChunk("o.Emission = " + AdaptNodeOutputForPreview(node, node.GetOutputSlots<MaterialSlot>().First().id, ConcreteSlotValueType.Vector3) + ";", true);
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutputForPreview(node, node.GetOutputSlots<MaterialSlot>().First().id, ConcreteSlotValueType.Vector4) + ";", true);
ListPool<INode>.Release(activeNodeList);
template = template.Replace("${ShaderName}", shaderName);
template = template.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
template = template.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(3));

}
return Regex.Replace(template, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
private static void GenerateSurfaceShaderInternal(
AbstractMasterNode masterNode,
ShaderGenerator shaderBody,
ShaderGenerator inputStruct,
ShaderGenerator lightFunction,
ShaderGenerator surfaceOutput,
ShaderGenerator nodeFunction,
PropertyGenerator shaderProperties,
ShaderGenerator propertyUsages,
ShaderGenerator vertexShader,
bool isPreview)
{
masterNode.GenerateSurfaceOutput(surfaceOutput);
masterNode.GenerateLightFunction(lightFunction);
var genMode = isPreview ? GenerationMode.Preview3D : GenerationMode.SurfaceShader;
var activeNodes = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, masterNode);
var activeMaterialNodes = activeNodes.OfType<AbstractMaterialNode>();
foreach (var node in activeMaterialNodes)
{
if (node is IGeneratesFunction) (node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, genMode);
if (node is IGeneratesVertexToFragmentBlock) (node as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(inputStruct, genMode);
if (node is IGeneratesVertexShaderBlock) (node as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(vertexShader, genMode);
if (node is IGenerateProperties)
{
(node as IGenerateProperties).GeneratePropertyBlock(shaderProperties, genMode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, genMode);
}
}
masterNode.GenerateNodeCode(shaderBody, genMode);
}
public static string GenerateSurfaceShader(AbstractMasterNode node, MaterialOptions options, string shaderName, bool isPreview, out List<PropertyGenerator.TextureInfo> configuredTextures)
{
var templateLocation = GetTemplatePath("shader.template");
if (!File.Exists(templateLocation))
{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}
var templateText = File.ReadAllText(templateLocation);
var shaderBodyVisitor = new ShaderGenerator();
var shaderInputVisitor = new ShaderGenerator();
var shaderLightFunctionVisitor = new ShaderGenerator();
var shaderOutputSurfaceVisitor = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var shaderPropertiesVisitor = new PropertyGenerator();
var shaderPropertyUsagesVisitor = new ShaderGenerator();
var vertexShaderBlock = new ShaderGenerator();
GenerateSurfaceShaderInternal(
node,
shaderBodyVisitor,
shaderInputVisitor,
shaderLightFunctionVisitor,
shaderOutputSurfaceVisitor,
shaderFunctionVisitor,
shaderPropertiesVisitor,
shaderPropertyUsagesVisitor,
vertexShaderBlock,
isPreview);
if (shaderInputVisitor.numberOfChunks == 0)
{
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true);
}
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
options.GetTags(tagsVisitor);
options.GetBlend(blendingVisitor);
options.GetCull(cullingVisitor);
options.GetDepthTest(zTestVisitor);
options.GetDepthWrite(zWriteVisitor);
var resultShader = templateText.Replace("${ShaderName}", shaderName);
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LightingFunctionName}", shaderLightFunctionVisitor.GetPragmaString());
resultShader = resultShader.Replace("${LightingFunction}", shaderLightFunctionVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${SurfaceOutputStructureName}", shaderOutputSurfaceVisitor.GetPragmaString());
resultShader = resultShader.Replace("${ShaderFunctions}", shaderFunctionVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ShaderInputs}", shaderInputVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${PixelShaderBody}", shaderBodyVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
string vertexShaderBody = vertexShaderBlock.GetShaderString(3);
if (vertexShaderBody.Length > 0)
{
resultShader = resultShader.Replace("${VertexShaderDecl}", "vertex:vert");
resultShader = resultShader.Replace("${VertexShaderBody}", vertexShaderBody);
}
else
{
resultShader = resultShader.Replace("${VertexShaderDecl}", "");
resultShader = resultShader.Replace("${VertexShaderBody}", "");
}
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
public int numberOfChunks
{
get { return m_ShaderChunks.Count; }
}
}
}

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/COPIED/SelectionDragger.cs.meta


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

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/DrawData/HeaderDrawData.cs.meta


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

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Drawer/HeaderDrawer.cs.meta


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

32
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMasterNode.cs


using System;
using System.Collections.Generic;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
interface IMasterNode
{
string GetShader(
MaterialOptions options,
GenerationMode mode,
out List<PropertyGenerator.TextureInfo> configuredTextures);
}
[Serializable]
public abstract class AbstractMasterNode : AbstractMaterialNode, IMasterNode
{
protected override bool generateDefaultInputs { get { return false; } }
public override IEnumerable<ISlot> GetInputsWithNoConnection()
{
return new List<ISlot>();
}
public override bool hasPreview
{
get { return true; }
}
public abstract string GetShader(MaterialOptions options, GenerationMode mode, out List<PropertyGenerator.TextureInfo> configuredTextures);
}
}

200
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractSurfaceMasterNode.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Serializable]
[Title("Math/Add Node")]
public abstract class AbstractSurfaceMasterNode : AbstractMasterNode
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";
public const string EmissionSlotName = "Emission";
public const string SmoothnessSlotName = "Smoothness";
public const string OcclusionSlotName = "Occlusion";
public const string AlphaSlotName = "Alpha";
public const int AlbedoSlotId = 0;
public const int NormalSlotId = 1;
public const int EmissionSlotId = 3;
public const int SmoothnessSlotId = 4;
public const int OcclusionSlotId = 5;
public const int AlphaSlotId = 6;
public abstract string GetSurfaceOutputName();
public abstract string GetLightFunction();
public override string GetShader(
MaterialOptions options,
GenerationMode mode,
out List<PropertyGenerator.TextureInfo> configuredTextures)
{
var templateLocation = ShaderGenerator.GetTemplatePath("shader.template");
if (!File.Exists(templateLocation))
{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}
var templateText = File.ReadAllText(templateLocation);
var shaderBodyVisitor = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var shaderPropertiesVisitor = new PropertyGenerator();
var shaderPropertyUsagesVisitor = new ShaderGenerator();
var shaderInputVisitor = new ShaderGenerator();
var vertexShaderBlock = new ShaderGenerator();
GenerateSurfaceShaderInternal(
shaderBodyVisitor,
shaderFunctionVisitor,
shaderInputVisitor,
vertexShaderBlock,
shaderPropertiesVisitor,
shaderPropertyUsagesVisitor,
mode);
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
options.GetTags(tagsVisitor);
options.GetBlend(blendingVisitor);
options.GetCull(cullingVisitor);
options.GetDepthTest(zTestVisitor);
options.GetDepthWrite(zWriteVisitor);
var resultShader = templateText.Replace("${ShaderName}", GetType() + guid.ToString());
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LightingFunctionName}", GetLightFunction());
resultShader = resultShader.Replace("${SurfaceOutputStructureName}", GetSurfaceOutputName());
resultShader = resultShader.Replace("${ShaderFunctions}", shaderFunctionVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ShaderInputs}", shaderInputVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${PixelShaderBody}", shaderBodyVisitor.GetShaderString(3));
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${VertexShaderDecl}", "vertex:vert");
resultShader = resultShader.Replace("${VertexShaderBody}", vertexShaderBlock.GetShaderString(3));
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
private void GenerateSurfaceShaderInternal(
ShaderGenerator shaderBody,
ShaderGenerator nodeFunction,
ShaderGenerator shaderInputVisitor,
ShaderGenerator vertexShaderBlock,
PropertyGenerator shaderProperties,
ShaderGenerator propertyUsages,
GenerationMode mode)
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
{
if (node is IGeneratesFunction) (node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, mode);
if (node is IGenerateProperties)
{
(node as IGenerateProperties).GeneratePropertyBlock(shaderProperties, mode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, mode);
}
}
// always add color because why not.
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true);
if (activeNodeList.Any(x => x is IRequiresMeshUV))
{
shaderInputVisitor.AddShaderChunk("half4 meshUV0 : TEXCOORD0;", true);
vertexShaderBlock.AddShaderChunk("o.meshUV0 = v.texcoord;", true);
}
if (activeNodeList.Any(x => x is IRequiresViewDirection))
{
shaderInputVisitor.AddShaderChunk("float3 viewDir;", true);
}
if (activeNodeList.Any(x => x is IRequiresWorldPosition))
{
shaderInputVisitor.AddShaderChunk("float3 worldPos;", true);
}
if (activeNodeList.Any(x => x is IRequiresScreenPosition))
{
shaderInputVisitor.AddShaderChunk("float4 screenPos;", true);
}
GenerateNodeCode(shaderBody, mode);
}
public void GenerateNodeCode(ShaderGenerator shaderBody, GenerationMode generationMode)
{
var firstPassSlotId = NormalSlotId;
// do the normal slot first so that it can be used later in the shader :)
var firstPassSlot = FindInputSlot<MaterialSlot>(firstPassSlotId);
var nodes = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, firstPassSlotId, NodeUtils.IncludeSelf.Exclude);
for (int index = 0; index < nodes.Count; index++)
{
var node = nodes[index];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
foreach (var edge in owner.GetEdges(firstPassSlot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
shaderBody.AddShaderChunk("o." + firstPassSlot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
// track the last index of nodes... they have already been processed :)
int pass2StartIndex = nodes.Count;
//Get the rest of the nodes for all the other slots
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, null, NodeUtils.IncludeSelf.Exclude);
for (var i = pass2StartIndex; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
ListPool<INode>.Release(nodes);
foreach (var slot in GetInputSlots<MaterialSlot>())
{
foreach (var edge in owner.GetEdges(slot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
shaderBody.AddShaderChunk("o." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
}
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractSurfaceMasterNode.cs.meta


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

57
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MetallicMasterNode.cs


using System;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Serializable]
[Title("Master/Metallic")]
public class MetallicMasterNode : AbstractSurfaceMasterNode
{
public const string MetallicSlotName = "Metallic";
public const int MetallicSlotId = 2;
public const string LightFunctionName = "Standard";
public const string SurfaceOutputStructureName = "SurfaceOutputStandard";
public MetallicMasterNode()
{
name = "MetallicMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
public override string GetSurfaceOutputName()
{
return SurfaceOutputStructureName;
}
public override string GetLightFunction()
{
return LightFunctionName;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MetallicMasterNode.cs.meta


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

57
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SpecularMasterNode.cs


using System;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Serializable]
[Title("Master/Specular")]
public class SpecularMasterNode : AbstractSurfaceMasterNode
{
public const string SpecularSlotName = "Specular";
public const int SpecularSlotId = 2;
public const string LightFunctionName = "StandardSpecular";
public const string SurfaceOutputStructureName = "SurfaceOutputStandardSpecular";
public SpecularMasterNode()
{
name = "SpecularMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
public override string GetSurfaceOutputName()
{
return SurfaceOutputStructureName;
}
public override string GetLightFunction()
{
return LightFunctionName;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SpecularMasterNode.cs.meta


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

41
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/3DPreview.template


Shader "${ShaderName}"
{
Properties
{
${ShaderPropertiesHeader}
}
SubShader
{
LOD 200
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert ${VertexShaderDecl}
#pragma glsl
#pragma debug
${ShaderFunctions}
${ShaderPropertyUsages}
struct Input
{
${ShaderInputs}
};
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
${VertexShaderBody}
}
void surf (Input IN, inout SurfaceOutput o)
{
${PixelShaderBody}
}
ENDCG
}
FallBack "Diffuse"
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/3DPreview.template.meta


fileFormatVersion: 2
guid: 7d370877f47fbe644a45ccf5960f4c54
DefaultImporter:
userData:

201
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs


using System;
using System.Collections.Generic;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Serializable][Title("Math/Add Node")]
public abstract class AbstractSurfaceMasterNode : AbstractMasterNode
{
public const string AlbedoSlotName = "Albedo";
public const string NormalSlotName = "Normal";
public const string EmissionSlotName = "Emission";
public const string SmoothnessSlotName = "Smoothness";
public const string OcclusionSlotName = "Occlusion";
public const string AlphaSlotName = "Alpha";
public const int AlbedoSlotId = 0;
public const int NormalSlotId = 1;
public const int EmissionSlotId = 3;
public const int SmoothnessSlotId = 4;
public const int OcclusionSlotId = 5;
public const int AlphaSlotId = 6;
public override void GenerateNodeCode(ShaderGenerator shaderBody, GenerationMode generationMode)
{
var firstPassSlotId = NormalSlotId;
// do the normal slot first so that it can be used later in the shader :)
var firstPassSlot = FindInputSlot<MaterialSlot>(firstPassSlotId);
var nodes = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, firstPassSlotId, NodeUtils.IncludeSelf.Exclude);
for (int index = 0; index < nodes.Count; index++)
{
var node = nodes[index];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
foreach (var edge in owner.GetEdges(firstPassSlot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
shaderBody.AddShaderChunk("o." + firstPassSlot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
// track the last index of nodes... they have already been processed :)
int pass2StartIndex = nodes.Count;
//Get the rest of the nodes for all the other slots
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, null, NodeUtils.IncludeSelf.Exclude);
for (var i = pass2StartIndex; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
ListPool<INode>.Release(nodes);
foreach (var slot in GetInputSlots<MaterialSlot>())
{
foreach (var edge in owner.GetEdges(slot.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
shaderBody.AddShaderChunk("o." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
}
}
}
[Serializable]
[Title("Master/Metallic")]
public class MetallicMasterNode : AbstractSurfaceMasterNode
{
public const string MetallicSlotName = "Metallic";
public const int MetallicSlotId = 2;
public const string LightFunctionName = "Standard";
public const string SurfaceOutputStructureName = "SurfaceOutputStandard";
public MetallicMasterNode()
{
name = "MetallicMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
public override void GenerateSurfaceOutput(ShaderGenerator surfaceOutput)
{
surfaceOutput.AddPragmaChunk(SurfaceOutputStructureName);
}
public override void GenerateLightFunction(ShaderGenerator lightFunction)
{
lightFunction.AddPragmaChunk(LightFunctionName);
}
}
[Serializable]
[Title("Master/Specular")]
public class SpecularMasterNode : AbstractSurfaceMasterNode
{
public const string SpecularSlotName = "Specular";
public const int SpecularSlotId = 2;
public const string LightFunctionName = "StandardSpecular";
public const string SurfaceOutputStructureName = "SurfaceOutputStandardSpecular";
public SpecularMasterNode()
{
name = "SpecularMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero));
// clear out slot names that do not match the slots
// we support
RemoveSlotsNameNotMatching(
new[]
{
AlbedoSlotId,
NormalSlotId,
EmissionSlotId,
SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}
public override void GenerateSurfaceOutput(ShaderGenerator surfaceOutput)
{
surfaceOutput.AddPragmaChunk(SurfaceOutputStructureName);
}
public override void GenerateLightFunction(ShaderGenerator lightFunction)
{
lightFunction.AddPragmaChunk(LightFunctionName);
}
}
[Serializable]
public abstract class AbstractMasterNode : AbstractMaterialNode, IGeneratesBodyCode
{
protected override bool generateDefaultInputs { get { return false; } }
public override IEnumerable<ISlot> GetInputsWithNoConnection()
{
return new List<ISlot>();
}
public override bool hasPreview
{
get { return true; }
}
public abstract void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode);
public abstract void GenerateSurfaceOutput (ShaderGenerator surfaceOutput);
public abstract void GenerateLightFunction(ShaderGenerator lightFunction);
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMasterNode.cs.meta

正在加载...
取消
保存