public void BuildTerrainTypeTexture()
{
if (WorkingShape.TypeTexture == null)
WorkingShape.TypeTexture = new Texture2D(Ballz.The().GraphicsDevice, width, height);
var types = WorkingShape.TerrainBitmap;
// Blurring is done in two passes, first pass goes here, second one in typeWeights
if (TypeWeightsBufferA == null)
TypeWeightsBufferA = new Color[width * height];
if (TypeWeightsBufferB == null)
TypeWeightsBufferB = new Color[width * height];
// Build type weights as color values
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int i = x + y * width;
var t = types[x, y];
Color c;
switch (t)
{
case TerrainType.Earth:
c = new Color(255, 0, 0);
break;
case TerrainType.Sand:
c = new Color(0, 255, 0);
break;
case TerrainType.Stone:
c = new Color(0, 0, 255);
break;
default:
c = new Color(0, 0, 0);
break;
}
TypeWeightsBufferA[i] = c;
}
}
using (new PerformanceReporter(Ballz.The()))
{
// Smooth everything with a 7 pixel gauss kernel (makes non-pixely material edges)
float[] gauss = new float[]
{
0.06136f,
0.24477f,
0.38774f,
0.24477f,
0.06136f
};
// Gauss X step
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int i = x + y * width;
Vector3 c = Vector3.Zero;
for (int d = -2; d <= 2; d++)
{
int ixy = Math.Min(Math.Max(x + d, 0), width - 1) + y * width;
c += TypeWeightsBufferA[ixy].ToVector3() * gauss[d + 2];
}
TypeWeightsBufferB[i] = new Color(c);
}
}
// Gauss Y step
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int i = x + y * width;
Vector3 c = Vector3.Zero;
for (int d = -2; d <= 2; d++)
{
int ixy = x + Math.Min(Math.Max(y + d, 0), height - 1) * width;
c += TypeWeightsBufferB[ixy].ToVector3() * gauss[d + 2];
}
TypeWeightsBufferA[i] = new Color(c);
}
}
}
WorkingShape.TypeTexture.SetData(TypeWeightsBufferA);
}