|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
[ScriptedImporter(2, "shadergraph")] |
|
|
|
[ScriptedImporter(1, "shadergraph")] |
|
|
|
public class ShaderGraphImporter : ScriptedImporter |
|
|
|
{ |
|
|
|
private string errorShader = @"
|
|
|
|
|
|
|
Fallback Off |
|
|
|
}";
|
|
|
|
|
|
|
|
|
|
|
|
var oldShader = AssetDatabase.LoadAssetAtPath<Shader>(ctx.assetPath); |
|
|
|
if (oldShader != null) |
|
|
|
ShaderUtil.ClearShaderErrors(oldShader); |
|
|
|
var text = GetShaderText<MaterialGraph>(ctx.assetPath); |
|
|
|
List<PropertyCollector.TextureInfo> configuredTextures; |
|
|
|
var text = GetShaderText<MaterialGraph>(ctx.assetPath, out configuredTextures); |
|
|
|
|
|
|
|
var shader = ShaderUtil.CreateShaderAsset(text); |
|
|
|
|
|
|
|
EditorMaterialUtility.SetShaderDefaults( |
|
|
|
shader, |
|
|
|
configuredTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(), |
|
|
|
configuredTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray()); |
|
|
|
EditorMaterialUtility.SetShaderNonModifiableDefaults( |
|
|
|
shader, |
|
|
|
configuredTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(), |
|
|
|
configuredTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray()); |
|
|
|
var shader = ShaderUtil.CreateShaderAsset(text); |
|
|
|
private static string GetShaderText<T>(string path) where T : IShaderGraph |
|
|
|
private static string GetShaderText<T>(string path, out List<PropertyCollector.TextureInfo> configuredTextures) where T : IShaderGraph |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
var name = Path.GetFileNameWithoutExtension(path); |
|
|
|
|
|
|
|
List<PropertyCollector.TextureInfo> configuredTextures; |
|
|
|
var shaderString = graph.GetShader(string.Format("graphs/{0}", name), GenerationMode.ForReals, out configuredTextures); |
|
|
|
Debug.Log(shaderString); |
|
|
|
return shaderString; |
|
|
|
|
|
|
// ignored
|
|
|
|
} |
|
|
|
configuredTextures = new List<PropertyCollector.TextureInfo>(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |