Quantcast
Channel: Question and Answer » hlsl
Viewing all articles
Browse latest Browse all 69

2D HLSL World position

$
0
0

I’m trying to get world position from my vertex shader to my pixel shader so that I can disable the shader once a preset X coordinate has been passed (no shading once I’m over X).

Getting the screen position is not a problem so far but despite my best efforts to look after it and implement examples the calculations just don’t return the preferred world positions I’m looking for.

Update: So got it to somewhat work, after compiling the shaders the output changes to such:Could anyone explain this?

Could anyone explain why this happens?

I should mention that I’m really new to HLSL, been only scripting so far.

Edit:Added matrices.

world = Matrix.Identity;
view = Matrix.CreateScale(new Vector3(1, 0.75f, 0)) * Matrix.CreateTranslation(-playerpos.X, -playerpos.Y, 1); 
projection = Matrix.CreateOrthographicOffCenter(0, view.Width, view.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
projection = halfPixelOffset * projection; <code>

texture lightMask;
sampler mainSampler : register(s0);
sampler lightSampler = sampler_state{Texture = lightMask;};
float4x4 World;
float4x4 View;
float4x4 Projection;

struct vs2ps
{
float4 Pos : POSITION0;
float4 TexCd : TEXCOORD0;
float3 PosW : TEXCOORD1;
};

vs2ps VS(float4 Pos : POSITION0,float4 TexCd : TEXCOORD0)
{
vs2ps Out;
Out.Pos = mul(Pos, World*View*Projection);
Out.TexCd = TexCd;
Out.PosW = mul(Pos, World);
return Out;
}

float4 PixelShaderFunction(vs2ps input) : COLOR0
{ float2 texCoord = input.TexCd;
float4 screenPosition = (input.PosW,1.0f);
float4 lightColor = tex2D(lightSampler, texCoord);
float4 mainColor = tex2D(mainSampler, texCoord);
if(screenPosition.x < 3500)
{
return (mainColor * lightColor);
}
else return mainColor;
}


Viewing all articles
Browse latest Browse all 69

Trending Articles