浏览代码

texture exposition working

/main
Tim Cooper 7 年前
当前提交
8d5e6fd1
共有 10 个文件被更改,包括 101 次插入42 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  2. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs
  4. 33
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
  5. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs
  6. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  8. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraph.cs
  9. 59
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  10. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/PropertyCollector.cs

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


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

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


var materialGraph = (UnityEngine.MaterialGraph.MaterialGraph) node.owner;
List<PropertyCollector.TextureInfo> defaultTextures;
previewData.shaderString = materialGraph.GetShader(node.guid + "_preview", out defaultTextures);
previewData.shaderString = materialGraph.GetShader(node.guid + "_preview", GenerationMode.Preview, out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;
}
else if (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex)

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


var name = Path.GetFileNameWithoutExtension(path);
List<PropertyCollector.TextureInfo> configuredTextures;
var shaderString = graph.GetShader(string.Format("graphs/{0}", name), out configuredTextures);
var shaderString = graph.GetShader(string.Format("graphs/{0}", name), GenerationMode.ForReals, out configuredTextures);
Debug.Log(shaderString);
return shaderString;
}

33
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs


surfaceDescription.AddShaderChunk("struct SurfaceDescription{", false);
surfaceDescription.Indent();
if (mode == GenerationMode.Preview)
if (node is MasterNode)
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
surfaceDescription.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + node.GetVariableNameForSlot(slot.id) + ";", false);
foreach (var slot in node.GetInputSlots<MaterialSlot>())
surfaceDescription.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + slot.shaderOutputName + ";", false);
foreach (var slot in node.GetInputSlots<MaterialSlot>())
surfaceDescription.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + slot.shaderOutputName + ";", false);
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
surfaceDescription.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + node.GetVariableNameForSlot(slot.id) + ";", false);
}
surfaceDescription.Deindent();
surfaceDescription.AddShaderChunk("};", false);

}
pixelShader.AddShaderChunk("SurfaceDescription surface = (SurfaceDescription)0;", false);
if (mode == GenerationMode.Preview)
{
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
pixelShader.AddShaderChunk(string.Format("surface.{0} = {0};", node.GetVariableNameForSlot(slot.id)), true);
}
else
if (node is MasterNode)
{
foreach (var input in node.GetInputSlots<MaterialSlot>())
{

}
}
}
else
{
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
pixelShader.AddShaderChunk(string.Format("surface.{0} = {0};", node.GetVariableNameForSlot(slot.id)), true);
}
pixelShader.AddShaderChunk("return surface;", false);
pixelShader.Deindent();
pixelShader.AddShaderChunk("}", false);

finalShader.AddShaderChunk(pixelShader.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
if (mode == GenerationMode.Preview)
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
else
if (node is MasterNode)
}
else
{
finalShader.AddShaderChunk(ShaderGenerator.GetPreviewSubShader(node, requirements), false);
}
finalShader.Deindent();

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs


get { return GetNodes<MasterNode>().FirstOrDefault(); }
}
public string GetShader(string name, out List<PropertyCollector.TextureInfo> configuredTextures)
public string GetShader(string name, GenerationMode mode, out List<PropertyCollector.TextureInfo> configuredTextures)
PreviewMode mode;
return GetShader(masterNode, GenerationMode.ForReals, name, out configuredTextures, out mode);
PreviewMode pmode;
return GetShader(masterNode, mode, name, out configuredTextures, out pmode);
}
}

20
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs


[SerializeField]
ShaderStage m_ShaderStage;
public static readonly string DefaultTextureName = "ShaderGraph_DefaultTexture";
public MaterialSlot() { }
public MaterialSlot(int slotId, string displayName, string shaderOutputName, SlotType slotType, SlotValueType valueType, Vector4 defaultValue, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)

