static void LoadBSP() { try { string path = EditorUtility.OpenFilePanelWithFilters("Open BSP", AssetUtils.GetAbsoluteAssetPath("Assets/Editor/Data/maps"), new string[] { "BSP files", "bsp" }); if (string.IsNullOrEmpty(path)) { return; } using (FileStream stream = File.OpenRead(path)) { DataStream ds = new DataStream(stream); BSPFile bsp = new BSPFile(ds); string name = Path.GetFileNameWithoutExtension(path); string destPath = "Assets/Scenes/Maps/" + name; AssetUtils.CreateFolder(destPath); var materials = GenerateMaterials(bsp, destPath); if (materials == null) // cancelled { return; } GenerateLevel(bsp, materials); } } finally { EditorUtility.ClearProgressBar(); } }
static void GenerateModel(string sourcePath, string modelsPath) { using (FileStream stream = File.OpenRead(sourcePath)) { var name = FileUtilEx.getFilenameNoExtension(sourcePath); var destPath = modelsPath + "/" + name; AssetUtils.CreateFolder(destPath); DataStream ds = new DataStream(stream); UnityEngine.Object asset; string ext = Path.GetExtension(sourcePath).ToLower(); if (ext == ".mdl") { asset = CreateMDL(ds, destPath, name); } else if (ext == ".bsp") { asset = CreateBSP(ds, destPath, name); } else { Debug.LogError("Unexpected model type: " + ext); return; } AssetDatabase.CreateAsset(asset, destPath + "/" + name + ".asset"); } }
private static Material[] GenerateSkins(BSPFile bsp, string name, string destPath, TextureAtlas atlas) { var skinsPath = destPath + "/skins"; AssetUtils.CreateFolder(skinsPath); string textureName = FileUtilEx.FixFilename(name); var texturePath = skinsPath + "/" + textureName + ".png"; if (!AssetUtils.AssetPathExists(texturePath)) { atlas.WriteTexture(texturePath); AssetDatabase.Refresh(); } int index = texturePath.LastIndexOf('.'); string materialPath = texturePath.Substring(0, index) + ".mat"; if (!AssetUtils.AssetPathExists(materialPath)) { TextureImporter importer = TextureImporter.GetAtPath(texturePath) as TextureImporter; importer.textureType = TextureImporterType.Default; importer.wrapMode = TextureWrapMode.Repeat; importer.filterMode = FilterMode.Point; importer.maxTextureSize = 2048; importer.textureFormat = TextureImporterFormat.DXT1; importer.SaveAndReimport(); var material = new Material(Shader.Find("Standard")); material.mainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath); material.SetFloat("_Glossiness", 0.0f); AssetDatabase.CreateAsset(material, materialPath); } return(new Material[] { AssetDatabase.LoadAssetAtPath <Material>(materialPath) }); }
private static MDLAnimation[] GenerateAnimations(MDLFile mdl, string destPath) { var frames = mdl.geometry.frames; if (frames.Length == 1) { return(null); // no animations } var animationsPath = destPath + "/animations"; AssetUtils.CreateFolder(animationsPath); var animations = new List <MDLAnimation>(mdl.animations.Count); foreach (var e in mdl.animations) { var name = e.Key; var anim = e.Value; var rotation = Quaternion.AngleAxis(-90, Vector3.up); var animationFrames = new MDLAnimationFrame[anim.length]; for (int frameIndex = 0; frameIndex < anim.length; ++frameIndex) { var frame = frames[anim.start + frameIndex]; var verts = frame.verts; Vector3[] vertices = new Vector3[verts.Length]; int[] triangles = new int[verts.Length]; for (int vertexIndex = 0; vertexIndex < verts.Length; vertexIndex += 3) { vertices[vertexIndex] = rotation * BSPFile.TransformVector(verts[vertexIndex]); vertices[vertexIndex + 1] = rotation * BSPFile.TransformVector(verts[vertexIndex + 1]); vertices[vertexIndex + 2] = rotation * BSPFile.TransformVector(verts[vertexIndex + 2]); triangles[vertexIndex] = vertexIndex + 2; triangles[vertexIndex + 1] = vertexIndex + 1; triangles[vertexIndex + 2] = vertexIndex; } animationFrames[frameIndex] = new MDLAnimationFrame(frame.name, vertices, triangles); } var animation = ScriptableObject.CreateInstance <MDLAnimation>(); animation.name = name; animation.frames = animationFrames; animation.type = GetAnimationType(name); if (name == "frame") { name = mdl.name; } if (name == "v_axe") { name = "shot"; // dirty little hack } var animationPath = animationsPath + "/" + name + "_animation.asset"; AssetDatabase.CreateAsset(animation, animationPath); animations.Add(AssetDatabase.LoadAssetAtPath <MDLAnimation>(animationPath)); } AssetDatabase.SaveAssets(); return(animations.ToArray()); }
static Material[] GenerateSkins(MDLFile mdl, string destPath) { List <string> textures = new List <string>(); List <Material> materials = new List <Material>(); var skinsPath = destPath + "/skins"; AssetUtils.CreateFolder(skinsPath); int skinId = 0; foreach (var skin in mdl.skins) { var textureName = mdl.skins.Length > 1 ? string.Format("{0}_skin_{1}.png", mdl.name, skinId++) : string.Format("{0}_skin.png", mdl.name); var texturePath = skinsPath + "/" + textureName; if (!AssetUtils.AssetPathExists(texturePath)) { Texture2D tex = new Texture2D(skin.width, skin.height); Color32[] pixels = new Color32[skin.width * skin.height]; for (int i = 0, j = 0; i < pixels.Length; ++i) { pixels[i] = new Color32(skin.data[j++], skin.data[j++], skin.data[j++], skin.data[j++]); } for (int x = 0; x < skin.width; ++x) { for (int y = 0; y < skin.height / 2; ++y) { int from = y * skin.width + x; int to = (skin.height - y - 1) * skin.width + x; var temp = pixels[to]; pixels[to] = pixels[from]; pixels[from] = temp; } } tex.SetPixels32(pixels); File.WriteAllBytes(AssetUtils.GetAbsoluteAssetPath(texturePath), tex.EncodeToPNG()); } textures.Add(texturePath); } AssetDatabase.Refresh(); foreach (var texture in textures) { int index = texture.LastIndexOf('.'); string materialPath = texture.Substring(0, index) + ".mat"; if (!AssetUtils.AssetPathExists(materialPath)) { TextureImporter importer = TextureImporter.GetAtPath(texture) as TextureImporter; importer.textureType = TextureImporterType.Default; importer.wrapMode = TextureWrapMode.Repeat; importer.filterMode = FilterMode.Point; importer.maxTextureSize = 2048; importer.textureFormat = TextureImporterFormat.DXT1; importer.SaveAndReimport(); var material = new Material(Shader.Find("Standard")); material.mainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texture); material.SetFloat("_Glossiness", 0.0f); AssetDatabase.CreateAsset(material, materialPath); } materials.Add(AssetDatabase.LoadAssetAtPath <Material>(materialPath)); } AssetDatabase.SaveAssets(); return(materials.ToArray()); }
static IList <Material> GenerateMaterials(BSPFile bsp, string destPath) { List <string> textures = new List <string>(); List <Material> materials = new List <Material>(); string materialsPath = destPath + "/materials"; AssetUtils.CreateFolder(materialsPath); int tex_id = 0; foreach (var t in bsp.textures) { string textureName = string.Format("{0}.png", FileUtilEx.FixFilename(t.name)); string texturePath = materialsPath + "/" + textureName; float progress = ((float)++tex_id) / bsp.textures.Length; if (EditorUtility.DisplayCancelableProgressBar("Level", "Reading texture: " + texturePath, progress)) { return(null); } if (!AssetUtils.AssetPathExists(texturePath)) { Texture2D tex = new Texture2D(t.width, t.height); Color32[] pixels = new Color32[t.width * t.height]; for (int i = 0, j = 0; i < pixels.Length; ++i) { pixels[i] = new Color32(t.data[j++], t.data[j++], t.data[j++], t.data[j++]); } for (int x = 0; x < t.width; ++x) { for (int y = 0; y < t.height / 2; ++y) { int from = y * t.width + x; int to = (t.height - y - 1) * t.width + x; var temp = pixels[to]; pixels[to] = pixels[from]; pixels[from] = temp; } } tex.SetPixels32(pixels); File.WriteAllBytes(AssetUtils.GetAbsoluteAssetPath(texturePath), tex.EncodeToPNG()); } textures.Add(texturePath); } AssetDatabase.Refresh(); int materialNum = 0; foreach (var texture in textures) { int index = texture.LastIndexOf('.'); string materialPath = texture.Substring(0, index) + ".mat"; float progress = ((float)++materialNum) / bsp.textures.Length; if (EditorUtility.DisplayCancelableProgressBar("Level", "Generating material: " + materialPath, progress)) { return(null); } if (!AssetUtils.AssetPathExists(materialPath)) { TextureImporter importer = TextureImporter.GetAtPath(texture) as TextureImporter; importer.textureType = TextureImporterType.Default; importer.wrapMode = TextureWrapMode.Repeat; importer.filterMode = FilterMode.Point; importer.maxTextureSize = 2048; importer.textureFormat = TextureImporterFormat.DXT1; importer.SaveAndReimport(); var material = new Material(Shader.Find("Standard")); material.mainTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(texture); material.SetFloat("_Glossiness", 0.0f); AssetDatabase.CreateAsset(material, materialPath); } materials.Add(AssetDatabase.LoadAssetAtPath <Material>(materialPath)); } return(materials); }
static void RunTest() { AssetUtils.CreateFolder("Assets/Models/shambler"); }