void ConvertToProperty(GraphData graphData) { AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ConvertToPropertyAction"); AssertHelpers.IsNotNull(inlinePropertiesToConvert, "InlinePropertiesToConvert is null while carrying out ConvertToPropertyAction"); graphData.owner.RegisterCompleteObjectUndo("Convert to Property"); foreach (var converter in inlinePropertiesToConvert) { var convertedProperty = converter.AsShaderProperty(); var node = converter as AbstractMaterialNode; graphData.AddGraphInput(convertedProperty); // Add reference to converted property for use in responding to this action later convertedPropertyReferences.Add(convertedProperty); var propNode = new PropertyNode(); propNode.drawState = node.drawState; propNode.group = node.group; graphData.AddNode(propNode); propNode.property = convertedProperty; var oldSlot = node.FindSlot <MaterialSlot>(converter.outputSlotId); var newSlot = propNode.FindSlot <MaterialSlot>(PropertyNode.OutputSlotId); foreach (var edge in graphData.GetEdges(oldSlot.slotReference)) { graphData.Connect(newSlot.slotReference, edge.inputSlot); } graphData.RemoveNode(node); } }
public static void ApplyActionLeafFirst(GraphData graph, Action <AbstractMaterialNode> action) { var temporaryMarks = PooledHashSet <string> .Get(); var permanentMarks = PooledHashSet <string> .Get(); var slots = ListPool <MaterialSlot> .Get(); // Make sure we process a node's children before the node itself. var stack = StackPool <AbstractMaterialNode> .Get(); foreach (var node in graph.GetNodes <AbstractMaterialNode>()) { stack.Push(node); } while (stack.Count > 0) { var node = stack.Pop(); if (permanentMarks.Contains(node.objectId)) { continue; } if (temporaryMarks.Contains(node.objectId)) { action.Invoke(node); permanentMarks.Add(node.objectId); } else { temporaryMarks.Add(node.objectId); stack.Push(node); node.GetInputSlots(slots); foreach (var inputSlot in slots) { var nodeEdges = graph.GetEdges(inputSlot.slotReference); foreach (var edge in nodeEdges) { var fromSocketRef = edge.outputSlot; var childNode = fromSocketRef.node; if (childNode != null) { stack.Push(childNode); } } } slots.Clear(); } } StackPool <AbstractMaterialNode> .Release(stack); ListPool <MaterialSlot> .Release(slots); temporaryMarks.Dispose(); permanentMarks.Dispose(); }
void ConvertToProperty(GraphData graphData) { AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ConvertToPropertyAction"); AssertHelpers.IsNotNull(inlinePropertiesToConvert, "InlinePropertiesToConvert is null while carrying out ConvertToPropertyAction"); graphData.owner.RegisterCompleteObjectUndo("Convert to Property"); var defaultCategory = graphData.categories.FirstOrDefault(); AssertHelpers.IsNotNull(defaultCategory, "Default Category is null while carrying out ConvertToPropertyAction"); foreach (var converter in inlinePropertiesToConvert) { var convertedProperty = converter.AsShaderProperty(); var node = converter as AbstractMaterialNode; graphData.AddGraphInput(convertedProperty); // Also insert this input into the default category if (defaultCategory != null) { var addItemToCategoryAction = new AddItemToCategoryAction(); addItemToCategoryAction.categoryGuid = defaultCategory.categoryGuid; addItemToCategoryAction.itemToAdd = convertedProperty; graphData.owner.graphDataStore.Dispatch(addItemToCategoryAction); } // Add reference to converted property for use in responding to this action later convertedPropertyReferences.Add(convertedProperty); var propNode = new PropertyNode(); propNode.drawState = node.drawState; propNode.group = node.group; graphData.AddNode(propNode); propNode.property = convertedProperty; var oldSlot = node.FindSlot <MaterialSlot>(converter.outputSlotId); var newSlot = propNode.FindSlot <MaterialSlot>(PropertyNode.OutputSlotId); foreach (var edge in graphData.GetEdges(oldSlot.slotReference)) { graphData.Connect(newSlot.slotReference, edge.inputSlot); } graphData.RemoveNode(node); } }
static void GenerateSurfaceDescriptionRemap( GraphData graph, AbstractMaterialNode rootNode, IEnumerable <MaterialSlot> slots, ShaderStringBuilder surfaceDescriptionFunction, GenerationMode mode) { if (rootNode is IMasterNode || rootNode is SubGraphOutputNode) { var usedSlots = slots ?? rootNode.GetInputSlots <MaterialSlot>(); foreach (var input in usedSlots) { if (input != null) { var foundEdges = graph.GetEdges(input.slotReference).ToArray(); var hlslName = NodeUtils.GetHLSLSafeName(input.shaderOutputName); if (rootNode is SubGraphOutputNode) { hlslName = $"{hlslName}_{input.id}"; } if (foundEdges.Any()) { surfaceDescriptionFunction.AppendLine("surface.{0} = {1};", hlslName, rootNode.GetSlotValue(input.id, mode, rootNode.concretePrecision)); } else { surfaceDescriptionFunction.AppendLine("surface.{0} = {1};", hlslName, input.GetDefaultValue(mode, rootNode.concretePrecision)); } } } } else if (rootNode.hasPreview) { var slot = rootNode.GetOutputSlots <MaterialSlot>().FirstOrDefault(); if (slot != null) { var hlslSafeName = $"{NodeUtils.GetHLSLSafeName(slot.shaderOutputName)}_{slot.id}"; surfaceDescriptionFunction.AppendLine("surface.{0} = {1};", hlslSafeName, rootNode.GetSlotValue(slot.id, mode, rootNode.concretePrecision)); } } }