• 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
BlurEffect

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

4 posts in this topic

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.

[sharedmedia=core:attachments:20521]
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:
[sharedmedia=core:attachments:20522]
And from the back:
[sharedmedia=core:attachments:20523]
 
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.

 

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

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