Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Best splatting terrain and shader  (Read 774 times)
nomadsoul
Customers
Community Member
*****
Posts: 173


« on: January 26, 2012, 09:24:09 AM »

Hello,

I have a probleme with my splatting shader ! I have created a splatting shader .fx that work great in Truevision. I want to use it better. It work like that :

     - The shader has 3 texture of rock and grass, the splatting work with a blend map in 3 different colors, each color is attribued for 1 texture ! For example the red color of the blendmap is for grass, the green for rock....ok ?   To work better i want that the BlendMap i use is already apply on the object not set in the shader.
Do you know how i can obtain and work in the shader with the texture which is already applied on my mesh ?     Thanks
this is my shader:
Code:
float4x4 matWorldViewProj : WorldViewProjection;
float4x4 WorldXf : World ;
float4x4 WorldViewIXf : WorldViewInverse ;
texture  baseTexturet : Diffuse

<
string ResourceName = "bottom.JPG";
>;
texture  firstTexturet : Diffuse
<
string ResourceName = "dirtandgrass.jpg";
>;
texture  secondTexturet : Diffuse
<
string ResourceName = "roadfull.jpg";
>;
texture  thirdTexturet : Diffuse
<
string ResourceName = "stone1.jpg";

>;
texture  blendMapTexturet : Diffuse
<
string ResourceName = "ss.png";
>;
sampler baseTexture = sampler_state
{
    texture = <baseTexturet>;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler FirstTexture = sampler_state
{
    texture = <firstTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler  secondTexture = sampler_state
{
    texture = <secondTexturet>;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler thirdTexture = sampler_state
{
    texture = <thirdTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler  blendMapTexture = sampler_state
{
    texture = <blendMapTexturet>;
    AddressU  = CLAMP;       
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};








struct VS_INPUT
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
 float3 Normal : NORMAL; //in object space   
    float3 Tangent : TANGENT0; //in object space
    float3 Binormal : BINORMAL0; //in object space
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
 //third row of the 3x3 transform from tangent to cube space
};

//Next, we create Vertex Shader:

VS_OUTPUT VS(VS_INPUT IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;

OUT.Position = OUT.Position + mul(IN.Position , matWorldViewProj);
OUT.TexCoords = IN.TexCoords;
 
 
return OUT;
}

//Then a Pixel Shader:

float4 PS(VS_OUTPUT IN) : COLOR0
{
float4 baseColor = tex2D(baseTexture, IN.TexCoords.xy*8);

float4 firstColor = tex2D(FirstTexture, IN.TexCoords.xy *20);
float4 secondColor = tex2D(secondTexture, IN.TexCoords.xy*40 );
float4 thirdColor = tex2D(thirdTexture, IN.TexCoords.xy*40);
float batchSize= 1;
float4 blendMap = tex2D(blendMapTexture, IN.TexCoords.xy /= batchSize);

float4 result =  lerp(baseColor, firstColor, blendMap.r);
result =  lerp(result, secondColor, blendMap.g);
result =  lerp(result, thirdColor, blendMap.b);

return result;
}

technique Terrain
{
pass one
{
AlphaBlendEnable = true;
AlphaTestEnable = true;

ColorOp[0] = SelectArg1;
ColorArg1[0] = Texture;
ColorOp[1] = Disable;

PixelShader = compile ps_2_0 PS();
VertexShader = compile vs_1_1 VS();
}
}


Logged
nomadsoul
Customers
Community Member
*****
Posts: 173


« Reply #1 on: January 28, 2012, 06:19:41 AM »

Nobody can help me?  Cry
ok i found a solution and it nearly work, the splatting is ok but the Basetexture is not render ( normaly this texture must appear when the blendmap is black):
Code:
float4x4 matWorldViewProj : WorldViewProjection;
float4x4 WorldXf : World ;
float4x4 WorldViewIXf : WorldViewInverse ;
texture  baseTexturet : Diffuse
<
string ResourceName = "bottom.JPG";
>;
texture  firstTexturet : Diffuse
<
string ResourceName = "dirtandgrass.jpg";
>;
texture  secondTexturet : Diffuse
<
string ResourceName = "roadfull.jpg";
>;
texture  thirdTexturet : Diffuse
<
string ResourceName = "stone1.jpg";

>;



sampler baseTexture = sampler_state
{
    texture = <baseTexturet>;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler FirstTexture = sampler_state
{
    texture = <firstTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler  secondTexture = sampler_state
{
    texture = <secondTexturet>;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler thirdTexture = sampler_state
{
    texture = <thirdTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
uniform sampler2D  blendMapTexture;









struct VS_INPUT
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
 float3 Normal : NORMAL; //in object space   
    float3 Tangent : TANGENT0; //in object space
    float3 Binormal : BINORMAL0; //in object space
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
 //third row of the 3x3 transform from tangent to cube space
};

//Next, we create Vertex Shader:

VS_OUTPUT VS(VS_INPUT IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;

OUT.Position = OUT.Position + mul(IN.Position , matWorldViewProj);
OUT.TexCoords = IN.TexCoords;
 
 
return OUT;
}

//Then a Pixel Shader:

float4 PS(VS_OUTPUT IN) : COLOR0
{
float4 baseColor = tex2D(baseTexture, IN.TexCoords.xy);

float4 firstColor = tex2D(FirstTexture, IN.TexCoords.xy*5 );
float4 secondColor = tex2D(secondTexture, IN.TexCoords.xy*5 );
float4 thirdColor = tex2D(thirdTexture, IN.TexCoords.xy*5);
float batchSize= 1;
float4 blendMap = tex2D(blendMapTexture, IN.TexCoords.xy /= batchSize);

float4 result =  lerp( baseColor, firstColor, blendMap.r);
result= lerp(result,secondColor,blendMap.g);
result= lerp(result,thirdColor,blendMap.b);

return  (firstColor * result.r + secondColor * result.g + thirdColor * result.b);

}

technique Terrain
{
pass one
{
AlphaBlendEnable = true;
AlphaTestEnable = true;

ColorOp[0] = SelectArg1;
ColorArg1[0] = Texture;
ColorOp[1] = Disable;

PixelShader = compile ps_2_0 PS();
VertexShader = compile vs_1_1 VS();
}
}


I would like my game work with this, splatting for grass, rock...If someone can solve it, thanks
Logged
jviper
Community Member
*
Posts: 2130

Discipline in training


« Reply #2 on: January 28, 2012, 04:34:32 PM »

You have the baseTexture using the "Diffuse" semantic. TV3D uses TEXTURE0, TEXTURE1, TEXTURE2, etc...... Try that changes anything. If it works, you could also change the semantics you used for the other textures firstTexture : TEXTURE1; secondTexture : TEXTURE2; thirdTexture : TEXTURE3;
Logged

JAbstract.....Don't just imagine, make it happen!
nomadsoul
Customers
Community Member
*****
Posts: 173


« Reply #3 on: January 30, 2012, 09:01:53 AM »



 Grin  final result of splatting rock with bump in one shader

If you want it in your game:

Code:
//Nomadsoul's shader
//found shader help on nvidia shader and truevision3d forum

float4x4 World: World;
float4x4 View: View;
float4x4 Projection: WorldViewProjection;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

float4x4 WorldInverseTranspose: WorldInverseTranspose;

float3 DiffuseLightDirection = float3(0.2,5,-1);
float4 DiffuseColor = float4(1,1, 1, 1);
float DiffuseIntensity = 0.0;

float Shininess = 200;
float4 SpecularColor = float4(1, 1, 1, 1);   
float SpecularIntensity = 10;
float3 ViewVector = float3(1, 0, 0);

texture ModelTexture
<
string ResourceName = "roadfull.jpg";
>;
sampler2D textureSampler = sampler_state {
    Texture = (ModelTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

float BumpConstant = 1;
texture NormalMap
<
string ResourceName = "normalmap1.png";
>;
sampler2D bumpSampler = sampler_state {
    Texture = (NormalMap);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
    float3 Tangent : TANGENT0;
    float3 Binormal : BINORMAL0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TextureCoordinate : TEXCOORD0;
    float3 Normal : TEXCOORD1;
    float3 Tangent : TEXCOORD2;
    float3 Binormal : TEXCOORD3;
};

//splatttin
texture  baseTexturet
<
string ResourceName = "stone1.jpg";
>;
texture  firstTexturet : diffuse
<
string ResourceName = "dirtandgrass.jpg";
>;
texture  secondTexturet : diffuse
<
string ResourceName = "assorted_rock.jpg";
>;
texture  thirdTexturet : diffuse
<
string ResourceName = "seashell-rock.jpg";

>;
sampler2D baseTexture = sampler_state
{
    texture = <baseTexturet>;
 
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};
sampler FirstTexture = sampler_state
{
    texture = <firstTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler  secondTexture = sampler_state
{
    texture = <secondTexturet>;
 
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
sampler thirdTexture = sampler_state
{
    texture = <thirdTexturet>;
   
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};
uniform sampler2D  blendMapTexture;


VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output= (VertexShaderOutput)0;

   // float4 worldPosition = mul(input.Position, World);
     //normalement float3 worldPosition = mul(input.Position, World).xyz;
   // float4 viewPosition = mul(worldPosition, View);
   // normalement float3 viewPosition = View[3].xyz;
    output.Position = output.Position + mul( input.Position, Projection);
   //normalement output.Position = mul( float4(worldPosition.xyz , 1.0) , Projection);
    output.Normal = normalize(mul(input.Normal, WorldInverseTranspose));
    output.Tangent = normalize(mul(input.Tangent, WorldInverseTranspose));
    output.Binormal = normalize(mul(input.Binormal, WorldInverseTranspose));

    output.TextureCoordinate = input.TextureCoordinate;
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    // Calculate the normal, including the information in the bump map
    float3 bump = BumpConstant * (tex2D(bumpSampler, input.TextureCoordinate) - (0.5, 0.5, 0.5));
    float3 bumpNormal = input.Normal + (bump.x * input.Tangent + bump.y * input.Binormal);
    bumpNormal = normalize(bumpNormal);

    // Calculate the diffuse light component with the bump map normal
    float diffuseIntensity = dot(normalize(DiffuseLightDirection), bumpNormal);
    if(diffuseIntensity < 0)
        diffuseIntensity = 0.01;

    // Calculate the specular light component with the bump map normal
    float3 light = normalize(DiffuseLightDirection);
    float3 r = normalize(2* dot(light, bumpNormal) * bumpNormal+light);
 
    float3 v = normalize(mul(normalize(ViewVector), World));
    float dotProduct = dot(r, v);

    float4 specular = SpecularIntensity * SpecularColor * max(pow(dotProduct, Shininess), 0) * diffuseIntensity;

    // Calculate the texture color
    float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
    textureColor.a = 1;
   float4 BumpColor= textureColor* (diffuseIntensity) + AmbientColor * AmbientIntensity + specular;
//splattinnnnng
float4 baseColor = tex2D(baseTexture, input.TextureCoordinate.xy);
float4 firstColor = tex2D(FirstTexture, input.TextureCoordinate.xy*5 );
float4 secondColor = tex2D(secondTexture, input.TextureCoordinate.xy*5 );
float4 thirdColor = tex2D(thirdTexture,input.TextureCoordinate.xy*5);
float batchSize= 1;
float4 blendMap = tex2D(blendMapTexture, input.TextureCoordinate.xy /= batchSize);

float4 result =  lerp(BumpColor ,firstColor, blendMap.r);
result= lerp(result,secondColor,blendMap.g);
result= lerp(result,thirdColor,blendMap.b);
//end splatting






    // Combine all of these values into one (including the ambient light)
   //normalement return (textureColor * (diffuseIntensity) + AmbientColor * AmbientIntensity + specular);
 return (firstColor * result.r + secondColor * result.g + thirdColor * result.b);
// return firstColor  * result.r;
}

technique BumpMapped
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
Logged
arnienet
Customers
Community Member
*****
Posts: 263


WWW
« Reply #4 on: January 31, 2012, 09:37:47 PM »

Looks interesting, thanks for sharing the code. The texture res looks a bit low, what sort of performance are you getting on which GFX card? Have you tried with higher res textures? Have you tried different light angles to show off the bump-mapping yet? Nice work there.
Logged

Total Dev time = 50% to code, 50% to test, 50% to find errors, 50% to fix, that's why it takes twice as long.

Dawn World MMO
nomadsoul
Customers
Community Member
*****
Posts: 173


« Reply #5 on: February 01, 2012, 06:36:23 AM »

Thanks, yes the res is low , my GFX card is an ATI radeon 1600 256 mo ddr2 i thinks( too old now but support shader 3). unfortunelty my shader doesn't work on tv_land ! yes i have tried different light angle and it work fine. Now i want to apply the bump effect only on one texture is splatting not for all and i don't know how to do that. The bump is best looking when it render on rock not on grass .

ps: sorry for my bad english Grin
Logged
nomadsoul
Customers
Community Member
*****
Posts: 173


« Reply #6 on: February 03, 2012, 04:23:02 PM »

 Grin it work ! bump mapping appear only on rock texture not on grass


In my shader change the code by the following:
Code:
float4 result =  lerp(baseColor ,firstColor, blendMap.r);
result= lerp(result,secondColor,blendMap.g);
result= lerp(result,thirdColor,blendMap.b);
//end splatting
//Bump Special texture=
float4 bumpresultfirst=firstColor*(BumpColor+0.6);
bumpresultfirst.a=BumpColor.a;
float4 bumpresultsecond=secondColor*(BumpColor+0.2);
bumpresultsecond.a=BumpColor.a;
float4 bumpresultThird=thirdColor*(BumpColor+0.2);
bumpresultThird.a=BumpColor.a;


    // Combine all of these values into one (including the ambient light)
   //normalement return (textureColor * (diffuseIntensity) + AmbientColor * AmbientIntensity + specular);
 return (firstColor * result.r + bumpresultsecond * result.g + bumpresultThird * result.b);
// return firstColor  * result.r;
}
Logged
arnienet
Customers
Community Member
*****
Posts: 263


WWW
« Reply #7 on: February 04, 2012, 03:35:53 PM »

Nice one, looking better with the higher res textures too  Cool.

I was thinking (it's not clear right now), are your rocks seperate to the lanscape, or part of the landscape?
« Last Edit: February 04, 2012, 03:46:56 PM by arnienet » Logged

Total Dev time = 50% to code, 50% to test, 50% to find errors, 50% to fix, that's why it takes twice as long.

Dawn World MMO
nomadsoul
Customers
Community Member
*****
Posts: 173


« Reply #8 on: February 18, 2012, 07:28:27 AM »

Normaly the rock it seperate, it is just to see how it work ! i created a another one with grass,ground and non-real time reflective puddle of water.  Grin
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks