Skip to content

Commit

Permalink
Export Legacy Shaders/Diffuse shader
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Jul 11, 2024
1 parent 35881e0 commit cc81958
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Editor/ExportSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ public override void OnInspectorGUI()
EditorUtility.RevealInFinder(script.path);
}

if (!InspectorMode)
if (!string.IsNullOrEmpty(script.path) && !script.path.EndsWith("Data"))
{
EditorGUILayout.HelpBox("The path doesn't end with Data. Are you sure you've picked a correct path to a Data folder?", MessageType.Warning);
}

//if (!InspectorMode)
{
var selected = Selection.assetGUIDs;
if (selected.Length > 0)
Expand Down
32 changes: 32 additions & 0 deletions Editor/Material/LegacyDiffuseShaderMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace UnityToRebelFork.Editor
{
public class LegacyDiffuseShaderMapping : ShaderMappingBase, IShaderMapping
{
public int Priority { get; } = 0;

public bool CanMap(UnityEngine.Shader shader)
{
if (shader.name == Shaders.LegacyShaders.DiffuseShaderAdapter.ShaderName)
return true;
return false;
}

public MaterialModel Map(UnityEngine.Material material)
{
var model = new MaterialModel();

MapCommonParameters(material, model);
MapDefaultTechnique(material, model);

var shaderArgs = new Shaders.LegacyShaders.DiffuseShaderAdapter(material);

model.MatDiffColor = shaderArgs._Color;

if (shaderArgs._MainTex != null)
model.Albedo = orchestrator.ScheduleExport(shaderArgs._MainTex);

return model;
}

}
}
11 changes: 11 additions & 0 deletions Editor/Material/LegacyDiffuseShaderMapping.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Material/Shaders/LegacyShaders.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Editor/Material/Shaders/LegacyShaders/DiffuseShaderAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace UnityToRebelFork.Editor.Shaders.LegacyShaders
{
public class DiffuseShaderAdapter
{
public static readonly string ShaderName = "Legacy Shaders/Diffuse";

UnityEngine.Material material;

public DiffuseShaderAdapter(UnityEngine.Material material)
{
this.material = material;
}

public UnityEngine.Color _Color
{
get { return material.GetColor("_Color"); }
}

public UnityEngine.Texture _MainTex
{
get { return material.GetTexture("_MainTex"); }
}
}
}
11 changes: 11 additions & 0 deletions Editor/Material/Shaders/LegacyShaders/DiffuseShaderAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Editor/Material/StandardShaderMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityToRebelFork.Editor.Shaders;

namespace UnityToRebelFork.Editor
Expand Down
1 change: 1 addition & 0 deletions Editor/RebelForkInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public override void InstallBindings()
Container.BindInterfacesAndSelfTo<StandardShaderMapping>().AsSingle();
Container.BindInterfacesAndSelfTo<StandardSpecularShaderMapping>().AsSingle();
Container.BindInterfacesAndSelfTo<DefaultShaderMapping>().AsSingle();
Container.BindInterfacesAndSelfTo<LegacyDiffuseShaderMapping>().AsSingle();

Container.Bind<NameCollisionResolver>().AsCached();
Container.Bind<PrefabVisitor>().AsTransient();
Expand Down
90 changes: 90 additions & 0 deletions Tests/Editor/GenerateShaderAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityToRebelFork.Editor;

namespace UnityToRebelFork
{
[TestFixture]
public class GenerateShaderAdapter
{
[Test]
[TestCaseSource(nameof(ShaderNames))]
public void GenerateShader(string shaderName)
{
var adapterBuilder = new StringBuilder();

var pathSeparator = shaderName.LastIndexOf('/');
var name = shaderName.Substring(pathSeparator + 1);
var nameSpace = (pathSeparator > 0) ? shaderName.Substring(0, pathSeparator) : "";
nameSpace = nameSpace.Replace('/', '.').Replace(" ", "");

var foundShader = UnityEngine.Shader.Find(shaderName);

if (nameSpace.Length > 0)
adapterBuilder.AppendLine("namespace UnityToRebelFork.Editor.Shaders."+nameSpace);
else
adapterBuilder.AppendLine("namespace UnityToRebelFork.Editor.Shaders");
adapterBuilder.AppendLine("{");
adapterBuilder.AppendLine($" public class {name}ShaderAdapter");
adapterBuilder.AppendLine(" {");
adapterBuilder.AppendLine($" public static readonly string ShaderName = \"{shaderName}\";");
adapterBuilder.AppendLine();
adapterBuilder.AppendLine($" UnityEngine.Material material;");
adapterBuilder.AppendLine();
adapterBuilder.AppendLine($" public {name}ShaderAdapter(UnityEngine.Material material)");
adapterBuilder.AppendLine(" {");
adapterBuilder.AppendLine(" this.material = material;");
adapterBuilder.AppendLine(" }");

for (int i = 0; i < foundShader.GetPropertyCount(); ++i)
{
var propertyName = foundShader.GetPropertyName(i);
var propertyType = foundShader.GetPropertyType(i);
switch (propertyType)
{
case ShaderPropertyType.Color:
adapterBuilder.AppendLine();
adapterBuilder.AppendLine($" public UnityEngine.Color {propertyName}");
adapterBuilder.AppendLine(" {");
adapterBuilder.AppendLine($" get {{ return material.GetColor(\"{propertyName}\"); }}");
adapterBuilder.AppendLine(" }");
break;
case ShaderPropertyType.Texture:
adapterBuilder.AppendLine();
adapterBuilder.AppendLine($" public UnityEngine.Texture {propertyName}");
adapterBuilder.AppendLine(" {");
adapterBuilder.AppendLine($" get {{ return material.GetTexture(\"{propertyName}\"); }}");
adapterBuilder.AppendLine(" }");
break;
case ShaderPropertyType.Range:
case ShaderPropertyType.Float:
adapterBuilder.AppendLine();
adapterBuilder.AppendLine($" public float {propertyName}");
adapterBuilder.AppendLine(" {");
adapterBuilder.AppendLine($" get {{ return material.GetFloat(\"{propertyName}\"); }}");
adapterBuilder.AppendLine(" }");
break;
}
}

adapterBuilder.AppendLine(" }");
adapterBuilder.AppendLine("}");

TestContext.Out.WriteLine(adapterBuilder.ToString());
}

[Test]
public void GenerateShader2()
{
}

public static string[] ShaderNames()
{
return ShaderUtil.GetAllShaderInfo().Select(_=>_.name).ToArray();
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/GenerateShaderAdapter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Tests/Editor/UnityToRebelFork.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "UnityToRebelFork.Tests",
"rootNamespace": "",
"references": [
"UnityToRebelFork",
"UnityToRebelFork.Editor"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Tests/Editor/UnityToRebelFork.Tests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc81958

Please sign in to comment.