2
\$\begingroup\$

I have the texture below and I need to get rectangular parts from it. I could finally combine meshes of different quads to improve performance, but I with quads of different tilings, this means different materials, then combining meshes will fail.

Can anybody tell me how to have a part of that texture in C#? Such that all quads will be of the same material only then combining meshes passes.

Thanks in advance.

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ This question seems to be incomplete. What API are you using? \$\endgroup\$ Commented Apr 19, 2014 at 9:29
  • \$\begingroup\$ @Andrew Russell actually I'm not that expert. I don't even know if there's any API that does this. But I've been working on a game for about 2 months. It's complete know, and I'm in optimization phase, since it's targeted to mobile phones. \$\endgroup\$ Commented Apr 19, 2014 at 12:18
  • \$\begingroup\$ What I mean is, are you using "XNA" or "Unity" or something like that? Or are you looking for something that works with the .NET framework only (System.Drawing perhaps). Although even if you provide that info, I'm not sure your question contains enough information about what you are trying to achieve to give you a good answer. Try expanding it. \$\endgroup\$ Commented Apr 20, 2014 at 10:12
  • \$\begingroup\$ Thanks @Andrew Russell, I missed that this is general game development department. I need this in Unity. \$\endgroup\$ Commented Apr 21, 2014 at 7:43
  • \$\begingroup\$ It sounds like you're wanting 2D graphics but your question isn't clear about that. Are you doing 2D graphics or is this something for 3D? \$\endgroup\$
    – jhocking
    Commented Apr 21, 2014 at 8:50

1 Answer 1

1
\$\begingroup\$

I modified this code to get what I need. I modified it and this is my final code:

    Vector2[] uvb;
    Vector2[] uva = new Vector2[] { new Vector2(0, 0), new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 1) };
    float w, h, left, bottom, posX, posY;
    Rect uv = new Rect();
    MeshFilter filter;

    for (int i=0; i<rects.Count; i++)
    {
        AdaptedRect rect = rects[i];

        w = rect.width * blockWidth;
        h = rect.height * blockHeight;

        left = rect.left * blockWidth;
        bottom = rect.bottom * blockHeight;

        posX = groundRect.left + left + w / 2;
        posY = groundRect.bottom + bottom + h / 2;

        squareBlocksPool[i].transform.position = new Vector3(posX, posY, squareBlocksPosZ);
        squareBlocksPool[i].transform.localScale = new Vector3(w, h, blockThickness);
        squareBlocksPool[i].SetActive(true);

        // ATLASING
        uv.x = 0;
        uv.y = 0;
        uv.width = rect.width / columns;
        uv.height = rect.height / rows;

        filter = (MeshFilter)squareBlocksPool[i].GetComponent<MeshFilter>();
        uvb = new Vector2[uva.Length];

        for (int p = 0; p < uva.Length; p++)
        {
            uvb[p] = new Vector2(uva[p].x * uv.width + uv.x, uva[p].y * uv.height + uv.y);
            filter.mesh.uv = uvb;
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .