1 registered members (AndrewAMD),
718
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: add an overlay to the toon shader
[Re: lostclimate]
#375601
06/26/11 19:04
06/26/11 19:04
|
Joined: Mar 2006
Posts: 2,252
Hummel
Expert
|
Expert
Joined: Mar 2006
Posts: 2,252
|
tangent space normal mapping added:
//==========================================//
//#define USE_NORMALMAPPING
#define USE_COLOR_INTENSITY_SHIFT
#define USE_COLOR_INTENSITY_MASK
const float max_colorintensity=4;
const float max_sunintensity=1.1;
const float min_sunintensity=0.1;
#define USE_OUTLINES
#define USE_PIXELSIZED_OUTLINES
//#define USE_COLORED_OUTLINES
const float outline_thickness=1.5;
const float3 outline_color=float3(0.5,0.6,0.8);//RGB in range 0..1 (instead of 0..255)
//#define USE_LIGHTING_ON_OUTLINES
#define USE_TEXTURED_OUTLINES
//#define USE_ALPHA_FOR_OUTLINES
const float texoutline_brightness=0.5;//0..darker..1..brighter..
const float texoutline_intensity=2;//0..less..1..more..
//==========================================//
bool AUTORELOAD;
float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matWorldView;
float4x4 matView;
float4 vecSunDir;
float4 vecSunColor;
float4 vecViewPort;
texture entSkin1;
texture entSkin3;
texture mtlSkin1;
sampler BUMP_SAMPLER = sampler_state { Texture = <entSkin3>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
sampler COLOR_SAMPLER = sampler_state { Texture = <entSkin1>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
sampler LGRAD_SAMPLER = sampler_state { Texture = <mtlSkin1>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
#ifdef USE_COLOR_INTENSITY_SHIFT
#ifdef USE_COLOR_INTENSITY_MASK
texture entSkin2;
sampler MASK_SAMPLER = sampler_state { Texture = <entSkin2>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
#endif
#endif
////////////////////////////////////////////
#ifndef USE_COLORED_OUTLINES
#undef USE_LIGHTING_ON_OUTLINES
#undef USE_TEXTURED_OUTLINES
#undef USE_ALPHA_FOR_OUTLINES
#endif
#ifdef USE_ALPHA_FOR_OUTLINES
#define USE_TEXTURED_OUTLINES
#endif
////////////////////////////////////////////
void VS(
in float4 vPos : POSITION,
in float3 normal : NORMAL,
in float2 InTex : TEXCOORD0,
#ifdef USE_NORMALMAPPING
in float4 InTangent : TEXCOORD2,
#endif
out float2 OutTex : TEXCOORD0,
#ifndef USE_NORMALMAPPING
out float3 outnormal : TEXCOORD1,
#else
out float3 SunDir : TEXCOORD1,
#endif
out float4 Pos : POSITION
)
{
#ifndef USE_NORMALMAPPING
outnormal=mul(normal,matWorld);
#else
float3x3 matTangent;
matTangent[0] = mul(InTangent.xyz,matWorld);
matTangent[1] = mul(cross(InTangent.xyz,normal),matWorld)*InTangent.w;
matTangent[2] = mul(normal,matWorld);
SunDir = normalize(mul( matTangent,vecSunDir.xyz));
#endif
Pos = mul(vPos,matWorldViewProj);
OutTex = InTex;
}
void PS(
in float2 Tex : TEXCOORD0,
#ifndef USE_NORMALMAPPING
in float3 normal : TEXCOORD1,
#else
in float3 SunDir : TEXCOORD1,
#endif
out float4 COL :COLOR0
)
{
#ifndef USE_NORMALMAPPING
normal = normalize(normal);
#else
vecSunDir.xyz = normalize(SunDir);
float3 normal = normalize(tex2D(BUMP_SAMPLER,Tex).xyz*2.f-1.f);
#endif
COL = tex2D(COLOR_SAMPLER,Tex);
// COL = tex2D(BUMP_SAMPLER,Tex);
float sun_blub=dot(vecSunDir,normal)*0.5f+0.5f;
float sun_diff=tex1D(LGRAD_SAMPLER,sun_blub);
sun_diff=lerp(min_sunintensity,max_sunintensity,sun_diff);
#ifdef USE_COLOR_INTENSITY_SHIFT
#ifdef USE_COLOR_INTENSITY_MASK
COL.rgb=lerp(COL.rgb,pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff))),tex2D(MASK_SAMPLER,Tex));
#else
COL.rgb=pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff)));
#endif
#endif
COL.rgb=COL.rgb*sun_diff;//*vecSunColor;
// COL.rgb=tex2D(MASK_SAMPLER,Tex);
}
#ifdef USE_OUTLINES
void VS_OUTLINES(
in float4 vPos : POSITION,
in float3 normal : NORMAL,
#ifdef USE_TEXTURED_OUTLINES
in float2 InTex : TEXCOORD0,
out float2 OutTex : TEXCOORD0,
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
out float3 outnormal : TEXCOORD1,
#endif
out float4 Pos : POSITION
)
{
#ifdef USE_PIXELSIZED_OUTLINES
float3 viewnormal=mul(normal,matWorldView);
Pos = mul(vPos,matWorldViewProj);
float2 sTex=(Pos.xy/Pos.w)*0.5f+0.5f;
sTex+=viewnormal.xy*outline_thickness*vecViewPort.zw;
Pos.xy=(sTex*Pos.w*2.f-Pos.w);
#else
Pos = mul(float4(vPos.xyz+normal*outline_thickness*0.2f,1),matWorldViewProj);
#endif
#ifdef USE_TEXTURED_OUTLINES
OutTex = InTex;
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
outnormal=mul(normal,matWorld);
#endif
}
#ifdef USE_COLORED_OUTLINES
void PS_OUTLINES(
#ifdef USE_TEXTURED_OUTLINES
in float2 Tex : TEXCOORD0,
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
in float3 normal : TEXCOORD1,
#endif
out float4 COL :COLOR0
)
{
COL.a=1;
#ifdef USE_TEXTURED_OUTLINES
COL = tex2D(COLOR_SAMPLER,Tex);
#else
COL.rgb=outline_color;
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
normal = normalize(normal);
float sun_blub=dot(vecSunDir,normal)*0.5f+0.5f;
float sun_diff=tex1D(LGRAD_SAMPLER,sun_blub);
sun_diff=lerp(min_sunintensity,max_sunintensity,sun_diff);
#ifdef USE_COLOR_INTENSITY_SHIFT
#if defined(USE_COLOR_INTENSITY_MASK) && defined(USE_TEXTURED_OUTLINES)
COL.rgb=lerp(COL.rgb,pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff))),tex2D(MASK_SAMPLER,Tex));
#else
COL.rgb=pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff)));
#endif
#endif
COL.rgb=COL.rgb*sun_diff;//*vecSunColor;
#endif
#ifdef USE_TEXTURED_OUTLINES
#ifndef USE_ALPHA_FOR_OUTLINES
COL.rgb=pow(COL.rgb,texoutline_intensity)*texoutline_brightness;
#endif
#endif
#ifndef USE_ALPHA_FOR_OUTLINES
COL.a=1;
#endif
}
#endif
#endif
technique Shading
{
pass P0
{
zenable=true;
zwriteenable=true;
alphablendenable=false;
alphatestenable=true;
AlphaRef=0;
AlphaFunc=Greater;
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
#ifdef USE_OUTLINES
pass Outlines
{
cullmode=cw;
VertexShader = compile vs_2_0 VS_OUTLINES();
#ifdef USE_ALPHA_FOR_OUTLINES
zenable=true;
zwriteenable=true;
alphablendenable=false;
alphatestenable=true;
AlphaRef=0;
AlphaFunc=Greater;
#endif
#ifdef USE_COLORED_OUTLINES
PixelShader = compile ps_2_0 PS_OUTLINES();
#endif
}
#endif
}
|
|
|
ToonSid Shader
[Re: lostclimate]
#375760
06/27/11 19:23
06/27/11 19:23
|
Joined: Mar 2006
Posts: 2,252
Hummel
Expert
|
Expert
Joined: Mar 2006
Posts: 2,252
|
To make it complete: object space normal mapping is now also implemented. Also you can use the second uv-set for the normalmaps if you need. Besides that I added a some #defines on the top which make it easier to declare which skins shall be used for the different textures. New shader:
//==========================================//
#define COLORMAP_TEXTURE entSkin1
#define NORMALMAP_TEXTURE entSkin2
#define INTENSMASK_TEXTURE entSkin3
#define LIGHTGRADIANT_TEXTURE mtlSkin1
//==========================================//
//#define USE_TS_NORMALMAPPING
#define USE_OS_NORMALMAPPING
#define USE_SECOND_UVSET_FOR_NORMALMAPPING
//===================//
#define USE_COLOR_INTENSITY_SHIFT
#define USE_COLOR_INTENSITY_MASK
const float max_colorintensity=4;
const float max_sunintensity=1.1;
const float min_sunintensity=0.1;
//===================//
#define USE_OUTLINES
#define USE_PIXELSIZED_OUTLINES
#define USE_COLORED_OUTLINES
const float outline_thickness=1.5;
const float3 outline_color=float3(0.5,0.6,0.8);//RGB in range 0..1 (instead of 0..255)
//===================// requires USE_COLORED_OUTLINES:
#define USE_LIGHTING_ON_OUTLINES
#define USE_TEXTURED_OUTLINES
//#define USE_ALPHA_FOR_OUTLINES
const float texoutline_brightness=0.8;//0..darker..1..brighter..
const float texoutline_intensity=2;//0..less..1..more..
//==========================================//
bool AUTORELOAD;
float4x4 matWorldViewProj;
float4x4 matWorld;
float4x4 matWorldView;
float4x4 matView;
float4 vecSunDir;
float4 vecSunColor;
float4 vecViewPort;
texture entSkin1;
texture entSkin2;
texture entSkin3;
texture entSkin4;
texture mtlSkin1;
texture mtlSkin2;
texture mtlSkin3;
texture mtlSkin4;
sampler COLOR_SAMPLER = sampler_state { Texture = <COLORMAP_TEXTURE>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
sampler LGRAD_SAMPLER = sampler_state { Texture = <LIGHTGRADIANT_TEXTURE>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
#if defined(USE_TS_NORMALMAPPING) || defined(USE_OS_NORMALMAPPING)
sampler BUMP_SAMPLER = sampler_state { Texture = <NORMALMAP_TEXTURE>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
#endif
#ifdef USE_COLOR_INTENSITY_SHIFT
#ifdef USE_COLOR_INTENSITY_MASK
sampler MASK_SAMPLER = sampler_state { Texture = <INTENSMASK_TEXTURE>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; };
#endif
#endif
////////////////////////////////////////////
#ifndef USE_COLORED_OUTLINES
#undef USE_LIGHTING_ON_OUTLINES
#undef USE_TEXTURED_OUTLINES
#undef USE_ALPHA_FOR_OUTLINES
#endif
#ifdef USE_ALPHA_FOR_OUTLINES
#define USE_TEXTURED_OUTLINES
#endif
#ifdef USE_TS_NORMALMAPPING
#undef USE_OS_NORMALMAPPING
#else
#ifdef USE_OS_NORMALMAPPING
#undef USE_TS_NORMALMAPPING
#endif
#endif
#if defined(USE_TS_NORMALMAPPING) || defined(USE_OS_NORMALMAPPING)
#define USE_NORMALMAPPING
#else
#undef USE_SECOND_UVSET_FOR_NORMALMAPPING
#endif
////////////////////////////////////////////
void VS(
in float4 vPos : POSITION,
in float3 normal : NORMAL,
in float2 InTex : TEXCOORD0,
#ifdef USE_SECOND_UVSET_FOR_NORMALMAPPING
in float2 InTex2 : TEXCOORD1,
#endif
#ifdef USE_TS_NORMALMAPPING
in float4 InTangent : TEXCOORD2,
#endif
out float2 OutTex : TEXCOORD0,
#ifdef USE_SECOND_UVSET_FOR_NORMALMAPPING
out float2 OutTex2 : TEXCOORD2,
#endif
#ifndef USE_NORMALMAPPING
out float3 outnormal : TEXCOORD1,
#else
#ifdef USE_TS_NORMALMAPPING
out float3 SunDir : TEXCOORD1,
#endif
#endif
out float4 Pos : POSITION
)
{
#ifndef USE_NORMALMAPPING
outnormal=mul(normal,matWorld);
#else
#ifdef USE_TS_NORMALMAPPING
float3x3 matTangent;
matTangent[0] = mul(InTangent.xyz,matWorld);
matTangent[1] = mul(cross(InTangent.xyz,normal),matWorld)*InTangent.w;
matTangent[2] = mul(normal,matWorld);
SunDir = normalize(mul( matTangent,vecSunDir.xyz));
#endif
#endif
Pos = mul(vPos,matWorldViewProj);
OutTex = InTex;
#ifdef USE_SECOND_UVSET_FOR_NORMALMAPPING
OutTex2 = InTex2;
#endif
}
void PS(
in float2 Tex : TEXCOORD0,
#ifdef USE_SECOND_UVSET_FOR_NORMALMAPPING
in float2 Tex2 : TEXCOORD2,
#endif
#ifndef USE_NORMALMAPPING
in float3 normal : TEXCOORD1,
#else
#ifdef USE_TS_NORMALMAPPING
in float3 SunDir : TEXCOORD1,
#endif
#endif
out float4 COL :COLOR0
)
{
#ifndef USE_NORMALMAPPING
normal = normalize(normal);
#else
#ifdef USE_TS_NORMALMAPPING
vecSunDir.xyz = normalize(SunDir);
#ifndef USE_SECOND_UVSET_FOR_NORMALMAPPING
float3 normal = normalize(tex2D(BUMP_SAMPLER,Tex).xyz*2.f-1.f);
#else
float3 normal = normalize(tex2D(BUMP_SAMPLER,Tex2).xyz*2.f-1.f);
#endif
#else
matWorld[3]=float4(0,0,0,1);
#ifndef USE_SECOND_UVSET_FOR_NORMALMAPPING
float3 normal=normalize(mul(float4(tex2D(BUMP_SAMPLER,Tex).xyz*2.f-1.f,0),matWorld)).xzy;
#else
float3 normal=normalize(mul(float4(tex2D(BUMP_SAMPLER,Tex2).xyz*2.f-1.f,0),matWorld)).xzy;
#endif
normal.z*=-1.f;
#endif
#endif
COL = tex2D(COLOR_SAMPLER,Tex);
float sun_blub=dot(vecSunDir,normal)*0.5f+0.5f;
float sun_diff=tex1D(LGRAD_SAMPLER,sun_blub);
sun_diff=lerp(min_sunintensity,max_sunintensity,sun_diff);
#ifdef USE_COLOR_INTENSITY_SHIFT
#ifdef USE_COLOR_INTENSITY_MASK
COL.rgb=lerp(COL.rgb,pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff))),tex2D(MASK_SAMPLER,Tex));
#else
COL.rgb=pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff)));
#endif
#endif
COL.rgb=COL.rgb*sun_diff;//*vecSunColor;
//COL=normal.y;
COL.a=1;
}
#ifdef USE_OUTLINES
void VS_OUTLINES(
in float4 vPos : POSITION,
in float3 normal : NORMAL,
#ifdef USE_TEXTURED_OUTLINES
in float2 InTex : TEXCOORD0,
out float2 OutTex : TEXCOORD0,
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
out float3 outnormal : TEXCOORD1,
#endif
out float4 Pos : POSITION
)
{
#ifdef USE_PIXELSIZED_OUTLINES
float3 viewnormal=mul(normal,matWorldView);
Pos = mul(vPos,matWorldViewProj);
float2 sTex=(Pos.xy/Pos.w)*0.5f+0.5f;
sTex+=viewnormal.xy*outline_thickness*vecViewPort.zw;
Pos.xy=(sTex*Pos.w*2.f-Pos.w);
#else
Pos = mul(float4(vPos.xyz+normal*outline_thickness*0.2f,1),matWorldViewProj);
#endif
#ifdef USE_TEXTURED_OUTLINES
OutTex = InTex;
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
outnormal=mul(normal,matWorld);
#endif
}
#ifdef USE_COLORED_OUTLINES
void PS_OUTLINES(
#ifdef USE_TEXTURED_OUTLINES
in float2 Tex : TEXCOORD0,
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
in float3 normal : TEXCOORD1,
#endif
out float4 COL :COLOR0
)
{
COL.a=1;
#ifdef USE_TEXTURED_OUTLINES
COL = tex2D(COLOR_SAMPLER,Tex);
#else
COL.rgb=outline_color;
#endif
#ifdef USE_LIGHTING_ON_OUTLINES
normal = normalize(normal);
float sun_blub=dot(vecSunDir,normal)*0.5f+0.5f;
float sun_diff=tex1D(LGRAD_SAMPLER,sun_blub);
sun_diff=lerp(min_sunintensity,max_sunintensity,sun_diff);
#ifdef USE_COLOR_INTENSITY_SHIFT
#if defined(USE_COLOR_INTENSITY_MASK) && defined(USE_TEXTURED_OUTLINES)
COL.rgb=lerp(COL.rgb,pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff))),tex2D(MASK_SAMPLER,Tex));
#else
COL.rgb=pow(COL.rgb,lerp(max_colorintensity,1,min(1,sun_diff)));
#endif
#endif
COL.rgb=COL.rgb*sun_diff;//*vecSunColor;
#endif
#ifdef USE_TEXTURED_OUTLINES
#ifndef USE_ALPHA_FOR_OUTLINES
COL.rgb=pow(COL.rgb,texoutline_intensity)*texoutline_brightness;
#endif
#endif
#ifndef USE_ALPHA_FOR_OUTLINES
COL.a=1;
#endif
}
#endif
#endif
technique Shading
{
pass P0
{
zenable=true;
zwriteenable=true;
alphablendenable=false;
alphatestenable=true;
AlphaRef=0;
AlphaFunc=Greater;
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
#ifdef USE_OUTLINES
pass Outlines
{
cullmode=cw;
VertexShader = compile vs_2_0 VS_OUTLINES();
#ifdef USE_ALPHA_FOR_OUTLINES
zenable=true;
zwriteenable=true;
alphablendenable=false;
alphatestenable=true;
AlphaRef=0;
AlphaFunc=Greater;
#endif
#ifdef USE_COLORED_OUTLINES
PixelShader = compile ps_2_0 PS_OUTLINES();
#endif
}
#endif
}
Updated the Download LinkHave fun
|
|
|
Re: ToonSid Shader
[Re: Hummel]
#375767
06/27/11 19:44
06/27/11 19:44
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
Up to the wiki with it, or some other more consistent place than this thread.
SHader is to good to be pushed to teh bottom of forumthreads
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: ToonSid Shader
[Re: Rackscha]
#376390
07/02/11 14:56
07/02/11 14:56
|
Joined: May 2006
Posts: 148 Latvia
MTD
Member
|
Member
Joined: May 2006
Posts: 148
Latvia
|
Very good, but somehow I cant run the test file. 1) It says that the downloaded file is corrupted. 2) Received compile error on mtlFX And the result is (GeForce 8800 GT):
|
|
|
Re: ToonSid Shader
[Re: MTD]
#376396
07/02/11 15:49
07/02/11 15:49
|
Joined: Mar 2006
Posts: 2,252
Hummel
Expert
|
Expert
Joined: Mar 2006
Posts: 2,252
|
It says that the downloaded file is corrupted. fixed.
|
|
|
|