I am trying to create a shader that allows to tile a specific region of a texture. This seem to be a common need however I can't seem to find any information about how to get this effect done.
The closest I have found is the following article made by Renaud Bedard which detail a little bit how he got it to work. : http://theinstructionlimit.com/wrap-spritesheet-atlas
I have tried to implement his process within a complete shader without any success.
float3x3 Matrices_Texture;
texture ColorTexture : DIFFUSE <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
>;
sampler2D ColorSampler = sampler_state {
Texture = <ColorTexture>;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
struct vertexInput {
float4 Position : POSITION0;
float2 TextureCoordinates : TEXCOORD0;
};
struct vertexOutput {
float4 Position : POSITION0;
float2 TextureCoordinates : TEXCOORD0;
float2 UV : TEXCOORD1;
float2 UVScale : TEXCOORD2;
};
vertexOutput mainVS(vertexInput input)
{
vertexOutput output;
output.TextureCoordinates = mul(float3(input.TextureCoordinates, 1), Matrices_Texture).xy;
output.UV = Matrices_Texture[2].xy;
output.UVScale = float2(Matrices_Texture[0][0], Matrices_Texture[1][1]);
output.Position = input.Position;
return output;
}
float4 mainPS(vertexOutput input) : COLOR0
{
float2 tc = input.TextureCoordinates;
tc = frac((tc - input.UV) / input.UVScale) * input.UVScale + input.UV;
float4 sample = tex2D(ColorSampler, tc);
return sample;
}
technique technique0 {
pass p0 {
CullMode = None;
VertexShader = compile vs_3_0 mainVS();
PixelShader = compile ps_3_0 mainPS();
}
}
Note that I have zero experience with HLSL, I tried my best to use the correct types and sementic but I still can't seem to get anywhere with this. If someone could provide additionnal information, it would be greatly appreciated.
Currently, it looks like it is tiling in the preview of the material however when applied to an object, no texture is being displayed. (Testing inside FX Composer 2.5)