• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Telanor

Having problems getting soft particles to work

9 posts in this topic

I've been trying to implement soft particles as described in the nvidia paper. I'm having a problem with this part: "If we want to compare consistent depth values, the fetched value Zbuf needs to be transformed into projection space". I'm using log-depth, so I can't use the formula they're using. They also never explain what any of the variables in the equation are or why the comparison needs to be done in projection-space and not "Normalized device coordinate" space.

I know how to reconstruct a world-space position from a log-depth value from a previous topic, so I tried several different ways to get it to work for the soft particles.

For reference, this is the equation for the reconstruction:
float depthVal = DepthMap.Sample(depthSampler, texCoord).r; 
depthVal = (pow(0.001 * FarPlane + 1, depthVal) - 1) / 0.001;
depthVal /= (1 * FarPlane) / (FarPlane - 1); //The 1 is the near plane value

float2 invProjPos = mul(input.ScreenPosition.xy * depthVal, InverseProjection);
float4 position = mul(float4(invProjPos, -depthVal, 1), InverseView);

position /= position.w;
So I figured to get the projection-space value, the equation should be just the first 3 lines. Next, I figured the particle's Z value should be calculated like this (from the vertex shader):
float4 worldPosition = mul(float4(input.Position, 1), instanceTransform);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);

output.Position.z = log(0.001 * output.Position.z + 1) / log(0.001 * NearFar.y + 1) * output.Position.w;
output.Z = output.Position.z;
Now I know the value output to the depth buffer will be divided by the W normally, so I also tried changing the last line to:

output.Z = output.Position.z / output.Position.w;

In either case, the result is the same: no fading what-so-ever. The particles still have their hard edges. I'm not really sure what I'm doing wrong here and was hoping someone could point it out.

Full shader code:
[spoiler]
const float SoftParticleContrast = 2.0;
const float SoftParticleScale = 1;
const float zEpsilon = 0.0;

float4x4 View;
float4x4 Projection;
float4 NearFar; //x = near, y = far, z = far * near, w = far - near

Texture2D Texture;
Texture2D<float> Depth;

SamplerState Sampler
{
    Filter = MIN_MAG_MIP_LINEAR;
};

struct VertexShaderInput
{
	float3 Position		: POSITION0;
	float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
	float4 Position		: SV_POSITION;
	float4 Color			: COLOR0;
	float2 TextureCoordinate	: TEXCOORD0;
	float  Z				: TEXCOORD1;
};

struct PixelShaderOutput
{
	float4 Color	: SV_TARGET0;
};

float Contrast(float Input, float ContrastPower)
{
	//piecewise contrast function
	bool IsAboveHalf = Input > 0.5 ;
	float ToRaise = saturate(2*(IsAboveHalf ? 1-Input : Input));
	float Output = 0.5*pow(ToRaise, ContrastPower); 

	Output = IsAboveHalf ? 1-Output : Output;

	return Output;
}

// Vertex shader helper function shared between the two techniques.
VertexShaderOutput VertexShaderCommon(VertexShaderInput input, float4x4 instanceTransform, float4 color, float4 rect)
{
	VertexShaderOutput output;

	// Apply the world and camera matrices to compute the output position.
	float4 worldPosition = mul(float4(input.Position, 1), instanceTransform);
	float4 viewPosition = mul(worldPosition, View);
	output.Position = mul(viewPosition, Projection);

	output.Position.z = log(0.001 * output.Position.z + 1) / log(0.001 * NearFar.y + 1) * output.Position.w;
	//output.Position.z = log(output.Position.z / 0.001) / log(FarPlane / 0.001) * output.Position.w;

	output.TextureCoordinate = input.TextureCoordinate * rect.zw + rect.xy;
	output.Color = color;
	output.Z = output.Position.z / output.Position.w;
	
	return output;
}

