public Triangle GetTriangle(KCLPlane Plane) { Vector3 A = Vertices[Plane.VertexIndex]; Vector3 CrossA = Normals[Plane.NormalAIndex].Cross(Normals[Plane.NormalIndex]); Vector3 CrossB = Normals[Plane.NormalBIndex].Cross(Normals[Plane.NormalIndex]); Vector3 B = A + CrossB * (Plane.Length / CrossB.Dot(Normals[Plane.NormalCIndex])); Vector3 C = A + CrossA * (Plane.Length / CrossA.Dot(Normals[Plane.NormalCIndex])); return(new Triangle(A, B, C)); }
public KCL(byte[] Data) { EndianBinaryReader er = new EndianBinaryReader(new MemoryStream(Data), Endianness.LittleEndian); try { Header = new MK7KCLHeader(er); er.BaseStream.Position = Header.VerticesOffset; uint nr = (Header.NormalsOffset - Header.VerticesOffset) / 0xC; Vertices = new Vector3[nr]; for (int i = 0; i < nr; i++) { Vertices[i] = er.ReadVector3(); } er.BaseStream.Position = Header.NormalsOffset; nr = (Header.PlanesOffset - Header.NormalsOffset) / 0xC; Normals = new Vector3[nr]; for (int i = 0; i < nr; i++) { Normals[i] = er.ReadVector3(); } er.BaseStream.Position = Header.PlanesOffset; nr = (Header.OctreeOffset - Header.PlanesOffset) / 0x10; Planes = new KCLPlane[nr]; for (int i = 0; i < nr; i++) { Planes[i] = new KCLPlane(er); } er.BaseStream.Position = Header.OctreeOffset; int nodes = (int)( ((~Header.XMask >> (int)Header.CoordShift) + 1) * ((~Header.YMask >> (int)Header.CoordShift) + 1) * ((~Header.ZMask >> (int)Header.CoordShift) + 1)); Octree = new KCLOctree(er, nodes); } finally { er.Close(); } }
public Triangle GetTriangle(KCLPlane Plane) { Vector3 A = Vertices[Plane.VertexIndex]; Vector3 CrossA = Normals[Plane.NormalAIndex].Cross(Normals[Plane.NormalIndex]); Vector3 CrossB = Normals[Plane.NormalBIndex].Cross(Normals[Plane.NormalIndex]); Vector3 B = A + CrossB * (Plane.Length / CrossB.Dot(Normals[Plane.NormalCIndex])); Vector3 C = A + CrossA * (Plane.Length / CrossA.Dot(Normals[Plane.NormalCIndex])); if (float.IsInfinity(B.X) || float.IsNaN(B.X) || float.IsInfinity(B.Y) || float.IsNaN(B.Y) || float.IsInfinity(B.Z) || float.IsNaN(B.Z)) { B = A; } if (float.IsInfinity(C.X) || float.IsNaN(C.X) || float.IsInfinity(C.Y) || float.IsNaN(C.Y) || float.IsInfinity(C.Z) || float.IsNaN(C.Z)) { C = A; } return(new Triangle(A, B, C)); }
public List <String> CreateFromFile(byte[] Data) //Return mat count to create pa { OBJ o = new OBJ(Data); List <String> matnames = new List <string>(); foreach (var v in o.Faces) { if (!matnames.Contains(v.Material)) { matnames.Add(v.Material); } } Dictionary <string, ushort> Mapping = new Dictionary <string, ushort>(); Dictionary <string, bool> Colli = new Dictionary <string, bool>(); for (ushort i = 0; i < matnames.Count; i++) { Mapping.Add(matnames[i], i); } foreach (string str in matnames) { Colli.Add(str, true); } List <Vector3> Vertex = new List <Vector3>(); List <Vector3> Normals = new List <Vector3>(); List <KCLPlane> planes = new List <KCLPlane>(); List <Triangle> Triangles = new List <Triangle>(); int face = 0; foreach (var v in o.Faces) { Console.Write("\r -Adding face " + (++face).ToString() + "/" + o.Faces.Count.ToString()); if (Colli[v.Material]) { Triangle t = new Triangle(o.Vertices[v.VertexIndieces[0]], o.Vertices[v.VertexIndieces[1]], o.Vertices[v.VertexIndieces[2]]); Vector3 qq = (t.PointB - t.PointA).Cross(t.PointC - t.PointA); if ((qq.X * qq.X + qq.Y * qq.Y + qq.Z * qq.Z) < 0.01) { continue; } KCLPlane p = new KCLPlane(); p.CollisionType = Mapping[v.Material]; Vector3 a = (t.PointC - t.PointA).Cross(t.Normal); a.Normalize(); a = -a; Vector3 b = (t.PointB - t.PointA).Cross(t.Normal); b.Normalize(); Vector3 c = (t.PointC - t.PointB).Cross(t.Normal); c.Normalize(); p.Length = (t.PointC - t.PointA).Dot(c); int q = ContainsVector3(t.PointA, Vertex); if (q == -1) { p.VertexIndex = (ushort)Vertex.Count; Vertex.Add(t.PointA); } else { p.VertexIndex = (ushort)q; } q = ContainsVector3(t.Normal, Normals); if (q == -1) { p.NormalIndex = (ushort)Normals.Count; Normals.Add(t.Normal); } else { p.NormalIndex = (ushort)q; } q = ContainsVector3(a, Normals); if (q == -1) { p.NormalAIndex = (ushort)Normals.Count; Normals.Add(a); } else { p.NormalAIndex = (ushort)q; } q = ContainsVector3(b, Normals); if (q == -1) { p.NormalBIndex = (ushort)Normals.Count; Normals.Add(b); } else { p.NormalBIndex = (ushort)q; } q = ContainsVector3(c, Normals); if (q == -1) { p.NormalCIndex = (ushort)Normals.Count; Normals.Add(c); } else { p.NormalCIndex = (ushort)q; } planes.Add(p); Triangles.Add(t); } } Vertices = Vertex.ToArray(); this.Normals = Normals.ToArray(); Planes = planes.ToArray(); Header = new MK7KCLHeader(); Octree = KCLOctree.FromTriangles(Triangles.ToArray(), Header, 2048, 128, 128, 50); return(matnames); }
public bool CreateFromFile() { System.Windows.Forms.OpenFileDialog f = new System.Windows.Forms.OpenFileDialog(); f.Filter = OBJ.Identifier.GetFileFilter(); if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK && f.FileName.Length > 0) { OBJ o = new OBJ(File.ReadAllBytes(f.FileName)); List <String> matnames = new List <string>(); foreach (var v in o.Faces) { if (!matnames.Contains(v.Material)) { matnames.Add(v.Material); } } UI.KCLCollisionTypeSelector ty = new UI.KCLCollisionTypeSelector(matnames.ToArray()); ty.DialogResult = System.Windows.Forms.DialogResult.None; ty.ShowDialog(); while (ty.DialogResult != System.Windows.Forms.DialogResult.OK) { ; } Dictionary <string, ushort> Mapping; Dictionary <string, bool> Colli; Mapping = ty.Mapping; Colli = ty.Colli; List <Vector3> Vertex = new List <Vector3>(); List <Vector3> Normals = new List <Vector3>(); List <KCLPlane> planes = new List <KCLPlane>(); List <Triangle> Triangles = new List <Triangle>(); foreach (var v in o.Faces) { if (Colli[v.Material]) { Triangle t = new Triangle(o.Vertices[v.VertexIndieces[0]], o.Vertices[v.VertexIndieces[1]], o.Vertices[v.VertexIndieces[2]]); Vector3 qq = (t.PointB - t.PointA).Cross(t.PointC - t.PointA); if ((qq.X * qq.X + qq.Y * qq.Y + qq.Z * qq.Z) < 0.01) { continue; } KCLPlane p = new KCLPlane(); p.CollisionType = Mapping[v.Material]; Vector3 a = (t.PointC - t.PointA).Cross(t.Normal); a.Normalize(); a = -a; Vector3 b = (t.PointB - t.PointA).Cross(t.Normal); b.Normalize(); Vector3 c = (t.PointC - t.PointB).Cross(t.Normal); c.Normalize(); p.Length = (t.PointC - t.PointA).Dot(c); int q = ContainsVector3(t.PointA, Vertex); if (q == -1) { p.VertexIndex = (ushort)Vertex.Count; Vertex.Add(t.PointA); } else { p.VertexIndex = (ushort)q; } q = ContainsVector3(t.Normal, Normals); if (q == -1) { p.NormalIndex = (ushort)Normals.Count; Normals.Add(t.Normal); } else { p.NormalIndex = (ushort)q; } q = ContainsVector3(a, Normals); if (q == -1) { p.NormalAIndex = (ushort)Normals.Count; Normals.Add(a); } else { p.NormalAIndex = (ushort)q; } q = ContainsVector3(b, Normals); if (q == -1) { p.NormalBIndex = (ushort)Normals.Count; Normals.Add(b); } else { p.NormalBIndex = (ushort)q; } q = ContainsVector3(c, Normals); if (q == -1) { p.NormalCIndex = (ushort)Normals.Count; Normals.Add(c); } else { p.NormalCIndex = (ushort)q; } planes.Add(p); Triangles.Add(t); } } Vertices = Vertex.ToArray(); this.Normals = Normals.ToArray(); Planes = planes.ToArray(); Header = new MK7KCLHeader(); Octree = KCLOctree.FromTriangles(Triangles.ToArray(), Header, 2048, 128, 128, 50); return(true); } return(false); }
Vector3D NormalAvg(KCLPlane Plane) => (Normals[Plane.NormalAIndex] + Normals[Plane.NormalBIndex] + Normals[Plane.NormalCIndex]) / 3;
public bool BackGroundWorkerTask(System.ComponentModel.BackgroundWorker worker, object argument) { BGArgs userState = (BGArgs)argument; OBJ o = new OBJ(File.ReadAllBytes(userState.filename)); List <String> matnames = new List <string>(); foreach (var v in o.Faces) { if (!matnames.Contains(v.Material)) { matnames.Add(v.Material); } } UI.KCLCollisionTypeSelector ty = new UI.KCLCollisionTypeSelector(matnames.ToArray(), userState.filename); ty.loadMK7KCLInformations(); ty.DialogResult = System.Windows.Forms.DialogResult.None; ty.ShowDialog(); while (ty.DialogResult != System.Windows.Forms.DialogResult.OK) { ; } roundVertexPositions(o); Dictionary <string, ushort> Mapping; Dictionary <string, bool> Colli; Mapping = ty.Mapping; Colli = ty.Colli; List <Vector3> Vertex = new List <Vector3>(); List <Vector3> Normals = new List <Vector3>(); List <KCLPlane> planes = new List <KCLPlane>(); List <Triangle> Triangles = new List <Triangle>(); userState.state = 1; userState.currProg = 0; userState.totProg = o.Faces.Count; foreach (var v in o.Faces) { if (Colli[v.Material]) { Triangle t = new Triangle(o.Vertices[v.VertexIndieces[0]], o.Vertices[v.VertexIndieces[1]], o.Vertices[v.VertexIndieces[2]]); Vector3 qq = (t.PointB - t.PointA).Cross(t.PointC - t.PointA); if ((qq.X * qq.X + qq.Y * qq.Y + qq.Z * qq.Z) < 0.1) { continue; } KCLPlane p = new KCLPlane(); p.CollisionType = Mapping[v.Material]; Vector3 a = (t.PointC - t.PointA).Cross(t.Normal); a.Normalize(); a = -a; Vector3 b = (t.PointB - t.PointA).Cross(t.Normal); b.Normalize(); Vector3 c = (t.PointC - t.PointB).Cross(t.Normal); c.Normalize(); p.Length = (t.PointC - t.PointA).Dot(c); int q = ContainsVector3(t.PointA, Vertex); if (q == -1) { p.VertexIndex = (ushort)Vertex.Count; Vertex.Add(t.PointA); } else { p.VertexIndex = (ushort)q; } q = ContainsVector3(t.Normal, Normals); if (q == -1) { p.NormalIndex = (ushort)Normals.Count; Normals.Add(t.Normal); } else { p.NormalIndex = (ushort)q; } q = ContainsVector3(a, Normals); if (q == -1) { p.NormalAIndex = (ushort)Normals.Count; Normals.Add(a); } else { p.NormalAIndex = (ushort)q; } q = ContainsVector3(b, Normals); if (q == -1) { p.NormalBIndex = (ushort)Normals.Count; Normals.Add(b); } else { p.NormalBIndex = (ushort)q; } q = ContainsVector3(c, Normals); if (q == -1) { p.NormalCIndex = (ushort)Normals.Count; Normals.Add(c); } else { p.NormalCIndex = (ushort)q; } planes.Add(p); Triangles.Add(t); } userState.currProg++; worker.ReportProgress(0, userState); if (worker.CancellationPending) { return(false); } } Vertices = Vertex.ToArray(); this.Normals = Normals.ToArray(); Planes = planes.ToArray(); Header = new MK7KCLHeader(); Octree = KCLOctree.FromTriangles(Triangles.ToArray(), Header, 2048, 128, 32, 10, userState, worker); if (Octree == null) { return(false); } return(true); }
public void FromOBJ(OBJ Model, Dictionary <string, ushort> TypeMapping, Dictionary <string, bool> CollideMapping) { List <Vector3> Vertex = new List <Vector3>(); List <Vector3> Normals = new List <Vector3>(); List <KCLPlane> planes = new List <KCLPlane>(); List <Triangle> Triangles = new List <Triangle>(); foreach (var v in Model.Faces) { if (CollideMapping[v.Material]) { Triangle t = new Triangle(Model.Vertices[v.VertexIndieces[0]], Model.Vertices[v.VertexIndieces[1]], Model.Vertices[v.VertexIndieces[2]]); Vector3 qq = (t.PointB - t.PointA).Cross(t.PointC - t.PointA); if ((qq.X * qq.X + qq.Y * qq.Y + qq.Z * qq.Z) < 0.01) { continue; } KCLPlane p = new KCLPlane(); p.CollisionType = (ushort)(((TypeMapping[v.Material] & 0xFF) << 8) | (TypeMapping[v.Material] >> 8)); Vector3 a = (t.PointC - t.PointA).Cross(t.Normal); a.Normalize(); a = -a; Vector3 b = (t.PointB - t.PointA).Cross(t.Normal); b.Normalize(); Vector3 c = (t.PointC - t.PointB).Cross(t.Normal); c.Normalize(); p.Length = (t.PointC - t.PointA).Dot(c); int q = ContainsVector3(t.PointA, Vertex); if (q == -1) { p.VertexIndex = (ushort)Vertex.Count; Vertex.Add(t.PointA); } else { p.VertexIndex = (ushort)q; } q = ContainsVector3(t.Normal, Normals); if (q == -1) { p.NormalIndex = (ushort)Normals.Count; Normals.Add(t.Normal); } else { p.NormalIndex = (ushort)q; } q = ContainsVector3(a, Normals); if (q == -1) { p.NormalAIndex = (ushort)Normals.Count; Normals.Add(a); } else { p.NormalAIndex = (ushort)q; } q = ContainsVector3(b, Normals); if (q == -1) { p.NormalBIndex = (ushort)Normals.Count; Normals.Add(b); } else { p.NormalBIndex = (ushort)q; } q = ContainsVector3(c, Normals); if (q == -1) { p.NormalCIndex = (ushort)Normals.Count; Normals.Add(c); } else { p.NormalCIndex = (ushort)q; } planes.Add(p); Triangles.Add(t); } } Vertices = Vertex.ToArray(); this.Normals = Normals.ToArray(); Planes = planes.ToArray(); Header = new MKDSKCLHeader(); //Octree = KCLOctree.FromTriangles(Triangles.ToArray(), Header, 2048, 128, 32, 10); Octree = KCLOctree.FromTriangles(Triangles.ToArray(), Header, 2048, 128, 128, 50); }