Skip to content

Commit

Permalink
Merge pull request #15 from soupday/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
soupday authored Sep 15, 2021
2 parents 680381b + 3b107ce commit 1ddfad2
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 130 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

### v 0.1.8
- After applying materials or baking materials in the tool window, the generated prefab is selected in the project window.
- Characters in the preview window are replaced with the generated Prefab after material setup.
- Fixed baking LOD Group character materials.
- Baking LOD Group characters now also creates LOD Group prefab.

### v 0.1.7
- Reduced memory use for asset searches and character discovery.
- Prevented Import Tool window from holding on to character object references and hogging all the memory.
Expand Down
19 changes: 11 additions & 8 deletions Editor/CharacterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum ProcessingType { None, Basic, HighQuality }
public bool bakeIsBaked = false;
public bool bakeCustomShaders = true;
public bool bakeSeparatePrefab = true;
public bool isLOD = false;

private BaseGeneration generation = BaseGeneration.None;
private GameObject fbx;
Expand All @@ -50,6 +51,8 @@ public CharacterInfo(string guid)
folder = Path.GetDirectoryName(path);
infoPath = Path.Combine(folder, name + "_ImportInfo.txt");
jsonPath = Path.Combine(folder, name + ".json");
if (path.iContains("_lod")) isLOD = true;

if (File.Exists(infoPath))
Read();
else
Expand Down Expand Up @@ -148,24 +151,23 @@ public void Read()
else logType = ProcessingType.None;
break;
case "qualRefractiveEyes":
if (value == "true") qualRefractiveEyes = true;
else qualRefractiveEyes = false;
qualRefractiveEyes = value == "true" ? true : false;
break;
case "bakeIsBaked":
if (value == "true") bakeIsBaked = true;
else bakeIsBaked = false;
bakeIsBaked = value == "true" ? true : false;
break;
case "bakeCustomShaders":
if (value == "true") bakeCustomShaders = true;
else bakeCustomShaders = false;
bakeCustomShaders = value == "true" ? true : false;
break;
case "bakeSeparatePrefab":
if (value == "true") bakeSeparatePrefab = true;
else bakeSeparatePrefab = false;
bakeSeparatePrefab = value == "true" ? true : false;
break;
case "generation":
generation = (BaseGeneration)System.Enum.Parse(typeof(BaseGeneration), value);
break;
case "isLOD":
isLOD = value == "true" ? true : false;
break;
}
}
}
Expand All @@ -175,6 +177,7 @@ public void Write()
StreamWriter writer = new StreamWriter(infoPath, false);
writer.WriteLine("logType=" + logType.ToString());
writer.WriteLine("generation=" + generation.ToString());
writer.WriteLine("isLOD=" + (isLOD ? "true" : "false"));
writer.WriteLine("qualRefractiveEyes=" + (qualRefractiveEyes ? "true" : "false"));
writer.WriteLine("bakeIsBaked=" + (bakeIsBaked ? "true" : "false"));
writer.WriteLine("bakeCustomShaders=" + (bakeCustomShaders ? "true" : "false"));
Expand Down
163 changes: 91 additions & 72 deletions Editor/ComputeBake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ComputeBake
private readonly string texFolder;
private readonly List<string> sourceTextureFolders;
private readonly ModelImporter importer;
private readonly List<string> importAssets;
private readonly List<string> importAssets;

public const int MAX_SIZE = 4096;
public const string COMPUTE_SHADER = "RLBakeShader";
Expand Down Expand Up @@ -205,87 +205,94 @@ private Texture2D CheckOverlay(Texture2D tex)
return Texture2D.linearGrayTexture;
}

public void BakeHQ()
public GameObject BakeHQ()
{
if (Util.IsCC3Character(fbx))
{
CopyToClone();

int childCount = clone.transform.childCount;
for (int i = 0; i < childCount; i++)
{
GameObject child = clone.transform.GetChild(i).gameObject;
Renderer renderer = child.GetComponent<Renderer>();
if (renderer)
{
foreach (Material sharedMat in renderer.sharedMaterials)
{
// in case any of the materials have been renamed after a previous import, get the source name.
string sourceName = Util.GetSourceMaterialName(fbxPath, sharedMat);
string shaderName = Util.GetShaderName(sharedMat);
Material bakedMaterial = null;

switch (shaderName)
{
case Pipeline.SHADER_HQ_SKIN:
bakedMaterial = BakeSkinMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_TEETH:
bakedMaterial = BakeTeethMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_TONGUE:
bakedMaterial = BakeTongueMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_HAIR:
bakedMaterial = BakeHairMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_EYE:
bakedMaterial = BakeEyeMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_EYE_OCCLUSION:
bakedMaterial = BakeEyeOcclusionMaterial(sharedMat, sourceName);
break;
}

if (bakedMaterial)
{
ReplaceMaterial(sharedMat, bakedMaterial);
}
}
}
}
BakeMaterials();

AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

SaveAsPrefab();
return SaveAsPrefab();

//System.Media.SystemSounds.Asterisk.Play();
}

return null;
}

