private void Awake()
    {
        mesh = new Mesh();

        vertices  = new Vector3[4 * MAX_QUAD_AMOUNT];
        uv        = new Vector2[4 * MAX_QUAD_AMOUNT];
        triangles = new int[6 * MAX_QUAD_AMOUNT];

        mesh.vertices  = vertices;
        mesh.uv        = uv;
        mesh.triangles = triangles;
        mesh.bounds    = new Bounds(Vector3.zero, Vector3.one * 10000f);

        GetComponent <MeshFilter>().mesh = mesh;

        //Setup internal UV normalized array
        Material material      = GetComponent <MeshRenderer>().material;
        Texture  mainTexture   = material.mainTexture;
        int      textureWidth  = mainTexture.width;
        int      textureHeight = mainTexture.height;

        List <UVCoords> uvCoordsList = new List <UVCoords>();

        foreach (ParticleUVPixels particleUVPixels in particleUVPixelsArray)
        {
            UVCoords uvCoords = new UVCoords
            {
                uv00 = new Vector2((float)particleUVPixels.uv00Pixels.x / textureWidth, (float)particleUVPixels.uv00Pixels.y / textureHeight),
                uv11 = new Vector2((float)particleUVPixels.uv11Pixels.x / textureWidth, (float)particleUVPixels.uv11Pixels.y / textureHeight)
            };
            uvCoordsList.Add(uvCoords);
        }
        uvCoordsArray = uvCoordsList.ToArray();
    }
示例#2
0
        public World_Mesh(Transform parent, Vector3 localPosition, Vector3 localScale, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset)
        {
            this.material = material;

            vertices  = new Vector3[4];
            uv        = new Vector2[4];
            triangles = new int[6];

            /* 0,1
             * 1,1
             * 0,0
             * 1,0
             */

            float meshWidthHalf  = meshWidth / 2f;
            float meshHeightHalf = meshHeight / 2f;

            vertices[0] = new Vector3(-meshWidthHalf, meshHeightHalf);
            vertices[1] = new Vector3(meshWidthHalf, meshHeightHalf);
            vertices[2] = new Vector3(-meshWidthHalf, -meshHeightHalf);
            vertices[3] = new Vector3(meshWidthHalf, -meshHeightHalf);

            if (uvCoords == null)
            {
                uvCoords = new UVCoords(0, 0, material.mainTexture.width, material.mainTexture.height);
            }

            Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);

            ApplyUVToUVArray(uvArray, ref uv);

            triangles[0] = 0;
            triangles[1] = 1;
            triangles[2] = 2;
            triangles[3] = 2;
            triangles[4] = 1;
            triangles[5] = 3;

            mesh = new Mesh();

            mesh.vertices  = vertices;
            mesh.uv        = uv;
            mesh.triangles = triangles;

            gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
            gameObject.transform.parent           = parent;
            gameObject.transform.localPosition    = localPosition;
            gameObject.transform.localScale       = localScale;
            gameObject.transform.localEulerAngles = new Vector3(0, 0, eulerZ);

            gameObject.GetComponent <MeshFilter>().mesh       = mesh;
            gameObject.GetComponent <MeshRenderer>().material = material;

            transform = gameObject.transform;

            SetSortingOrderOffset(sortingOrderOffset);
        }
示例#3
0
    public void UpdateQuad(int quadIndex, Vector3 position, float rotation, Vector3 quadSize,
                           bool skewed, int uvIndex)
    {
        //Relocate vertices
        int vIndex  = quadIndex * 4;
        int vIndex0 = vIndex;
        int vIndex1 = vIndex + 1;
        int vIndex2 = vIndex + 2;
        int vIndex3 = vIndex + 3;

        if (skewed)
        {
            vertices[vIndex0] = position + Quaternion.Euler(0, 0, rotation) *
                                new Vector3(-quadSize.x, -quadSize.y);

            vertices[vIndex1] = position + Quaternion.Euler(0, 0, rotation) *
                                new Vector3(-quadSize.x, +quadSize.y);

            vertices[vIndex2] = position + Quaternion.Euler(0, 0, rotation) *
                                new Vector3(+quadSize.x, +quadSize.y);

            vertices[vIndex3] = position + Quaternion.Euler(0, 0, rotation) *
                                new Vector3(+quadSize.x, -quadSize.y);
        }
        else
        {
            vertices[vIndex0] = position + Quaternion.Euler(90f, 0, rotation - 180) * quadSize;
            vertices[vIndex1] = position + Quaternion.Euler(90f, 0, rotation - 270) * quadSize;
            vertices[vIndex2] = position + Quaternion.Euler(90f, 0, rotation - 0) * quadSize;
            vertices[vIndex3] = position + Quaternion.Euler(90f, 0, rotation - 90) * quadSize;
        }

        // UV
        UVCoords uvCoords = uvCoordsArray[uvIndex];

        uv[vIndex0] = uvCoords.uv00;
        uv[vIndex1] = new Vector2(uvCoords.uv00.x, uvCoords.uv11.y);
        uv[vIndex2] = uvCoords.uv11;
        uv[vIndex3] = new Vector2(uvCoords.uv11.x, uvCoords.uv00.y);

        //Create triangles
        int tIndex = quadIndex * 6;

        triangles[tIndex + 0] = vIndex0;
        triangles[tIndex + 1] = vIndex1;
        triangles[tIndex + 2] = vIndex2;

        triangles[tIndex + 3] = vIndex0;
        triangles[tIndex + 4] = vIndex2;
        triangles[tIndex + 5] = vIndex3;

        updateVertices  = true;
        updateUV        = true;
        updateTriangles = true;
    }
