private static Texture GetTex(PatchMapInfo pm, int layer, Color col) { if (pm == null) { return(null); } if (col == BLACK) { return(null); } if (layer < 1 || layer > COUNT) { return(null); } Texture t = null; if (col == RED) { t = pm.R[layer - 1]; } if (col == GREEN) { t = pm.G[layer - 1]; } if (col == BLUE) { t = pm.B[layer - 1]; } return(t); }
/// <summary> /// Save /// </summary> /// <param name="path"></param> /// <param name="backup"></param> public static void Save(string path, bool backup) { if (m_Shader == null || !m_Changed) { return; //not even loaded... } if (!backup) { m_Changed = false; } bool changed = false; #region patchmaps Loading.Update("Saving " + Path.GetFileName(path) + " - Patchmaps"); foreach (PatchMapInfo pi in m_PatchInfos) { if (!pi.Changed) { continue; } pi.Exists = true; changed = true; if (!backup) { pi.Changed = false; } for (int i = 0; i < COUNT; i++) { if (pi.Patch[i] != null) { Texture p = pi.Patch[i]; string pp = path + string.Format("patch{0:D2}{1:D2}-{2:D2}.dds", pi.X, pi.Y, i); TextureLoader.Save(pp, ImageFileFormat.Dds, p); //Mips try { using ( Texture mip = TextureLoader.FromFile(p.Device, pp, 0, 0, 0, Usage.Dynamic, Format.A8R8G8B8, Pool.Default, Filter.Linear, Filter.Linear, 0)) { //mip.GenerateMipSubLevels(); TextureLoader.Save(pp, ImageFileFormat.Dds, mip); mip.Dispose(); } } catch (Exception ex) { CrashDialog.Show(ex); } } } } #endregion #region textures.csv if (changed) { Loading.Update("Saving " + Path.GetFileName(path) + "... - textures.csv"); var w = new StreamWriter(string.Format("{0}textures.csv", path), false); w.WriteLine( "patch x,patch y,base texture filename,rotate,u translate,v translate,u scale,v scale,visible,num tiles,tileable,normal map filename,specular tint color,specular power,mask type,mask window size"); w.WriteLine(); for (int y = 0; y < m_PatchInfos.GetLength(1); y++) { for (int x = 0; x < m_PatchInfos.GetLength(0); x++) { PatchMapInfo i = m_PatchInfos[x, y]; const string format = "{0},{1},{2},0.00,0.00,0.00,1.00,1.00,1,{3},{4},,0,32.0,0,16"; for (int j = 0; j < COUNT; j++) { if (i.Patch[j] != null) { w.WriteLine(string.Format(format, x, y, Path.GetFileNameWithoutExtension(TerrainTex.Get(i.R[j])), i.RTiles[j], i.RTiles[j] > 1 ? "1" : "0")); w.WriteLine(string.Format(format, x, y, Path.GetFileNameWithoutExtension(TerrainTex.Get(i.G[j])), i.GTiles[j], i.GTiles[j] > 1 ? "1" : "0")); w.WriteLine(string.Format(format, x, y, Path.GetFileNameWithoutExtension(TerrainTex.Get(i.B[j])), i.BTiles[j], i.BTiles[j] > 1 ? "1" : "0")); w.WriteLine(); } } } } w.Flush(); w.Close(); } #endregion }
/// <summary> /// Init /// </summary> public static void Init() { if (m_Shader != null) { return; } //Init string err; string shaderSrc = Encoding.Default.GetString(Resources.patchmap); Effect s = Effect.FromString(Program.FORM.renderControl1.DEVICE, shaderSrc, null, null, ShaderFlags.Debug, null, out err); if (s == null || !string.IsNullOrEmpty(err)) { // There are Compilation errors show them and then close app MessageBox.Show(err ?? "ERROR"); return; } s.Technique = "PM"; var sh = new Shader(); sh.Effect = s; sh.Patch = sh.Effect.GetParameter(null, "tex"); sh.R = sh.Effect.GetParameter(null, "texR"); sh.G = sh.Effect.GetParameter(null, "texG"); sh.B = sh.Effect.GetParameter(null, "texB"); sh.RTiles = sh.Effect.GetParameter(null, "numR"); sh.GTiles = sh.Effect.GetParameter(null, "numG"); sh.BTiles = sh.Effect.GetParameter(null, "numB"); sh.Layer = sh.Effect.GetParameter(null, "layer"); m_Shader = sh; effCursor = s.GetParameter(null, "cursor"); effRadius = s.GetParameter(null, "radius"); effMode = s.GetParameter(null, "mode"); effBorders = s.GetParameter(null, "border"); //Load patchmap data.. string pTer = Program.ZONE.PATH + string.Format(@"\ter{0}.mpk\", Program.ZONE.ZoneID.ToString("D3")); var r = new StreamReader(pTer + "textures.csv"); r.ReadLine(); int cX = -1; int cY = -1; PatchMapInfo cur = null; while (!r.EndOfStream) { string l = r.ReadLine(); if (String.IsNullOrEmpty(l)) { continue; } string[] split = l.Split(','); int pX = int.Parse(split[0]); int pY = int.Parse(split[1]); string tex = split[2] + ".dds"; float tiles = int.Parse(split[9]); if (cX != pX || cY != pY || cur == null) { cur = new PatchMapInfo(); cur.Exists = true; cur.X = pX; cur.Y = pY; cur.Changed = false; m_PatchInfos[pX, pY] = cur; cX = pX; cY = pY; string ppp = pTer + "patch{0:D2}{1:D2}-{2:D2}.dds"; for (int i = 0; i < COUNT; i++) { if (File.Exists(string.Format(ppp, pX, pY, i))) { Texture p = LocalTextures.Get(string.Format(ppp, pX, pY, i), false); cur.Patch[i] = p; } } } Texture t = TerrainTex.Get(tex); float n = tiles; for (int i = 0; i < COUNT; i++) { if (cur.R[i] == null) { cur.R[i] = t; cur.RTiles[i] = n; break; } if (cur.G[i] == null) { cur.G[i] = t; cur.GTiles[i] = n; break; } if (cur.B[i] == null) { cur.B[i] = t; cur.BTiles[i] = n; break; } } } r.Close(); //Cur Thingies SelectCurrent(0, 0); SelectedLayer = 1; SelectedColor = Color.Red; }
/// <summary> /// Begin /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="m"></param> public static void Render(int x, int y, Mesh m) { //Get Info PatchMapInfo ci = m_PatchInfos[x, y]; if (ci == null) { ci = new PatchMapInfo(); ci.X = x; ci.Y = y; ci.Changed = false; ci.Exists = false; m_PatchInfos[x, y] = ci; } //1.) Patches if (m_Shader != null) { Shader s = m_Shader; Effect sh = s.Effect; sh.Begin(0); //a) Begin for (int l = 0; l < COUNT; l++) { if (ci.Patch[l] == null || ci.R[l] == null) { if (l == 0) { m.DrawSubset(0); //Render Base } break; } if (s.Patch != null) { sh.SetValue(s.Patch, ci.Patch[l]); } if (s.R != null) { sh.SetValue(s.R, ci.R[l]); } if (s.G != null) { sh.SetValue(s.G, ci.G[l]); } if (s.B != null) { sh.SetValue(s.B, ci.B[l]); } if (s.RTiles != null) { sh.SetValue(s.RTiles, ci.RTiles[l]); } if (s.GTiles != null) { sh.SetValue(s.GTiles, ci.GTiles[l]); } if (s.BTiles != null) { sh.SetValue(s.BTiles, ci.BTiles[l]); } if (s.Layer != null) { sh.SetValue(s.Layer, l); } for (int c = 1; c <= 3; c++) { //Three passes - for each color one sh.BeginPass(c); m.DrawSubset(0); sh.EndPass(); } } //2. Render Effects if (effRadius != null) { sh.SetValue(effRadius, Program.MODE == Program.eMode.Texturing ? Program.TOOL_TEXTURE.Radius / 65536f : 0); } if (effCursor != null) { sh.SetValue(effCursor, new[] { Program.FORM.lastMapPos.X / 65536f, Program.FORM.lastMapPos.Y / 65536f }); } if (effMode != null) { sh.SetValue(effMode, (int)Program.TOOL_TEXTURE.PaintMode); } if (effBorders != null) { sh.SetValue(effBorders, /*(Program.FORM.m_TexCtrl != null && Program.FORM.m_TexCtrl.Visible)*/ Program.TOOL_TEXTURE.ShowGrid); } sh.BeginPass(0); m.DrawSubset(0); sh.EndPass(); sh.End(); } }