if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
if (concreteValueType == ConcreteSlotValueType.Texture2D)
return DefaultTextureName;
if (generationMode.IsPreview())
return matOwner.GetVariableNameForSlot(id);

}
}
public void AddProperty(PropertyCollector properties, GenerationMode generationMode)
public void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
if (!generationMode.IsPreview())
// share tex2d for all non connected slots :)
if (concreteValueType == ConcreteSlotValueType.Texture2D)
{
var prop = new TextureShaderProperty();
prop.name = DefaultTextureName;
prop.modifiable = false;
prop.generatePropertyBlock = true;
properties.AddShaderProperty(prop);
}
if (concreteValueType == ConcreteSlotValueType.SamplerState ||
concreteValueType == ConcreteSlotValueType.Texture2D)
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;

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


if (edges.Any())
continue;
inputSlot.AddProperty(properties, generationMode);
inputSlot.AddDefaultProperty(properties, generationMode);
}
}

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


}
}
public override void CollectShaderProperties(PropertyCollector visitor, GenerationMode 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);
}
(node as IGenerateProperties).CollectShaderProperties(visitor, generationMode);
(node as IGenerateProperties).CollectShaderProperties(collector, generationMode);
}
}

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


// Step 3...
// For each input that is used and connects through we want to generate code.
// First we assign the input variables to the subgraph
var subGraphInputs = subGraphAsset.subGraph.properties;
// we do this by renaming the properties to be the names of where the variables come from
// weird, but works.
var sSubGraph = SerializationHelper.Serialize<SubGraph>(subGraphAsset.subGraph);
var subGraph = SerializationHelper.Deserialize<SubGraph>(sSubGraph, null);
var subGraphInputs = subGraph.properties;
//todo:
// copy whole subgraph
// rename properties to match what we want (external scope)
// then generate graph
subGraphAsset.subGraph.CollectShaderProperties(propertyGen, GenerationMode.ForReals);
subGraph.CollectShaderProperties(propertyGen, GenerationMode.ForReals);
var varName = prop.name;
var slotId = prop.guid.GetHashCode();
var slot = FindInputSlot<MaterialSlot>(slotId);
var varValue = GetSlotValue(slotId, generationMode);
var inSlotId = prop.guid.GetHashCode();
var inSlot = FindInputSlot<MaterialSlot>(inSlotId);
var edges = owner.GetEdges(inSlot.slotReference).ToArray();
outputString.AddShaderChunk(
ConvertConcreteSlotValueTypeToString(precision, slot.concreteValueType)
+ " "
+ varName
+ " = "
+ varValue
+ ";", false);
string varValue = inSlot.GetDefaultValue(generationMode);
if (edges.Any())
{
var fromSocketRef = edges[0].outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(fromSocketRef.nodeGuid);
if (fromNode != null)
{
var slot = fromNode.FindOutputSlot<MaterialSlot>(fromSocketRef.slotId);
if (slot != null)
prop.name = fromNode.GetSlotValue(slot.id, generationMode);
}
}
else if (inSlot.concreteValueType == ConcreteSlotValueType.Texture2D)
{
prop.name = MaterialSlot.DefaultTextureName;
}
else
{
var varName = prop.name;
outputString.AddShaderChunk(
ConvertConcreteSlotValueTypeToString(precision, inSlot.concreteValueType)
+ " "
+ varName
+ " = "
+ varValue
+ ";", false);
}
}
// Step 4...

var subGraphOutputNode = subGraphAsset.subGraph.outputNode;
var subGraphOutputNode = subGraph.outputNode;
outputString.AddShaderChunk(bodyGenerator.GetShaderString(0), false);
// Step 5...

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/PropertyCollector.cs


public string GetPropertiesBlock(int baseIndentLevel)
{
var sb = new StringBuilder();
foreach (var prop in m_Properties)
foreach (var prop in m_Properties.Where(x => x.generatePropertyBlock))
{
for (var i = 0; i < baseIndentLevel; i++)
sb.Append("\t");

正在加载...
取消
保存