Exemplo n.º 1
0
    void Start()
    {
        //mesh = new MMesh(gameObject.GetComponent<MeshFilter>().mesh);

        //List<MVertex> triangle1= new List<MVertex>();
        //triangle1.Add(new MVertex(new Vector3(0f, 0f, 0f), Vector3.down));
        //triangle1.Add(new MVertex(new Vector3(1f, 0f, 0f), Vector3.down));
        //triangle1.Add(new MVertex(new Vector3(1f, 0f, 1f), Vector3.down));

        //List<MVertex> triangle2 = new List<MVertex>();
        //triangle2.Add(new MVertex(new Vector3(1f, 1f, 0f), Vector3.right));
        //triangle2.Add(new MVertex(new Vector3(1f, 0f, 0f), Vector3.right));
        //triangle2.Add(new MVertex(new Vector3(1f, 0f, 1f), Vector3.right));

        //MTriangle ta = new MTriangle(mesh,triangle1);
        //MTriangle tb = new MTriangle(mesh, triangle2);

        mesh = new MMesh(gameObject.GetComponent<MeshFilter>().mesh);
        //texture = new Texture2D(64,64);
        //RenderUtility.RenderToTexture(mesh,Color.black,texture);

        //this.renderer.material.mainTexture = texture;

           // Debug.Log(mesh.Vertices.Count);
    }
Exemplo n.º 2
0
Arquivo: Main.cs Projeto: Fizzly/MMesh
    void Start()
    {
        rasterizer = new Rasterizer();

        referencePlaneMesh = MeshCreator.CreatePlane();
           // rasterizedPlaneMesh = MeshCreator.CreatePlane(1f, 1f, 0.3343f, 0.6345f);

        currentMesh = new MMesh(rasterizedPlaneMesh);

        referenceGameObject = CreateGameobjectWithMesh("ReferenceObject", referencePlaneMesh);
        rasterizedGameObject = CreateGameobjectWithMesh("RasterizedObject", rasterizedPlaneMesh);

        currentMesh.ApplyPadding((1f / 512) * 8f);

        referenceGameObject.renderer.material.mainTexture = rasterizer.RasterizeMesh(currentMesh, 512);
        rasterizedGameObject.renderer.material.mainTexture = referenceGameObject.renderer.material.mainTexture;
    }
Exemplo n.º 3
0
 public static void RenderToTexture(MMesh mesh, Color color, Texture2D texture)
 {
     foreach(MTriangle triangle in mesh.Triangles)
         RasterizeTriangle(triangle,texture);
 }
Exemplo n.º 4
0
        public MUVTriangle(MMesh parent, List<MUVVertex> vertices)
        {
            this.parent = parent;

            this.vertices = new List<MUVVertex>();
            this.connections = new List<MUVTriangle>();
            this.edges = new List<MUVEdge>();

            for (int i = 0; i < 3; i++)
            {
                MUVVertex vertex = vertices[i];
                center += vertex.UV;

                if (parent.UVVertices.Contains(vertex))
                {
                    int index = parent.UVVertices.IndexOf(vertex);
                    if (parent.UVVertices[index].Parents.Contains(this) == false) parent.UVVertices[index].Parents.Add(this);
                    MUVVertex tVertex = parent.UVVertices[index];
                    tVertex.Parents.Add(this);
                    this.vertices.Add(tVertex);
                }
                else
                {
                    vertex.Parents.Add(this);
                    this.vertices.Add(vertex);
                    parent.UVVertices.Add(vertex);
                }
            }

            AddEdge(vertices[0], vertices[1]);
            AddEdge(vertices[1], vertices[2]);
            AddEdge(vertices[2], vertices[0]);

            center = center / 3;

            parent.UVTriangles.Add(this);
            normal = (normal / 3).normalized;
            FindConnections();
        }
Exemplo n.º 5
0
        public MTriangle(MMesh parent,List<MVertex> vertices)
        {
            this.parent = parent;

            this.vertices = new List<MVertex>();
            this.connections = new List<MTriangle>();

            for (int i = 0; i < 3;i++)
            {
                MVertex vertex = vertices[i];
                normal += vertex.Normal;
                center += vertex.Position;

                uvCenter += vertex.Uv.UV;

                if (parent.Vertices.Contains(vertex))
                {
                    int index = parent.Vertices.IndexOf(vertex);
                    if (parent.Vertices[index].Parents.Contains(this) == false) parent.Vertices[index].Parents.Add(this);
                    MVertex tVertex = parent.Vertices[index];
                    tVertex.Parents.Add(this);
                    this.vertices.Add(tVertex);
                }
                else
                {
                    vertex.Parents.Add(this);
                    this.vertices.Add(vertex);
                    parent.Vertices.Add(vertex);
                }
            }

            center = center / 3;
            uvCenter = uvCenter / 3;

            parent.Triangles.Add(this);
            normal = (normal / 3).normalized;
            FindConnections();
        }
