Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,306 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Terrain Toon Shader with Shadow Map #152187
09/04/07 23:12
09/04/07 23:12
Joined: Apr 2006
Posts: 136
San Francisco
T
Tor Offline OP
Member
Tor  Offline OP
Member
T

Joined: Apr 2006
Posts: 136
San Francisco
Hey everyone, for my project we wanted our terrain to take advantage of sunlight so we decided to use the "toon" multitexture terrain shader. Then we ran into the problem with casting shadows from our buildings... so I added in a shadow map for my level builder. It just takes the alpha layer of the mtlSkin1 and applies it as a shadow.

It works the fastest with .dds files... I'm using dxt5 for my shader. Thought I'd put this back out since the original helped me out a ton!

Code:
  texture entSkin1;
texture entSkin2;
texture entSkin3;
texture entSkin4;

texture mtlSkin1;
texture mtlSkin2;

matrix matWorldViewProj;
matrix matWorld;
vector vecSunDir;
vector vecFog;
vector vecViewPos;

sampler multiTex1 = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

sampler multiTex2 = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

sampler multiTex3 = sampler_state
{
Texture = <entSkin3>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

sampler multiTex4 = sampler_state
{
Texture = <entSkin4>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};

sampler multiRes1 = sampler_state
{
Texture = <mtlSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Mirror;
AddressV = Mirror;
};

sampler toon_lookup = sampler_state
{
Texture = <mtlSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;

// clamp is important!
AddressU = Clamp;
AddressV = Clamp;
};

void mtoonVS(
in float4 inPosition : POSITION0,
in float4 inNormal : NORMAL0,
in float4 inTexcoord : TEXCOORD0,

out float4 outPosition : POSITION0,
out float4 outNormal : TEXCOORD0,
out float4 outLight : TEXCOORD1,
out float4 outTexcoord : TEXCOORD2,
out float outFog : FOG0)
{
outPosition = mul(inPosition, matWorldViewProj);
outNormal = mul(inNormal, matWorld);
outLight = -vecSunDir;
outTexcoord = inTexcoord;
outFog = 1 - (distance(inPosition, vecViewPos) - vecFog.x) * vecFog.z;
}

void mtoonPS(
in float4 inNormal : TEXCOORD0,
in float4 inLight : TEXCOORD1,
in float4 inTexcoord : TEXCOORD2,
out float4 outColor : COLOR0)
{
float4 Color2;
float lerpFactor;
inTexcoord *= 16;
outColor = tex2D(multiTex1, inTexcoord.xy);
Color2 = tex2D(multiTex2, inTexcoord.xy);
lerpFactor = tex2D(multiRes1, inTexcoord.xy/16).r;
outColor.rgb = lerp(outColor.rgb,Color2.rgb, lerpFactor);
Color2 = tex2D(multiTex3, inTexcoord.xy);
lerpFactor = tex2D(multiRes1, inTexcoord.xy/16).g;
outColor.rgb = lerp(outColor.rgb,Color2.rgb, lerpFactor);
Color2 = tex2D(multiTex4, inTexcoord.xy);
lerpFactor = tex2D(multiRes1, inTexcoord.xy/16).b;
outColor.rgb = lerp(outColor.rgb,Color2.rgb, lerpFactor);


// toon shade
float4 ToonShade = tex2D(toon_lookup, dot(inLight.xyz, inNormal.xyz));
lerpFactor = tex2D(multiRes1, inTexcoord.xy/16).a;
outColor.rgb *= ToonShade.rgb * lerpFactor;
}

technique toon
{
pass p0
{
VertexShader = compile vs_1_1 mtoonVS();
PixelShader = compile ps_2_0 mtoonPS();
}
}




"Towlie, you're the worst character ever." I know...
Re: Terrain Toon Shader with Shadow Map [Re: Tor] #152188
09/05/07 17:23
09/05/07 17:23
Joined: Dec 2000
Posts: 4,608
mk_1 Offline

Expert
mk_1  Offline

Expert

Joined: Dec 2000
Posts: 4,608
Thanks.
Maybe you can edit my effect in the wiki (add your new stuff in green or something and comment it as "add this if you need a shadow map").


Follow me on twitter
Re: Terrain Toon Shader with Shadow Map [Re: mk_1] #152189
09/05/07 23:10
09/05/07 23:10
Joined: Apr 2006
Posts: 136
San Francisco
T
Tor Offline OP
Member
Tor  Offline OP
Member
T

Joined: Apr 2006
Posts: 136
San Francisco
Yea, that might be a good idea. Myabe help out the younger guys. I definately learned quite a bit adding in the shadows. Cuz at first I was using a seperate texture to do it... and then I realized afte going through the code a few times that the alpha layer wasn't being used... so I could just use another layer instead of a whole texture map. Saveed some cycle.

All in all my approach went from a good chunk of code to two lines... LOL ^_^

Also, I noticed it went a few frames faster when I implicitiy set the texture division to 16 instead of the variable. Not sure on that tho, my tests weren't that specific... so, I could be wrong. ::shrug::

I'll work on putting on the wiki with a little "here's how..." bit.


"Towlie, you're the worst character ever." I know...
Re: Terrain Toon Shader with Shadow Map [Re: Tor] #152190
09/06/07 18:20
09/06/07 18:20
Joined: Mar 2006
Posts: 2,758
Antwerp,Belgium
frazzle Offline
Expert
frazzle  Offline
Expert

Joined: Mar 2006
Posts: 2,758
Antwerp,Belgium
Thanks indeed for the effort, the code structure looks nicely done but I wonder if you took in mind about the performance. Factorizing some parameters could lead into an overall frame rate increasing result

Cheers

Frazzle


Antec® Case
Intel® X58 Chipset
Intel® i7 975 Quad Core
8 GB RAM DDR3
SSD OCZ®-VERTEX2 3.5 x4 ; HD 600 GB
NVIDIA® GeForce GTX 295 Memory 1795GB
Re: Terrain Toon Shader with Shadow Map [Re: frazzle] #152191
09/06/07 22:35
09/06/07 22:35
Joined: Dec 2000
Posts: 4,608
mk_1 Offline

Expert
mk_1  Offline

Expert

Joined: Dec 2000
Posts: 4,608
Another thing you should change: division is far slower than a multiplication so maybe change that to * .125f or whatever value you need, should speed things up a little bit.


Follow me on twitter

Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1