Skip to content

Commit

Permalink
More shaderdb work
Browse files Browse the repository at this point in the history
  • Loading branch information
Powback committed May 21, 2019
1 parent e0f4602 commit 7d6c3ad
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 81 deletions.
97 changes: 19 additions & 78 deletions Assets/Scripts/MapLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BC2;
using System.Reflection;
using ShaderDBParser;
using Assets.Scripts.MaterialManager;

public class MapLoad : MonoBehaviour
{
Expand Down Expand Up @@ -58,15 +59,14 @@ public class MapLoad : MonoBehaviour
[NonSerialized]
public Partition partition;
int i;
ShaderDB shaderdb = new ShaderDB();
Dictionary<string, Material> materials = new Dictionary<string, Material>();

[NonSerialized]
public MaterialManager materialManager;

void Start()
{

materialManager = new MaterialManager(mapName);
Util.mapLoad = this;
shaderdb.LoadShaderDatabase("levels/" + mapName + "/Shaders/Dx11_Single.dx11shaderdatabase");
partition = Util.LoadPartition("levels/" + mapName);
// StartCoroutine ("StartImport");
// StartCoroutine (StartConvert());
Expand Down Expand Up @@ -207,14 +207,15 @@ void GenerateItem(Inst inst) {
string name = "Unknown";
string mesh = inst.type + " | " + inst.guid;
List<Partition> partitions = new List<Partition>();
Dictionary<string, List<string>> shaders = new Dictionary<string, List<string>>();
string meshPartitionPath = "";

string shaderName = "";
string shaderPath = "";
string refMeshClean = "";

//This part is just trying to get the actual model name. It goes through different partitions and blueprints in order to get an accurate model name.
//Normally, it's fine to just do name + _lod0_data, but sometimes we have objects that reference non-existant objects, such as container_large_blue.
//While container_large exists, _blue is just referencing an other instance, and thus an other material for said container.
//It's mostly not an issue.
if(inst.type == "Entity.ReferenceObjectData" && Util.GetField("ReferencedObject", inst) != null && (Util.GetField("ReferencedObject", inst).reference != null && Util.GetField("ReferencedObject", inst).reference != "null"))
if (inst.type == "Entity.ReferenceObjectData" && Util.GetField("ReferencedObject", inst) != null && (Util.GetField("ReferencedObject", inst).reference != null && Util.GetField("ReferencedObject", inst).reference != "null"))
{
name = Util.GetField("ReferencedObject", inst).reference;

Expand Down Expand Up @@ -242,8 +243,7 @@ void GenerateItem(Inst inst) {
if (Util.GetField("Mesh", staticModelEntityData) != null)
{
string refMesh = Util.GetField("Mesh", staticModelEntityData).reference;
string refMeshClean = Util.ClearGUIDString(refMesh);
meshPartitionPath = refMeshClean;
refMeshClean = Util.ClearGUIDString(refMesh);

string refMeshGuid = Util.GetGuid(refMesh);

Expand All @@ -255,31 +255,23 @@ void GenerateItem(Inst inst) {
{
foreach (Complex complex in Util.GetArray("SurfaceShaders", shaderInstance).complex)
{
string shaderName = "";
string shaderPath = "";

List<string> textureNames = new List<string>();
foreach (Field field in complex.field)
{
if (field.name == "Name")
{
shaderName = Util.ClearGUIDString(field.value);
}
if (!shaders.ContainsKey(shaderName))

if (field.name == "Shader")
{
if (field.name == "Shader")
{
shaderPath = refMeshClean + "/" + Util.ClearGUIDString(field.reference);

shaderdb.ResourceDictionary.TryGetValue(shaderPath.ToLower(),
out textureNames);
}

if (!string.IsNullOrEmpty(shaderPath) && textureNames != null && textureNames.Count != 0)
{
shaders.Add(shaderName, textureNames);
}
shaderPath = Util.ClearGUIDString(field.reference);
}

}

materialManager.RegisterShader(refMeshClean, shaderPath, shaderName);
}
}
Inst rigidMeshAsset = Util.GetInst(refMeshGuid, meshPartition);
Expand All @@ -290,16 +282,11 @@ void GenerateItem(Inst inst) {
mesh = refMeshMesh + "_lod0_data";

}

}

}

}
}

}

}

string meshpath = "Resources/" + mesh + ".meshdata";
Expand Down Expand Up @@ -330,53 +317,7 @@ void GenerateItem(Inst inst) {
MeshFilter mf = subGO.AddComponent<MeshFilter>();
MeshCollider mc = subGO.AddComponent<MeshCollider> ();

if (Regex.IsMatch(subsetNames[subsetInt].ToLower(), "glass"))
{
mr.material = glassMaterial;
}
else
{
string materialName = subsetNames[subsetInt];
string materialPath = meshPartitionPath + "/" + materialName;
Material mat;
if (this.materials.TryGetValue(materialPath, out mat))
{
mr.material = mat;
}
else
{
List<string> textures;
foreach (string shaderName in shaders.Keys)
{
if (materialName.Contains(shaderName))
{
if (shaders.TryGetValue(shaderName, out textures))
{
mat = new Material(materialwhite);
mat.name = materialName;
foreach (string textureName in textures)
{
string textureType = Util.GetTextureType(textureName);
Texture2D texture = Util.LoadiTexture(textureName + ".itexture");

if (textureType != "" && texture != null)
{
mat.SetTexture(textureType, texture);
}
}
this.materials.Add(materialPath, mat);
mr.material = mat;
}
break;
}
else
{
mr.material = materialwhite;
}
}
}

}
mr.material = materialManager.GetMaterial(refMeshClean, shaderPath, subsetNames[subsetInt]);

mf.mesh = sub;
mf.mesh.RecalculateNormals();
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/MaterialManager.meta

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

111 changes: 111 additions & 0 deletions Assets/Scripts/MaterialManager/MaterialManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
using ShaderDBParser;
namespace Assets.Scripts.MaterialManager
{
public class MaterialManager
{

Dictionary<string, Material> materials = new Dictionary<string, Material>();
Dictionary<string, Dictionary<string, List<string>>> shaders = new Dictionary<string, Dictionary<string, List<string>>>();
ShaderDB shaderdb = new ShaderDB();

public MaterialManager(string levelName)
{
shaderdb.LoadShaderDatabase("levels/" + levelName + "/Shaders/Dx11_Single.dx11shaderdatabase");

}

List<string> GetTextures(string meshPath, string shaderPath)
{
string shaderDataPath = meshPath + "/" + shaderPath;
List<string> textureNames = new List<string>();
shaderdb.ResourceDictionary.TryGetValue(shaderDataPath.ToLower(), out textureNames);
return textureNames;
}


public void RegisterShader(string meshPath, string shaderPath, string shaderName)
{

Dictionary<string, List<string>> shader;

//Check if the shader already exists, if so, use that instead.
if(!shaders.TryGetValue(meshPath, out shader))
{
shader = new Dictionary<string, List<string>>();
shaders.Add(meshPath, shader);
}
if(!shader.ContainsKey(shaderName))
{
List<string> textureNames = GetTextures(meshPath, shaderPath);
shader.Add(shaderName, textureNames);
}
}




public Material GetMaterial(string meshPath, string shaderPath, string subsetName)
{
Material mat;
MapLoad mapLoad = Util.GetMapload();

string materialPath = meshPath + "/" + shaderPath;

// We have already processed this material.
if(materials.TryGetValue(materialPath, out mat))
{
return mat;
}
Dictionary<string, List<string>> shader;
// We don't have that shader registered.
if (!shaders.TryGetValue(meshPath, out shader))
{
return mapLoad.materialwhite;
}

// Loop through the shader names of the shader, figure out if any of them fits inside the subset name.
foreach (string shaderName in shader.Keys)
{
if (subsetName.Contains(shaderName))
{
//The current shader fits within the subset name.
List<string> textures = new List<string>();
if(!shader.TryGetValue(shaderName, out textures))
{
Debug.Log(shaderName);
}

mat = new Material(mapLoad.materialwhite);
mat.name = shaderName;
if(textures == null)
{
return mapLoad.materialwhite;
}
//Loop through all the textures registered to that shader, and apply them to a new material.
foreach (string textureName in textures)
{
//Figure out what type of texture this is and try to load it.
string textureType = Util.GetTextureType(textureName);
Texture2D texture = Util.LoadiTexture(textureName + ".itexture");

if (textureType != "" && texture != null)
{
mat.SetTexture(textureType, texture);
}
}
//Cache this material for later recovery.
this.materials.Add(materialPath, mat);
return mat;
}
}
return mapLoad.materialwhite;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/MaterialManager/MaterialManager.cs.meta

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

7 changes: 4 additions & 3 deletions Assets/Scripts/Types/Physics/HavokAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void GenerateHavokItem(Inst inst) {
// instantiatedDictionary.TryGetValue (name, out go);
// Util.Log ("Saving time on caching yo");
// } else {
GameObject go = loadMesh ("Resources/" + modelName);
GameObject go = loadMesh ("Resources/" + modelName, name);
// instantiatedDictionary.Add (name, go.gameObject);
// }
//
Expand Down Expand Up @@ -104,7 +104,7 @@ public void GenerateHavokItem(Inst inst) {
}


public GameObject loadMesh(string meshpath) {
public GameObject loadMesh(string meshpath, string refMeshName) {
GameObject go = Instantiate (emptyGO, Vector3.zero, Quaternion.identity) as GameObject;


Expand All @@ -120,14 +120,15 @@ public GameObject loadMesh(string meshpath) {
instantiatedMeshDictionary.Add (meshpath, bc2mesh);
}



subsetMesh = bc2mesh.subMesh;
subsetNames = bc2mesh.subMeshNames;
foreach(Mesh sub in subsetMesh) {

GameObject subGO = (GameObject) Instantiate(emptyGO, Vector3.zero,Quaternion.identity);
MeshRenderer mr = subGO.AddComponent<MeshRenderer>();
MeshFilter mf = subGO.AddComponent<MeshFilter>();
mr.material = new Material(Util.GetMapload().materialwhite);
mr.material.name = subsetNames[subsetInt];
mf.mesh = sub;
mf.mesh.RecalculateNormals();
Expand Down

0 comments on commit 7d6c3ad

Please sign in to comment.