diff --git a/Assets/Scripts/Modules/BrushModel.cs b/Assets/Scripts/Modules/BrushModel.cs index 91e6c39..443b8ed 100644 --- a/Assets/Scripts/Modules/BrushModel.cs +++ b/Assets/Scripts/Modules/BrushModel.cs @@ -12,10 +12,18 @@ public class BrushModel private readonly List tempTextureUVs = new List(); private readonly List tempLightmapUVs = new List(); private readonly List tempIndices = new List(); - + + 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(); - mf.sharedMesh = mesh; - - var mr = meshGO.AddComponent(); - 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(); + mf.sharedMesh = mesh; + + var mr = meshGO.AddComponent(); + mr.sharedMaterial = debugMaterial; + mr.shadowCastingMode = ShadowCastingMode.Off; + mr.receiveShadows = false; + mr.lightProbeUsage = LightProbeUsage.Off; + mr.reflectionProbeUsage = ReflectionProbeUsage.Off; + } }