Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > XNA

XNA Tutorials and support for game development using Microsoft's XNA Game Studio framework.

Reply
 
Thread Tools Display Modes
Old 01-31-2008, 07:47 PM   #1 (permalink)
Newbie
 
Join Date: Nov 2007
Age: 22
Posts: 63
Default Simple 2D Shader

I never knew this and it always drove me crazy why a simple shader wouldn't give me the same results as a SpriteBatch, well finally I found out why. You have to subtract half a pixel from the x/y of a vertex. Who would have thought...

Code:
float light = 1.0f;
sampler textureSampler;
float4x4 worldViewProj : WORLDVIEWPROJ;

struct PositionTexCoord
{
	float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
};

float4 PS_Process(PositionTexCoord IN) : COLOR
{
	return tex2D(textureSampler, IN.TexCoord) * light;
}

void VS_Process(in PositionTexCoord IN, out PositionTexCoord OUT)
{
	float4 position = IN.Position;
	
	position.x -= 0.5f;
	position.y -= 0.5f;

	OUT.Position = mul(position, worldViewProj);
	OUT.TexCoord = IN.TexCoord;
}

technique Baisc
{
	pass p0
	{
		vertexshader = compile vs_2_0 VS_Process();
		pixelshader = compile ps_2_0 PS_Process();
	}
}
Chase.XNA is offline   Reply With Quote
Old 01-31-2008, 08:26 PM   #2 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,763
Default

Quote:
Originally Posted by Chase.XNA View Post
I never knew this and it always drove me crazy why a simple shader wouldn't give me the same results as a SpriteBatch, well finally I found out why. You have to subtract half a pixel from the x/y of a vertex. Who would have thought...

Code:
float light = 1.0f;
sampler textureSampler;
float4x4 worldViewProj : WORLDVIEWPROJ;

struct PositionTexCoord
{
	float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
};

float4 PS_Process(PositionTexCoord IN) : COLOR
{
	return tex2D(textureSampler, IN.TexCoord) * light;
}

void VS_Process(in PositionTexCoord IN, out PositionTexCoord OUT)
{
	float4 position = IN.Position;
	
	position.x -= 0.5f;
	position.y -= 0.5f;

	OUT.Position = mul(position, worldViewProj);
	OUT.TexCoord = IN.TexCoord;
}

technique Baisc
{
	pass p0
	{
		vertexshader = compile vs_2_0 VS_Process();
		pixelshader = compile ps_2_0 PS_Process();
	}
}
Hah, i remember reading about that on MSDN, i believe the reason you don't do this for normal texturing is because you want each pixel to blend with the pixels beside it, this way your textures ingame look smooth.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5