Browse Source

Recreate the BSP tree structure through a GameObject hierarchy. This gives a bit more insight into the structure of the map data.

console
Nico de Poel 5 years ago
parent
commit
e753e1cc34
  1. 69
      Assets/Scripts/Modules/BrushModel.cs

69
Assets/Scripts/Modules/BrushModel.cs

@ -12,10 +12,18 @@ public class BrushModel
private readonly List<Vector2> tempTextureUVs = new List<Vector2>();
private readonly List<Vector2> tempLightmapUVs = new List<Vector2>();
private readonly List<ushort> tempIndices = new List<ushort>();
private readonly GameObject rootGameObject;
private readonly Material debugMaterial;
public BrushModel(string name)
{
this.name = name;
rootGameObject = new GameObject(name);
rootGameObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
debugMaterial = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
}
public void ImportMeshData(QModel model)
@ -25,49 +33,31 @@ public class BrushModel
for (int modelIdx = 0; modelIdx < subModels.Length; ++modelIdx)
{
var headNode = subModels[modelIdx].GetHeadNode(model);
CreateNodeMeshes(headNode, surfaces);
}
// DEBUG
var go = new GameObject(name);
var mat = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
for (int i = 0; i < meshes.Count; ++i)
{
var mesh = meshes[i];
var meshGO = new GameObject($"Surface_{i}");
meshGO.transform.SetParent(go.transform);
var modelGO = new GameObject($"SubModel_{modelIdx}");
modelGO.transform.SetParent(rootGameObject.transform);
var mf = meshGO.AddComponent<MeshFilter>();
mf.sharedMesh = mesh;
var mr = meshGO.AddComponent<MeshRenderer>();
mr.sharedMaterial = mat;
mr.shadowCastingMode = ShadowCastingMode.Off;
mr.receiveShadows = false;
mr.lightProbeUsage = LightProbeUsage.Off;
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
var headNode = subModels[modelIdx].GetHeadNode(model);
CreateNodeMeshes(headNode, surfaces, modelGO);
}
go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
}
private void CreateNodeMeshes(QNode node, QSurface[] surfaces)
private void CreateNodeMeshes(QNode node, QSurface[] surfaces, GameObject parentGO)
{
if (node.contents < 0) // Leaf node
return;
CreateSurfaceMeshes(node, surfaces);
var nodeGO = new GameObject("Node");
nodeGO.transform.SetParent(parentGO.transform);
CreateSurfaceMeshes(node, surfaces, nodeGO);
foreach (var childNode in node.Children)
{
CreateNodeMeshes(childNode, surfaces);
CreateNodeMeshes(childNode, surfaces, nodeGO);
}
}
private void CreateSurfaceMeshes(QNode node, QSurface[] surfaces)
private void CreateSurfaceMeshes(QNode node, QSurface[] surfaces, GameObject nodeGO)
{
for (int surfIdx = 0; surfIdx < node.numSurfaces; ++surfIdx)
{
@ -96,6 +86,7 @@ public class BrushModel
}
Mesh mesh = new Mesh();
mesh.name = $"Surface_{node.firstSurface + surfIdx}";
mesh.SetVertices(tempVertices);
mesh.SetUVs(0, tempTextureUVs);
mesh.SetUVs(1, tempLightmapUVs);
@ -103,9 +94,27 @@ public class BrushModel
mesh.RecalculateNormals();
mesh.UploadMeshData(true);
meshes.Add(mesh);
CreateMeshObject(mesh, nodeGO);
}
var texNum = surface.TextureInfo.Texture.TextureNum;
}
}
private void CreateMeshObject(Mesh mesh, GameObject parentGO)
{
var meshGO = new GameObject(mesh.name);
meshGO.transform.SetParent(parentGO.transform);
var mf = meshGO.AddComponent<MeshFilter>();
mf.sharedMesh = mesh;
var mr = meshGO.AddComponent<MeshRenderer>();
mr.sharedMaterial = debugMaterial;
mr.shadowCastingMode = ShadowCastingMode.Off;
mr.receiveShadows = false;
mr.lightProbeUsage = LightProbeUsage.Off;
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
}
}
Loading…
Cancel
Save