示例#4
0
    private void InitializeUVArray()
    {
        var material      = GetComponent <MeshRenderer>().material;
        var uvCoordsList  = new List <UVCoords>();
        var mainTexture   = material.mainTexture;
        var textureWidth  = mainTexture.width;
        var textureHeight = mainTexture.height;

        foreach (ParticleUVPixels particleUVPixels in particleUVPixelsArray)
        {
            UVCoords uvCoords = new UVCoords {
                uv00 = new Vector2(
                    (float)particleUVPixels.uv00Pixels.x / textureWidth,
                    (float)particleUVPixels.uv00Pixels.y / textureHeight
                    ),
                uv11 = new Vector2(
                    (float)particleUVPixels.uv11Pixels.x / textureWidth,
                    (float)particleUVPixels.uv11Pixels.y / textureHeight
                    ),
            };
            uvCoordsList.Add(uvCoords);
        }
        uvCoordsArray = uvCoordsList.ToArray();
    }
示例#5
0
        public static World_Mesh Create(Vector3 lowerLeftCorner, Vector3 upperRightCorner, Material material, UVCoords uvCoords, int sortingOrderOffset = 0)
        {
            float   width      = upperRightCorner.x - lowerLeftCorner.x;
            float   height     = upperRightCorner.y - lowerLeftCorner.y;
            Vector3 localScale = upperRightCorner - lowerLeftCorner;
            Vector3 position   = lowerLeftCorner + localScale * .5f;

            return(new World_Mesh(null, position, Vector3.one, 0f, width, height, material, uvCoords, sortingOrderOffset));
        }
示例#6
0
 public static World_Mesh Create(Vector3 lowerLeftCorner, float width, float height, Material material, UVCoords uvCoords, int sortingOrderOffset = 0)
 {
     return(Create(lowerLeftCorner, lowerLeftCorner + new Vector3(width, height), material, uvCoords, sortingOrderOffset));
 }
示例#7
0
 public static World_Mesh Create(Vector3 position, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset = 0)
 {
     return(new World_Mesh(null, position, Vector3.one, eulerZ, meshWidth, meshHeight, material, uvCoords, sortingOrderOffset));
 }
示例#8
0
 public void SetUVCoords(UVCoords uvCoords)
 {
     Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);
     ApplyUVToUVArray(uvArray, ref uv);
     mesh.uv = uv;
 }
示例#9
0
 /// <summary>
 /// Adds a disconnected vertex to the geometry with the given coordinates, UV coordinates and normal vector
 /// </summary>
 /// <param name="coordinates">The 3D coordinates of the vertex</param>
 /// <param name="uvCoordinates">The texture UV coordinates of the vertex</param>
 /// <param name="normalVector">The normal vector of the vertex</param>
 /// <returns>Returns the index of the created vertex</returns>
 public int AddVertex(Vector3 coordinates, Vector2 uvCoordinates, Vector3 normalVector)
 {
     UVCoords.Add(uvCoordinates);
     return(AddVertex(coordinates, normalVector));
 }
示例#10
0
 public void DrawTexture(int texture, int x, int y, int w, int h, UVCoords uv, int color)
 {
     Game.Screen2D.Draw_Texture(texture, x, y, x + w - 1, y + h - 1, color, color, color, color, uv.U1, uv.V1, uv.U2, uv.V2);
 }