Accessing Z coordinates

Hi,
im trying to achieve a shader that makes any texture that is in front of the player see-through.
but i do not understand how to access the Z coor of any pixel that is processed by my shader to determine if it has to be see through or not.
from what i understood is that float4 has 4 indizes but i can only access the 1st and 2nd. any index beyond that generates errors while compiling.
I started with editing the basic shader that the content pipeline gives you when you created a new 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

Texture2D SpriteTexture;
sampler s0;
sampler2D SpriteTextureSampler = sampler_state {
	Texture = <SpriteTexture>;
};
struct VertexShaderOutput {
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
	float2 TextureCoordinates : TEXCOORD0;
};
float4 MainPS(VertexShaderOutput input) : COLOR {
	float4 color = tex2D(s0, input.TextureCoordinates);
	int dx = input.Position.x - 200;
	int dy = input.Position.y - 200;
	float dist = dx * dx + dy * dy;
	if (dist < 10000) {
		discard;
	}
	return color;
}
technique SpriteDrawing {
	pass P0 {
		PixelShader = compile PS_SHADERMODEL MainPS();
	}
};

when i assign input.Position.z or input.Position[2] to an int or float it still compiles and runs.
but when i try to use that variable for anything then it doesnt compile anymore without any error messages.
(i use 2D but use Z to give my tiles on my tilemap the ability to be above or below the player and other objects.)

There is no z-component in the Pixelshader, as the PixelShader works on the backbuffer and that’s a 2D surface. The PS isn’t called at all, when the z-check fails and you have depthbuffer checks enabled. you could basically handover the z-alue from VS to PS in its own variable (not SV_POSITION) to have it interpolated in the PS, but that may not necessarily result in what you expect.

To handle transparency, you first render all opaque objects.

when this is done you render all transparent objects - otherwise the transparent objects would fill the depthbuffer and everything “behind” it wouldn’t be rendered at all.

1 Like

Thanks that helped.
I guess ill have to change my plan to a simple check if the player is near or not. (without the shader)
and then use a mask on those tiles.

As soon as transparency is involved, you’ll have to pre-sort and render the (transparent) objects (so handling transparency is its own part in the rendering pipeline) anway (as you can’t use the depthbuffer, you have to render them from back to forth manually)

making an object “see trough” would be then as easy as a distance check for all objects and set them to transparaent or not I guess.

Looks like this is handled,

Just popping by to say Hi @N_K, Welcome to the Forums!

Happy Coding!