Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: reflective plane shader.  (Read 1042 times)
chrizb2
Customers
Community Member
*****
Posts: 67


« on: October 30, 2007, 11:39:45 AM »

Hi,

We are struggeling with shading a gameboard. The idea is of course that you simply apply a shader to reflect the pieces on the board. We found this shader:

Code:

//////////////////////////////////////////////////////////////////
//DEFINES //
//////////////////////////////////////////////////////////////////
float4x4 WORLDIT : WorldInverseTranspose;
float4x4 WVP : WorldViewProjection;
float4x4 WORLD : World;
float4x4 VIEWI : ViewInverse;


//////////////////////////////////////////////////////////////////
//CONSTANTS //
//////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
//TWEAKABLES //
//////////////////////////////////////////////////////////////////
float bumpStrength = 0.5f; //Max = 1, strength of normal map


//////////////////////////////////////////////////////////////////
//SAMPLERS //
//////////////////////////////////////////////////////////////////
texture EnvironmentMap : TEXTURE0;
samplerCUBE EnvironmentMapSampler = sampler_state
{
Texture = <EnvironmentMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

Texture NormalMap : TEXTURE1;
sampler SampNormalMap = sampler_state {

Texture = <NormalMap>;

};


//////////////////////////////////////////////////////////////////
//STRUCTS //
//////////////////////////////////////////////////////////////////

struct vIN {
    float4 Position : POSITION;
    float4 TexCoord : TEXCOORD0;
    float3 tangent : TANGENT;
    float3 normal : NORMAL;
    float3 binormal : BINORMAL;
};


struct vOUT {
    float4 HPosition: POSITION;
    float2 TexCoord : TEXCOORD0;
    float3 WorldView: TEXCOORD2;
    float3 normal : TEXCOORD3;
    float3 binormal : TEXCOORD4;
    float3 tangent : TEXCOORD5;
};


//////////////////////////////////////////////////////////////////
//VERTEX SHADER //
//////////////////////////////////////////////////////////////////

vOUT mainVS(in vIN IN) {
    vOUT OUT;
   
    float3 Pw = mul(IN.Position, WORLD).xyz;
   
    OUT.TexCoord = IN.TexCoord;
   
    OUT.WorldView = VIEWI[3].xyz - Pw; //Funky way to get viewvec, but it works (normal way inverts view)
    OUT.HPosition = mul(IN.Position, WVP);

OUT.tangent = IN.tangent;
OUT.binormal = IN.binormal;
OUT.normal = IN.normal;

    return OUT;
}


//////////////////////////////////////////////////////////////////
//PIXEL SHADER //
//////////////////////////////////////////////////////////////////

float4 mainPS(in vOUT IN) : COLOR
{

float3 normal = (tex2D(SampNormalMap, IN.TexCoord) * 2 - 1);

//------------------------------------------------------------
float3 tan = normalize(IN.tangent); //Must make sure we convert normal from tangent space to world space
float3 bino = normalize(IN.binormal); //in order to match worldview. Took me 2 days to figure that out =(
float3 no = normalize(IN.normal);

float3x3 matInverse = transpose(float3x3(tan, bino, no));

   normal = mul(mul(normal, matInverse), WORLD);
//------------------------------------------------------------

float3 N = normalize(normal)* bumpStrength;
float3 V = normalize(IN.WorldView);

float3 nV = -V;
   

float3 R = reflect(nV, N); //Reflection vector
float4 reflColor = texCUBE(EnvironmentMapSampler, R);

   reflColor.a  = 1.0f;
   
return reflColor;

}


//////////////////////////////////////////////////////////////////
//TECHNIQUES //
//////////////////////////////////////////////////////////////////
technique main {
pass p0 {
        VertexShader = compile vs_2_0 mainVS();
        PixelShader = compile ps_2_0 mainPS();
}
}

The issue with it is of course that it doesn't work with a plane, and that it seems to reflect the wrong side of the board (ie it would need to be mirrored.

Any suggestions on how this could work with a plane?

Thanks in advance for any comments.

Logged

Looks can be deceiving
visit http://www.phpchess.com
WEst
Community Member
*
Posts: 813

Daniel Martinek


WWW
« Reply #1 on: October 30, 2007, 12:01:18 PM »

You can simply use TVs built in water effect to do this. Simply but no (or a flat normalmap) into the normal map slot and you have a perfect reflection. Ofcourse this is only a fake, but it's possible.
Logged

Greetings

Daniel Martinek
Lead Programmer
Roaming Nova Studios
Lyrical
Customers
Community Member
*****
Posts: 461


WWW
« Reply #2 on: October 31, 2007, 01:36:17 PM »

How would i go about creating a shader to add reflection onto a car body.
would this be in a simlar way?

chears.
Logged
WEst
Community Member
*
Posts: 813

Daniel Martinek


WWW
« Reply #3 on: October 31, 2007, 02:12:35 PM »

If you have a car body, a shader with a cubemapped reflection would be appropiate, like the one posted above, or like the ones supplied by nVidia or ATI in their Shader Authoring Tools.

But you would have to render your world to a cubemapped rendersurface in TV3D and send it to the shader.
Logged

Greetings

Daniel Martinek
Lead Programmer
Roaming Nova Studios
newborn
Customers
Community Member
*****
Posts: 2437


WWW
« Reply #4 on: October 31, 2007, 02:57:30 PM »

But you would have to render your world to a cubemapped rendersurface in TV3D and send it to the shader.

ie: heavy to render and good luck having it working. may I suggest using prerendered texture instead of computing a cubemap every loop?
Logged

Lyrical
Customers
Community Member
*****
Posts: 461


WWW
« Reply #5 on: October 31, 2007, 08:12:43 PM »

Thanks i will give it a go.
Logged
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #6 on: November 01, 2007, 12:35:08 PM »

it's not that bad if a)you use a low-res cubemap (which you can usually get away with), and b)you don't render every face every frame, but only 1 or 2 faces, and gradually update the whole cubemap.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
chrizb2
Customers
Community Member
*****
Posts: 67


« Reply #7 on: November 16, 2007, 04:03:50 PM »

Thanks...it worked well with the water reflection.

code looks now like this:
Code:
        #region TVGraphicsFXFunctions

        public static bool GraphicsFXSetReflection(int nMeshID, float sNegAltitude, int nRSIndex)
        {
            if (dicRenderSurfaces.ContainsKey(nRSIndex) == false) return false;
            TV_GraphicsFX.SetWaterReflection(TV_Global.GetMeshFromID(nMeshID), dicRenderSurfaces[nRSIndex],
                    null, 0, new TV_PLANE(new TV_3DVECTOR(0, 1, 0), sNegAltitude));
            return true;
        }

        public static bool GraphicsFXSetWater(int nMeshID, int nReflectRSIndex, int nRefractRSIndex, int nStyle, TV_PLANE waterPlane)
        {
            if (dicRenderSurfaces.ContainsKey(nReflectRSIndex) && dicRenderSurfaces.ContainsKey(nRefractRSIndex))
            {
                TV_GraphicsFX.SetWaterReflection(TV_Global.GetMeshFromID(nMeshID), dicRenderSurfaces[ nReflectRSIndex],
                    dicRenderSurfaces[ nRefractRSIndex], nStyle, waterPlane);
                return true;
            }
            return false;
        }

        // Set reflection animation
        public static void GraphicsFXSetWaterAnimation(int nMeshID, bool fDirectional, float sDirectionSpeedX, float sDirectionSpeedZ)
        {
            TV_GraphicsFX.SetWaterReflectionBumpAnimation(TV_Global.GetMeshFromID(nMeshID), fDirectional, sDirectionSpeedX, sDirectionSpeedZ);
        }
        // Set colour for water
        public static void GraphicsFXSetWaterReflectionColor(int nMeshID, TV_COLOR reflectTint, TV_COLOR refractTint, float sCustomFresnel)
        {
            TV_GraphicsFX.SetWaterReflectionColor(TV_Global.GetMeshFromID(nMeshID), reflectTint, refractTint, sCustomFresnel);
        }


        #endregion

Thanks West Smiley
Logged

Looks can be deceiving
visit http://www.phpchess.com
Pages: [1]
  Print  
 
Jump to:  

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