Skip to content

Commit

Permalink
Complex texture export
Browse files Browse the repository at this point in the history
  • Loading branch information
gleblebedev committed Oct 15, 2023
1 parent 6e259e8 commit 37e24f8
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 44 deletions.
4 changes: 4 additions & 0 deletions Editor/ExportContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using UnityEngine;
using UnityEngine.SceneManagement;

namespace UnityToRebelFork.Editor
Expand All @@ -6,7 +7,10 @@ public class ExportContext
{
public ExportContext(UnityEngine.SceneManagement.Scene scene)
{
}

public ExportContext(GameObject prefabRoot)
{
}

public string TempFolder { get; set; } = "Tmp/";
Expand Down
29 changes: 23 additions & 6 deletions Editor/ExportSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityEditor;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;

namespace UnityToRebelFork.Editor
{
Expand Down Expand Up @@ -51,13 +52,29 @@ public override void OnInspectorGUI()
//}
//else
{
if (GUILayout.Button("Export current scene", GUILayout.Height(40)))
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

if (prefabStage != null)
{
_container = new DiContainer(new[] { StaticContext.Container });
var activeScene = SceneManager.GetActiveScene();
RebelForkInstaller.Install(_container, script, new ExportContext(activeScene));
var orchestrator = _container.Resolve<ExportOrchestrator>();
orchestrator.ScheduleExport(activeScene);
if (GUILayout.Button($"Export Prefab ({Path.GetFileName(prefabStage.prefabAssetPath)})", GUILayout.Height(40)))
{
_container = new DiContainer(new[] { StaticContext.Container });
var prefabRootObject = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.assetPath);
RebelForkInstaller.Install(_container, script, new ExportContext(prefabRootObject));
var orchestrator = _container.Resolve<ExportOrchestrator>();
orchestrator.ScheduleExport(prefabRootObject);
}
}
else
{
if (GUILayout.Button($"Export Scene ({Path.GetFileName(SceneManager.GetActiveScene().path)})", GUILayout.Height(40)))
{
_container = new DiContainer(new[] { StaticContext.Container });
var activeScene = SceneManager.GetActiveScene();
RebelForkInstaller.Install(_container, script, new ExportContext(activeScene));
var orchestrator = _container.Resolve<ExportOrchestrator>();
orchestrator.ScheduleExport(activeScene);
}
}
}

Expand Down
67 changes: 66 additions & 1 deletion Editor/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using UnityEditor;

namespace UnityToRebelFork.Editor
{
public class Helpers
Expand All @@ -15,15 +17,78 @@ public static Scene CreateDefaultScene(string name)
scene.Name = name;

scene.Components.Add(new RenderPipeline());
scene.Components.Add(new Octree());
var octree = new Octree();
scene.Components.Add(octree);
//scene.Components.Add(new ReflectionProbeManager());
scene.Components.Add(new PhysicsWorld());
scene.Components.Add(new Zone() { ZoneTexture = ResourceRef.Create<TextureCube>("Textures/DefaultSkybox.xml"), BoundingBoxMin = octree.BoundingBoxMin, BoundingBoxMax = octree.BoundingBoxMax });
scene.Components.Add(new Skybox()
{
Model = new ResourceRef("Model", "Models/Box.mdl"),
Material = new ResourceRefList("Material", "Materials/DefaultSkybox.xml"),
});
return scene;
}

/// <summary>
/// Get stable hash code.
/// </summary>
/// <typeparam name="T1">Object type.</typeparam>
/// <param name="t1">Object.</param>
/// <returns>Stable hash code.</returns>
public static int GetHashCode<T1>(T1 t1)
{
if (t1 == null) return 0;
if (t1 is UnityEngine.Object obj)
{
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long localId))
{
return CombineHashCodes(guid, localId);
}
}
return t1.GetHashCode();
}

/// <summary>
/// Combine hash codes with stable algorithm.
/// </summary>
/// <typeparam name="T1">First object type.</typeparam>
/// <typeparam name="T2">Second object type.</typeparam>
/// <param name="t1">First object.</param>
/// <param name="t2">Second object.</param>
/// <returns>Combined stable hash code.</returns>
public static int CombineHashCodes<T1, T2>(T1 t1, T2 t2)
{
var code = GetHashCode(t1);
code = code * 31 + GetHashCode(t2);
return code;
}

