public override void InitEditor() { base.InitEditor(); //load data m_pEditingData = new CSceneType(); m_pEditingData.Load(); }
protected void Initial() { m_pAllTemplates = new CSceneTemplate(); m_pAllTemplates.Load(); List <string> sAllTemplates = new List <string>(); foreach (CSceneTemplateElement element in m_pAllTemplates.m_pElement) { sAllTemplates.Add(element.m_sElementName); } m_sTemplateChooses = sAllTemplates.ToArray(); m_pSceneType = new CSceneType(); m_pSceneType.Load(); }
void OnWizardCreate() { #region Step1: Check files if (string.IsNullOrEmpty(TemplateName)) { CRuntimeLogger.LogError("必须有个模板名称"); return; } Texture2D heighttx = (Texture2D)EditorCommonFunctions.GetReadWritable(HeightMap); if (null == heighttx || SceneConstant.m_iHightmapSize != heighttx.width || SceneConstant.m_iHightmapSize != heighttx.height) { CRuntimeLogger.LogError(string.Format("必须是{0}x{0}的高度贴图,如果你的图片正确,可以检查下导入设置是否是Editor/GUI", SceneConstant.m_iHightmapSize)); return; } Texture2D groundtx = (Texture2D)EditorCommonFunctions.GetReadWritable(GroundMap); if (null == groundtx || SceneConstant.m_iHightmapSize != groundtx.width || SceneConstant.m_iHightmapSize != groundtx.height) { CRuntimeLogger.LogError(string.Format("必须是{0}x{0}的地皮贴图,如果你的图片正确,可以检查下导入设置是否是Editor/GUI", SceneConstant.m_iHightmapSize)); return; } Texture2D doctx = (Texture2D)EditorCommonFunctions.GetReadWritable(DocMap); if (null == doctx || SceneConstant.m_iDecoratemapSize != doctx.width || SceneConstant.m_iDecoratemapSize != doctx.height) { CRuntimeLogger.LogError(string.Format("必须是{0}x{0}的装饰贴图", SceneConstant.m_iDecoratemapSize)); return; } #endregion #region Step2: Create string sGroundFileName = SceneConstant.m_sArtworkPath + "Templates/" + string.Format("T{0}X{0}", SceneConstant.m_iHightmapSize) + "/GenerateMesh/" + TemplateName + ".asset"; Texture2D wmap, nmap; SceneEditorUtility.CreateGround(heighttx, groundtx, doctx, sGroundFileName, GroundType, false, out wmap, out nmap); CSceneTexture sceneTx = new CSceneTexture { m_sSceneType = GroundType }; if (!sceneTx.Load() || 81 != sceneTx.m_pElement.Count) { CRuntimeLogger.LogError(GroundType + "贴图配置文件有问题"); return; } CSceneGroudTemplate groundTemplate = new CSceneGroudTemplate { m_sSceneType = GroundType }; if (!groundTemplate.Load()) { CRuntimeLogger.LogError(GroundType + "贴图配置文件有问题"); return; } CSceneType type = new CSceneType(); type.Load(); if (null == type[GroundType]) { CRuntimeLogger.LogError(GroundType + "的配置找不到?"); return; } #endregion #region Step3: Make Record CSceneTemplate templates = new CSceneTemplate(); templates.Load(); CSceneTemplateElement element = templates[TemplateName]; if (null == element) { element = templates.CreateElement(TemplateName); } element.m_sDecoratePath = AssetDatabase.GetAssetPath(DocMap); element.m_sGroundPath = AssetDatabase.GetAssetPath(GroundMap); element.m_sHeightPath = AssetDatabase.GetAssetPath(HeightMap); templates.Save(); #endregion }
protected void BuildTextureDataWithFolder() { #region Step1: Find all files string sFolderTx = Application.dataPath + "/" + SceneConstant.m_sArtworkPath + TypeName + SceneConstant.m_sSceneTexturesPath; string sFolderDoc = Application.dataPath + "/" + SceneConstant.m_sArtworkPath + TypeName + SceneConstant.m_sDecoratesPath; CRuntimeLogger.Log("将从" + sFolderTx + "和" + sFolderDoc + "收集信息"); DirectoryInfo dirtx = new DirectoryInfo(sFolderTx); DirectoryInfo dirdoc = new DirectoryInfo(sFolderDoc); if (!dirtx.Exists || !dirdoc.Exists) { CRuntimeLogger.LogWarning("你选择目录内容不太对吧!"); return; } List <FileInfo> allTextures = new List <FileInfo>(); allTextures.AddRange(dirtx.GetFiles("*.png", SearchOption.AllDirectories)); allTextures.AddRange(dirdoc.GetFiles("*.png", SearchOption.AllDirectories)); List <FileInfo> allModels = dirdoc.GetFiles("*.fbx", SearchOption.AllDirectories).ToList(); #endregion #region Step2: Create SceneType CSceneType scenetypes = new CSceneType(); scenetypes.Load(); if (null == scenetypes[TypeName]) { CSceneTypeElement newType = scenetypes.CreateElement(TypeName); newType.m_bCanRot = CanRotate; newType.m_eEdgeType = EdgeType; newType.m_iCliffType = CliffType; newType.m_bHasGroundOffset = HasGroundOffset; } else { scenetypes[TypeName].m_bCanRot = CanRotate; scenetypes[TypeName].m_eEdgeType = EdgeType; scenetypes[TypeName].m_iCliffType = CliffType; scenetypes[TypeName].m_bHasGroundOffset = HasGroundOffset; } scenetypes.Save(); #endregion #region Step3: Bake Texture Atlas Dictionary <int, int> codes = new Dictionary <int, int>(new IntEqualityComparer()); Dictionary <string, Texture2D> txDic = new Dictionary <string, Texture2D>(); foreach (FileInfo fileInfo in allTextures) { string sFileName = CommonFunctions.GetLastName(fileInfo.FullName); string[] twoparts = sFileName.Split('_'); string sAssetPath = "Assets" + fileInfo.FullName.Replace("\\", "/").Replace(Application.dataPath.Replace("\\", "/"), ""); Texture2D t1 = AssetDatabase.LoadAssetAtPath(sAssetPath, typeof(Texture2D)) as Texture2D; txDic.Add(sFileName, (Texture2D)EditorCommonFunctions.GetReadWritable(t1)); //Check whether is a template texture int iCode, iRepeat; if (2 == twoparts.Length && !string.IsNullOrEmpty(twoparts[0]) && int.TryParse(twoparts[0].Substring(1, twoparts[0].Length - 1), out iCode) && int.TryParse(twoparts[1], out iRepeat)) { if (codes.ContainsKey(iCode)) { if (codes[iCode] < iRepeat) { codes[iCode] = iRepeat; } } else { codes.Add(iCode, iRepeat); } } } Dictionary <string, Rect> textureDic = EditorCommonFunctions.PackTexture( txDic, SceneConstant.m_sArtworkPath + TypeName + SceneConstant.m_sSceneTexturesPath, Shader.Find("Mobile/Unlit (Supports Lightmap)"), EditorCommonFunctions.EPackColorFormat.ForcePng); #endregion #region Step4: Make template CSceneGroudTemplate tamplate = new CSceneGroudTemplate { m_sSceneType = TypeName }; foreach (KeyValuePair <string, Rect> kvp in textureDic) { CSceneGroudTemplateElement newGT = tamplate.CreateElement(); tamplate.ChangeName(newGT.m_sElementName, kvp.Key); newGT.m_vUV = CommonFunctions.Rect2V4(kvp.Value); } tamplate.Save(); #endregion #region Step5: Make Scene Texture CSceneTexture newT = new CSceneTexture { m_sSceneType = TypeName }; for (int lt = 0; lt < 3; ++lt) { for (int rt = 0; rt < 3; ++rt) { for (int rb = 0; rb < 3; ++rb) { for (int lb = 0; lb < 3; ++lb) { int iCode, iRots; bool bFlip; FindCodeAndRot(codes, lt, rt, rb, lb, out iCode, out iRots, out bFlip); if (iCode < 0) { CRuntimeLogger.LogError(string.Format("组合没有找到{0}-{1}-{2}-{3}", lt, rt, rb, lb)); return; } CSceneTextureElement newE = newT.CreateElement(); int iCode2 = lt * CommonFunctions.IntPow(3, 0) + rt * CommonFunctions.IntPow(3, 1) + rb * CommonFunctions.IntPow(3, 2) + lb * CommonFunctions.IntPow(3, 3); newT.ChangeName(newE.m_sElementName, "T" + iCode2); if (iCode2 != newE.m_iID) { newT.ChangeId(newE.m_iID, iCode2); } newE.m_iTemplate = iCode; newE.m_iRotNumber = iRots; newE.m_iTextureCount = codes[iCode]; newE.m_bReflect = bFlip; } } } } newT.Save(); #endregion #region Step6: Make Decorate Dictionary <int, string> modelTemplates = new Dictionary <int, string>(new IntEqualityComparer()); Dictionary <int, int> modelRepeates = new Dictionary <int, int>(new IntEqualityComparer()); foreach (FileInfo fileInfo in allModels) { string sFileName = CommonFunctions.GetLastName(fileInfo.FullName); string[] twoparts = sFileName.Split('_'); string sAssetPath = "Assets" + fileInfo.FullName.Replace("\\", "/").Replace(Application.dataPath.Replace("\\", "/"), ""); //Check whether is a template texture int iCode, iRepeat; if (3 == twoparts.Length && int.TryParse(twoparts[1], out iCode) && int.TryParse(twoparts[2], out iRepeat) && iCode < 3 && iCode >= 0) { //Make template data if (!modelTemplates.ContainsKey(iCode)) { modelTemplates.Add(iCode, twoparts[0] + "_" + iCode); } if (!modelRepeates.ContainsKey(iCode)) { modelRepeates.Add(iCode, iRepeat); } else if (modelRepeates[iCode] < iRepeat) { modelRepeates[iCode] = iRepeat; } //Make the mesh List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> trangles = new List <int>(); GameObject go = AssetDatabase.LoadMainAssetAtPath(sAssetPath) as GameObject; if (null != go) { foreach (MeshFilter mf in go.GetComponentsInChildren <MeshFilter>(true)) { Mesh editablemesh = (Mesh)EditorCommonFunctions.GetReadWritable(mf.sharedMesh); int iVertCount = verts.Count; Rect rct; MeshRenderer mr = mf.gameObject.GetComponent <MeshRenderer>(); if (null != mr && null != mr.sharedMaterial && null != mr.sharedMaterial.mainTexture && textureDic.ContainsKey(mr.sharedMaterial.mainTexture.name)) { rct = textureDic[mr.sharedMaterial.mainTexture.name]; } else if (textureDic.ContainsKey(editablemesh.name)) { rct = textureDic[editablemesh.name]; } else { CRuntimeLogger.LogWarning("Mesh找不到贴图:" + sAssetPath); return; } if (mf.transform == go.transform) { verts.AddRange(editablemesh.vertices); } else { for (int i = 0; i < editablemesh.vertices.Length; ++i) { Vector3 worldPos = mf.transform.localToWorldMatrix.MultiplyVector(editablemesh.vertices[i]) + mf.transform.position; verts.Add(go.transform.worldToLocalMatrix.MultiplyVector(worldPos - go.transform.position)); } } for (int i = 0; i < editablemesh.uv.Length; ++i) { float fX = Mathf.Clamp01(editablemesh.uv[i].x); float fY = Mathf.Clamp01(editablemesh.uv[i].y); uvs.Add(new Vector2(rct.xMin + rct.width * fX, rct.yMin + rct.height * fY)); } for (int i = 0; i < editablemesh.triangles.Length; ++i) { trangles.Add(iVertCount + editablemesh.triangles[i]); } } } Mesh theMesh = new Mesh { vertices = verts.ToArray(), uv = uvs.ToArray(), triangles = trangles.ToArray() }; theMesh.RecalculateNormals(); Unwrapping.GenerateSecondaryUVSet(theMesh); ; AssetDatabase.CreateAsset(theMesh, "Assets/" + SceneConstant.m_sArtworkPath + TypeName + SceneConstant.m_sDecoratesPath + "/Generate/" + sFileName + ".asset"); } } CSceneDecorate doc = new CSceneDecorate { m_sSceneType = TypeName }; CSceneDecorate olddoc = new CSceneDecorate { m_sSceneType = TypeName }; olddoc.Load(); foreach (KeyValuePair <int, string> kvp in modelTemplates) { CSceneDecorateElement element = doc.CreateElement(kvp.Value); if (element.m_iID != kvp.Key) { doc.ChangeId(element.m_iID, kvp.Key); } element.m_iDecrateRepeat = modelRepeates[kvp.Key]; if (null != olddoc[kvp.Key]) { element.m_bBlockPathfinding = olddoc[kvp.Key].m_bBlockPathfinding; element.m_iDecrateSize = olddoc[kvp.Key].m_iDecrateSize; element.m_bOnlyRotateY = olddoc[kvp.Key].m_bOnlyRotateY; } } doc.Save(); AssetDatabase.Refresh(); #endregion }
public static void CreateGround(Texture2D heighttx, Texture2D groundtx, Texture2D doctx, string sGroundFileName, string sGroundType, bool bCreatePathmap, out Texture2D wmap, out Texture2D nmap) { #region Step1: Check files wmap = null; nmap = null; CSceneTexture sceneTx = new CSceneTexture { m_sSceneType = sGroundType }; if (!sceneTx.Load() || 81 != sceneTx.m_pElement.Count) { CRuntimeLogger.LogError(sGroundType + "贴图配置文件有问题"); return; } CSceneGroudTemplate groundTemplate = new CSceneGroudTemplate { m_sSceneType = sGroundType }; if (!groundTemplate.Load()) { CRuntimeLogger.LogError(sGroundType + "贴图配置文件有问题"); return; } CSceneType type = new CSceneType(); type.Load(); if (null == type[sGroundType]) { CRuntimeLogger.LogError(sGroundType + "的配置找不到?"); return; } #endregion #region Step2: Read colors and make cliff ground type replace, 4096 x 4 int iProgressOne = heighttx.width * heighttx.height; int iProgressTotal = iProgressOne * 10; int iProgressNow = 0; if (bCreatePathmap) { wmap = new Texture2D(heighttx.width - 1, heighttx.height - 1, TextureFormat.RGB24, false); nmap = new Texture2D(heighttx.width - 1, heighttx.height - 1, TextureFormat.RGB24, false); } Vector3[,] verts = new Vector3[heighttx.width, heighttx.height]; int[,] gtypes = new int[heighttx.width, heighttx.height]; for (int i = 0; i < heighttx.width; ++i) { for (int j = 0; j < heighttx.height; ++j) { ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "读高度图数据", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); Color h = heighttx.GetPixel(i, j); Color g = groundtx.GetPixel(i, j); verts[i, j] = new Vector3( i - (heighttx.width - 1) / 2, (h.r - 0.5f) * 2.0f * SceneConstant.m_fHighgroundHeight, j - (heighttx.height - 1) / 2); gtypes[i, j] = CompareRGB(g.r, g.g, g.b); } } iProgressNow = iProgressOne; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "读高度图数据", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); //Make Clifs for (int i = 0; i < heighttx.width - 1; ++i) { for (int j = 0; j < heighttx.height - 1; ++j) { if (Mathf.Max(verts[i, j].y, verts[i + 1, j].y, verts[i + 1, j + 1].y, verts[i, j + 1].y) - Mathf.Min(verts[i, j].y, verts[i + 1, j].y, verts[i + 1, j + 1].y, verts[i, j + 1].y) > SceneConstant.m_fCliffRate * SceneConstant.m_fHighgroundHeight) { gtypes[i, j] = type[sGroundType].m_iCliffType; gtypes[i + 1, j] = type[sGroundType].m_iCliffType; gtypes[i + 1, j + 1] = type[sGroundType].m_iCliffType; gtypes[i, j + 1] = type[sGroundType].m_iCliffType; } ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "改写悬崖", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); } } iProgressNow = iProgressOne * 2; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "改写悬崖", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); if (type[sGroundType].m_bHasGroundOffset) { for (int i = 0; i < heighttx.width; ++i) { for (int j = 0; j < heighttx.height; ++j) { verts[i, j] += Vector3.up * gtypes[i, j] * 0.05f * SceneConstant.m_fHighgroundHeight; ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "改写地皮高度", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); } } } iProgressNow = iProgressOne * 3; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "改写地皮高度", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); if (bCreatePathmap) { for (int i = 0; i < heighttx.width - 1; ++i) { for (int j = 0; j < heighttx.height - 1; ++j) { float fR, fG, fB; Vector3 vNormal = Vector3.Cross(verts[i, j + 1] - verts[i + 1, j], verts[i + 1, j + 1] - verts[i, j]); vNormal = vNormal.normalized * 0.5f + new Vector3(0.5f, 0.5f, 0.5f); float fHeight = (verts[i, j].y + verts[i + 1, j].y + verts[i + 1, j + 1].y + verts[i, j + 1].y) * 0.25f; fR = fHeight / (SceneConstant.m_fHighgroundHeight * 4.0f) + 0.5f; bool bHasCutOff = verts[i, j].y < SceneConstant.m_fHighgroundHeight * SceneConstant.m_fCutOffHeightRate || verts[i + 1, j].y < SceneConstant.m_fHighgroundHeight * SceneConstant.m_fCutOffHeightRate || verts[i + 1, j + 1].y < SceneConstant.m_fHighgroundHeight * SceneConstant.m_fCutOffHeightRate || verts[i, j + 1].y < SceneConstant.m_fHighgroundHeight * SceneConstant.m_fCutOffHeightRate; if (Mathf.Max(verts[i, j].y, verts[i + 1, j].y, verts[i + 1, j + 1].y, verts[i, j + 1].y) - Mathf.Min(verts[i, j].y, verts[i + 1, j].y, verts[i + 1, j + 1].y, verts[i, j + 1].y) > SceneConstant.m_fCliffRate * SceneConstant.m_fHighgroundHeight) { bHasCutOff = true; } fB = bHasCutOff ? 1.0f : 0.0f; fG = bHasCutOff ? 0.0f : 1.0f; wmap.SetPixel(i, j, new Color(fR, fG, fB, 1.0f)); nmap.SetPixel(i, j, new Color(vNormal.x, vNormal.y, vNormal.z, 1.0f)); ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "创建寻路地图", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); } } wmap.Apply(); //apply for farther read } iProgressNow = iProgressOne * 4; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "创建寻路地图", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); #endregion #region Step3: Build the ground, 4096 List <Vector3> poses = new List <Vector3>(); List <Vector2> theuvs = new List <Vector2>(); List <int> trangles = new List <int>(); //Create Mesh for (int i = 0; i < (heighttx.width - 1); ++i) { for (int j = 0; j < (heighttx.height - 1); ++j) { ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "创建地面", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); Vector2[] uvs = GetUVs(gtypes[i, j], gtypes[i + 1, j], gtypes[i + 1, j + 1], gtypes[i, j + 1], sceneTx, groundTemplate, type[sGroundType].m_bCanRot); Vector3[] vs = { verts[i, j], verts[i + 1, j], verts[i + 1, j + 1], verts[i, j + 1] }; //Check Vanish if (vs[0].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[1].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[2].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[3].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight) { //No trangles } else if (vs[0].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[1].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[2].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight) { //Hide 0-1-2, add 0-2-3 int iIndexNow = poses.Count; poses.Add(vs[0]); poses.Add(vs[2]); poses.Add(vs[3]); theuvs.Add(uvs[0]); theuvs.Add(uvs[2]); theuvs.Add(uvs[3]); trangles.Add(iIndexNow + 0); trangles.Add(iIndexNow + 2); trangles.Add(iIndexNow + 1); } else if (vs[1].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[2].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[3].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight) { //Hide 1-2-3, add 0-1-3 int iIndexNow = poses.Count; poses.Add(vs[0]); poses.Add(vs[1]); poses.Add(vs[3]); theuvs.Add(uvs[0]); theuvs.Add(uvs[1]); theuvs.Add(uvs[3]); trangles.Add(iIndexNow + 0); trangles.Add(iIndexNow + 2); trangles.Add(iIndexNow + 1); } else if (vs[0].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[2].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[3].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight) { //Hide 0-2-3, add 0-1-2 int iIndexNow = poses.Count; poses.Add(vs[0]); poses.Add(vs[1]); poses.Add(vs[2]); theuvs.Add(uvs[0]); theuvs.Add(uvs[1]); theuvs.Add(uvs[2]); trangles.Add(iIndexNow + 0); trangles.Add(iIndexNow + 2); trangles.Add(iIndexNow + 1); } else if (vs[0].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[1].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight && vs[3].y < SceneConstant.m_fCutOffHeightRate * SceneConstant.m_fHighgroundHeight) { //Hide 0-1-3, add 1-2-3 int iIndexNow = poses.Count; poses.Add(vs[1]); poses.Add(vs[2]); poses.Add(vs[3]); theuvs.Add(uvs[1]); theuvs.Add(uvs[2]); theuvs.Add(uvs[3]); trangles.Add(iIndexNow + 0); trangles.Add(iIndexNow + 2); trangles.Add(iIndexNow + 1); } else { //Add all int iIndexNow = poses.Count; poses.Add(vs[0]); poses.Add(vs[1]); poses.Add(vs[2]); poses.Add(vs[3]); theuvs.Add(uvs[0]); theuvs.Add(uvs[1]); theuvs.Add(uvs[2]); theuvs.Add(uvs[3]); trangles.Add(iIndexNow + 0); trangles.Add(iIndexNow + 3); trangles.Add(iIndexNow + 1); trangles.Add(iIndexNow + 1); trangles.Add(iIndexNow + 3); trangles.Add(iIndexNow + 2); } } } iProgressNow = iProgressOne * 5; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "保存地面(需要等一下)...", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); Mesh theMesh = new Mesh { vertices = poses.ToArray(), uv = theuvs.ToArray(), triangles = trangles.ToArray() }; theMesh.RecalculateNormals(); Unwrapping.GenerateSecondaryUVSet(theMesh); ; AssetDatabase.CreateAsset(theMesh, "Assets/" + sGroundFileName); AssetDatabase.Refresh(); GameObject newGround = new GameObject(); newGround.transform.position = Vector3.zero; newGround.AddComponent <MeshRenderer>().sharedMaterial = AssetDatabase.LoadAssetAtPath <Material>("Assets/" + SceneConstant.m_sArtworkPath + sGroundType + SceneConstant.m_sSceneTexturesPath + ".mat"); newGround.AddComponent <MeshFilter>().sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>("Assets/" + sGroundFileName); newGround.name = "Ground"; #endregion #region Step4: Put Decorates 4096 x 5 CSceneDecorate doc = new CSceneDecorate { m_sSceneType = sGroundType }; doc.Load(); bool[, ,] chs = new bool[3, doctx.width, doctx.height]; for (int i = 0; i < doctx.width; ++i) { for (int j = 0; j < doctx.height; ++j) { Color h = doctx.GetPixel(i, j); chs[0, i, j] = h.r > 0.5f; chs[1, i, j] = h.g > 0.5f; chs[2, i, j] = h.b > 0.5f; ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "读障碍物分布图", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); } } iProgressNow = iProgressOne * 6; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "读障碍物分布图", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); List <CDecorateInfo> addDocs = new List <CDecorateInfo>(); for (int i = 0; i < doctx.width; ++i) { for (int j = 0; j < doctx.height; ++j) { for (int k = 0; k < 3; ++k) { ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "放置障碍物", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); if (null != doc[k]) { if (chs[k, i, j]) { if (bCreatePathmap && doc[k].m_bBlockPathfinding) { Color colorNow = wmap.GetPixel(i, j); wmap.SetPixel(i, j, new Color(colorNow.r, 0.0f, 1.0f)); } if (2 == doc[k].m_iDecrateSize && i < (doctx.width - 1) && j < (doctx.height - 1) && chs[k, i + 1, j] && chs[k, i, j + 1] && chs[k, i + 1, j + 1]) { addDocs.Add(new CDecorateInfo { m_iCh = k, m_vMid = new Vector3( i - (doctx.width / 2 - 1), verts[i + 1, j + 1].y, j - (doctx.height / 2 - 1)), m_iSize = 2 }); chs[k, i + 1, j] = false; chs[k, i, j + 1] = false; chs[k, i + 1, j + 1] = false; if (bCreatePathmap && doc[k].m_bBlockPathfinding) { Color colorNow1 = wmap.GetPixel(i + 1, j); wmap.SetPixel(i + 1, j, new Color(colorNow1.r, 0.0f, 1.0f)); Color colorNow2 = wmap.GetPixel(i, j + 1); wmap.SetPixel(i, j + 1, new Color(colorNow2.r, 0.0f, 1.0f)); Color colorNow3 = wmap.GetPixel(i + 1, j + 1); wmap.SetPixel(i + 1, j + 1, new Color(colorNow3.r, 0.0f, 1.0f)); } } else { addDocs.Add(new CDecorateInfo { m_iCh = k, m_vMid = new Vector3( i - (doctx.width / 2 - 1) - 0.5f, 0.25f * (verts[i, j].y + verts[i + 1, j].y + verts[i, j + 1].y + verts[i + 1, j + 1].y), j - (doctx.height / 2 - 1) - 0.5f), m_iSize = 1 }); } } } } } } iProgressNow = iProgressOne * 9; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "放置障碍物", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); int iDecIndex = 0; foreach (CDecorateInfo dc in addDocs) { ++iProgressNow; EditorUtility.DisplayProgressBar("创建中", string.Format("{0}...{1}/{2}", "创建障碍物", iProgressNow, iProgressTotal), iProgressNow / (float)iProgressTotal); GameObject newDec = new GameObject(); newDec.transform.position = dc.m_vMid + new Vector3( Random.Range(-0.2f, 0.2f), doc[dc.m_iCh].m_bOnlyRotateY ? 0.0f : Random.Range(-0.2f, 0.2f), Random.Range(-0.2f, 0.2f)); newDec.transform.eulerAngles = doc[dc.m_iCh].m_bOnlyRotateY ? new Vector3(0.0f, Random.Range(-360.0f, 360.0f), 0.0f) : new Vector3(Random.Range(-360.0f, 360.0f), Random.Range(-360.0f, 360.0f), Random.Range(-360.0f, 360.0f)); if (2 == dc.m_iSize) { if (!doc[dc.m_iCh].m_bOnlyRotateY) { newDec.transform.localScale = new Vector3( Random.Range(1.6f, 2.4f), Random.Range(1.6f, 2.4f), Random.Range(1.6f, 2.4f) ); } else { newDec.transform.localScale = new Vector3( Random.Range(1.6f, 2.4f), Random.Range(0.8f, 1.2f), Random.Range(1.6f, 2.4f) ); } } else { newDec.transform.localScale = new Vector3( Random.Range(0.8f, 1.2f), Random.Range(0.8f, 1.2f), Random.Range(0.8f, 1.2f) ); } string sMeshName = doc[dc.m_iCh].m_sElementName + "_" + Random.Range(1, doc[dc.m_iCh].m_iDecrateRepeat); newDec.AddComponent <MeshRenderer>().sharedMaterial = AssetDatabase.LoadAssetAtPath <Material>("Assets/" + SceneConstant.m_sArtworkPath + sGroundType + SceneConstant.m_sSceneTexturesPath + ".mat"); newDec.AddComponent <MeshFilter>().sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>("Assets/" + SceneConstant.m_sArtworkPath + sGroundType + SceneConstant.m_sDecoratesPath + "/Generate/" + sMeshName + ".asset"); newDec.name = "Dec" + iDecIndex; newDec.transform.parent = newGround.transform; ++iDecIndex; } #endregion EditorUtility.ClearProgressBar(); if (bCreatePathmap) { wmap.Apply(); nmap.Apply(); } }