public override void Action(int instanceId, string pathName, string resourceFile) { var graph = new MaterialGraph(); var mainTextureNode = new MainTextureNode(); var mainTextureSamplerNode = new SampleTexture2DNode(); var masterNode = new ImageEffectMasterNode(); graph.AddNode(masterNode); graph.AddNode(mainTextureSamplerNode); graph.AddNode(mainTextureNode); graph.Connect(mainTextureNode.GetSlotReference(MainTextureNode.OutputSlotId), mainTextureSamplerNode.GetSlotReference(SampleTexture2DNode.TextureInputId)); graph.Connect(mainTextureSamplerNode.GetSlotReference(SampleTexture2DNode.OutputSlotRGBAId), masterNode.GetSlotReference(ImageEffectMasterNode.ColorSlotId)); var nodes = new AbstractMaterialNode[] { masterNode, mainTextureSamplerNode, mainTextureNode }; for (int i = 0; i < nodes.Length; i++) { var node = nodes[i]; var drawState = node.drawState; var drawStatePosition = drawState.position; drawStatePosition.x -= 300 * i; drawState.position = drawStatePosition; node.drawState = drawState; } File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph)); AssetDatabase.Refresh(); }
protected override AbstractMaterialNode CreateNode(ShaderGraphBuilder builder, MaterialExpressionTextureSample unrealNode) { var node = new SampleTexture2DNode() { previewExpanded = false, }; if (unrealNode.Texture != null) { // FIXME: don't use unresolved reference directly and don't guess the extension node.FindInputSlot <Texture2DInputMaterialSlot>(SampleTexture2DNode.TextureInputId).texture = Helper.LoadTexture(unrealNode.Texture.FileName); } return(node); }
public override void Action(int instanceId, string pathName, string resourceFile) { var graph = new MaterialGraph(); var mainTextureNode = new MainTextureNode(); var mainTextureSamplerNode = new SampleTexture2DNode(); var masterNode = new ImageEffectMasterNode(); graph.AddNode(masterNode); graph.AddNode(mainTextureSamplerNode); graph.AddNode(mainTextureNode); graph.Connect(mainTextureNode.GetSlotReference(MainTextureNode.OutputSlotId), mainTextureSamplerNode.GetSlotReference(SampleTexture2DNode.TextureInputId)); graph.Connect(mainTextureSamplerNode.GetSlotReference(SampleTexture2DNode.OutputSlotRGBAId), masterNode.GetSlotReference(ImageEffectMasterNode.ColorSlotId)); var nodes = new AbstractMaterialNode[] { masterNode, mainTextureSamplerNode, mainTextureNode }; for (int i = 0; i < nodes.Length; i++) { var node = nodes[i]; var drawState = node.drawState; var drawStatePosition = drawState.position; drawStatePosition.x -= 300 * i; drawState.position = drawStatePosition; node.drawState = drawState; } File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph)); AssetDatabase.Refresh(); if (CreateShaderWithMaterial) { var shaderName = Path.GetFileNameWithoutExtension(pathName); var shader = Shader.Find(shaderName); if (shader == null) { return; } var material = new Material(shader); var materialPath = Path.Combine(Path.GetDirectoryName(pathName), shaderName + ".mat"); AssetDatabase.CreateAsset(material, materialPath); AssetDatabase.SaveAssets(); } }
void CreateNode(object obj, Vector2 nodePosition) { var texture2D = obj as Texture2D; if (texture2D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture"); bool isNormalMap = false; if (EditorUtility.IsPersistent(texture2D) && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(texture2D))) { var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture2D)) as TextureImporter; if (importer != null) { isNormalMap = importer.textureType == TextureImporterType.NormalMap; } } var node = new SampleTexture2DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); if (isNormalMap) { node.textureType = TextureType.Normal; } var inputslot = node.FindInputSlot <Texture2DInputMaterialSlot>(SampleTexture2DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture2D; } } var textureArray = obj as Texture2DArray; if (textureArray != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture Array"); var property = new Texture2DArrayShaderProperty { displayName = textureArray.name, value = { textureArray = textureArray } }; graph.AddShaderProperty(property); var node = new SampleTexture2DArrayNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture2DArrayInputMaterialSlot>(SampleTexture2DArrayNode.TextureInputId); if (inputslot != null) { inputslot.textureArray = textureArray; } } var texture3D = obj as Texture3D; if (texture3D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture 3D"); var property = new Texture3DShaderProperty { displayName = texture3D.name, value = { texture = texture3D } }; graph.AddShaderProperty(property); var node = new SampleTexture3DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture3DInputMaterialSlot>(SampleTexture3DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture3D; } } var cubemap = obj as Cubemap; if (cubemap != null) { graph.owner.RegisterCompleteObjectUndo("Drag Cubemap"); var property = new CubemapShaderProperty { displayName = cubemap.name, value = { cubemap = cubemap } }; graph.AddShaderProperty(property); var node = new SampleCubemapNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindInputSlot <CubemapInputMaterialSlot>(SampleCubemapNode.CubemapInputId); if (inputslot != null) { inputslot.cubemap = cubemap; } } var subGraphAsset = obj as SubGraphAsset; if (subGraphAsset != null) { graph.owner.RegisterCompleteObjectUndo("Drag Sub-Graph"); var node = new SubGraphNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; node.subGraphAsset = subGraphAsset; graph.AddNode(node); } var blackboardField = obj as BlackboardField; if (blackboardField != null) { AbstractShaderProperty property = blackboardField.userData as AbstractShaderProperty; if (property != null) { graph.owner.RegisterCompleteObjectUndo("Drag Property"); var node = new PropertyNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); // Setting the guid requires the graph to be set first. node.propertyGuid = property.guid; } } }
void CreateNode(object obj, Vector2 nodePosition) { var texture2D = obj as Texture2D; if (texture2D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture"); bool isNormalMap = false; if (EditorUtility.IsPersistent(texture2D) && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(texture2D))) { var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture2D)) as TextureImporter; if (importer != null) { isNormalMap = importer.textureType == TextureImporterType.NormalMap; } } var node = new SampleTexture2DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); if (isNormalMap) { node.textureType = TextureType.Normal; } var inputslot = node.FindInputSlot <Texture2DInputMaterialSlot>(SampleTexture2DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture2D; } } var textureArray = obj as Texture2DArray; if (textureArray != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture Array"); var node = new SampleTexture2DArrayNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture2DArrayInputMaterialSlot>(SampleTexture2DArrayNode.TextureInputId); if (inputslot != null) { inputslot.textureArray = textureArray; } } var texture3D = obj as Texture3D; if (texture3D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture 3D"); var node = new SampleTexture3DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture3DInputMaterialSlot>(SampleTexture3DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture3D; } } var cubemap = obj as Cubemap; if (cubemap != null) { graph.owner.RegisterCompleteObjectUndo("Drag Cubemap"); var node = new SampleCubemapNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindInputSlot <CubemapInputMaterialSlot>(SampleCubemapNode.CubemapInputId); if (inputslot != null) { inputslot.cubemap = cubemap; } } var subGraphAsset = obj as SubGraphAsset; if (subGraphAsset != null) { graph.owner.RegisterCompleteObjectUndo("Drag Sub-Graph"); var node = new SubGraphNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; node.asset = subGraphAsset; graph.AddNode(node); } var blackboardField = obj as BlackboardField; if (blackboardField != null) { graph.owner.RegisterCompleteObjectUndo("Drag Graph Input"); switch (blackboardField.userData) { case AbstractShaderProperty property: { var node = new PropertyNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); // Setting the guid requires the graph to be set first. node.propertyGuid = property.guid; break; } case ShaderKeyword keyword: { var node = new KeywordNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); // Setting the guid requires the graph to be set first. node.keywordGuid = keyword.guid; break; } default: throw new ArgumentOutOfRangeException(); } } }
void CreateNode(object obj, Vector2 nodePosition) { var texture2D = obj as Texture2D; if (texture2D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture"); bool isNormalMap = false; if (EditorUtility.IsPersistent(texture2D) && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(texture2D))) { var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture2D)) as TextureImporter; if (importer != null) { isNormalMap = importer.textureType == TextureImporterType.NormalMap; } } var node = new SampleTexture2DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); if (isNormalMap) { node.textureType = TextureType.Normal; } var inputslot = node.FindInputSlot <Texture2DInputMaterialSlot>(SampleTexture2DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture2D; } } var textureArray = obj as Texture2DArray; if (textureArray != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture Array"); var node = new SampleTexture2DArrayNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture2DArrayInputMaterialSlot>(SampleTexture2DArrayNode.TextureInputId); if (inputslot != null) { inputslot.textureArray = textureArray; } } var texture3D = obj as Texture3D; if (texture3D != null) { graph.owner.RegisterCompleteObjectUndo("Drag Texture 3D"); var node = new SampleTexture3DNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindSlot <Texture3DInputMaterialSlot>(SampleTexture3DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture3D; } } var cubemap = obj as Cubemap; if (cubemap != null) { graph.owner.RegisterCompleteObjectUndo("Drag Cubemap"); var node = new SampleCubemapNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); var inputslot = node.FindInputSlot <CubemapInputMaterialSlot>(SampleCubemapNode.CubemapInputId); if (inputslot != null) { inputslot.cubemap = cubemap; } } var subGraphAsset = obj as SubGraphAsset; if (subGraphAsset != null) { graph.owner.RegisterCompleteObjectUndo("Drag Sub-Graph"); var node = new SubGraphNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; node.asset = subGraphAsset; graph.AddNode(node); } var blackboardField = obj as BlackboardField; if (blackboardField != null) { graph.owner.RegisterCompleteObjectUndo("Drag Graph Input"); switch (blackboardField.userData) { case AbstractShaderProperty property: { // This could be from another graph, in which case we add a copy of the ShaderInput to this graph. if (graph.properties.FirstOrDefault(p => p.guid == property.guid) == null) { var copy = (AbstractShaderProperty)property.Copy(); graph.SanitizeGraphInputName(copy); graph.SanitizeGraphInputReferenceName(copy, property.overrideReferenceName); // We do want to copy the overrideReferenceName property = copy; graph.AddGraphInput(property); } var node = new PropertyNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); // Setting the guid requires the graph to be set first. node.propertyGuid = property.guid; break; } case ShaderKeyword keyword: { // This could be from another graph, in which case we add a copy of the ShaderInput to this graph. if (graph.keywords.FirstOrDefault(k => k.guid == keyword.guid) == null) { var copy = (ShaderKeyword)keyword.Copy(); graph.SanitizeGraphInputName(copy); graph.SanitizeGraphInputReferenceName(copy, keyword.overrideReferenceName); // We do want to copy the overrideReferenceName keyword = copy; graph.AddGraphInput(keyword); } var node = new KeywordNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; graph.AddNode(node); // Setting the guid requires the graph to be set first. node.keywordGuid = keyword.guid; break; } default: throw new ArgumentOutOfRangeException(); } } }
void CreateNode(Object obj, Vector2 nodePosition) { var texture2D = obj as Texture2D; if (texture2D != null) { m_Graph.owner.RegisterCompleteObjectUndo("Drag Texture"); bool isNormalMap = false; if (EditorUtility.IsPersistent(texture2D) && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(texture2D))) { var importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture2D)) as TextureImporter; if (importer != null) { isNormalMap = importer.textureType == TextureImporterType.NormalMap; } } var node = new SampleTexture2DNode(); if (isNormalMap) { node.textureType = TextureType.Normal; } var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; m_Graph.AddNode(node); var inputslot = node.FindSlot <Texture2DInputMaterialSlot>(SampleTexture2DNode.TextureInputId); if (inputslot != null) { inputslot.texture = texture2D; } } var cubemap = obj as Cubemap; if (cubemap != null) { m_Graph.owner.RegisterCompleteObjectUndo("Drag Cubemap"); var property = new CubemapShaderProperty { displayName = cubemap.name, value = { cubemap = cubemap } }; m_Graph.AddShaderProperty(property); var node = new SampleCubemapNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; m_Graph.AddNode(node); var inputslot = node.FindSlot <CubemapInputMaterialSlot>(SampleCubemapNode.CubemapInputId); if (inputslot != null) { inputslot.cubemap = cubemap; } } var subGraphAsset = obj as MaterialSubGraphAsset; if (subGraphAsset != null) { m_Graph.owner.RegisterCompleteObjectUndo("Drag Sub-Graph"); var node = new SubGraphNode(); var drawState = node.drawState; drawState.position = new Rect(nodePosition, drawState.position.size); node.drawState = drawState; node.subGraphAsset = subGraphAsset; m_Graph.AddNode(node); } }