I assume you are building your own VertexArrayObject from VertexPositionNormalTexture already. You will first have to define your custom vertex layout and use that instead:
[StructLayout(LayoutKind.Sequential)]
public struct MyCustomVertex: IVertexWindable
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TextureCoordinate;
public float TextureIndex; // You could also combine these into a Vector3 again, if you like
public static readonly int Size = 36;
public static readonly VertexDeclaration Layout = new VertexDeclaration(new VertexElement[]
{
VertexElement.Position<Vector3>(),
VertexElement.Normal<Vector3>(),
VertexElement.TextureCoordinate<Vector2>(),
new VertexElement("MY_SEMANTIC", PixelFormat.R32_Float),
});
public void FlipWinding()
{
TextureCoordinate.X = (1.0f - TextureCoordinate.X);
}
}
Next you need to create a custom effect. I assume you are currently using the effect from the default game template (MyGameEffectMain.pdxfx). If not let me know. That effect has a variable diffuseAlbedo
of type ComputeColor
which defines the final color.
The easiest way to change that is to first define our own ComputeColor
:
// ComputeMyCustomColor.pdxsl
class ComputeMyCustomColor : ComputeColor
{
// EDIT: Forgot stage modifier. Needed if used inside 'mixin compose'
stage Texture2DArray TextureArray // This will be available as 'ComputeMyCustomColorKeys.TextureArray'
stage stream float2 TexCoord : TEXCOORD0;
stage stream float TextureIndex : MY_SEMANTIC;
override float4 Compute()
{
return TextureArray.Sample(Texturing.Sampler, float3(streams.TexCoord, streams.TextureIndex));
}
};
That will probably be similar to your SharpDX shader. Next, modify MyGameEffectMain
to use that color computation. Replace
if (MaterialParameters.AlbedoDiffuse != null)
mixin compose albedoDiffuse = MaterialParameters.AlbedoDiffuse;
with
mixin compose albedoDiffuse = ComputeMyCustomColor;
Finally, add your texture array to each ModelComponent.Parameters (or to RenderSystem.Pipeline.Parameters, for simplicity):
modelComponent.Parameters.Set(ComputeMyCustomColorKeys.TextureArray, myTextureArray);
That should be it!
There are multiple ways to do this of cause. You could dump the MyGameEffectMain and use a much simpler one, closer to SimpleEffect. You could also make ComputeMyCustomColor a bit more generic, so it would be easier to configure from Paradox Studio.