Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This is an example of a shape consisting of multiple points, triangles and eventually a shape:enter image description here

Red Dots = Vector3 (X, Y, Z) or Vector2 (X, Y)

If I have a Texture of a certain size, how do I texturize this area in the best way so that the texture inside the shape matches the shape and does not overlap anywhere? Perhaps also with a chance to scale the texture in case it's too small or to big for the shape, but still so that it gets rendered correctly.

Do I treat the shape as a rectangle? Figure out it's 4 corners? Or do I calculate the distance between Center - (Texture Width / 2) and Point (to see how "many" times the texture can fit between on that axis to estimate at what Coordinates the Texture should be at that certain point?

I've looked at Texture Mapping but haven't found any concrete examples that it explains it well, it's also confusing with 0.0-1.0 values for Texture Coordinates.

share|improve this question
    
If you want to add a solution, do it as another answer, don't edit other answers with your own. –  Byte56 Nov 24 '12 at 7:23
add comment

2 Answers

This is the effect that will occur if you treat the shape as a rectangle, and use its bounding box as the 0,0 -> 1,1 mapping:

This is your texture: enter image description here This is your shape with texture coords mapped to bounding box(approximate guesses shown): enter image description here This is the final result: enter image description here

As you can see you've basically sliced a shape out of the original texture. If that's not what you want to do, you'll need to find some other mapping that makes sense for your particular object.

share|improve this answer
    
This is exactly as I thought about. But it seems scaled, how do I scale it and perhaps rotate it? I presumed that the center was X:0,Y:0. TopRight corner, X:1, Y:-1, BottomRight corner: X1 Y:1, BottomLeft corner: X: -1 Y: 1, TopLeft corner: X: -1, Y: -1... Perhaps that's why my methods have failed. I'll try using this as a reference point to see if I get results. If I want to scale it 2*Size I just use 2.0 instead of 1.0? –  Deukalion Nov 24 '12 at 2:59
    
That depends on how you're wrapping your texture. Typically values greater than 1 have their >1 components ignored. This results in the texture repeating itself. Rotating it would take some more complex math and is not really in the scope of this question. To scale it bigger you would actually use smaller texture coordinates, not bigger. –  Eric B Nov 24 '12 at 4:16
add comment
up vote 0 down vote accepted

Solution:

public static class MappingHelper
{

    public static VertexPositionTexture[] TextureMapping(VertexPositionTexture[] vertices, float scale = 1f)
    {

        bool initialized = false;

        float x, y;
        float lX = 0, hX = 0, lY = 0, hY = 0;

        for (int i=0; i < vertices.Length; i++)
        {

            x = vertices[i].Position.X;
            y = vertices[i].Position.Y;

            if (!initialized)
            {
                hX = x;
                lX = x;
                hX = y;
                hY = y;

                initialized = true;
            }
            else
            {
                if (x > hX)
                {
                    hX = x;
                }
                else if (x < lX)
                {
                    lX = x;
                }

                if (y > hY)
                {
                    hY = y;
                }
                else if (y < lY)
                {
                    lY = y;
                }
            }


        }

        float width = (Math.Abs(lX) + Math.Abs(hX)) / scale;
        float height = (Math.Abs(lY) + Math.Abs(hY)) / scale;

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i].TextureCoordinate.X = vertices[i].Position.X / width;
            vertices[i].TextureCoordinate.Y = vertices[i].Position.Y / height;
        }

        return vertices;

    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.