sampling a texture according to a projected position

Started by
1 comment, last by Matt_Aufderheide 3 years, 8 months ago

I'm trying to draw camera-facing sprites in my scene using only a pixel shader and one screen aligned quad. I want to draw all the sprites in the scene, given 3d world position data. I basically have it working, but there is something not quite right in my coordinates…i will show some code (HLSL)..the issue is I'm not sure the scaling is done right, and the y positioning seems a bit off (all the sprites textures are 64*64 and scaled to a specific value, they are also all supposed to be at the same y position…

float2 aspect = float2(1.0f, screenres.x / screenres.y);
	float2 texcoord = In.Tex * 2 - 1;
	texcoord.x *= -1; 
	float spritescale = 1;

	for (int i = 0; i < numsprites; i++)
	{
		spritescale = 64 * spriteData[i].z;
		float4 spritecoord = float4(spriteData[i].x, spritescale/2, spriteData[i].y, 1.0f);
		spritecoord = mul(matView, spritecoord);
		spritecoord = mul(matProj, spritecoord);
		spritecoord.xy /= spritecoord.w;

		if (spritecoord.z < 1.0f){continue;}
 //skip if behind viewer

		spritecoord.xy = (spritecoord.xy/aspect) + (texcoord / aspect);

		float4 sprite=spritetex.SampleLevel(standard_samp, spritecoord.xy * (spritecoord.w / spritescale), 0);
		color.rgb = lerp(color.rgb, sprite.rgb, sprite.a);
	}
Advertisement
got it working correctly.. here is my new code if anyone cares:
	float2 aspect = float2(1.0f, screenres.x / screenres.y);
	float2 texcoord = In.Tex * 2 - 1;
	texcoord.x *= -1; 
	float spritescale = 1;

	for (int i = 0; i < spritenum; i++)
	{
		spritescale = 64 * spriteData[i].z;
		float4 spritecoord = float4(spriteData[i].x, spritescale/1.25f, spriteData[i].y, 1.0f);

		float mydist = mul(mul(matProj, matView), spritecoord.xyz).z;//length(vecViewPos.xyz - spritecoord.xyz);
		if (mydist > zbuffer){continue;}
			
		spritecoord = mul(matView, spritecoord);
		spritecoord = mul(matProj, spritecoord);
		spritecoord.xy /= spritecoord.w;
		
		if (spritecoord.z < 1.0f){continue;}

		spritecoord.xy = (spritecoord.xy/aspect) + (texcoord / aspect);

		float4 sprite=spritetex.SampleLevel(standard_samp, (spritecoord.xy+float2(16.0f/ spritecoord.w,0)) * (spritecoord.w / spritescale), 0);
		if (sprite.a > 0) { zbuffer = mydist; wpos = float3(spriteData[i].x, spritescale / 1.25f, spriteData[i].y); }
		color.rgb = lerp(color.rgb, sprite.rgb, sprite.a);
	}

This topic is closed to new replies.

Advertisement