/// <summary>
/// Combine hash codes with stable algorithm.
/// </summary>
/// <typeparam name="T2">Second object type.</typeparam>
/// <param name="code">Existing hashcode.</param>
/// <param name="t2">Second object.</param>
/// <returns>Combined stable hash code.</returns>
public static int CombineHashCodes<T2>(int code, T2 t2)
{
code = code * 31 + GetHashCode(t2);
return code;
}

/// <summary>
/// Combine hash codes with stable algorithm.
/// </summary>
/// <returns>Combined stable hash code.</returns>
public static int CombineHashCodes(params object[] objects)
{
int code = 0;
foreach (var o in objects)
{
code = CombineHashCodes(code, o);
}
return code;
}
}
}
35 changes: 35 additions & 0 deletions Editor/Material/StandardShaderMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ public MaterialModel Map(UnityEngine.Material material)

if (shaderArgs._OcclusionMap != null || shaderArgs._MetallicGlossMap != null)
{
var recipe = new TextureRecipe();
// R - Roughness ------------
if (shaderArgs._SmoothnessTextureChannel < 0.5)
{
if (shaderArgs._MetallicGlossMap != null)
{
recipe.RChannel = shaderArgs._MetallicGlossMap;
recipe.RMask = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
recipe.RSourceRange = new Vector2(1.0f, 0.0f);
}
}
else
{
if (shaderArgs._MainTex != null)
{
recipe.RChannel = shaderArgs._MainTex;
recipe.RMask = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
recipe.RSourceRange = new Vector2(1.0f, 0.0f);
}
}
// G - Metallic ------------
if (shaderArgs._MetallicGlossMap != null)
{
recipe.GChannel = shaderArgs._MetallicGlossMap;
recipe.GMask = new Vector4(0.299f, 0.587f, 0.114f, 0.0f);
}

// A - Occlusion ------------
if (shaderArgs._OcclusionMap != null)
{
recipe.AChannel = shaderArgs._OcclusionMap;
recipe.AMask = new Vector4(0.299f, 0.587f, 0.114f, 0.0f);
}

model.Properties = orchestrator.ScheduleExport(recipe);
}

return model;
Expand Down
40 changes: 5 additions & 35 deletions Editor/MeshReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,6 @@ public struct MaterialAndTopology

public IList<LodLevel> Levels { get; } = new List<LodLevel>();

public String HashCodeTest
{
get
{
var s = new StringBuilder();
foreach (var level in Levels)
{
s.AppendLine($"ScreenRelativeTransitionHeight = {level.ScreenRelativeTransitionHeight.GetHashCode()}");
foreach (var renderer in level.Renderers)
{
AssetDatabase.TryGetGUIDAndLocalFileIdentifier(renderer.Mesh, out string guid, out long localId);
s.AppendLine($"Mesh {guid.GetHashCode()}, {localId.GetHashCode()} = {GetMeshHashCode(renderer.Mesh)}");
s.AppendLine($"Transform = {renderer.Transform.GetHashCode()}");
foreach (var rendererMaterial in renderer.Materials)
{
s.AppendLine($"{rendererMaterial} = {rendererMaterial.GetHashCode()}");
}
}
}
return s.ToString();
}
}

static int CombineHashCodes<T1, T2>(T1 t1, T2 t2)
{
var code = t1.GetHashCode();
code = code * 31 + t2.GetHashCode();
return code;
}

public MeshReference(LODGroup lodGroup, ExportOrchestrator orchestrator)
{
var worldToLocalMatrix = lodGroup.transform.worldToLocalMatrix;
Expand Down Expand Up @@ -118,7 +88,7 @@ public override int GetHashCode()
var hashCode = 0;
foreach (var lodLevel in Levels)
{
hashCode = CombineHashCodes(hashCode, lodLevel);
hashCode = Helpers.CombineHashCodes(hashCode, lodLevel);
}
return hashCode;
}
Expand Down Expand Up @@ -179,10 +149,10 @@ public override bool Equals(object obj)
public override int GetHashCode()
{
int meshHashCode = GetMeshHashCode(Mesh);
var hash = CombineHashCodes(Transform, meshHashCode);
var hash = Helpers.CombineHashCodes(Transform, meshHashCode);
foreach (var material in Materials)
{
hash = CombineHashCodes(hash, material != null ? material.GetHashCode() : 0);
hash = Helpers.CombineHashCodes(hash, material != null ? material.GetHashCode() : 0);
}
return hash;
}
Expand All @@ -209,7 +179,7 @@ public static int GetMeshHashCode(UnityEngine.Mesh mesh)
{
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(mesh, out string guid, out long localId))
{
return CombineHashCodes(guid, localId);
return Helpers.CombineHashCodes(guid, localId);
}
}

