XNA Game Development Forums
2012/05/21 19:11:33 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: BasicShader + Textures - but How?  (Read 2845 times)
inbreed
Newbie
*
Offline Offline

Posts: 23


Beer! more beer!


View Profile
« on: 2007/04/29 13:26:03 »

Hi,

i Really Like the MH Shaders so far, but what i see is:

Effects - OR  - Texture.

What i would like to habe is

Effects - AND - Texture

But i really dont know where to start.

Do i have to apply 2 shaders each draw(), or 1 huge one?

if 1 shader: i absolutely dont know how to mix them, because of the rifferent return values, and my googleskills seem to be killed..

is there any easy way?
thanks
Logged
spectralfreak
Newbie
*
Offline Offline

Posts: 2


View Profile
« Reply #1 on: 2007/04/29 14:01:56 »

Merging effect + texture is actually pretty easy.

Just add this code:

//Add to the top of the shader
Texture tex;
sampler TextureSampler = sampler_state
{
   texture=<tex>;
   magfilter = LINEAR;
   minfilter = LINEAR;
   mipfilter = LINEAR;
   AddressU = mirror;
   AddressV = mirror;
};

In the pixel shader, instead of returning the light color, put it into its own float4

float4 LightColor = saturate(TotalAmbient + TotalDiffuse + TotalSpecular);

then add this line
float4 TexColor = tex2D(TextureSampler, VIn.TextureCoord); //Grabs the pixel color from the texture

//VIn is what I called the output from the vertex shader, so you might want to use that instead.

Then, you sum the colors together and return

return saturate(LightColor+TexColor);

Any objects that use this need to either set the "Tex" parameter, or just set GraphicsDevice.Textures[0] to whatever texture you're using.




Logged
inbreed
Newbie
*
Offline Offline

Posts: 23


Beer! more beer!


View Profile
« Reply #2 on: 2007/04/29 21:24:41 »

hooray thankyou!!! Smiley
Logged
spectralfreak
Newbie
*
Offline Offline

Posts: 2


View Profile
« Reply #3 on: 2007/04/30 07:21:19 »

You're welcome Smiley.
Logged
dirtysteve
Newbie
*
Offline Offline

Posts: 1


View Profile
« Reply #4 on: 2010/01/06 15:04:55 »

Would it be possible for one of you guys to post the modified shader in full?
Logged
Mikeske
Newbie
*
Offline Offline

Posts: 33


View Profile
« Reply #5 on: 2010/01/28 11:43:11 »

putting my 2 cents in:

spectralfreak... I could be mistaking but don't you mean
Quote
return saturate(LightColor*TexColor);
also, the calculations of LightColor should be
Code:
float4 LightingColor = float4(saturate(TotalAmbient + TotalDiffuse + TotalSpecular),1);
but I'm no expert on this matter, perhaps our Gurucould shed some light on this matter?  Cool

Code:
float4x4 World;
float4x4 View;
float4x4 Projection;

sampler TextureSampler;

float3 AmbientColor;
float3 EyePosition;
float3 DiffuseColor;
float3 LightDirection;
float3 LightDiffuseColor;
float SpecularPower;
float3 LightSpecularColor;

struct VS_INPUT
{
    float4 Position : POSITION0;
    float2 Texcoord : TEXCOORD0;
    float4 Color : COLOR0;
    float3 Normal : NORMAL0;   
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
float4 Color : COLOR0;
float3 Normal : TEXCOORD1;
float3 ViewDirection : TEXCOORD2;
};

VS_OUTPUT Transform(VS_INPUT Input)
{
    VS_OUTPUT Output;

    float4 worldPosition = mul(Input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    Output.Position = mul(viewPosition, Projection);

    Output.Texcoord = Input.Texcoord;
    Output.Color = Input.Color;
    Output.Normal = mul(Input.Normal, World);
    Output.ViewDirection = EyePosition - worldPosition;

    return Output;
}

struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
float4 Color : COLOR0;
float3 Normal : TEXCOORD1;
float3 ViewDirection : TEXCOORD2;
};

float4 BasicShader(PS_INPUT Input) : COLOR0
{
float3 Normal = normalize(Input.Normal);
float3 ViewDirection = normalize(Input.ViewDirection);
LightDirection = normalize(-LightDirection);

float EdgeComponent = dot(Normal, ViewDirection);
float3 TotalAmbient = saturate(AmbientColor * EdgeComponent);

// Diffuse Calculations
float NDotL = dot(Normal, LightDirection);
float3 DiffuseAverage = (DiffuseColor + LightDiffuseColor) * 0.5f;
float3 TotalDiffuse = saturate(DiffuseAverage * NDotL);

// Specular Calculations
float3 Reflection = normalize(2.0f * Normal * NDotL - LightDirection);
float RDotV = max(0.0f, dot(Reflection, ViewDirection));
float3 TotalSpecular = saturate(LightSpecularColor * pow(RDotV, SpecularPower));

float4 TextureColor = Input.Color.rgba * tex2D(TextureSampler, Input.Texcoord);
float4 LightingColor = float4(saturate(TotalAmbient + TotalDiffuse + TotalSpecular),1);
return saturate(TextureColor*LightingColor);
}

technique TransformColorTexture
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 Transform();
        PixelShader = compile ps_2_0 BasicShader();
    }
}
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Nice smooth textures Hazy Mind XNA Engine sajmons 2 2224 Last post 2007/02/22 05:16:24
by sajmons
Question about loading textures General Discussion nicknz 2 1962 Last post 2007/04/23 05:36:17
by EclipsE
modified basicshader.fx to support textures Hazy Mind XNA Engine Quadgnim 7 2549 Last post 2007/09/20 03:53:14
by Quadgnim
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.074 seconds with 18 queries.