// Hardware instancing reads the per-instance world transform from a secondary vertex stream.
VertexShaderOutput HardwareInstancingVertexShader(VertexShaderInput input, float4x4 instanceTransform : BLENDWEIGHT, float4 color : COLOR, float4 rect : RECT)
{
	return VertexShaderCommon(input, transpose(instanceTransform), color, rect);
}

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
	PixelShaderOutput output;

	clip(input.Color.a <= 0 ? -1 : 1);

	float depthVal = Depth.Load(int4(input.Position.xy, 0, 0));
	depthVal = (pow(0.001 * NearFar.y + 1, depthVal) - 1) / 0.001;
	depthVal /= NearFar.z / NearFar.w;
	float zdiff = (depthVal - input.Z);
	float c = Contrast(zdiff * SoftParticleScale, SoftParticleContrast);

	if( c * zdiff <= zEpsilon )
	{
		discard;
	}
	//output.Color = float4(depthVal * 100, 0, 0, 1);
	output.Color = Texture.Sample(Sampler, input.TextureCoordinate) * input.Color * c;

	return output;
}

technique11 HardwareInstancing
{
	pass Pass1
	{
		SetVertexShader(CompileShader(vs_4_0, HardwareInstancingVertexShader()));
		SetPixelShader(CompileShader(ps_4_0, PixelShaderFunction()));
	}
}
[/spoiler]
1

Share this post


Link to post
Share on other sites

As long as you use some kind of linear z distribution it should not matter in which space you are. Best to use either camera/eye space or projection space, which ever is easier in your engine. In this case world space is not really useful, in general it is not really useful for billboard/particle/projection handling. I think that you will already align your particles to the camera, therefor you particle will most likely use a constant depth. Use this depth (make it lineare if neccessary) to compare it with the (linear) camera depth of your gbuffer.

1

Share this post


Link to post
Share on other sites
Well, as far as I can tell, that is what I'm doing. The gbuffer value I'm reading in should be converted to projection-space and compared with the projection-space particle depth. I must have done something wrong though, because the particles look exactly the same as before; there's no fade-out near the intersection area. Edited by Telanor
1

Share this post


Link to post
Share on other sites

Have you stepped through the shader to see where your values are going wrong?

0

Share this post


Link to post
Share on other sites
Ok so I ran through the shader a few times. It seems that input.Z is in the range of 0-1 (which is what the nvidia contrast function seems to expect) and depthVal wasn't, so I removed these two lines:
depthVal = (pow(0.001 * NearFar.y + 1, depthVal) - 1) / 0.001;
depthVal /= NearFar.z / NearFar.w;
Now they're both in the 0-1 range... but all the particles are invisible. Stepping through one of the pixels came out with the following values:

depthVal = 0.210246200
input.Z = 0.184789200
zdiff = 0.025457070
c = 0.001296125

My only guess at this issue is that maybe my Z values aren't linear, but I don't know how to fix that if that's the case.
0

Share this post


Link to post
Share on other sites

What is your blending code ?

Here's an example:

depthVal = normalized and linear depth value of g-buffer, 1 is farest
z = current depth of the pixel, normalized and linear

zdiff = depthVal-z;
alpha_blend_factor = smoothstep(0.0, 0.1, zdiff); //<-- play around with the 0.1 value

final_alpha_blend_factor = alpha_blend_factor * particle_color.a;


1

Share this post


Link to post
Share on other sites
Sorry for not replying sooner, somehow I didn't get a notification email. I'm using the same blending function from the nvidia sample:

float Contrast(float Input, float ContrastPower)
{
	//piecewise contrast function
	bool IsAboveHalf = Input > 0.5 ;
	float ToRaise = saturate(2*(IsAboveHalf ? 1-Input : Input));
	float Output = 0.5*pow(ToRaise, ContrastPower); 

	Output = IsAboveHalf ? 1-Output : Output;

	return Output;
}
The spoiler tag in the OP has the full shader code
1

Share this post


Link to post
Share on other sites

Double check your zdiff and depth reconstruction implementation. Btw. why are you using a log-depth buffer, why don't use a linear one ? Nevertheless, the blending seems ok, even if your example pixel will be rendered, the very low c = 0.001296125 value will make it more or less invisible. Use testcode first, until you see the particle squares (like the uncommented output.Color = float4(depthVal * 100, 0, 0, 1); ), then try to add some smooth blending.

0

Share this post


Link to post
Share on other sites
Well the reconstruction code looks fine to me but I'm particularly bad at this kind of math, so even if I stare at it for hours it's going to look fine to me with my basic understanding.

We're using a log depth because a) we didn't want all the artifacts associated with the standard z/w buffer and b) it doesn't require disabling early-z. While I probably could switch, it'd be a bit of a pain, and I think it could work with log depth with the right math.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0