298 lines
12 KiB
C#
298 lines
12 KiB
C#
//public void Split()
|
||
//{
|
||
// var currentSelect = Selection.activeTransform;
|
||
// var terrainDat = currentSelect.GetComponent<Terrain>().terrainData;
|
||
// HeightmapWidth = terrainDat.heightmapWidth;
|
||
// HeightmapHeight = terrainDat.heightmapHeight;
|
||
|
||
// //每行有90个顶点
|
||
// tRes = (HeightmapWidth) / 90;
|
||
// var terrainName = currentSelect.name;
|
||
// var terrain = currentSelect.GetComponent<Terrain>().terrainData;
|
||
// //高度图的长宽
|
||
// int w = terrain.heightmapWidth;
|
||
// int h = terrain.heightmapHeight;
|
||
// //地形尺寸
|
||
// Vector3 meshScale = terrain.size;
|
||
// //尺寸缩放比,y方向是高度,不做缩放
|
||
// meshScale = new Vector3(meshScale.x / (h - 1) * tRes, meshScale.y, meshScale.z / (w - 1) * tRes);
|
||
// //uv缩放比,每一个像素对应的纹理坐标
|
||
// Vector2 uvScale = new Vector2((float)(1.0 / (w - 1)), (float)(1.0 / (h - 1)));
|
||
// //地形的高度值
|
||
// float[,] tData = terrain.GetHeights(0, 0, w, h);
|
||
// //每一个格子的宽高
|
||
// w = (int)((w - 1) / tRes + 1);
|
||
// h = (int)((h - 1) / tRes + 1);
|
||
// //顶点数,就是总共的格子数
|
||
// Vector3[] tVertices = new Vector3[w * h];
|
||
// Vector2[] tUV = new Vector2[w * h];
|
||
// //面数*2
|
||
// int[] tPolys = new int[(w - 1) * (h - 1) * 6];
|
||
// int y = 0;
|
||
// int x = 0;
|
||
// for (y = 0; y < h; y++)
|
||
// {
|
||
// for (x = 0; x < w; x++)
|
||
// {
|
||
// //tVertices[y*w + x] = Vector3.Scale(meshScale, new Vector3(x, tData[(int)(x*tRes),(int)(y*tRes)], y));
|
||
// tVertices[y * w + x] = Vector3.Scale(meshScale, new Vector3(-y, tData[(int)(x * tRes), (int)(y * tRes)], x)); //Thank Cid Newman
|
||
// tUV[y * w + x] = Vector2.Scale(new Vector2(y * tRes, x * tRes), uvScale);
|
||
// }
|
||
// }
|
||
|
||
// y = 0;
|
||
// x = 0;
|
||
// int index = 0;
|
||
// for (y = 0; y < h - 1; y++)
|
||
// {
|
||
// for (x = 0; x < w - 1; x++)
|
||
// {
|
||
// tPolys[index++] = (y * w) + x;
|
||
// tPolys[index++] = ((y + 1) * w) + x;
|
||
// tPolys[index++] = (y * w) + x + 1;
|
||
|
||
// tPolys[index++] = ((y + 1) * w) + x;
|
||
// tPolys[index++] = ((y + 1) * w) + x + 1;
|
||
// tPolys[index++] = (y * w) + x + 1;
|
||
// }
|
||
// }
|
||
|
||
// bool ExportNameSuccess = false;
|
||
// int num = 1;
|
||
// string Next;
|
||
// do
|
||
// {
|
||
// Next = terrainName + num;
|
||
|
||
// if (!System.IO.File.Exists(T4MPrefabFolder + "Terrains/" + terrainName + ".prefab"))
|
||
// {
|
||
// FinalExpName = terrainName;
|
||
// ExportNameSuccess = true;
|
||
// }
|
||
// else if (!System.IO.File.Exists(T4MPrefabFolder + "Terrains/" + Next + ".prefab"))
|
||
// {
|
||
// FinalExpName = Next;
|
||
// ExportNameSuccess = true;
|
||
// }
|
||
// num++;
|
||
// } while (!ExportNameSuccess);
|
||
|
||
// //StreamWriter sw = new StreamWriter(T4MPrefabFolder+"Terrains/Meshes/"+FinalExpName+".obj");
|
||
// StreamWriter sw = new StreamWriter(FinalExpName + ".obj");
|
||
// try
|
||
// {
|
||
|
||
// sw.WriteLine("# T4M File");
|
||
// System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
|
||
// for (int i = 0; i < tVertices.Length; i++)
|
||
// {
|
||
// UpdateProgress();
|
||
// StringBuilder sb = new StringBuilder("v ", 20);
|
||
// sb.Append(tVertices[i].x.ToString()).Append(" ").
|
||
// Append(tVertices[i].y.ToString()).Append(" ").
|
||
// Append(tVertices[i].z.ToString());
|
||
// sw.WriteLine(sb);
|
||
// }
|
||
|
||
// for (int i = 0; i < tUV.Length; i++)
|
||
// {
|
||
// UpdateProgress();
|
||
// StringBuilder sb = new StringBuilder("vt ", 22);
|
||
// sb.Append(tUV[i].x.ToString()).Append(" ").
|
||
// Append(tUV[i].y.ToString());
|
||
// sw.WriteLine(sb);
|
||
// }
|
||
// for (int i = 0; i < tPolys.Length; i += 3)
|
||
// {
|
||
// UpdateProgress();
|
||
// StringBuilder sb = new StringBuilder("f ", 43);
|
||
// sb.Append(tPolys[i] + 1).Append("/").Append(tPolys[i] + 1).Append(" ").
|
||
// Append(tPolys[i + 1] + 1).Append("/").Append(tPolys[i + 1] + 1).Append(" ").
|
||
// Append(tPolys[i + 2] + 1).Append("/").Append(tPolys[i + 2] + 1);
|
||
// sw.WriteLine(sb);
|
||
// }
|
||
// }
|
||
// catch (Exception err)
|
||
// {
|
||
// Debug.Log("Error saving file: " + err.Message);
|
||
// }
|
||
// sw.Close();
|
||
// AssetDatabase.SaveAssets();
|
||
|
||
// string path = T4MPrefabFolder + "Terrains/Texture/" + FinalExpName + ".png";
|
||
|
||
// var keepTexture = false;
|
||
// //Control Texture Creation or Recuperation
|
||
// string AssetName = AssetDatabase.GetAssetPath(currentSelect.GetComponent<Terrain>().terrainData) as string;
|
||
// UnityEngine.Object[] AssetName2 = AssetDatabase.LoadAllAssetsAtPath(AssetName);
|
||
// if (AssetName2 != null && AssetName2.Length > 1 && keepTexture)
|
||
// {
|
||
// for (var b = 0; b < AssetName2.Length; b++)
|
||
// {
|
||
// if (AssetName2[b].name == "SplatAlpha 0")
|
||
// {
|
||
// Texture2D texture = AssetName2[b] as Texture2D;
|
||
// byte[] bytes = texture.EncodeToPNG();
|
||
// File.WriteAllBytes(path, bytes);
|
||
// AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
|
||
// }
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// Texture2D NewMaskText = new Texture2D(512, 512, TextureFormat.ARGB32, true);
|
||
// Color[] ColorBase = new Color[512 * 512];
|
||
// for (var t = 0; t < ColorBase.Length; t++)
|
||
// {
|
||
// ColorBase[t] = new Color(1, 0, 0, 0);
|
||
// }
|
||
// NewMaskText.SetPixels(ColorBase);
|
||
// byte[] data = NewMaskText.EncodeToPNG();
|
||
// File.WriteAllBytes(path, data);
|
||
// AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
|
||
// }
|
||
// AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
|
||
|
||
// UpdateProgress();
|
||
|
||
// //Modification de la Texture
|
||
// TextureImporter TextureI = AssetImporter.GetAtPath(path) as TextureImporter;
|
||
// TextureI.textureFormat = TextureImporterFormat.ARGB32;
|
||
// TextureI.isReadable = true;
|
||
// TextureI.anisoLevel = 9;
|
||
// TextureI.mipmapEnabled = false;
|
||
// TextureI.wrapMode = TextureWrapMode.Clamp;
|
||
// AssetDatabase.Refresh();
|
||
|
||
// AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
|
||
|
||
// UpdateProgress();
|
||
|
||
// //Creation du Materiel
|
||
// Material Tmaterial;
|
||
// Tmaterial = new Material(Shader.Find("T4MShaders/ShaderModel2/Diffuse/T4M 4 Textures"));
|
||
// AssetDatabase.CreateAsset(Tmaterial, T4MPrefabFolder + "Terrains/Material/" + FinalExpName + ".mat");
|
||
// AssetDatabase.ImportAsset(T4MPrefabFolder + "Terrains/Material/" + FinalExpName + ".mat", ImportAssetOptions.ForceUpdate);
|
||
// AssetDatabase.Refresh();
|
||
|
||
// //Recuperation des Texture du terrain
|
||
// if (keepTexture)
|
||
// {
|
||
// SplatPrototype[] texts = currentSelect.GetComponent<Terrain>().terrainData.splatPrototypes;
|
||
// for (int e = 0; e < texts.Length; e++)
|
||
// {
|
||
// if (e < 4)
|
||
// {
|
||
// Tmaterial.SetTexture("_Splat" + e, texts[e].texture);
|
||
// Tmaterial.SetTextureScale("_Splat" + e, texts[e].tileSize * 8.9f);
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// //Attribution de la Texture Control sur le materiau
|
||
// Texture test = (Texture)AssetDatabase.LoadAssetAtPath(path, typeof(Texture));
|
||
// Tmaterial.SetTexture("_Control", test);
|
||
|
||
|
||
// UpdateProgress();
|
||
|
||
|
||
// //Deplacement de l'obj dans les repertoire mesh
|
||
// FileUtil.CopyFileOrDirectory(FinalExpName + ".obj", T4MPrefabFolder + "Terrains/Meshes/" + FinalExpName + ".obj");
|
||
// FileUtil.DeleteFileOrDirectory(FinalExpName + ".obj");
|
||
|
||
|
||
|
||
// //Force Update
|
||
// AssetDatabase.ImportAsset(T4MPrefabFolder + "Terrains/Meshes/" + FinalExpName + ".obj", ImportAssetOptions.ForceUpdate);
|
||
|
||
// UpdateProgress();
|
||
|
||
// //Instance du T4M
|
||
// GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(T4MPrefabFolder + "Terrains/Meshes/" + FinalExpName + ".obj", typeof(GameObject));
|
||
|
||
// AssetDatabase.Refresh();
|
||
|
||
|
||
// GameObject forRotate = (GameObject)UnityEngine.Object.Instantiate(prefab, currentSelect.transform.position, Quaternion.identity) as GameObject;
|
||
// Transform childCheck = forRotate.transform.Find("default");
|
||
// var Child = childCheck.gameObject;
|
||
// forRotate.transform.DetachChildren();
|
||
// UnityEngine.Object.DestroyImmediate(forRotate);
|
||
// Child.name = FinalExpName;
|
||
// Child.AddComponent<T4MObjSC>();
|
||
// //Child.transform.rotation= Quaternion.Euler(0, 90, 0);
|
||
|
||
// UpdateProgress();
|
||
|
||
// //Application des Parametres sur le Script
|
||
// Child.GetComponent<T4MObjSC>().T4MMaterial = Tmaterial;
|
||
// Child.GetComponent<T4MObjSC>().ConvertType = "UT";
|
||
|
||
// //Regalges Divers
|
||
// var vertexInfo = 0;
|
||
// var partofT4MObj = 0;
|
||
// var trisInfo = 0;
|
||
// int countchild = Child.transform.childCount;
|
||
// if (countchild > 0)
|
||
// {
|
||
// Renderer[] T4MOBJPART = Child.GetComponentsInChildren<Renderer>();
|
||
// for (int i = 0; i < T4MOBJPART.Length; i++)
|
||
// {
|
||
// if (!T4MOBJPART[i].gameObject.AddComponent<MeshCollider>())
|
||
// T4MOBJPART[i].gameObject.AddComponent<MeshCollider>();
|
||
// T4MOBJPART[i].gameObject.isStatic = true;
|
||
// T4MOBJPART[i].material = Tmaterial;
|
||
// T4MOBJPART[i].gameObject.layer = 30;
|
||
// T4MOBJPART[i].gameObject.AddComponent<T4MPartSC>();
|
||
// Child.GetComponent<T4MObjSC>().T4MMesh = T4MOBJPART[0].GetComponent<MeshFilter>();
|
||
// partofT4MObj += 1;
|
||
// vertexInfo += T4MOBJPART[i].gameObject.GetComponent<MeshFilter>().sharedMesh.vertexCount;
|
||
// trisInfo += T4MOBJPART[i].gameObject.GetComponent<MeshFilter>().sharedMesh.triangles.Length / 3;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// Child.AddComponent<MeshCollider>();
|
||
// Child.isStatic = true;
|
||
// Child.GetComponent<Renderer>().material = Tmaterial;
|
||
// Child.layer = 30;
|
||
// vertexInfo += Child.GetComponent<MeshFilter>().sharedMesh.vertexCount;
|
||
// trisInfo += Child.GetComponent<MeshFilter>().sharedMesh.triangles.Length / 3;
|
||
// partofT4MObj += 1;
|
||
// }
|
||
|
||
// UpdateProgress();
|
||
|
||
|
||
// GameObject BasePrefab2 = PrefabUtility.CreatePrefab(T4MPrefabFolder + "Terrains/" + FinalExpName + ".prefab", Child);
|
||
// AssetDatabase.ImportAsset(T4MPrefabFolder + "Terrains/" + FinalExpName + ".prefab", ImportAssetOptions.ForceUpdate);
|
||
// GameObject forRotate2 = (GameObject)PrefabUtility.InstantiatePrefab(BasePrefab2) as GameObject;
|
||
|
||
// UnityEngine.Object.DestroyImmediate(Child.gameObject);
|
||
|
||
// Child = forRotate2.gameObject;
|
||
|
||
// currentSelect.GetComponent<Terrain>().enabled = false;
|
||
|
||
// EditorUtility.SetSelectedWireframeHidden(Child.GetComponent<Renderer>(), true);
|
||
|
||
// EditorUtility.ClearProgressBar();
|
||
|
||
// AssetDatabase.DeleteAsset(T4MPrefabFolder + "Terrains/Meshes/Materials");
|
||
// terrainName = "";
|
||
// AssetDatabase.StartAssetEditing();
|
||
// //Modification des attribut du mesh avant de le pr茅fabriquer
|
||
// ModelImporter OBJI = ModelImporter.GetAtPath(T4MPrefabFolder + "Terrains/Meshes/" + FinalExpName + ".obj") as ModelImporter;
|
||
// OBJI.globalScale = 1;
|
||
// OBJI.splitTangentsAcrossSeams = true;
|
||
// OBJI.normalImportMode = ModelImporterTangentSpaceMode.Calculate;
|
||
// OBJI.tangentImportMode = ModelImporterTangentSpaceMode.Calculate;
|
||
// OBJI.generateAnimations = ModelImporterGenerateAnimations.None;
|
||
// OBJI.meshCompression = ModelImporterMeshCompression.Off;
|
||
// OBJI.normalSmoothingAngle = 180f;
|
||
// //AssetDatabase.ImportAsset (T4MPrefabFolder+"Terrains/Meshes/"+FinalExpName+".obj", ImportAssetOptions.TryFastReimportFromMetaData);
|
||
// AssetDatabase.ImportAsset(T4MPrefabFolder + "Terrains/Meshes/" + FinalExpName + ".obj", ImportAssetOptions.ForceSynchronousImport);
|
||
// AssetDatabase.StopAssetEditing();
|
||
// PrefabUtility.ResetToPrefabState(Child);
|
||
//} |