I’m trying to adapt this tutorial into a small test project. :LWJGL ShaderLesson6
The mouse position is suppose to be the source of light. It only seems to illuminate when I move the cursor closer to 0,0 though. See the gif below for an example.
And this is the shader:
#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;
Texture2D SpriteTexture;
Texture2D NormalTexture;
sampler2D SpriteSampler = sampler_state
{
Texture = <SpriteTexture>;
};
sampler2D NormalSampler = sampler_state
{
Texture = <NormalTexture>;
};
float2 Resolution;
float3 LightPos;
float4 LightColor;
float4 AmbientColor;
float3 Falloff;
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float4 color : COLOR0;
float2 texCoord : TEXCOORD0;
};
float4 MainPS(VertexShaderOutput input) : COLOR0
{
float4 DiffuseColor = tex2D(SpriteSampler, input.texCoord);
float3 NormalMap = tex2D(NormalSampler, input.texCoord).rgb;
float3 LightDir = float3(LightPos.xy - (input.texCoord.xy / Resolution.xy), LightPos.z);
LightDir.x *= Resolution.x / Resolution.y;
float D = length(LightDir);
float3 N = normalize(NormalMap * 2.0 - 1.0);
float3 L = normalize(LightDir);
float3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
float3 Ambient = AmbientColor.rgb * AmbientColor.a;
float Attenuation = 1.0 / (Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
float3 Intensity = Ambient * Diffuse * Attenuation;
float3 FinalColor = DiffuseColor.rgb * Intensity;
return input.color * float4(FinalColor, DiffuseColor.a);
}
technique SpriteDrawing
{
pass P0
{
PixelShader = compile PS_SHADERMODEL MainPS();
}
};
And in game1:
> protected override void LoadContent()
> {
> // Create a new SpriteBatch, which can be used to draw textures.
> spriteBatch = new SpriteBatch(GraphicsDevice);
> normalMap = Content.Load<Texture2D>("normalMap2");
> textureMap = Content.Load<Texture2D>("textureMap2");
> normalEffect = Content.Load<Effect>("normalEffect3");
> lightColor = new Vector4(0.8f, 1f, 1f, 0);
> AmbientColor = new Vector4(0.6f, 0.6f, 1f, 0.2f);
> Falloff = new Vector3(.4f, 3f, 20f);
> lightColor = new Vector4(1f, 1f, 1f, 1f);
> }
> protected override void Update(GameTime gameTime)
> {
> if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
> Exit();
> mouseState = Mouse.GetState();
> float x = mouseState.X / (float)graphics.PreferredBackBufferWidth;
> float y = mouseState.Y / (float)graphics.PreferredBackBufferHeight;
> light = new Vector3(x, y, 0.075f);
> base.Update(gameTime);
> }
> protected override void Draw(GameTime gameTime)
> {
> GraphicsDevice.Clear(Color.Transparent);
> spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, null, null, null, normalEffect, null);
> SetShaderValues();
> spriteBatch.Draw(textureMap, Vector2.Zero, Color.White);
> spriteBatch.End();
> base.Draw(gameTime);
> }
> public void SetShaderValues()
> {
> Vector2 res = new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
> normalEffect.Parameters["Resolution"].SetValue(res);
> normalEffect.Parameters["LightPos"].SetValue(light);
> normalEffect.Parameters["NormalTexture"].SetValue(normalMap);
> normalEffect.Parameters["LightColor"].SetValue(lightColor);
> normalEffect.Parameters["AmbientColor"].SetValue(AmbientColor);
> normalEffect.Parameters["Falloff"].SetValue(Falloff);
> normalEffect.Parameters["SpriteTexture"].SetValue(textureMap);
> normalEffect.CurrentTechnique.Passes[0].Apply();
> }
A push in the right direction would be appreciated!