Advertisement
Selzier

Unity ADT & WMO Importer

Jun 14th, 2020
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7.  
  8. public class ImportAdtWorld : MonoBehaviour
  9. {
  10. #if UNITY_EDITOR
  11.     public TextAsset[] csvFiles;
  12.     private float maxSize;
  13.     private float mapSize;
  14.     private float adtSize;
  15.     private bool adtCsv, wmoCsv;
  16.     private List<string> missingModelsList = new List<string>();
  17.  
  18.     // Start is called before the first frame update
  19.     void Start() {
  20.         maxSize = 51200f / 3f;        
  21.         mapSize = maxSize * 2f;
  22.         adtSize = mapSize / 64f;
  23.         foreach (TextAsset i in csvFiles) {
  24.             ParseCsvFile(AssetDatabase.GetAssetPath(i));
  25.         }        
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update() { }
  30.  
  31.     private void ParseCsvFile(string csvPath, GameObject parentObject = null) {
  32.         //TextAsset csvText = new TextAsset(System.IO.File.ReadAllText(csvPath));
  33.         string filename = System.IO.Path.GetFileName(csvPath);
  34.         string csvDir = System.IO.Path.GetDirectoryName(csvPath);
  35.         //string csvPath = AssetDatabase.GetAssetPath(file);
  36.         //Debug.Log("Starting: " + csvPath);
  37.         string fileData = System.IO.File.ReadAllText(csvPath);
  38.         string[] lines = fileData.Split("\n"[0]);
  39.         string[] lineData = (lines[0].Trim()).Split(";"[0]);
  40.  
  41.         string adtName = filename.Replace("_ModelPlacementInformation.csv", "");
  42.  
  43.         Transform parent = null, wmoParent = null;
  44.         if (adtName.Contains("adt_") && parentObject == null) {
  45.             parent = GameObject.Find(adtName).transform;            
  46.             GameObject wmoParentGO = new GameObject();
  47.             wmoParentGO.name = "WMOs";
  48.             wmoParent = wmoParentGO.transform;
  49.             wmoParent.parent = parent;
  50.             wmoParent.SetSiblingIndex(0);
  51.             wmoParent.transform.localPosition = Vector3.zero;
  52.             wmoParent.transform.localEulerAngles = Vector3.zero;
  53.         }
  54.         else {
  55.             parent = parentObject.transform;            
  56.         }              
  57.        
  58.         if (lineData[9] == "Type") {
  59.             adtCsv = true; wmoCsv = false;
  60.         }
  61.         else if (lineData[9] == "DoodadSet") {
  62.             wmoCsv = true; adtCsv = false;
  63.         }
  64.  
  65.         GameObject doodadParentGO = new GameObject();
  66.         doodadParentGO.name = "Doodads";
  67.         Transform doodadParent = doodadParentGO.transform;
  68.         doodadParent.parent = parent;
  69.         doodadParent.SetSiblingIndex(0);
  70.         doodadParent.transform.localPosition = Vector3.zero;
  71.         doodadParent.transform.localEulerAngles = Vector3.zero;
  72.  
  73.         for (int i = 1; i < lines.Length; i++) {
  74.             // Temp Vars
  75.             float posX = 0f, posY = 0f, posZ = 0f, rotW = 0f, rotX = 0f, rotY = 0f, rotZ = 0f, scale = 1f;
  76.             int modelID = 0;
  77.             string type = "unassigned";
  78.             // Read a line from the CSV file
  79.             lineData = (lines[i].Trim()).Split(";"[0]);
  80.             // Instantiate Gameobject
  81.             string objPath = @lineData[0]; //path from csv
  82.             objPath = System.IO.Path.Combine(@"../" + objPath);
  83.             string objPathFull = System.IO.Path.GetFullPath(csvDir + objPath);
  84.             objPath = "Assets" + objPathFull.Substring(Application.dataPath.Length);
  85.  
  86.             GameObject iPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(objPath, typeof(GameObject));
  87.             if (iPrefab == null) {
  88.                 missingModelsList.Add(objPath);
  89.                 Debug.LogError(objPath);
  90.             }            
  91.  
  92.             GameObject iGO = GameObject.Instantiate(iPrefab);            
  93.             WowModel wowModel = iGO.AddComponent(typeof(WowModel)) as WowModel;
  94.             wowModel.modelID = modelID;
  95.             wowModel.type = type;
  96.             Transform iTransform = iGO.transform;
  97.  
  98.             // Populate local vars from CSV file
  99.             if (adtCsv == true) {
  100.                 posX = float.Parse(lineData[1]); // x pos
  101.                 posY = float.Parse(lineData[2]); // y pos
  102.                 posZ = float.Parse(lineData[3]); // z pos
  103.                 rotX = float.Parse(lineData[4]); // x rot
  104.                 rotY = float.Parse(lineData[5]); // y rot                
  105.                 rotZ = float.Parse(lineData[6]); // z rot
  106.                 scale = float.Parse(lineData[7]); // scale
  107.                 modelID = int.Parse(lineData[8]); // Model ID
  108.                 type = lineData[9]; // Type of file
  109.             }
  110.             else if (wmoCsv == true) {
  111.                 posX = float.Parse(lineData[1]); // x pos
  112.                 posY = float.Parse(lineData[2]); // y pos
  113.                 posZ = float.Parse(lineData[3]); // z pos
  114.                 rotW = float.Parse(lineData[4]); // w rot
  115.                 rotX = float.Parse(lineData[5]); // x rot
  116.                 rotY = float.Parse(lineData[6]); // y rot
  117.                 rotZ = float.Parse(lineData[7]); // z rot
  118.                 scale = float.Parse(lineData[8]); // scale
  119.                 type = lineData[9]; // Type of file
  120.             }
  121.  
  122.             // Set Position & Rotation
  123.             if (type == "wmo") {
  124.                 iTransform.parent = wmoParent;
  125.                 iTransform.localPosition = new Vector3((maxSize - posX) * -1f, posY, maxSize - posZ);
  126.                 Vector3 ang = new Vector3(rotX / Mathf.Rad2Deg, (rotY + 90f) / Mathf.Rad2Deg, rotZ / Mathf.Rad2Deg);
  127.                 Vector3 ang2 = new Vector3(Mathf.Rad2Deg * ang.x, Mathf.Rad2Deg * -ang.y, Mathf.Rad2Deg * ang.z);
  128.                 iTransform.localEulerAngles = ang2;
  129.                 iTransform.localScale = new Vector3(scale, scale, scale);
  130.             }
  131.             else if (type == "m2") {
  132.                 iTransform.parent = doodadParent;
  133.                 iTransform.localPosition = new Vector3((maxSize - posX) * -1f, posY, maxSize - posZ);
  134.                 Vector3 ang = new Vector3(rotX / Mathf.Rad2Deg, (rotY + 90f) / Mathf.Rad2Deg, rotZ / Mathf.Rad2Deg);
  135.                 Vector3 ang2 = new Vector3(Mathf.Rad2Deg * ang.x, Mathf.Rad2Deg * -ang.y, Mathf.Rad2Deg * ang.z);
  136.                 iTransform.localEulerAngles = ang2;
  137.                 iTransform.localScale = new Vector3(scale, scale, scale);
  138.             }
  139.             else {
  140.                 iTransform.parent = doodadParent;
  141.                 iTransform.localPosition = new Vector3(-posX, posZ, -posY);
  142.                 iTransform.localRotation = new Quaternion(rotX, -rotZ, rotY, rotW);
  143.                 iTransform.localScale = new Vector3(scale, scale, scale);
  144.             }
  145.  
  146.             // Define vars for materials and their paths
  147.             string materialPath = @lineData[0]; //path from csv
  148.             materialPath = materialPath.Substring(0, materialPath.Length - 4) + ".mtl";
  149.             materialPath = System.IO.Path.Combine(@"../" + materialPath);
  150.             string materialPathFull = System.IO.Path.GetFullPath(csvDir + materialPath);            
  151.             materialPath = "Assets" + materialPathFull.Substring(Application.dataPath.Length);
  152.             string materialFileName = System.IO.Path.GetFileName(materialPath);
  153.             string materialPathOnly = materialPathFull.Replace(materialFileName, "");
  154.  
  155.             //Debug.Log("Material path____: " + materialPath);
  156.             //Debug.Log("Material pathfull: " + materialPathFull);
  157.             //Debug.Log("Material filename: " + materialFileName);
  158.  
  159.             // Transfer values from MTL file into list
  160.             List<int> materialID = new List<int>();
  161.             List<float> illumValue = new List<float>();
  162.             List<string> texturePath = new List<string>();
  163.  
  164.             // Apply Texture To Material
  165.             string mtlData = System.IO.File.ReadAllText(materialPathFull);
  166.             string[] mtlLines = mtlData.Split("\n"[0]);
  167.             for (int j = 0; j < mtlLines.Length; j += 3) {
  168.                 int.TryParse(mtlLines[j].Substring(7), out int matid);
  169.                 materialID.Add(matid);
  170.             }
  171.             for (int j = 1; j < mtlLines.Length; j += 3) {
  172.                 int.TryParse(mtlLines[j].Substring(6), out int illum);
  173.                 illumValue.Add(illum);
  174.             }
  175.             for (int j = 2; j < mtlLines.Length; j += 3) {
  176.                 // Now it's going every 3rd line in the mtl file to pull texture filename and path
  177.                 string texturePathCurrent = mtlLines[j];
  178.                 texturePathCurrent = texturePathCurrent.Replace("map_Kd ", "");
  179.                 string texturePathFull = System.IO.Path.GetFullPath(materialPathOnly + @texturePathCurrent);
  180.                 //Debug.Log("MaterialPathFull: " + materialPathOnly + " , " + texturePathCurrent);
  181.                 texturePathCurrent = "Assets" + texturePathFull.Substring(Application.dataPath.Length);
  182.                 texturePathCurrent = texturePathCurrent.Substring(0, texturePathCurrent.Length - 4) + ".png";
  183.                 texturePath.Add(texturePathCurrent);                
  184.             }
  185.  
  186.             foreach (Transform child in iTransform) {
  187.                 // Set Material
  188.                 MeshRenderer meshRenderer = child.GetComponent<MeshRenderer>();                
  189.                 Material material = meshRenderer.material;
  190.                 string materialName = material.name;
  191.                 materialName = materialName.Replace(" (Instance)", "");
  192.                 int matint = int.Parse(materialName);
  193.                 Material dbMat = (Material)AssetDatabase.LoadAssetAtPath("Assets/Materials/" + materialName + ".mat", typeof(Material));
  194.                 if (dbMat == null) {
  195.                     // Create a material file
  196.                     AssetDatabase.CreateAsset(material, "Assets/Materials/" + materialName + ".mat");
  197.                     dbMat = (Material)AssetDatabase.LoadAssetAtPath("Assets/Materials/" + materialName + ".mat", typeof(Material));
  198.                     // Apply Texture to material
  199.                     Texture texture = (Texture)AssetDatabase.LoadAssetAtPath(texturePath[materialID.IndexOf(matint)], typeof(Texture));
  200.                     //Debug.Log("Texture path: " + texturePath[materialID.IndexOf(matint)]);
  201.  
  202.                     if (texture == null) {
  203.                         Debug.LogError("Could not find texture at: " + texturePath[materialID.IndexOf(matint)]);
  204.                     }
  205.                     else {
  206.                         dbMat.SetTexture("_MainTex", texture);
  207.                     }
  208.                 }
  209.                 meshRenderer.material = dbMat;                
  210.             }
  211.  
  212.             // Final check
  213.             if (type == "wmo") {                
  214.                 string mpiPath = objPath.Replace(".obj", "_ModelPlacementInformation.csv");
  215.                 if (System.IO.File.Exists(mpiPath)) {
  216.                     //Debug.Log("Found mpi file at: " + mpiPath);
  217.                     StartCoroutine(StartParse(mpiPath, 1f, iGO));                    
  218.                 }
  219.             }
  220.             else if (type == "m2") {
  221.                 // m2 doodad
  222.             }
  223.             else {
  224.                 //wmo csv
  225.                
  226.             }
  227.         }
  228.        
  229.         foreach (string i in missingModelsList) {
  230.             Debug.LogError("Missing Model: " + i);
  231.         }
  232.     }
  233.  
  234.     public IEnumerator StartParse(string path, float delay, GameObject parentObject) {
  235.         yield return new WaitForSeconds(delay);
  236.         ParseCsvFile(path, parentObject);
  237.     }
  238.    
  239. #endif
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement