Liberty Coding

Software and Game Developer

This page contains rantings of a programmer on a journey of discovery and fun. What was accomplished along this path is also shown here.

This is a pixel shader I wrote while I was attending the Games Academy.  The instructor had a tool that his company was developing called ShaderTool. It is available on steam and is a lot of fun and very useful.  He added my shader into their library of shaders inside the software.

Texture2D diffuse;
float blocksPerSide;

SamplerState TextureSampler 
{
  Filter = MIN_MAG_MIP_POINT;
  AddressU = Wrap;
  AddressV = Wrap;
};

struct VS_INPUT
{
  float3 Position : POSITION;
  float3 Normal   : NORMAL;
  float2 UV       : TEXCOORD0;
};


struct PS_INPUT
{
  float4 Position : SV_POSITION;
  float3 Normal   : NORMAL;
  float2 UV       : TEXCOORD0;
};


PS_INPUT VS_Main(VS_INPUT input)
{
  PS_INPUT output = (PS_INPUT)0;

  output.Position = float4(input.Position, 1);
  output.Normal = input.Normal;
  output.UV = input.UV;
  return output;
}


float4 PS_Main(PS_INPUT input) : SV_Target
{
  float4 color = (float4)0;

  float ratio = 1.0f / blocksPerSide;

  float xmin = (input.UV.x - fmod(input.UV.x,ratio));
  float ymin = (input.UV.y - fmod(input.UV.y,ratio));

  color = diffuse.Sample(TextureSampler, float2(xmin + (ratio * .5f),ymin + (ratio * .5f)));

  return color;
}

technique11 Main
{
  pass p0
  {
    SetVertexShader(CompileShader(vs_5_0, VS_Main()));
    SetHullShader(NULL);
    SetDomainShader(NULL);
    SetGeometryShader(NULL);
    SetPixelShader(CompileShader(ps_5_0, PS_Main()));
  }
}