I’m trying to use a custom shader effetct on a generated mesh and unfortunately all that I get is a blank screen.
HLSL is something new to me and I’m not familiar with, so I’ve tried reusing some simple examples with no luck.
I’ve tried to use a couple of simple shaders, just to get them running with my code.
This is an ambient light sample I’ve digged out from XNA 3.5 and updated to monogame.
HLSL:
float4x4 matWorldViewProj;
struct OUT
{
float4 Pos: POSITION;
};
OUT VertexShaderFunction( float4 Pos: POSITION )
{
OUT Out = (OUT) 0;
Out.Pos = mul(Pos, matWorldViewProj);
return Out;
}
float4 PixelShaderFunction() : COLOR
{
// Set ambient intensity to 80%
float Ai = 0.8f;
// Set the color of our ambient light
float4 Ac = float4(0.85, 0.75, 0.2, .0);
// return our color to the COLOR register
return Ai * Ac;
}
technique AmbientLight
{
pass P0
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
And below another one I’ve dug up from the forum:
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
matrix WorldViewProjection;
static const float4 MyRed = float4(1.0f, 0.0f, 0.0f, 1.0f);
static const float4 MyBlue = float4(0.0f, 0.0f, 1.0f, 1.0f);
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
};
VertexShaderOutput MainVS(in VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
float4 pos = mul(input.Position, WorldViewProjection);
output.Position = pos;
float4 col = (pos.x * MyRed) + pos.x * (1.0f - MyBlue);
output.Color = col;
return output;
}
float4 MainPS(VertexShaderOutput input) : COLOR
{
//return MyRed; // works
return input.Color; // use vertex shader
}
technique BasicColorDrawing
{
pass P0
{
VertexShader = compile VS_SHADERMODEL MainVS();
PixelShader = compile PS_SHADERMODEL MainPS();
}
};
This is the draw method I’m using which works completely fine with basic effect, however I don’t know what I’m doing wrong when I try to use the cusotm one:
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
vertexBuffer = new DynamicVertexBuffer(Game.GraphicsDevice, typeof(VertexPositionColor), ((VertexPositionColor[])CurveData[0]).Length, BufferUsage.WriteOnly);
indexBuffer = new DynamicIndexBuffer(Game.GraphicsDevice, IndexElementSize.ThirtyTwoBits, ((int[])CurveData[1]).Length, BufferUsage.WriteOnly);
vertexBuffer.SetData((VertexPositionColor[])CurveData[0], 0, ((VertexPositionColor[])CurveData[0]).Length);
indexBuffer.SetData((int[])CurveData[1]);
Game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
Game.GraphicsDevice.Indices = indexBuffer;
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
Game.GraphicsDevice.RasterizerState = rasterizerState;
customEffect.CurrentTechnique = customEffect.Techniques["BasicColorDrawing"];
foreach (var pass in customEffect.CurrentTechnique.Passes)
{
pass.Apply();
customEffect.Parameters["WorldViewProjection"].SetValue(WorldMatrix*ViewMatrix*ProjectionMatrix);
Game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indexBuffer.IndexCount / 3);
}
I’d like to have the mesh with a texture shader eventually. Any tips would be appreciated.