Screenspace Decal

i tried, don’t work. im not sure is my reconstruct wordposition wrong or not. Bcoz i was try to reconstruct wordposition from linear depth buffer

What makes me scratch my head is
clip(float3(0.5, 0.5, 0.5) - abs(oPosition.xyz));

Would not it be better with
clip(0.5 - length(oPosition.xyz));
?

I’ve never seen clip using float3 while it accepts it.

It think he uses linear depth too.

i just follow the code from this article
http://martindevans.me/game-development/2015/02/27/Drawing-Stuff-On-Other-Stuff-With-Deferred-Screenspace-Decals/

He does

//Perform bounds check
clip(0.5 - abs(objectPosition.xyz));

I dunno how hlsl handles mixing a float with a float3 ? If in object space, length shoud be ok ?

tried, still same…

After reading the tutorial, it seems to be ok as the box is one unit large. It does prevent your box to be drawn if > 0.5 as intended

It’s a box, so you want abs(). Length would work for circles.

is my reconstruct world position from linear depth correct?

no, you forgot to convert to object space

//Convert from world space to object space float4 objectPosition = mul(float4(worldPosition, 1), InvWorld);

After the inverseview right? Actually that my mistake, but after added this line still same. Have u try?

yeah I’ve got it working with no modifications

I have updated the deferred engine to work with that, but i streamlined some stuff.

This is the code now

float4 DecalPixelShader(VertexShaderOutput input) : SV_TARGET
{
    //read depth, use point sample or load
    float depth = DepthMap.Load(int3(input.Position.xy, 0)).r;

	//Basically extend the depth of this ray to the end of the far plane, this gives us the position of the sphere only
	float3 cameraDirVS = input.PositionVS.xyz * (FarClip / -input.PositionVS.z);

    //compute ViewSpace position
	float3 positionVS = depth * cameraDirVS;

	//Transform to box space
	float3 positionOS = mul(float4(positionVS, 1), InverseWorldView).xyz;

	clip(1 - abs(positionOS.xyz));

	float2 textureCoordinate = (positionOS.xy + 1) / 2;

	return DecalMap.Sample(AnisotropicSampler, textureCoordinate);
}

InverseWorldView is inverseView * InverseWorld

is the lineardepth?

InverseWorldView is inverseView * InverseWorld
is same as
inverseWorldView = inverse(view * world)

yes linear depth, and no they are not the same

1 Like

thanks, i will take a look on ur code

Check if you maybe want to project to the xz plane. I remember that I had to switch y with z somewhere. Good luck!

@kosmonautgames
Did it work also with the normals? I only got the diffuse part working…

I didn’t bother with normals yet, maybe if i find an appropriate texture.

Right now i manipulate only albedo, no roughness/metalness/mattype etc.

Normal blending (for alpha < 1) is a very interesting topic though, maybe worth exploring.

Yes, I also think it is interesting. Also the use case for footsteps mentioned in a blog somewhere, icefallgames or something like that.

weird… i try to render the texture show at right. i have textureCoordinate issue i had follow all ur code, but dun know why i have this issue.

Sry my mistake, I got it. it actually projection to z direction

Had the same thing ^^
Thats what I meant flipping y and z. I also tried to project to the “floor”. So the “wall” projection did not work out with the original code.

Is there a simple example project of this working by chance?