Expand Down Expand Up @@ -257,7 +227,7 @@ public override bool Equals(object obj)
public override int GetHashCode()
{
var hash = ScreenRelativeTransitionHeight.GetHashCode();
foreach (var renderer in Renderers) hash = CombineHashCodes(hash, renderer);
foreach (var renderer in Renderers) hash = Helpers.CombineHashCodes(hash, renderer);
return hash;
}

Expand Down
1 change: 1 addition & 0 deletions Editor/Model/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static string GetName(this VariantType valueType)
case VariantType.VarInt64: return "Int64";
case VariantType.VarCustom: return "Custom";
case VariantType.VarStringvariantmap: return "StringVariantMap";
case VariantType.VarVariantList: return "VariantList";
//case VariantType.VaVe: return "VariantVector";
//case VariantType.VarM: return "VariantMap";
//case VariantType.VariantCurve: return "VariantCurve";
Expand Down
2 changes: 2 additions & 0 deletions Editor/Model/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public bool Equals(Variant other)
case VariantType.VarStringList: return this.Compare(this.StringList, other.StringList);
case VariantType.VarStringvariantmap: return this.Compare(this.Stringvariantmap, other.Stringvariantmap);
case VariantType.VarColor: return this.Color == other.Color;
case VariantType.VarVariantList: return this.VariantList == other.VariantList;
case VariantType.VarBuffer: return true; // Buffer is not supported.
default: throw new NotImplementedException(_type.GetName());
}
}
Expand Down
50 changes: 49 additions & 1 deletion Editor/Model/VariantList.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
using System;
using System.Collections.Generic;

namespace UnityToRebelFork.Editor
{
public class VariantList : List<Variant>
public class VariantList : List<Variant>, IEquatable<VariantList>
{
public bool Equals(VariantList other)
{
if (other == null)
{
return this.Count == 0;
}

if (other.Count != this.Count)
{
return false;
}

for (var index = 0; index < this.Count; index++)
{
if (this[index] != other[index])
return false;
}

return true;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((VariantList)obj);
}

public override int GetHashCode()
{
var code = new HashCode();
foreach (var val in this)
{
code.Add(val);
}
return code.ToHashCode();
}

public static bool operator ==(VariantList left, VariantList right)
{
return Equals(left, right);
}

public static bool operator !=(VariantList left, VariantList right)
{
return !Equals(left, right);
}
}
}
11 changes: 11 additions & 0 deletions Editor/PrefabExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public override IEnumerable Export(GameObject asset)
scene.Children.Add(root);
if (!Settings.EmptyNodes)
new EmptyNodeCleanupVisitor().Visit(root);
scene.Children.Add(new Node()
{
Name = "Prefab Light",
Rotation = Quaternion.AngleAxis(90, Vector3.right),
Components = {
new Light()
{
LightType = LightType.Directional
}
}
});
new UniqueIdVisitor().Vist(scene);

using (var stream = _settings.CreateFile(EvaluateResourcePath(asset)))
Expand Down
1 change: 1 addition & 0 deletions Editor/RebelForkInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public override void InstallBindings()
Container.BindInterfacesAndSelfTo<SceneExporter>().AsSingle();
Container.BindInterfacesAndSelfTo<ExportOrchestrator>().AsSingle();
Container.BindInterfacesAndSelfTo<TextureExporter>().AsSingle();
Container.BindInterfacesAndSelfTo<TextureRecipeExporter>().AsSingle();

Container.BindInterfacesAndSelfTo<StandardShaderMapping>().AsSingle();
Container.BindInterfacesAndSelfTo<StandardSpecularShaderMapping>().AsSingle();
Expand Down
2 changes: 1 addition & 1 deletion Editor/TextureProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void ProcessAndSaveTexture(UnityEngine.Texture sourceTexture, int width,
texture => SaveTexture(texture, fullOutputPath, hasAlpha));
}

private void SaveTexture(UnityEngine.Texture2D texture, string fullOutputPath, bool hasAlpha = true)
public void SaveTexture(UnityEngine.Texture2D texture, string fullOutputPath, bool hasAlpha = true)
{
if (string.IsNullOrWhiteSpace(fullOutputPath))
return;
Expand Down
Loading

0 comments on commit 37e24f8

Please sign in to comment.