Skip to content

Commit cc81958

Browse files
committed
Export Legacy Shaders/Diffuse shader
1 parent 35881e0 commit cc81958

12 files changed

+223
-3
lines changed

Editor/ExportSettingsEditor.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ public override void OnInspectorGUI()
8686
EditorUtility.RevealInFinder(script.path);
8787
}
8888

89-
if (!InspectorMode)
89+
if (!string.IsNullOrEmpty(script.path) && !script.path.EndsWith("Data"))
90+
{
91+
EditorGUILayout.HelpBox("The path doesn't end with Data. Are you sure you've picked a correct path to a Data folder?", MessageType.Warning);
92+
}
93+
94+
//if (!InspectorMode)
9095
{
9196
var selected = Selection.assetGUIDs;
9297
if (selected.Length > 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace UnityToRebelFork.Editor
2+
{
3+
public class LegacyDiffuseShaderMapping : ShaderMappingBase, IShaderMapping
4+
{
5+
public int Priority { get; } = 0;
6+
7+
public bool CanMap(UnityEngine.Shader shader)
8+
{
9+
if (shader.name == Shaders.LegacyShaders.DiffuseShaderAdapter.ShaderName)
10+
return true;
11+
return false;
12+
}
13+
14+
public MaterialModel Map(UnityEngine.Material material)
15+
{
16+
var model = new MaterialModel();
17+
18+
MapCommonParameters(material, model);
19+
MapDefaultTechnique(material, model);
20+
21+
var shaderArgs = new Shaders.LegacyShaders.DiffuseShaderAdapter(material);
22+
23+
model.MatDiffColor = shaderArgs._Color;
24+
25+
if (shaderArgs._MainTex != null)
26+
model.Albedo = orchestrator.ScheduleExport(shaderArgs._MainTex);
27+
28+
return model;
29+
}
30+
31+
}
32+
}

Editor/Material/LegacyDiffuseShaderMapping.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Material/Shaders/LegacyShaders.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace UnityToRebelFork.Editor.Shaders.LegacyShaders
2+
{
3+
public class DiffuseShaderAdapter
4+
{
5+
public static readonly string ShaderName = "Legacy Shaders/Diffuse";
6+
7+
UnityEngine.Material material;
8+
9+
public DiffuseShaderAdapter(UnityEngine.Material material)
10+
{
11+
this.material = material;
12+
}
13+
14+
public UnityEngine.Color _Color
15+
{
16+
get { return material.GetColor("_Color"); }
17+
}
18+
19+
public UnityEngine.Texture _MainTex
20+
{
21+
get { return material.GetTexture("_MainTex"); }
22+
}
23+
}
24+
}

Editor/Material/Shaders/LegacyShaders/DiffuseShaderAdapter.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Material/StandardShaderMapping.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using UnityEditor;
21
using UnityEngine;
3-
using UnityEngine.Rendering;
42
using UnityToRebelFork.Editor.Shaders;
53

64
namespace UnityToRebelFork.Editor

Editor/RebelForkInstaller.cs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public override void InstallBindings()
2626
Container.BindInterfacesAndSelfTo<StandardShaderMapping>().AsSingle();
2727
Container.BindInterfacesAndSelfTo<StandardSpecularShaderMapping>().AsSingle();
2828
Container.BindInterfacesAndSelfTo<DefaultShaderMapping>().AsSingle();
29+
Container.BindInterfacesAndSelfTo<LegacyDiffuseShaderMapping>().AsSingle();
2930

3031
Container.Bind<NameCollisionResolver>().AsCached();
3132
Container.Bind<PrefabVisitor>().AsTransient();

Tests/Editor/GenerateShaderAdapter.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Linq;
2+
using System.Text;
3+
using NUnit.Framework;
4+
using UnityEditor;
5+
using UnityEngine;
6+
using UnityEngine.Rendering;
7+
using UnityToRebelFork.Editor;
8+
9+
namespace UnityToRebelFork
10+
{
11+
[TestFixture]
12+
public class GenerateShaderAdapter
13+
{
14+
[Test]
15+
[TestCaseSource(nameof(ShaderNames))]
16+
public void GenerateShader(string shaderName)
17+
{
18+
var adapterBuilder = new StringBuilder();
19+
20+
var pathSeparator = shaderName.LastIndexOf('/');
21+
var name = shaderName.Substring(pathSeparator + 1);
22+
var nameSpace = (pathSeparator > 0) ? shaderName.Substring(0, pathSeparator) : "";
23+
nameSpace = nameSpace.Replace('/', '.').Replace(" ", "");
24+
25+
var foundShader = UnityEngine.Shader.Find(shaderName);
26+
27+
if (nameSpace.Length > 0)
28+
adapterBuilder.AppendLine("namespace UnityToRebelFork.Editor.Shaders."+nameSpace);
29+
else
30+
adapterBuilder.AppendLine("namespace UnityToRebelFork.Editor.Shaders");
31+
adapterBuilder.AppendLine("{");
32+
adapterBuilder.AppendLine($" public class {name}ShaderAdapter");
33+
adapterBuilder.AppendLine(" {");
34+
adapterBuilder.AppendLine($" public static readonly string ShaderName = \"{shaderName}\";");
35+
adapterBuilder.AppendLine();
36+
adapterBuilder.AppendLine($" UnityEngine.Material material;");
37+
adapterBuilder.AppendLine();
38+
adapterBuilder.AppendLine($" public {name}ShaderAdapter(UnityEngine.Material material)");
39+
adapterBuilder.AppendLine(" {");
40+
adapterBuilder.AppendLine(" this.material = material;");
41+
adapterBuilder.AppendLine(" }");
42+
43+
for (int i = 0; i < foundShader.GetPropertyCount(); ++i)
44+
{
45+
var propertyName = foundShader.GetPropertyName(i);
46+
var propertyType = foundShader.GetPropertyType(i);
47+
switch (propertyType)
48+
{
49+
case ShaderPropertyType.Color:
50+
adapterBuilder.AppendLine();
51+
adapterBuilder.AppendLine($" public UnityEngine.Color {propertyName}");
52+
adapterBuilder.AppendLine(" {");
53+
adapterBuilder.AppendLine($" get {{ return material.GetColor(\"{propertyName}\"); }}");
54+
adapterBuilder.AppendLine(" }");
55+
break;
56+
case ShaderPropertyType.Texture:
57+
adapterBuilder.AppendLine();
58+
adapterBuilder.AppendLine($" public UnityEngine.Texture {propertyName}");
59+
adapterBuilder.AppendLine(" {");
60+
adapterBuilder.AppendLine($" get {{ return material.GetTexture(\"{propertyName}\"); }}");
61+
adapterBuilder.AppendLine(" }");
62+
break;
63+
case ShaderPropertyType.Range:
64+
case ShaderPropertyType.Float:
65+
adapterBuilder.AppendLine();
66+
adapterBuilder.AppendLine($" public float {propertyName}");
67+
adapterBuilder.AppendLine(" {");
68+
adapterBuilder.AppendLine($" get {{ return material.GetFloat(\"{propertyName}\"); }}");
69+
adapterBuilder.AppendLine(" }");
70+
break;
71+
}
72+
}
73+
74+
adapterBuilder.AppendLine(" }");
75+
adapterBuilder.AppendLine("}");
76+
77+
TestContext.Out.WriteLine(adapterBuilder.ToString());
78+
}
79+
80+
[Test]
81+
public void GenerateShader2()
82+
{
83+
}
84+
85+
public static string[] ShaderNames()
86+
{
87+
return ShaderUtil.GetAllShaderInfo().Select(_=>_.name).ToArray();
88+
}
89+
}
90+
}

Tests/Editor/GenerateShaderAdapter.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "UnityToRebelFork.Tests",
3+
"rootNamespace": "",
4+
"references": [
5+
"UnityToRebelFork",
6+
"UnityToRebelFork.Editor"
7+
],
8+
"optionalUnityReferences": [
9+
"TestAssemblies"
10+
],
11+
"includePlatforms": [
12+
"Editor"
13+
],
14+
"excludePlatforms": [],
15+
"allowUnsafeCode": true,
16+
"overrideReferences": false,
17+
"precompiledReferences": [],
18+
"autoReferenced": true,
19+
"defineConstraints": [],
20+
"versionDefines": [],
21+
"noEngineReferences": false
22+
}

Tests/Editor/UnityToRebelFork.Tests.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)