Exemplo n.º 6
0
    public Texture2D RasterizeMesh(MMesh mesh, int resolution)
    {
        Texture2D tex = new Texture2D(resolution, resolution);

        Vector2 vertexUV1;
        Vector2 vertexUV2;
        Vector2 vertexUV3;

        MTriangle[] meshTriangles = mesh.Triangles.ToArray();
        MUVTriangle[] meshUVTriangles = mesh.UVTriangles.ToArray();
        MTriangle triangle;
        MUVTriangle uvTriangle;

        float step = (1f / resolution);

        int trianglecount = 0;

        for (int index = 0; index < meshTriangles.Length; index++ )
        {
            triangle = meshTriangles[index];
            uvTriangle = meshUVTriangles[index];

            vertexUV1 = uvTriangle.Vertices[0].UV;
            vertexUV2 = uvTriangle.Vertices[1].UV;
            vertexUV3 = uvTriangle.Vertices[2].UV;

            /* get the bounding box of the triangle */
            float minX = Mathf.Min(vertexUV1.x, Mathf.Min(vertexUV2.x, vertexUV3.x));
            float maxY = Mathf.Max(vertexUV1.y, Mathf.Max(vertexUV2.y, vertexUV3.y));
            float minY = Mathf.Min(vertexUV1.y, Mathf.Min(vertexUV2.y, vertexUV3.y));
            float maxX = Mathf.Max(vertexUV1.x, Mathf.Max(vertexUV2.x, vertexUV3.x));

            // Crop rect to size of texture and iterate through every pixel in that rect
            int pixMinX = (int)Mathf.Max(Mathf.Floor(minX * resolution) - 1, 0); int pixMaxX = (int)Mathf.Min(Mathf.Ceil(maxX * resolution) + 1, resolution);
            int pixMinY = (int)Mathf.Max(Mathf.Floor(minY * resolution) - 1, 0); int pixMaxY = (int)Mathf.Min(Mathf.Ceil(maxY * resolution) + 1, resolution);

            Vector3 n0 = triangle.Vertices[0].Normal;
            Vector3 n1 = triangle.Vertices[1].Normal;
            Vector3 n2 = triangle.Vertices[2].Normal;

            //Prepare variables for this triangle
            Vector3 v0 = triangle.Vertices[0].Position;
            Vector3 v1 = triangle.Vertices[1].Position;
            Vector3 v2 = triangle.Vertices[2].Position;

            //HeronsForumula finds the area of a triangle based on its side lengths (The vertex positions are used as inputs here)
            float area = HeronsForumula(vertexUV1, vertexUV2, vertexUV3);

            for (int x = pixMinX; x < pixMaxX; x++)
            {
                for (int y = pixMinY; y < pixMaxY; y++)
                {

                    Vector2 pixel = new Vector2(((float)x) * step, ((float)y) * step);
                    //Early rejection of pixel if outside of triangle or outside of texture.

                    if (Vector2.Angle(vertexUV1 - pixel, vertexUV2 - pixel) + Vector2.Angle(vertexUV2 - pixel, vertexUV3 - pixel) + Vector2.Angle(vertexUV3 - pixel, vertexUV1 - pixel) > 359.9)
                    {
                        /*
                        //For constructing the barycentric coordinate of this pixel within this triangle, we use the area of the main triangle, and the areas of the
                        // triangles that result when we subdivide the triangle by drawing lines from each of its vertices to the pixel.

                        //Find the areas of the triangles formed by this subdivision, indexed such that the triangle is across from, not next to, its vertex
                        float a0 = HeronsForumula(pixel, vertexUV2, vertexUV3);
                        float a1 = HeronsForumula(vertexUV1, pixel, vertexUV3);
                        float a2 = HeronsForumula(vertexUV1, vertexUV2, pixel);

                        // the position of the pixel is composed of the position of each vertex. the percentage or "wieght" of each vertex in this position
                        // is obtained by taking the ratio of the opposing triangle's area to the total area
                        Vector3 bary = new Vector3(a0 / area, a1 / area, a2 / area);

                        // prepare Tangent Space Transformation Matrix
                        Vector3 pixelZ = (bary.x * n0 + bary.y * n1 + bary.z * n2).normalized; // normal
                        Vector4 tangent = bary.x * tt0 + bary.y * tt1 + bary.z * tt2;
                        Vector3 pixelX = new Vector3(tangent.x, tangent.y, tangent.z).normalized; // tangent
                        Vector3 pixelY = Vector3.Cross(pixelZ, pixelX);	 // binormal

                        Vector3 tangentSpaceNormal = new Vector3(0.5f, 0.5f, 0f);
                        tangentSpaceNormal.z = Mathf.Sqrt(1f - tangentSpaceNormal.x * tangentSpaceNormal.x - tangentSpaceNormal.y * tangentSpaceNormal.y);

                        // Simply multiply to get world space normal!
                        Vector3 worldNormal = tangentSpaceNormal.x * pixelX + tangentSpaceNormal.y * pixelY + tangentSpaceNormal.z * pixelZ;

                        //					worldSpaceNormal.SetPixel(x, y, new Color(worldNormal.x*0.5f + 0.5f, worldNormal.y*0.5f + 0.5f, worldNormal.z*0.5f + 0.5f));
                        tex.SetPixel(x, y, new Color(tangentSpaceNormal.x, tangentSpaceNormal.y, tangentSpaceNormal.z));
                         */

                        tex.SetPixel(x, y, new Color(triangle.Normal.x * 0.5f + 0.5f, triangle.Normal.y * 0.5f + 0.5f, triangle.Normal.z * 0.5f + 0.5f));

                    }

                }
            }

        }

        tex.Apply();

        tex.filterMode = FilterMode.Point;

        return tex;
    }