HLSL - PS.1.1 Terrain Shader

Posted By: Steempipe

HLSL - PS.1.1 Terrain Shader - 11/03/04 06:46

Alright... Sometimes it is just nice to put something here when the forum quiets down

You might ask, why go thru the trouble of converting an already good ps.1.1 terrain shader... maybe just to see if I could (thats how we learn):) But, probably more so because it will be easier to incorporate great things to it without having to deal with the cryptic ASM code.
Easier to take it from ps.1.1 to ps.1.4 and beyond.

Your immediate mission: Add lighting and fog... J/K. But it could give some good shader excercises.
-Somewhere in here is a thread "HLSL Envmapping" where you can grab fog code and see how I crammed it in there.
-lighting is a piece of cake, too.

The code is copied/pasted from my FX file. If you're not sure on how to load the FX file, then look around in the Tutorials Forum for a Tut that I put there.... Or look in the manual.
If you cannot figure out how to get the blendmaps in the A and the B channels and do not have PSP or PS... look in the Tutorial Forum for a Terrain Masking Tutorial I made and it will have info on using free "Imagemagick" to do that.


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

MultiTex.fx

A simple example of using HLSL to multitexture.

-Requires pixelshader.1.1 and DX9.00c
-Lighting and Fog is not worked in.

This should give a "jumping off" point for someone
who wants to convert it to ps.1.4 and ps.2.0, to
allow more textures in the pass. As well as to
explore some things like lighting.

By: Eric Hendrickson-Lambert (AKA Steempipe)

11/2/04: Implemented.

If used, credit is nice... these things take time.

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

float4x4 matWorldViewProj;



//Texture entSkin1 -- Mine has a big skin, not needed here.
Texture entSkin2; // Grass Texture in RGB
Texture entSkin3; // Sand Texture in RGB
Texture entSkin4; // Rock Texture in RGB
Texture mtlSkin1; // Blendmaps in B and in A


sampler s_GrassTex = sampler_state
{
Texture = <entSkin2>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler s_SandTex = sampler_state
{
Texture = <entSkin3>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};

sampler s_RockTex = sampler_state
{
Texture = <entSkin4>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};


sampler s_BlendTex = sampler_state
{
Texture = <mtlSkin1>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
AddressU = Wrap;
Addressv = Wrap;
};


struct MULTITEX_VS_OUT // Output to the pixelshader fragment
{
float4 Pos : POSITION;
float2 GrassTex : TEXCOORD0;
float2 SandTex : TEXCOORD1;
float2 RockTex : TEXCOORD2;
float2 BlendTex : TEXCOORD3;
};



MULTITEX_VS_OUT Multi_VS(float4 inPos : POSITION,
float3 inNormal : NORMAL,
float2 inTex1 : TEXCOORD0,
float2 inTex2 : TEXCOORD1,
float2 inTex3 : TEXCOORD2,
float2 inTex4 : TEXCOORD3)
{
MULTITEX_VS_OUT Out;

//////////////////////////////////////////////////////////////////////
// Just the bare minimum needed...
// Take the vertex position of an object and transpose it
// to screen space.

Out.Pos = mul(inPos, matWorldViewProj);

Out.GrassTex = inTex2.xy * 15; // UV Scale -- Could be vecSkill41
Out.SandTex = inTex2.xy * 15; // UV Scale -- Could be vecSkill41
Out.RockTex = inTex2.xy * 15; // UV Scale -- Could be vecSkill41
Out.BlendTex = inTex1.xy;

return Out;
}


float4 Multi_PS(MULTITEX_VS_OUT IN): COLOR
{

//////////////////////////////////////////////////////////////////////
// Lerp the RockTex and GrassTex
// using the blendmap in the A channel of BlendTex

float4 Tex1_Color1 = tex2D(s_RockTex, IN.RockTex);
float4 Tex1_Color2 = tex2D(s_GrassTex, IN.GrassTex);
float4 Tex1_Color3 = tex2D(s_BlendTex, IN.BlendTex).a;
float4 Tex1_BlendOp = lerp(Tex1_Color1, Tex1_Color2, Tex1_Color3);

//////////////////////////////////////////////////////////////////////
// Lerp the SandTex with result from Rock/GrassTex
// using the blendmap in the B channel of BlendTex

float4 Tex2_Color1 = tex2D(s_SandTex, IN.SandTex);
float4 Tex2_Color2 = tex2D(s_BlendTex, IN.BlendTex).b;

//////////////////////////////////////////////////////////////////////
// Assemble the final blend

float4 Final_Output = lerp(Tex1_BlendOp, Tex2_Color1, Tex2_Color2);

//////////////////////////////////////////////////////////////////////
// Could add a detailmap... could live in the A channel of someone.

//float4 Detailmap = tex2D(s_RockTex, IN.RockTex);
//float4 Final_Output_Detailmap = Tex2_BlendOp + Detailmap - 0.5;

//////////////////////////////////////////////////////////////////////
// Send to the framebuffer

return Final_Output; // return Final_Output_Detailmap
// if using that portion on the
// effect.
}



technique multitex_fx
{
pass One
{
sampler[0] = (s_GrassTex);
sampler[1] = (s_SandTex);
sampler[2] = (s_RockTex);
sampler[3] = (s_BlendTex);

VertexShader = compile vs_1_1 Multi_VS();
PixelShader = compile ps_1_1 Multi_PS();
}
}


////////////// End of FX
Posted By: Ahriman

Re: HLSL - PS.1.1 Terrain Shader - 11/03/04 09:35

Just started getting used asm coding, now I have to learn HLSL . Thank you very much Eric I appreciate it. This should atleast point me in the right direction for all of this.
Posted By: Steempipe

Re: HLSL - PS.1.1 Terrain Shader - 11/03/04 10:19

Your welcome. I look forward to seeing screenshots of effects that you program in the future. ASM, HLSL..... fun stuff.

Thanks,
Eric
Posted By: William

Re: HLSL - PS.1.1 Terrain Shader - 11/03/04 11:25

This is great! Thanks Steempipe.
Posted By: Ahriman

Re: HLSL - PS.1.1 Terrain Shader - 11/05/04 20:34

Quick question for you Eric, I am sure this is possible but I want to make sure. Would it be possible instead of using an Alpha for the texture placement, using vertex height? So that you could then use PS.1.1 and use the 4 textures on the terrain.
Posted By: Steempipe

Re: HLSL - PS.1.1 Terrain Shader - 11/06/04 07:14

Interesting idea and we should explore that. Also, I am kicking around another thought... kind of forming it little by little. Thanks for the suggestion.

William: No problem, man.

Eric
© 2024 lite-C Forums