private void ReplaceMaterial(Material from, Material to)
public void BakeMaterials()
{
int childCount = clone.transform.childCount;
for (int i = 0; i < childCount; i++)
Renderer[] renderers = clone.GetComponentsInChildren<Renderer>();
List<Material> processed = new List<Material>(renderers.Length);

foreach (Renderer renderer in renderers)
{
GameObject child = clone.transform.GetChild(i).gameObject;
Renderer renderer = child.GetComponent<Renderer>();
if (renderer)
{
for (int j = 0; j < renderer.sharedMaterials.Length; j++)
foreach (Material sharedMat in renderer.sharedMaterials)
{
if (renderer.sharedMaterials[j] == from)
// don't process duplicates...
if (processed.Contains(sharedMat)) continue;
processed.Add(sharedMat);

// in case any of the materials have been renamed after a previous import, get the source name.
string sourceName = Util.GetSourceMaterialName(fbxPath, sharedMat);
string shaderName = Util.GetShaderName(sharedMat);
Material bakedMaterial = null;

switch (shaderName)
{
Material[] copy = (Material[])renderer.sharedMaterials.Clone();
copy[j] = to;
renderer.sharedMaterials = copy;
case Pipeline.SHADER_HQ_SKIN:
bakedMaterial = BakeSkinMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_TEETH:
bakedMaterial = BakeTeethMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_TONGUE:
bakedMaterial = BakeTongueMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_HAIR:
bakedMaterial = BakeHairMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_EYE:
bakedMaterial = BakeEyeMaterial(sharedMat, sourceName);
break;

case Pipeline.SHADER_HQ_EYE_OCCLUSION:
bakedMaterial = BakeEyeOcclusionMaterial(sharedMat, sourceName);
break;
}

if (bakedMaterial)
{
ReplaceMaterial(sharedMat, bakedMaterial);
}
}
}
}
}

private void ReplaceMaterial(Material from, Material to)
{
Renderer[] renderers = clone.GetComponentsInChildren<Renderer>();

foreach (Renderer renderer in renderers)
{
for (int j = 0; j < renderer.sharedMaterials.Length; j++)
{
if (renderer.sharedMaterials[j] == from)
{
Material[] copy = (Material[])renderer.sharedMaterials.Clone();
copy[j] = to;
renderer.sharedMaterials = copy;
}
}
}
Expand All @@ -296,20 +303,32 @@ public void CopyToClone()
clone = (GameObject)PrefabUtility.InstantiatePrefab(fbx);
}

