I want to repeat a selected part of the texture into a quad.

I was looking at this thread, however the solution involved changing my HLSL.

Is there any way to do it without changing my shader settings?

SamplerState SampleType
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;

float4 TexturePixelShader(PixelInputType input) : SV_Target
    float4 textureColor;

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(SampleType, input.tex);

    return textureColor;

And here's my current texture calculations:

// Get desired texture coordinates.
tLeft = (float)relTextureX / aTextureWidth ;
tRight = (float)(relTextureX + relTextureWidth) / aTextureWidth;
tTop = (float)relTextureY / aTextureHeight;
tBottom = (float)(relTextureY + relTextureHeight) / aTextureHeight;

// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if (!vertices)
    return false;

// Load the vertex array with data.
vertices[0].position = D3DXVECTOR3(left, top, 0.0f);  // Top left.
vertices[0].texture = D3DXVECTOR2(tLeft, tTop);

vertices[1].position = D3DXVECTOR3(right, bottom, 0.0f);  // Bottom right.
vertices[1].texture = D3DXVECTOR2(tRight, tBottom);

vertices[2].position = D3DXVECTOR3(left, bottom, 0.0f);  // Bottom left.
vertices[2].texture = D3DXVECTOR2(tLeft, tBottom);

// Second triangle.
vertices[3].position = D3DXVECTOR3(left, top, 0.0f);  // Top left.
vertices[3].texture = D3DXVECTOR2(tLeft, tTop);

vertices[4].position = D3DXVECTOR3(right, top, 0.0f);  // Top right.
vertices[4].texture = D3DXVECTOR2(tRight, tTop);

vertices[5].position = D3DXVECTOR3(right, bottom, 0.0f);  // Bottom right.
vertices[5].texture = D3DXVECTOR2(tRight, tBottom);

You will have to put your texture wrap mode to repeat and use UV texture coordinates larger than the usual 0.0 - 1.0 range. Like for example 0.0 - 2.0 on the x axis will repeat your texture twice on the x axis!


in the shader file I believe in the vertex shader file you can do something like this:

output.texcoord = input.texcoord * 2.0f;

What this will do is move the texture UV coord's over 2 units over. What you will do is achieve texture repeating. If you don't you can just either have it times by 1.0f .

So, the output would be for the pixelshader or fragment shader (openGL) and the input would be for the vertex input inside the shader.

It must be done in the vertex shader because the pixel shader only focuses on the pixels inside the mesh.

  • \$\begingroup\$ Are you saying there's no way to do this outside of the shader file? \$\endgroup\$ Nov 25 '13 at 16:06
  • \$\begingroup\$ I don't think so...I'm pretty sure that repeating texture coords would have to be done in the shader file. \$\endgroup\$ Nov 25 '13 at 18:52
  • \$\begingroup\$ @user2280704 @SICGames2013 Well, you can do exactly same with vertices[i].texture on CPU side. \$\endgroup\$ Nov 28 '13 at 12:29
  • \$\begingroup\$ Creating a dynamic buffer for the vertices and indices. I never thought about that but using the shader file wouldn't be a pain the butt either. \$\endgroup\$ Nov 28 '13 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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