Tim Cooper
8 年前
当前提交
5e3b99fe
共有 7 个文件被更改,包括 128 次插入 和 145 次删除
-
110MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs
-
5MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs
-
29MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Presenters/TitleBarPresenter.cs
-
1MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Assets/IGraphAsset.cs
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
-
126MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs
|
|||
using UnityEditor.Experimental.AssetImporters; |
|||
using UnityEngine.MaterialGraph; |
|||
using UnityEngine.MaterialGraph; |
|||
using System.Linq; |
|||
#if UNITY_EDITOR
|
|||
using System.Reflection; |
|||
using UnityEditor; |
|||
#endif
|
|||
using UnityEngine.Graphing; |
|||
using UnityEditor; |
|||
using UnityEditor.MaterialGraph.Drawing; |
|||
using UnityEngine.Events; |
|||
[ScriptedImporter(1, "ShaderGraph")] |
|||
public class ShaderGraphImporter : ScriptedImporter |
|||
class ShaderGraphTextGenerator : ICustomShaderImporter |
|||
public override void OnImportAsset(AssetImportContext ctx) |
|||
{ |
|||
var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8); |
|||
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph); |
|||
public string GetShaderText(string path) |
|||
{ |
|||
try |
|||
{ |
|||
var textGraph = File.ReadAllText(path, Encoding.UTF8); |
|||
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph); |
|||
if (graph == null) |
|||
return; |
|||
IMasterNode masterNode = graph.masterNode; |
|||
if (masterNode == null) |
|||
return null; |
|||
var graphAsset = ScriptableObject.CreateInstance<MaterialGraphAsset> (); |
|||
graphAsset.materialGraph = graph; |
|||
ctx.SetMainAsset("MainAsset", graphAsset); |
|||
|
|||
var shader = RegenerateShader (graphAsset); |
|||
if (shader == null) |
|||
return; |
|||
|
|||
ctx.AddSubAsset("Shader", shader); |
|||
} |
|||
|
|||
public Shader RegenerateShader(MaterialGraphAsset asset) |
|||
{ |
|||
IMasterNode masterNode = asset.materialGraph.masterNode; |
|||
if (masterNode == null) |
|||
return null; |
|||
|
|||
var path = "Assets/UnityShaderEditor/Editor/HelperShader.shader"; |
|||
List<PropertyGenerator.TextureInfo> configuredTextures; |
|||
var shaderString = masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures); |
|||
|
|||
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader; |
|||
if (shader == null) |
|||
return null; |
|||
|
|||
File.WriteAllText (path, shaderString); |
|||
ShaderUtil.UpdateShaderAsset (shader, shaderString); |
|||
List<PropertyGenerator.TextureInfo> configuredTextures; |
|||
var shaderString = masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures); |
|||
return shaderString; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// ignored
|
|||
} |
|||
return null; |
|||
} |
|||
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter; |
|||
if (shaderImporter == null) |
|||
return null; |
|||
public bool IsValidForPath(string path) |
|||
{ |
|||
try |
|||
{ |
|||
var textGraph = File.ReadAllText(path, Encoding.UTF8); |
|||
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph); |
|||
var textureNames = new List<string>(); |
|||
var textures = new List<Texture>(); |
|||
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.Modifiable)) |
|||
{ |
|||
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture; |
|||
if (texture == null) |
|||
continue; |
|||
textureNames.Add(textureInfo.name); |
|||
textures.Add(texture); |
|||
} |
|||
shaderImporter.SetDefaultTextures(textureNames.ToArray(), textures.ToArray()); |
|||
return graph != null; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// ignored
|
|||
} |
|||
return false; |
|||
} |
|||
textureNames.Clear(); |
|||
textures.Clear(); |
|||
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.NonModifiable)) |
|||
{ |
|||
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture; |
|||
if (texture == null) |
|||
continue; |
|||
textureNames.Add(textureInfo.name); |
|||
textures.Add(texture); |
|||
} |
|||
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray()); |
|||
shaderImporter.SaveAndReimport(); |
|||
public void OnInspectorGUI(string path, UnityAction defaultOnInspectorGUI) |
|||
{ |
|||
defaultOnInspectorGUI(); |
|||
if (GUILayout.Button("Open Shader Editor")) |
|||
{ |
|||
var window = new MaterialGraphEditWindow(); |
|||
window.Show(); |
|||
window.ChangeSelction(AssetDatabase.LoadAssetAtPath<Shader>(path)); |
|||
} |
|||
} |
|||
var imported = shaderImporter.GetShader(); |
|||
return imported; |
|||
} |
|||
public void OpenAsset(string path) |
|||
{ |
|||
var window = new MaterialGraphEditWindow(); |
|||
window.Show(); |
|||
window.ChangeSelction(AssetDatabase.LoadAssetAtPath<Shader>(path)); |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue