Jump to content

  • Log In with Google      Sign In   
  • Create Account

Cel-Shading problem with backfaces seemingly being drawn on top of frontfaces.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 BlurEffect   Members   -  Reputation: 163

Like
0Likes
Like

Posted 19 March 2014 - 04:44 PM

Hey guys,
 
I'm having an issue with the Cel Shader I implemented recently for my DirectX 11 application. It all worked fine but then I additionally implemented hardware instancing and now something seems to be messed up and I can't really wrap my head around it. I'm doing two rendering passes with the first one drawing only the backfaces of objects and exteing the vertices along their normals. The second pass normally renders the frontfaces that now have black edges around their silhouette due to the first pass. I found this technique in a paper, so it's definitely a way to do it. It seems, however, that depending on the position from which you look onto the objects parts of the backfaces are now drawn in front of the frontfaces. Maybe I'm just messed something up with the normals in the shader when I had to alter some stuff to make the instancing work. Maybe one of you guys has an idea what the problem is. I'm posting the code for my shaders below and also a few screenshots of the problem.
 
Vertex Shader for colouring/lighting the models (used for the frontfaces)
cbuffer cbPerFrame
{
	row_major float4x4 ViewProjection;
};

struct VS_INPUT
{
	// vertex data
	float4 Position	: POSITION;
	float3 Normal	: NORMAL;
	float2 TexCoord : TEXCOORD;

	// instance data
	row_major float4x4 World  : WORLD;   
	float4			Colour : COLOUR;
};

struct VS_OUTPUT
{
	float4 Position	: SV_POSITION;
	float4 Colour	: COLOUR;
	float3 Normal	  : NORMAL;
	float2 TexCoord : TEXCOORD;
};

VS_OUTPUT VS( VS_INPUT input )
{
    VS_OUTPUT output;

	// transfrom vertex position
       output.Position = mul( input.Position, ( mul( input.World, ViewProjection) ) );
	
	// transform normal to world space and normalize the normal vector
	output.Normal = mul( input.Normal, input.World );
	output.Normal = normalize( output.Normal );

	// forward texture coordinate unchanged
        output.TexCoord = input.TexCoord;

	// forward object colour
	output.Colour = input.Colour;

    return output;
}

The corresponding pixel shader

// describes a diffuse directional light source
cbuffer LightBuffer
{
	float4 diffuseColour;
        float3 lightDirection;
	float  padding;			
};

struct PS_INPUT
{
	float4 Position	: SV_POSITION;
	float4 Colour	: COLOUR;
	float3 Normal	: NORMAL;
	float2 TexCoord : TEXCOORD;
};

// this texture is used to determine the tone in which to shade the pixel
Texture2D ToneTexture;
SamplerState ToneSamplerState;

float4 PS( PS_INPUT input ) : SV_TARGET
{
    float4 finalColour;
    float  lightIntensity;


    // Calculate the amount of light on this pixel. (Saturate clamps the value to be between 0 and 1)
    lightIntensity = saturate( dot( input.Normal, normalize( -lightDirection ) ) );

    float4 shade = ToneTexture.Sample( ToneSamplerState, float2( lightIntensity, 0 ) );

    // Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
    finalColour = saturate( diffuseColour * shade );

    // Multiply the texture pixel and the final diffuse color to get the final pixel color result.
    finalColour = finalColour * input.Colour;

    return finalColour;

}

The following two shaders are used to render the backfaces only using an edge colour.

The vertex shader:

cbuffer cbPerFrame
{
	row_major float4x4 ViewProjection;
};

struct VS_INPUT
{
	// vertex data
	float4 Position	: POSITION;
	float3 Normal	: NORMAL;
	float2 TexCoord : TEXCOORD;

	// instance data
	row_major float4x4 World  : WORLD;   
	float4	 Colour : COLOUR;
};

struct VS_OUTPUT
{
	float4 Position	: SV_POSITION;
	float4 Colour	: COLOUR;
	float3 Normal	: NORMAL;
	float2 TexCoord : TEXCOORD;
};

VS_OUTPUT VS( VS_INPUT input )
{
    VS_OUTPUT output;

	// transfrom vertex position
       output.Position = mul( input.Position,( mul( (input.World), ViewProjection) ) );
	
	// transform normal to world space and normalize the normal vector
	output.Normal = mul( input.Normal, input.World );
	output.Normal = normalize( output.Normal );

	// forward texture coordinate unchanged
       output.TexCoord = input.TexCoord;

	// forward object colour
	output.Colour = float4(0,0,0,1);

        // extend backfaces along the vertex normals
	output.Position = output.Position + ( mul( 0.1f, float4( output.Normal.x, output.Normal.y, output.Normal.z, 0 ) ) );

        return output;
}

And again, the corresponding pixel shader:

struct PS_INPUT
{
	float4 Position	: SV_POSITION;
	float4 Colour	: COLOUR;
	float3 Normal	: NORMAL;
	float2 TexCoord : TEXCOORD;
};

float4 PS(PS_INPUT input) : SV_TARGET
{
    return input.Colour;
}

This is how stuff looks when the camera looks at it from the front, everything is fine.

However, as the camera is moved to the side or rotated and looks at the objects from the side or back, more and more parts of the objects seem to be overdrawn with the backfaces.
View from the side:
And from the back:
 
If you need more information or code just ask and I will provide it. I've been stuck with this issue for a while now and can't spot the source of the problem. Any help is appreciated, even if it's just an idea of what might go wrong with my rendering. Thank you very much in advance.

 



Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 14244

Like
1Likes
Like

Posted 19 March 2014 - 05:48 PM

Offset the position by the normal before multiplying it by any matrices.

And then only multiply it by the WorldViewProjection matrix.  Any compound matrix multiples done in a shader are things that can be precomputed offline.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 BlurEffect   Members   -  Reputation: 163

Like
0Likes
Like

Posted 20 March 2014 - 02:19 AM

Wow, that did the trick. Thanks a lot! It's a bit weird, because I think the shader worked fine with offsetting the vertices at the end before I switched to hardware instancing. About what you said about the matrices: Isn't it more expensive to calculate the WorldViewProjection for every object in the regular C++ code instead of doing that on the GPU in the schaders?

As I said, I'm using hardware instancing and I'm streaming in the world matrices of the objects with the vertex input. As the ViewProjection is the same for every object during a frame, I update it via a constant buffer.



#4 hdxpete   Members   -  Reputation: 488

Like
0Likes
Like

Posted 20 March 2014 - 03:58 AM

while the actual calculation of three matrixs M * V * P may take a shorter amount of time on the GPU then the CPU. typically the CPU is a frame or two ahead of the GPU anyway. Also moving any calculations from the gpu to c++ with make your shaders faster meaning you can draw more pixels per second meaning you have a higher fps.

 

for shaders i follow the KISS method.



#5 L. Spiro   Crossbones+   -  Reputation: 14244

Like
0Likes
Like

Posted 20 March 2014 - 05:00 AM

Since you are using instancing you are correct that you need to have the world matrix one its own.

 

Otherwise, expanding on the above, keep in mind that a vertex shader is called for every primitive.  Calculating a WorldViewProjection matrix once for each object on the CPU is still often faster than once-per-primitive on the GPU, obviously depending on how many primitives in a call.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS