public JSONObject ToJson() { var obj = new JSONObject(); obj.Add(nameof(Id), new JSONString(Id)); obj.Add(nameof(Name), new JSONString(Name)); obj.Add(nameof(Uri), new JSONString(Uri)); obj.Add(nameof(StartIndex), new JSONNumber(StartIndex)); obj.Add(nameof(EndIndex), new JSONNumber(EndIndex)); obj.Add(nameof(IsAnimated), new JSONBool(IsAnimated)); obj.Add(nameof(Bits), new JSONNumber(Bits)); obj.Add(nameof(Color), new JSONString(Color)); obj.Add(nameof(Type), new JSONString(Type.ToString())); obj.Add(nameof(UVs), UVs.ToJson()); return(obj); }
private void CalculateUVs(int subMesh, Vector3 direction) { int vertCount = (Triangles [subMesh].Count / 6) * 4; Vector2[] planeUVs = new Vector2[vertCount]; for (int i = 0; i < vertCount; i++) { int index = Vertices.Count - vertCount + i; Vector2 uv = Vec3ToVec2(Vertices [index], direction); uv.x *= tiling; uv.y *= tiling; planeUVs [i] = uv; } UVs.AddRange(planeUVs); }
public void Optimise() { List <C2Vertex> points = new List <C2Vertex>(); List <Vector3> verts = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < Verts.Count; i++) { C2Vertex p = new C2Vertex(Verts[i], UVs[i]); //Console.WriteLine("Vert " + i); int newID = points.IndexOf(p); //int newID = verts.IndexOf(Verts[i]); if (newID == -1) { //Console.WriteLine("Adding " + p.ToString()); points.Add(p); verts.Add(Verts[i]); uvs.Add(UVs[i]); newID = verts.Count - 1; } for (int j = 0; j < Faces.Count; j++) { Faces[j].ReplaceVertID(i, newID); } } //Console.WriteLine("Reduced Vert count from " + Verts.Count + " to " + verts.Count); //Console.WriteLine("Reduced UV count from " + UVs.Count + " to " + uvs.Count); Verts.Clear(); for (int i = 0; i < verts.Count; i++) { Verts.Add(verts[i]); } UVs.Clear(); for (int i = 0; i < uvs.Count; i++) { UVs.Add(uvs[i]); } }
public void AddFace(Vector3 v1, Vector3 v2, Vector3 v3, Vector2 uv1, Vector2 uv2, Vector2 uv3, int matID) { int iv1, iv2, iv3, iuv1, iuv2, iuv3; Verts.Add(v1); iv1 = Verts.Count - 1; Verts.Add(v2); iv2 = Verts.Count - 1; Verts.Add(v3); iv3 = Verts.Count - 1; UVs.Add(uv1); iuv1 = UVs.Count - 1; UVs.Add(uv2); iuv2 = UVs.Count - 1; UVs.Add(uv3); iuv3 = UVs.Count - 1; AddFace(iv1, iv2, iv3, iuv1, iuv2, iuv3, matID); }
private void StartTubeSegment() { currentSegmentLength = 0; Verts.Clear(); Tris.Clear(); UVs.Clear(); CurrentSegment = new GameObject(); CurrentSegment.AddComponent <MeshFilter>(); CurrentSegment.AddComponent <MeshRenderer>(); Mesh = CurrentSegment.GetComponent <MeshFilter>().mesh; Mesh.MarkDynamic(); CurrentSegment.GetComponent <Renderer>().material = Material; Mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; UpdateVertsAndUVs(); Mesh.RecalculateNormals(); LoopCount = 1; }
public void AddTriangle(MeshTriangle _triangle) { int currentVerticeCount = vertices.Count; Vertices.AddRange(_triangle.Vertices); Normals.AddRange(_triangle.Normals); UVs.AddRange(_triangle.UVs); if (SubmeshIndices.Count < _triangle.SubmeshIndex + 1) { for (int i = submeshIndices.Count; i < (_triangle.SubmeshIndex + 1); i++) { SubmeshIndices.Add(new List <int>()); } } for (int i = 0; i < 3; i++) { SubmeshIndices[_triangle.SubmeshIndex].Add(currentVerticeCount + i); } }
public MyClass(Vertex[] vertices) { this.vertices = vertices; Name = "aaa"; UVs.Add(new UV(0, 0)); UVs.Add(new UV(0, 1)); UVs.Add(new UV(1, 1)); UVs.Add(new UV(1, 0)); int i = 0; foreach (Vertex vertex in vertices) { Vertices.Add(new SharpGL.SceneGraph.Vertex(vertex.x, vertex.y, vertex.z)); Face face = new Face(); face.Indices.Add(new Index(i++, i % 4)); Faces.Add(face); } }
public void Build(MeshFilter meshFilter) { #if UNITY_EDITOR Mesh mesh = meshFilter.sharedMesh; #else Mesh mesh = meshFilter.mesh; #endif if (mesh == null) { mesh = new Mesh(); if (Dynamic) { mesh.MarkDynamic(); } } mesh.Clear(); // Set verts mesh.vertices = Vertices.TrimAndGetBuffer(); // Set indices mesh.triangles = Indices.TrimAndGetBuffer(); // Set uvs mesh.uv = UVs.TrimAndGetBuffer(); // Set normals mesh.normals = Normals.TrimAndGetBuffer(); // Set Colours if (Colours.Count > 0) { mesh.colors = Colours.TrimAndGetBuffer(); } meshFilter.mesh = mesh; }
public override void SetBuffer(NkVertex[] VertexBuffer, ushort[] IndexBuffer) { // TODO: Put NkVertex in a single buffer directly Vector4[] ColorArr = new Vector4[VertexBuffer.Length]; Vector2[] VertArr = new Vector2[VertexBuffer.Length]; Vector2[] UVArr = new Vector2[VertexBuffer.Length]; for (int i = 0; i < VertexBuffer.Length; i++) { NkColor C = VertexBuffer[i].Color; NkVector2f Pos = VertexBuffer[i].Position; NkVector2f UV = VertexBuffer[i].UV; ColorArr[i] = new Vector4(C.R / 255.0f, C.G / 255.0f, C.B / 255.0f, C.A / 255.0f); VertArr[i] = new Vector2(Pos.X, Pos.Y); UVArr[i] = new Vector2(UV.X, UV.Y); } Colors.SetData(ColorArr); Vertices.SetData(VertArr); UVs.SetData(UVArr); Elements.SetData(IndexBuffer); }
internal override Dictionary <string, OBJObjectBuilder> LoadBuilderDictionaries(StreamReader reader) { Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = (string objectName) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("default"); //var buffer = new DoubleBuffer(reader, 256 * 1024); var buffer = new CharWordReader(reader, 4 * 1024); //do the reading while (true) { if (buffer.currentChar == char.MinValue) { //Debug.Log(""); } buffer.SkipWhitespaces(); if (buffer.endReached == true) { break; } buffer.ReadUntilWhiteSpace(); //comment or blank if (buffer.Is("#")) { buffer.SkipUntilNewLine(); continue; } if (Materials == null && buffer.Is("mtllib")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string mtlLibPath = buffer.GetString(); LoadMaterialLibrary(mtlLibPath); continue; } if (buffer.Is("v")) { Vertices.Add(buffer.ReadVector()); continue; } //normal if (buffer.Is("vn")) { Normals.Add(buffer.ReadVector()); continue; } //uv if (buffer.Is("vt")) { UVs.Add(buffer.ReadVector()); continue; } //new material if (buffer.Is("usemtl")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string materialName = buffer.GetString(); currentMaterial = materialName; if (SplitMode == SplitMode.Material) { setCurrentObjectFunc.Invoke(materialName); } continue; } //new object if ((buffer.Is("o") || buffer.Is("g")) && SplitMode == SplitMode.Object) { buffer.ReadUntilNewLine(); string objectName = buffer.GetString(1); setCurrentObjectFunc.Invoke(objectName); continue; } //face data (the fun part) if (buffer.Is("f")) { //loop through indices while (true) { bool newLinePassed; buffer.SkipWhitespaces(out newLinePassed); if (newLinePassed == true) { break; } int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; vertexIndex = buffer.ReadInt(); if (buffer.currentChar == '/') { buffer.MoveNext(); if (buffer.currentChar != '/') { uvIndex = buffer.ReadInt(); } if (buffer.currentChar == '/') { buffer.MoveNext(); normalIndex = buffer.ReadInt(); } } //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); continue; } buffer.SkipUntilNewLine(); } return(builderDict); }
public void SetUVs(UVs uVs) { MeshFilter.mesh.uv = uVs.UV; }
public void addUV(UV uv) { UVs.Add(uv); }
/// <summary> /// This function makes a simple cube shape. /// </summary> private void CreateCubeGeometry() { UVs.Add(new UV(0, 0)); UVs.Add(new UV(0, 1)); UVs.Add(new UV(1, 1)); UVs.Add(new UV(1, 0)); // Add the vertices. Vertices.Add(new Vertex(-1, -1, -1)); // 0 Vertices.Add(new Vertex(1, -1, -1)); // 1 Vertices.Add(new Vertex(1, -1, 1)); // 2 Vertices.Add(new Vertex(-1, -1, 1)); // 3 Vertices.Add(new Vertex(-1, 1, -1)); // 4 Vertices.Add(new Vertex(1, 1, -1)); // 5 Vertices.Add(new Vertex(1, 1, 1)); // 6 Vertices.Add(new Vertex(-1, 1, 1)); // 7 // Add the faces. Face face = new Face(); // bottom face.Indices.Add(new Index(1, 0)); face.Indices.Add(new Index(2, 1)); face.Indices.Add(new Index(3, 2)); face.Indices.Add(new Index(0, 3)); Faces.Add(face); face = new Face(); // top face.Indices.Add(new Index(7, 0)); face.Indices.Add(new Index(6, 1)); face.Indices.Add(new Index(5, 2)); face.Indices.Add(new Index(4, 3)); Faces.Add(face); face = new Face(); // right face.Indices.Add(new Index(5, 0)); face.Indices.Add(new Index(6, 1)); face.Indices.Add(new Index(2, 2)); face.Indices.Add(new Index(1, 3)); Faces.Add(face); face = new Face(); // left face.Indices.Add(new Index(7, 0)); face.Indices.Add(new Index(4, 1)); face.Indices.Add(new Index(0, 2)); face.Indices.Add(new Index(3, 3)); Faces.Add(face); face = new Face(); // front face.Indices.Add(new Index(4, 0)); face.Indices.Add(new Index(5, 1)); face.Indices.Add(new Index(1, 2)); face.Indices.Add(new Index(0, 3)); Faces.Add(face); face = new Face(); // back face.Indices.Add(new Index(6, 0)); face.Indices.Add(new Index(7, 1)); face.Indices.Add(new Index(3, 2)); face.Indices.Add(new Index(2, 3)); Faces.Add(face); }
/// <summary> /// Adds a face to the meshes vertices, textures and normals /// </summary> /// <param name="vertPalette">all of the vertices usable. for a cube it might be 24 long</param> /// <param name="uvPalette">all of the texture coordinates</param> /// <param name="a" >vertex x index</param> /// <param name="aT">texture x index (u)</param> /// <param name="b" >vertex y index</param> /// <param name="bT">texture y index (v)</param> /// <param name="c" >vertex z index</param> /// <param name="cT">texture z index (w)</param> /// <param name="is3dTexture">says whether the face will have a 3d texture</param> public void AddFace(List <float> vertPalette, List <float> uvPalette, int a, int aT, int b, int bT, int c, int cT, bool is3dTexture) { if (a > 0 && b > 0 && c > 0) { if (aT > 0 && bT > 0 && cT > 0) { // say... /* a = 5 * b = 3 * c = 1 * * aT = 1 * bT = 2 * cT = 3 * * now they're: * * a = 4 * b = 2 * c = 0 * aT = 0 * bT = 1 * cT = 2 */ a -= 1; b -= 1; c -= 1; aT -= 1; bT -= 1; cT -= 1; int[] vertexIndices = new int[3] { a, b, c }; int[] textureIndices = new int[3] { aT, bT, cT }; Vector3 vertex1 = vertPalette.GetVertexFromList(a * 3); Vector3 vertex2 = vertPalette.GetVertexFromList(b * 3); Vector3 vertex3 = vertPalette.GetVertexFromList(c * 3); Vector3 normals = (vertex2 - vertex1).Cross(vertex3 - vertex1).Normalised(); for (int i = 0; i < 3; i++) { int vertIndex = vertexIndices[i]; int textIndex = textureIndices[i]; if (vertIndex < (vertPalette.Count / 3)) { Vertices.Add(vertPalette[(vertIndex * 3)]); Vertices.Add(vertPalette[(vertIndex * 3) + 1]); Vertices.Add(vertPalette[(vertIndex * 3) + 2]); if (uvPalette.Count > 0) { if (is3dTexture) { if (textIndex < (uvPalette.Count / 3)) { UVs.Add(uvPalette[(textIndex * 3)]); UVs.Add(uvPalette[(textIndex * 3) + 1]); UVs.Add(uvPalette[(textIndex * 3) + 2]); } } else { if (textIndex < (uvPalette.Count / 2)) { UVs.Add(uvPalette[(textIndex * 2)]); UVs.Add(uvPalette[(textIndex * 2) + 1]); } } } else { UVs.Add(0.0f); UVs.Add(0.0f); } Normals.Add(normals.X); Normals.Add(normals.Y); Normals.Add(normals.Z); } } } } }
/// <summary> /// Abstract method. It will be called to finalize the mesh, such as close the mesh at the end. /// </summary> override protected void FinalizeMesh() { if (closeFront) { int a = frontVertices [0], b = frontVertices [1], c = frontVertices [2], d = frontVertices [3]; // Dup vertices Vector3 pa = vertices [a]; Vector3 pb = vertices [b]; Vector3 pc = vertices [c]; Vector3 pd = vertices [d]; vertices.Add(pa); UVs.Add(new Vector2(0, 0)); normals.Add(frontNormal); a = vertices.Count - 1; vertices.Add(pb); UVs.Add(new Vector2(1, 0)); normals.Add(frontNormal); b = vertices.Count - 1; vertices.Add(pc); UVs.Add(new Vector2(1, 1)); normals.Add(frontNormal); c = vertices.Count - 1; vertices.Add(pd); UVs.Add(new Vector2(0, 1)); normals.Add(frontNormal); d = vertices.Count - 1; AddQuad3(c, b, d, a); } if (closeBack) { int a = backVertices [0], b = backVertices [1], c = backVertices [2], d = backVertices [3]; // Dup vertices Vector3 pa = vertices [a]; Vector3 pb = vertices [b]; Vector3 pc = vertices [c]; Vector3 pd = vertices [d]; vertices.Add(pa); UVs.Add(new Vector2(0, 0)); normals.Add(backNormal); a = vertices.Count - 1; vertices.Add(pb); UVs.Add(new Vector2(1, 0)); normals.Add(backNormal); b = vertices.Count - 1; vertices.Add(pc); UVs.Add(new Vector2(1, 1)); normals.Add(backNormal); c = vertices.Count - 1; vertices.Add(pd); UVs.Add(new Vector2(0, 1)); normals.Add(backNormal); d = vertices.Count - 1; AddQuad3(a, b, d, c); } }
/// <summary> /// Generates a single mesh part. /// </summary> /// <param name="piece">The piece. For each piece, this method is called.</param> /// <param name="position">The current position on the path.</param> /// <param name="direction">The current direction at the position on the path.</param> /// <param name="xd">This vector indicates an ortogonal direction to the path direction.</param> /// <param name="yd">This vector indicates an ortogonal direction to the direction and the xd vector. /// Use xd and yd as a plane ortogonal to the direction.</param> override protected void GenerateMeshPart(int piece, Vector3 position, Vector3 direction, Vector3 xd, Vector3 yd) { float r; if (widthType == WidthType.Constant) { r = width; } else { float minTime = float.MaxValue; float maxTime = float.MinValue; for (int i = 0; i < widthCurve.length; i++) { float time = widthCurve.keys [i].time; if (time > maxTime) { maxTime = time; } if (time < minTime) { minTime = time; } } float timeSpan = maxTime - minTime; float evalTime = minTime + ((float)piece / (float)pieces) * timeSpan; r = widthCurve.Evaluate(evalTime); } // Generate first Vertex Vector3 point = position + r / 2 * xd - height * Vector3.up; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2(0, piece * 1f / (float)(pieces))); // Generate normals Vector3 normal = flipped ? xd : -xd; normals.Add(normal); if (piece == 0) { frontVertices.Add(vertices.Count - 1); frontNormal = flipped ? direction : -direction; } else if (piece == pieces) { backVertices.Add(vertices.Count - 1); backNormal = flipped ? -direction : direction; } // Generate second vertex point = position + r / 2 * xd; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2(1, piece * 1f / (float)(pieces))); // Generate normals normal = flipped ? xd : -xd; normals.Add(normal); if (piece == 0) { frontVertices.Add(vertices.Count - 1); frontNormal = flipped ? direction : -direction; } else if (piece == pieces) { backVertices.Add(vertices.Count - 1); backNormal = flipped ? -direction : direction; } for (int j = 0; j <= sections; j++) { // Generate vertex point = position + (r / 2f - (float)j / (float)sections * r) * xd; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2((float)j / (float)sections, piece * 1f / (float)(pieces))); // Generate normals normal = flipped ? yd : -yd; normals.Add(normal); } // Generate first vertex point = position - r / 2 * xd; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2(1, piece * 1f / (float)(pieces))); // Generate normals normal = flipped ? -xd : xd; normals.Add(normal); if (piece == 0) { frontVertices.Add(vertices.Count - 1); frontNormal = flipped ? direction : -direction; } else if (piece == pieces) { backVertices.Add(vertices.Count - 1); backNormal = flipped ? -direction : direction; } // Generate second Vertex point = position - r / 2 * xd - height * Vector3.up; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2(0, piece * 1f / (float)(pieces))); // Generate normals normal = flipped ? -xd : xd; normals.Add(normal); if (piece == 0) { frontVertices.Add(vertices.Count - 1); frontNormal = flipped ? direction : -direction; } else if (piece == pieces) { backVertices.Add(vertices.Count - 1); backNormal = flipped ? -direction : direction; } // Add triangles if (piece > 0) { int baseIndex = piece * (sections + 5); int a = baseIndex; int b = baseIndex - (sections + 5); int c = baseIndex + 1; int d = baseIndex + 1 - (sections + 5); AddQuad2(a, b, c, d); for (int j = 2; j < sections + 2; j++) { a = baseIndex + j; b = baseIndex + j - (sections + 5); c = baseIndex + j + 1; d = baseIndex + j + 1 - (sections + 5); AddQuad1(a, b, c, d); } a = baseIndex + sections + 3; b = baseIndex + sections + 3 - (sections + 5); c = baseIndex + sections + 3 + 1; d = baseIndex + sections + 3 + 1 - (sections + 5); AddQuad2(a, b, c, d); } }
public void Convert(string inputPath, string outputPath, Bitmap bitmap, UVs[] sliceCoordinates) { string namespaceName = string.Format(NAMESPACE_FORMAT, Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath))); StringBuilder sb = new StringBuilder(); Size size = bitmap.Size; if (size.Width % TileConfig.c_TILEWIDTH != 0 || size.Height % TileConfig.c_TILEHEIGHT != 0) { throw new Exception("Size not compatible with GBA tiles. Width and height of pixels must be multiples of 8."); } Console.WriteLine("Processing colour palette"); int xStart = 0; int yStart = 0; int width = bitmap.Width; int height = bitmap.Height; Color[] preProcessedPalette = PaletteHelper.GeneratePaletteFromImage(bitmap, xStart, yStart, width, height); // Validate pallet length { if (preProcessedPalette.Length > 256) { throw new Exception("Palette length out of range for the GBA"); } // Todo, currently not supported if (preProcessedPalette.Length > 16) { throw new Exception("Palette length out of range for 4bbp format"); } } Compression.CompressionType compressionType = Compression.CompressionType.BitPacked; uint maxColours = MathX.IsPowerOf2((uint)preProcessedPalette.Length) ? (uint)preProcessedPalette.Length : MathX.NextPowerOf2((uint)preProcessedPalette.Length); uint destBpp = (uint)MathX.IndexOfHighestSetBit(maxColours); if (!MathX.IsPowerOf2(destBpp)) { destBpp = MathX.NextPowerOf2(destBpp); } Console.WriteLine(string.Format("Compression type = {0}", compressionType.ToString())); Console.WriteLine(string.Format("Destination Bit Depth = {0}", destBpp)); List <int> dataOffsets = new List <int>(); List <StringBuilder> spriteData = new List <StringBuilder>(); dataOffsets.Add(0); int totalData = 0; // Collect data and add offsets { Console.WriteLine("Processing sprite data"); for (int i = 0; i < sliceCoordinates.Length; ++i) { StringBuilder dataSb = new StringBuilder(); UVs slice = sliceCoordinates[i]; int spriteWidth = slice.width, spriteHeight = slice.height; int dataCount = WriteSpriteData(dataSb, bitmap, preProcessedPalette, spriteWidth, spriteHeight, slice.x, slice.y, compressionType, destBpp); spriteData.Add(dataSb); // Add offsets if (i < sliceCoordinates.Length - 1) { dataOffsets.Add(dataOffsets[i] + dataCount); } totalData += dataCount; } } sb.Append(MACRO_DEFINES); sb.Append(namespaceName); WriteHeader(sliceCoordinates, preProcessedPalette, totalData, compressionType, destBpp, sb); WritePalette(preProcessedPalette, sb); // Write width { sb.Append(VAR_WIDTHMAP); sb.Append(namespaceTabs + "{\n"); sb.Append(namespaceTabs + TAB_CHAR); for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteWidth = sliceCoordinates[i].width; sb.AppendFormat("{0}, ", spriteWidth); if ((i + 1) % c_arrayNewlineCount == 0) { sb.AppendFormat("\n" + namespaceTabs + TAB_CHAR); } } sb.Append("\n"); sb.Append(namespaceTabs + "};\n\n"); } // Write height { sb.Append(VAR_HEIGHTMAP); sb.Append(namespaceTabs + "{\n"); sb.Append(namespaceTabs + TAB_CHAR); for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteHeight = sliceCoordinates[i].height; // Todo sb.AppendFormat("{0}, ", spriteHeight); if ((i + 1) % c_arrayNewlineCount == 0) { sb.AppendFormat("\n" + namespaceTabs + TAB_CHAR); } } sb.Append("\n"); sb.Append(namespaceTabs + "};\n\n"); } // Write offsets { const string tabs = namespaceTabs + TAB_CHAR; sb.Append(VAR_OFFSETS); sb.Append(namespaceTabs + "{\n" + tabs); for (int i = 0; i < dataOffsets.Count; i++) { sb.AppendFormat("{0}, ", dataOffsets[i]); if ((i + 1) % c_arrayNewlineCount == 0) { sb.AppendFormat("\n" + tabs); } } sb.Append('\n' + namespaceTabs + "};\n\n"); } // Write data { sb.Append(VAR_DATA); sb.Append(namespaceTabs + "{\n"); foreach (StringBuilder dataSB in spriteData) { sb.Append(dataSB.ToString()); } sb.Append(namespaceTabs + "};\n\n"); } sb.Append("}\n"); Console.WriteLine("Sprite \"" + outputPath + "\" successfully converted"); File.WriteAllText(outputPath, sb.ToString()); }
public void AddListUV(Vector2 uv) { UVs.Add(uv); }
/// <summary> /// Generates a single mesh part. /// </summary> /// <param name="piece">The piece. For each piece, this method is called.</param> /// <param name="position">The current position on the path.</param> /// <param name="direction">The current direction at the position on the path.</param> /// <param name="xd">This vector indicates an ortogonal direction to the path direction.</param> /// <param name="yd">This vector indicates an ortogonal direction to the direction and the xd vector. /// Use xd and yd as a plane ortogonal to the direction.</param> override protected void GenerateMeshPart(int piece, Vector3 position, Vector3 direction, Vector3 xd, Vector3 yd) { float r; if (radiusType == RadiusType.Constant) { r = radius; } else { float minTime = float.MaxValue; float maxTime = float.MinValue; for (int i = 0; i < radiusCurve.length; i++) { float time = radiusCurve.keys [i].time; if (time > maxTime) { maxTime = time; } if (time < minTime) { minTime = time; } } float timeSpan = maxTime - minTime; float evalTime = minTime + ((float)piece / (float)pieces) * timeSpan; r = radiusCurve.Evaluate(evalTime); } for (int j = 0; j <= sections; j++) { float angle = Mathf.Deg2Rad * angleOffset + fTwist + j * 2 * Mathf.PI / sections; // Generate vertex Vector3 point = position + r * Mathf.Sin(angle) * xd + r * Mathf.Cos(angle) * yd; vertices.Add(transform.InverseTransformPoint(point)); // Generate UVs UVs.Add(new Vector2(j * 1f / (float)(sections), piece * 1f / (float)(pieces))); // Generate normals Vector3 normal = flipped ? (position - point).normalized : (point - position).normalized; normals.Add(normal); if (piece == 0) { frontVertices.Add(vertices.Count - 1); frontNormal = flipped ? direction : -direction; frontPosition = transform.InverseTransformPoint(position); } else if (piece == pieces) { backVertices.Add(vertices.Count - 1); backNormal = flipped ? -direction : direction; backPosition = transform.InverseTransformPoint(position); } } fTwist += twist / 100; // Add triangles if (piece > 0) { for (int j = 0; j < sections; j++) { int baseIndex = piece * (sections + 1); int a = baseIndex + j; int b = baseIndex + j - (sections + 1); int c = baseIndex + j + 1; int d = baseIndex + j + 1 - (sections + 1); AddQuad1(a, b, c, d); } } }
/// <summary> /// Abstract method. It will be called to finalize the mesh, such as close the mesh at the end. /// </summary> override protected void FinalizeMesh() { if (closeFront) { for (int j = 0; j < frontVertices.Count - 1; j++) { int a = frontVertices [0], b = frontVertices [j + 1], c = frontVertices [j]; // Dup vertices Vector3 pa = vertices [a]; Vector3 pb = vertices [b]; Vector3 pc = vertices [c]; vertices.Add(pa); UVs.Add(pa - frontPosition); normals.Add(frontNormal); a = vertices.Count - 1; vertices.Add(pb); UVs.Add(pb - frontPosition); normals.Add(frontNormal); b = vertices.Count - 1; vertices.Add(pc); UVs.Add(pc - frontPosition); normals.Add(frontNormal); c = vertices.Count - 1; if (flipped) { triangles2.Add(c); triangles2.Add(b); triangles2.Add(a); } else { triangles2.Add(a); triangles2.Add(b); triangles2.Add(c); } } } if (closeBack) { for (int j = 0; j < backVertices.Count - 1; j++) { int a = backVertices [0], b = backVertices [j + 1], c = backVertices [j]; // Dup vertices Vector3 pa = vertices [a]; Vector3 pb = vertices [b]; Vector3 pc = vertices [c]; vertices.Add(pa); UVs.Add(pa - backPosition); normals.Add(backNormal); a = vertices.Count - 1; vertices.Add(pb); UVs.Add(pb - backPosition); normals.Add(backNormal); b = vertices.Count - 1; vertices.Add(pc); UVs.Add(pc - backPosition); normals.Add(backNormal); c = vertices.Count - 1; if (flipped) { triangles2.Add(a); triangles2.Add(b); triangles2.Add(c); } else { triangles2.Add(c); triangles2.Add(b); triangles2.Add(a); } } } }
public void AddVertex(Vector3 position, Vector3 normal, Vector2 textureCoord) { Vertices.Add(position); Normals.Add(normal); UVs.Add(textureCoord); }
public void AddListUV(float u, float v) { UVs.Add(new Vector2(u, v)); }
/// <summary> /// Initializes a new instance of the <see cref="PRTMRawDataMetaChunk"/> class. /// </summary> /// <param name="meta">The meta.</param> /// <remarks></remarks> public PRTMRawDataMetaChunk(ref Meta meta) { BinaryReader BR = new BinaryReader(meta.MS); BR.BaseStream.Position = 176; int tempc = BR.ReadInt32(); int tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset; RawDataChunkInfo = new RawDataOffsetChunk[tempc]; for (int x = 0; x < tempc; x++) { RawDataChunkInfo[x] = new RawDataOffsetChunk(); BR.BaseStream.Position = tempr + (x * 16) + 6; RawDataChunkInfo[x].ChunkSize = 56; RawDataChunkInfo[x].Size = BR.ReadInt32(); if (RawDataChunkInfo[x].ChunkSize == 0) { RawDataChunkInfo[x].ChunkSize = RawDataChunkInfo[x].Size; } RawDataChunkInfo[x].Offset = BR.ReadInt32(); RawDataChunkInfo[x].ChunkCount = RawDataChunkInfo[x].Size / RawDataChunkInfo[x].ChunkSize; } BR.BaseStream.Position = 136; VerticeCount = BR.ReadInt32(); tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset; BR.BaseStream.Position = tempr; for (int x = 0; x < VerticeCount; x++) { Vector3 vec = new Vector3(); vec.X = BR.ReadSingle(); vec.Y = BR.ReadSingle(); vec.Z = BR.ReadSingle(); Vertices.Add(vec); Vector3 vec2 = new Vector3(); vec2.X = BR.ReadSingle(); vec2.Y = BR.ReadSingle(); vec2.Z = BR.ReadSingle(); Normals.Add(vec2); Vector3 vec3 = new Vector3(); vec3.X = BR.ReadSingle(); vec3.Y = BR.ReadSingle(); vec3.Z = BR.ReadSingle(); Binormals.Add(vec3); Vector3 vec4 = new Vector3(); vec4.X = BR.ReadSingle(); vec4.Y = BR.ReadSingle(); vec4.Z = BR.ReadSingle(); Tangents.Add(vec4); Vector2 vec5 = new Vector2(); vec5.X = BR.ReadSingle(); vec5.Y = BR.ReadSingle(); UVs.Add(vec5); } BR.BaseStream.Position = 168; HeaderSize = BR.ReadInt32() + 8; BR.BaseStream.Position = 144; IndiceCount = BR.ReadInt32(); tempr = BR.ReadInt32() - meta.Map.SecondaryMagic - meta.offset; BR.BaseStream.Position = tempr; FaceCount = IndiceCount / 3; this.Indices = new short[IndiceCount]; for (int x = 0; x < IndiceCount; x++) { Indices[x] = (short)BR.ReadUInt16(); } SubMeshInfo = new ModelSubMeshInfo[1]; for (int x = 0; x < 1; x++) { SubMeshInfo[x] = new ModelSubMeshInfo(); BR.BaseStream.Position = HeaderSize + RawDataChunkInfo[0].Offset + (x * 72) + 4; SubMeshInfo[x].ShaderNumber = 0; SubMeshInfo[x].IndiceStart = 0; SubMeshInfo[x].IndiceCount = IndiceCount; } }
public void Convert(string inputPath, string outputPath, Bitmap bitmap, UVs[] sliceCoordinates) { CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath); Size size = bitmap.Size; if (size.Width % TileConfig.TileWidth != 0 || size.Height % TileConfig.TileHeight != 0) { throw new Exception("Size not compatible with GBA tiles. Width and height of pixels must be multiples of 8."); } Console.WriteLine("Processing colour palette"); int xStart = 0; int yStart = 0; int width = bitmap.Width; int height = bitmap.Height; Color[] preProcessedPalette = PaletteHelper.GeneratePaletteFromImage(bitmap, xStart, yStart, width, height); // Validate pallet length { if (preProcessedPalette.Length > 256) { throw new Exception("Palette length out of range for the GBA"); } // Todo, currently not supported if (preProcessedPalette.Length > 16) { throw new Exception("Palette length out of range for 4bbp format"); } } Console.WriteLine(String.Format("Palette length: {0}", preProcessedPalette.Length)); Compression.CompressionType compressionType = Compression.CompressionType.BitPacked; uint maxColours = MathX.IsPowerOf2((uint)preProcessedPalette.Length) ? (uint)preProcessedPalette.Length : MathX.NextPowerOf2((uint)preProcessedPalette.Length); uint destBpp = (uint)MathX.IndexOfHighestSetBit(maxColours); if (!MathX.IsPowerOf2(destBpp)) { destBpp = MathX.NextPowerOf2(destBpp); } Console.WriteLine(string.Format("Compression type = {0}", compressionType.ToString())); Console.WriteLine(string.Format("Destination Bit Depth = {0}", destBpp)); List <int> dataOffsets = new List <int>(); List <List <UInt32> > spriteData = new List <List <UInt32> >(); dataOffsets.Add(0); int totalData = 0; // Collect data and add offsets { Console.WriteLine("Processing sprite data"); for (int i = 0; i < sliceCoordinates.Length; ++i) { StringBuilder dataSb = new StringBuilder(); UVs slice = sliceCoordinates[i]; int spriteWidth = slice.width, spriteHeight = slice.height; List <UInt32> data = WriteSpriteData(bitmap, preProcessedPalette, spriteWidth, spriteHeight, slice.x, slice.y, compressionType, destBpp); spriteData.Add(data); // Add offsets if (i < sliceCoordinates.Length - 1) { dataOffsets.Add(dataOffsets[i] + data.Count); } totalData += data.Count; } } WriteHeader(cppWriter, sliceCoordinates, preProcessedPalette, totalData, compressionType, destBpp); WritePalette(cppWriter, preProcessedPalette); // Write width { for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteWidth = sliceCoordinates[i].width; cppWriter.Write((byte)spriteWidth); } } // Write height { for (int i = 0; i < sliceCoordinates.Length; ++i) { int spriteHeight = sliceCoordinates[i].height; cppWriter.Write((byte)spriteHeight); } } // Write offsets { for (int i = 0; i < dataOffsets.Count; i++) { cppWriter.Write((UInt32)dataOffsets[i]); } } // Write data { foreach (var dataList in spriteData) { foreach (UInt32 data in dataList) { cppWriter.Write((UInt32)data); } } } Console.WriteLine("Sprite \"" + outputPath + "\" successfully converted"); cppWriter.Finalise(); }