public void SaveAsPrefab()
{
public GameObject SaveAsPrefab()
{
string prefabFolder = Util.CreateFolder(fbxFolder, Importer.PREFABS_FOLDER);
//string namedPrefabFolder = Util.CreateFolder(prefabFolder, characterName);
//string namedPrefabFolder = Util.CreateFolder(prefabFolder, characterName);

string prefabPath;
if (characterInfo.bakeSeparatePrefab)
prefabPath = Path.Combine(prefabFolder, characterName + "_Baked.prefab");
if (characterInfo.isLOD)
{
string lodPrefabPath = Path.Combine(prefabFolder, characterName + "_LODModels.prefab");
GameObject variant = PrefabUtility.SaveAsPrefabAsset(clone, lodPrefabPath);
GameObject.DestroyImmediate(clone);
GameObject prefab = RL.CreateOneLODPrefabFromModel(characterInfo, variant, characterInfo.bakeSeparatePrefab ? "_Baked" : "");
return prefab;
}
else
prefabPath = Path.Combine(prefabFolder, characterName + ".prefab");

GameObject variant = PrefabUtility.SaveAsPrefabAsset(clone, prefabPath);
Selection.activeObject = variant;
GameObject.DestroyImmediate(clone);
{
string prefabPath;
if (characterInfo.bakeSeparatePrefab)
prefabPath = Path.Combine(prefabFolder, characterName + "_Baked.prefab");
else
prefabPath = Path.Combine(prefabFolder, characterName + ".prefab");

GameObject variant = PrefabUtility.SaveAsPrefabAsset(clone, prefabPath);
Selection.activeObject = variant;
GameObject.DestroyImmediate(clone);
return variant;
}
}

private Material CreateBakedMaterial(Texture2D baseMap, Texture2D maskMap, Texture2D normalMap,
Expand Down
9 changes: 4 additions & 5 deletions Editor/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using UnityEditor;
using UnityEditor.Rendering.HighDefinition;
using UnityEngine;
using UnityEngine.Networking;

namespace Reallusion.Import
{
Expand Down Expand Up @@ -114,7 +113,7 @@ public void SetQuality(MaterialQuality qual)
quality = qual;
}

public bool Import()
public GameObject Import()
{
// make sure custom diffusion profiles are installed
Pipeline.AddDiffusionProfilesHDRP();
Expand Down Expand Up @@ -184,15 +183,15 @@ public bool Import()
AssetDatabase.Refresh();

// create prefab
RL.CreatePrefabFromFbx(characterInfo, fbx);
GameObject prefab = RL.CreatePrefabFromFbx(characterInfo, fbx);

Util.LogInfo("Done!");

Selection.activeObject = fbx;
Selection.activeObject = prefab;

//System.Media.SystemSounds.Asterisk.Play();

return true;
return prefab;
}

void ProcessObjectTree(GameObject obj)
Expand Down
27 changes: 19 additions & 8 deletions Editor/ImporterWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using System;
using System.Xml.Serialization;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;

Expand Down Expand Up @@ -487,20 +486,26 @@ private void OnGUIActionArea(Rect actionBlock)
if (GUILayout.Button("Default", GUILayout.Width(ACTION_BUTTON_WIDTH), GUILayout.Height(BUTTON_HEIGHT)))
{
Util.LogInfo("Doing: Connect Default Materials.");
ImportCharacter(contextCharacter, MaterialQuality.Default);
GameObject prefab = ImportCharacter(contextCharacter, MaterialQuality.Default);
contextCharacter.logType = CharacterInfo.ProcessingType.Basic;
contextCharacter.Write();
CreateTreeView(true);

if (prefab)
Util.AddPreviewCharacter(contextCharacter.Fbx, prefab, Vector3.zero, true);
}
GUILayout.FlexibleSpace();
if (!contextCharacter.CanHaveHighQualityMaterials) GUI.enabled = false;
if (GUILayout.Button("High Quality", GUILayout.Width(ACTION_BUTTON_WIDTH), GUILayout.Height(BUTTON_HEIGHT)))
{
Util.LogInfo("Doing: Connect High Quality Materials.");
ImportCharacter(contextCharacter, MaterialQuality.High);
GameObject prefab = ImportCharacter(contextCharacter, MaterialQuality.High);
contextCharacter.logType = CharacterInfo.ProcessingType.HighQuality;
contextCharacter.Write();
CreateTreeView(true);

if (prefab)
Util.AddPreviewCharacter(contextCharacter.Fbx, prefab, Vector3.zero, true);
}
GUI.enabled = true;
GUILayout.FlexibleSpace();
Expand All @@ -514,13 +519,19 @@ private void OnGUIActionArea(Rect actionBlock)
if (GUILayout.Button("Bake", GUILayout.Width(FUNCTION_BUTTON_WIDTH), GUILayout.Height(BUTTON_HEIGHT)))
{
if (contextCharacter.logType == CharacterInfo.ProcessingType.HighQuality)
{

{
ComputeBake baker = new ComputeBake(contextCharacter.Fbx, contextCharacter);
baker.BakeHQ();
GameObject prefab = baker.BakeHQ();

contextCharacter.bakeIsBaked = true;
contextCharacter.Write();

if (prefab)
{
Vector3 position = Vector3.zero;
if (contextCharacter.bakeSeparatePrefab) position = new Vector3(-0.35f, 0f, 0.35f);
Util.AddPreviewCharacter(contextCharacter.Fbx, prefab, position, false);
}
}
}
GUI.enabled = true;
Expand Down Expand Up @@ -568,11 +579,11 @@ private void OnGUITreeViewArea(Rect treeviewBlock)
GUILayout.EndArea();
}

private void ImportCharacter(CharacterInfo info, MaterialQuality quality)
private GameObject ImportCharacter(CharacterInfo info, MaterialQuality quality)
{
Importer import = new Importer(info);
import.SetQuality(quality);
import.Import();
return import.Import();
}

private static void ClearAllData()
Expand Down
1 change: 0 additions & 1 deletion Editor/Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Diagnostics;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

Expand Down
Loading

0 comments on commit 1ddfad2

Please sign in to comment.