Halftone Shader

I am trying to achieve a specific black and white halftone effect using shaders, but I am very new to both shaderlab and Cg. I’ve been working through all of the examples I can find in an attempt to get a better understanding of how to accomplish this.

I found an example post-process image effect written in CgFX, which if I understand it correctly is sort of a container for Cg shaders:
http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html#post_halftone_dots

I could use some assistance stripping out the useful functions and omitting the rest. Ideally I would like to utilize this effect as a shader, and not a post-process effect, but either would be a great result to have and to share with the community.

Above is how the end result should appear, and below is the CgFX code provided by NVIDIA. Most of my issues with understanding it is syntactical. I’m not sure what areas of code are redundant or there for simply accommodating older hardware, and I have no idea what is going on under the “techniques” section.

Is it possible to port this type of effect to vertex and fragment functions, or will I be stuck attempting to recreate this as a post-process image effect?

/*********************************************************************NVMH3****
*******************************************************************************
$Revision: #3 $

Copyright NVIDIA Corporation 2008
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

% Radial-dot B&W halftones applied to the underlying scene. The dots are
% pre-calculated and are fetched, according to the desired intensity,
% from a small volume texture.

keywords: image_processing pattern 



To learn more about shading, shaders, and to bounce ideas off other shader
    authors and users, visit the NVIDIA Shader Library Forums at:

    [url]http://developer.nvidia.com/forums/[/url]

*******************************************************************************
******************************************************************************/

/*****************************************************************/
/*** HOST APPLICATION IDENTIFIERS ********************************/
/*** Potentially predefined by varying host environments *********/
/*****************************************************************/

// #define _XSI_		/* predefined when running in XSI */

#ifndef FXCOMPOSER_VERSION	/* for very old versions */
#define FXCOMPOSER_VERSION 180
#endif /* FXCOMPOSER_VERSION */

// #define FLIP_TEXTURE_Y	/* Different in OpenGL  DirectX */

/*****************************************************************/
/*** EFFECT-SPECIFIC CODE BEGINS HERE ****************************/
/*****************************************************************/

//
// #define this macro to permit reading of COLLADA-FX 
//   shared-surface render targets
//
// #define SHARED_BG_IMAGE

#include "include/Quad.cgh"

#define DOTS_PER_BIT 8.0
#define IMG_DIVS 4.0

float Script : STANDARDSGLOBAL <
    string UIWidget = "none";
    string ScriptClass = "scene";
    string ScriptOrder = "postprocess";
    string ScriptOutput = "color";
    string Script = "Technique=Main;";
> = 0.8;

// color and depth used for full-screen clears

float4 gClearColor <
    string UIWidget = "Color";
    string UIName = "Background";
> = {0,0,0,0};

float gClearDepth <string UIWidget = "none";> = 1.0;

///////////////////////////////////////////////////////////
/////////////////////////////////////// Textures //////////
///////////////////////////////////////////////////////////

texture gDotTex  <
    string UIName = "Dot Pattern";
    string Type = "3D";
    string ResourceName = "HalftoneDots16x16.dds";
>;

sampler3D gDotSampler = sampler_state 
{
    Texture = <gDotTex>;
    WrapS  = Repeat;        
    WrapT  = Repeat;
    WrapR  = ClampToEdge;
    MinFilter = LinearMipMapLinear;
    MagFilter = Linear;
};

//////////// Note that these are SIZED render targets! /////

#define DOWNSIZE (1.0/IMG_DIVS)

DECLARE_SIZED_QUAD_TEX(gSceneTexture,gSceneSampler,"A8R8G8B8",DOWNSIZE)
DECLARE_SIZED_QUAD_DEPTH_BUFFER(DepthBuffer,"D24S8",DOWNSIZE)

//////////////////////////////////////////////////////
/////////////////////////////////// pixel shader /////
//////////////////////////////////////////////////////

float4 tonePS(QuadVertexOutput IN,
		    uniform sampler2D SceneSampler,
		    uniform sampler3D DotSampler
) : COLOR {
    QUAD_REAL4 scnC = tex2D(SceneSampler,IN.UV);
    float lum = dot(float3(.2,.7,.1),scnC.xyz);
    float3 lx = float3((DOTS_PER_BIT*IMG_DIVS*IN.UV.xy),1-lum);
    QUAD_REAL4 dotC = tex3D(DotSampler,lx);
    return float4(dotC.xyz,1.0);
}

///////////////////////////////////////
/// TECHNIQUES ////////////////////////
///////////////////////////////////////

technique Main < string Script =
#ifndef SHARED_BG_IMAGE
    "RenderColorTarget0=gSceneTexture;"
    "RenderDepthStencilTarget=DepthBuffer;"
    "ClearSetColor=gClearColor;"
    "ClearSetDepth=gClearDepth;"
	"Clear=Color;"
	"Clear=Depth;"
    "ScriptExternal=Color;" // calls all "previous" techniques  materials
    "Pass=PostP0;";
#else /* defined(SHARED_BG_IMAGE)  - no nead to create one, COLLADA has done it for us  */
    "ClearSetColor=gClearColor;"
    "ClearSetDepth=gClearDepth;"
	"Clear=Color;"
	"Clear=Depth;"
    "Pass=PostP0;";
#endif /* SHARED_BG_IMAGE */
> {
    pass PostP0 < string Script =
	"RenderColorTarget0=;"
	"RenderDepthStencilTarget=;"
	"Draw=Buffer;";
    > {
	VertexProgram = compile vp40 ScreenQuadVS2(QuadTexelOffsets);
		DepthTestEnable = false;
		DepthMask = false;
		BlendEnable = false;
		CullFaceEnable = false;
		DepthFunc = LEqual;
	FragmentProgram = compile fp40 tonePS(gSceneSampler,gDotSampler);
    }
}

//////////////////////////////////////////////
/////////////////////////////////////// eof //
//////////////////////////////////////////////

[/code]

Did you make any headway with this as I too am very interested in learning how to code shaders.

Regards,
Matt.

Unfortunately not yet. I am thinking it might make more sense to begin with a cel-shader (not the default toon shader, as it does NOT do the same thing) so I can get a better sense of how the lighting works in shaders.

Cel-shading and halftoning are the two shader effects specifically requested for this project, so I really need to make them happen.

I made one. This is a rather excellent tutorial, and with not too much work you can adapt it to Cg.

http://webstaff.itn.liu.se/~stegu/webglshadertutorial/shadertutorial.html

Here is a sample project that uses a halftone camera effect shader with CGA4 color palette.