Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

[SOLVED] NormalMap with Spritebatch

$
0
0

@karkarotto wrote:

Hi guys,

I have a problem with a shader which should apply a BumpMap-effect to a sprite.
I tried various tutorials but i could not get it working.

My last try was this one:

From what i read it should exactly do what I want :slight_smile:
But: After i use my Shader in spriteBatch.Begin the Draw-Call won't draw anything. It is like the shader would not receive the base texture at all.

My Shader:

// Effect applies normalmapped lighting to a 2D sprite.

float3 LightDirection;
float3 LightColor = 1.0;
float3 AmbientColor = 0.35;

sampler TextureSampler : register(s0);
sampler NormalSampler : register(s1)
{
	Texture = (NormalTexture);
};

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
	//Look up the texture value
	float4 tex = tex2D(TextureSampler, texCoord);

	//Look up the normalmap value
	float4 normal = 2 * tex2D(NormalSampler, texCoord) - 1;

	// Compute lighting.
	float lightAmount = dot(normal.xyz, LightDirection);
	color.rgb *= AmbientColor + (lightAmount * LightColor);

	return tex * color;
}

technique Normalmap
{
    pass Pass1
    {
        PixelShader = compile ps_4_0_level_9_1 main();
    }
}

My Draw-Call:

    protected override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        //This is the light direction to use to light any norma. maps.
        Vector2 dir = MoveInCircle(gameTime, 1.0f);
        Vector3 lightDirection = new Vector3(dir.X, dir.Y, 0f);
        lightDirection.Normalize();

        //Clear the device to XNA blue.
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // Draw without shader
        spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null);
        spriteBatch.Draw(this.baseTexture, Vector2.Zero, Color.White);
        spriteBatch.End();

        //Set the light directions.
        this.newEffect.Parameters["LightDirection"].SetValue(lightDirection);
        this.newEffect.Parameters["NormalTexture"].SetValue(this.normal);
        this.newEffect.Parameters["LightColor"].SetValue(new Vector3(1f, 1f, 1f));
        this.newEffect.Parameters["AmbientColor"].SetValue(new Vector3(.25f, 0.25f, 0.25f));

        //Draw with shader
        spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, this.newEffect);

        spriteBatch.Draw(this.baseTexture, new Vector2(600, 0), Color.White);
        spriteBatch.End();

    }

I am convinced that I'am overlooking something obvious (and probably easy too).
Maybe someone has the clue for me that I need.

Thanks in advance

Posts: 7

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles