XNA UK User Group

A helping hand for bedroom coders throughout the land.
in

RandomChaos

SkyBox Shader Fix

I have found my skybox shader has a bug in it, the shader displays the cubemap as a reflection, the following shader code will give a better result.

So, if you have used my skybox sample then replace the shader with this code:

Texture surfaceTexture;
samplerCUBE TextureSampler = sampler_state 
{ 
    texture = <surfaceTexture> ; 
    magfilter = LINEAR; 
    minfilter = LINEAR; 
    mipfilter = LINEAR; 
    AddressU = Mirror;
    AddressV = Mirror;
};

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

float3 EyePosition : CameraPosition;

struct VS_INPUT 
{
    float4 Position    : POSITION0;
    float3 Normal : NORMAL0;    
};

struct VS_OUTPUT 
{
    float4 Position    : POSITION0;
    float3 ViewDirection : TEXCOORD2;
        
};

float4 CubeMapLookup(float3 CubeTexcoord)
{    
    return texCUBE(TextureSampler, CubeTexcoord);
}

VS_OUTPUT Transform(VS_INPUT Input)
{
    float4x4 WorldViewProjection = mul(mul(World, View), Projection);
    float3 ObjectPosition = mul(Input.Position, World);
    
    VS_OUTPUT Output;
    Output.Position    = mul(Input.Position, WorldViewProjection);
    
    
    Output.ViewDirection = ObjectPosition - EyePosition;    
    
    return Output;
}

struct PS_INPUT 
{    
    float3 ViewDirection : TEXCOORD2;
};

float4 BasicShader(PS_INPUT Input) : COLOR0
{    
    float3 ViewDirection = normalize(Input.ViewDirection);    
    ViewDirection.x *= -1;
    return CubeMapLookup(ViewDirection);
}

technique BasicShader 
{
    pass P0
    {
        VertexShader = compile vs_2_0 Transform();
        PixelShader  = compile ps_2_0 BasicShader();
    }
}

Sorry for giving you a crappy shader in the first place :P

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About Nemo Krad

Have been a professional developer since 1995. My skill set ranges from C/C++, VB6,MSSQL, Java Script, VB Script, HTML/ASP, Java, C#, ASP.NET as well as others. I started 3D and games development when the first release of XNA came out in December 2006 and have become addicted to it.