Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: What's wrong with this shader?  (Read 1071 times)
MenDAKE
Community Member
*
Posts: 402


« on: October 14, 2007, 08:34:31 AM »

I'm trying to use the "metal" shader from Nvidia and the effect works well except that the underlying texture appears to be flipped or something. I'm not even sure what's wrong with the texture, but it's in completely the wrong place. There's nothing wrong with the texture because it looks fine with the shader is NOT applied, but as soon as I apply the shader it gets screwed up. If this doesn't make much sense I can post a screenshot.

I'm very, very new to shaders, so I'm not sure how to solve this problem. Here's the Nvidia shader source. Can anyone help me?

Code:
/*********************************************************************NVMH3****
File: $Id: //sw/devtools/FXComposer2/2.0/SDK/MEDIA/HLSL_FX/metal.fx#1 $

Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

% A phong-shaded metallic surface lit from either
% a point or directional source.
% Textured, untextured, quadratic falloff or not
keywords: material classic

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

#define FLIP_TEXTURE_Y

float Script : STANDARDSGLOBAL <
    string UIWidget = "none";
    string ScriptClass = "object";
    string ScriptOrder = "standard";
    string ScriptOutput = "color";
    string Script = "Technique=Technique?SimplePS:TexturedPS:SimpleFalloffPS:TexturedFalloffPS;";
> = 0.8;

/************* UN-TWEAKABLES **************/

//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////

float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;

/************* TWEAKABLES **************/

// "DirPos" Lamp 0 /////////
float4 Lamp0DirPos : Position < // or direction if W==0
    string Object = "Light0";
    string UIName =  "Lamp 0 Position/Direction";
    string Space = "World";
> = {-0.5f,2.0f,1.25f,1.0};
float3 Lamp0Color : Specular <
    string UIName =  "Lamp 0";
    string Object = "Light0";
    string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
float Lamp0Intensity <
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 10000.0f;
    float UIStep = 0.01;
    string UIName =  "Lamp 0 Quadratic Intensity";
> = 1.0f;

float3 AmbiColor : Ambient <
    string UIName =  "Ambient Light";
    string UIWidget = "Color";
> = {0.07f, 0.07f, 0.07f};

float3 SurfColor : DIFFUSE <
    string UIName =  "Surface";
    string UIWidget = "Color";
> = {1.0f, 0.6f, 0.1f};

float SpecExpon : SpecularPower <
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName =  "Specular Power";
> = 12.0;

float Kd <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.0;
    float UIStep = 0.05;
    string UIName =  "Diffuse";
> = 0.1;

//////////

texture ColorTexture : DIFFUSE <
    string ResourceName = "default_color.dds";
    string UIName =  "Diffuse Texture";
    string ResourceType = "2D";
>;

sampler2D ColorSampler = sampler_state {
    Texture = <ColorTexture>;
    MinFilter = Linear;
    MipFilter = Linear;
    MagFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
}; 

/************* DATA STRUCTS **************/

/* data from application vertex buffer */
struct appdata {
    float3 Position : POSITION;
    float4 UV : TEXCOORD0;
    float4 Normal : NORMAL;
    float4 Tangent : TANGENT0;
    float4 Binormal : BINORMAL0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
    float4 HPosition : POSITION;
    float2 UV : TEXCOORD0;
    float3 LightVec : TEXCOORD1;
    float3 WorldNormal : TEXCOORD2;
    float3 WorldTangent : TEXCOORD3;
    float3 WorldBinormal : TEXCOORD4;
    float3 WorldView : TEXCOORD5;
};

/*********** vertex shader for pixel-shaded versions ******/

/*********** Generic Vertex Shader ******/

vertexOutput std_dp_VS(appdata IN) {
    vertexOutput OUT = (vertexOutput)0;
    OUT.WorldNormal = mul(IN.Normal,WorldITXf).xyz;
    OUT.WorldTangent = mul(IN.Tangent,WorldITXf).xyz;
    OUT.WorldBinormal = mul(IN.Binormal,WorldITXf).xyz;
    float4 Po = float4(IN.Position.xyz,1);
    float3 Pw = mul(Po,WorldXf).xyz;
    if (Lamp0DirPos.w == 0) {
OUT.LightVec = -normalize(Lamp0DirPos.xyz);
    } else {
// we are still passing a (non-normalized) vector
OUT.LightVec = Lamp0DirPos.xyz - Pw;
    }
#ifdef FLIP_TEXTURE_Y
    OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
    OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
    OUT.WorldView = normalize(ViewIXf[3].xyz - Pw);
    OUT.HPosition = mul(Po,WvpXf);
    return OUT;
}

/********* pixel shaders ********/

void metal_shared(vertexOutput IN,
float3 LightColor,
out float3 DiffuseContrib,
out float3 SpecularContrib)
{
    float3 Ln = normalize(IN.LightVec.xyz);
    float3 Nn = normalize(IN.WorldNormal);
    float3 Vn = normalize(IN.WorldView);
    float3 Hn = normalize(Vn + Ln);
    float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
    DiffuseContrib = litV.y * Kd * LightColor + AmbiColor;
    SpecularContrib = litV.z * LightColor;
}

float4 metalPS(vertexOutput IN) : COLOR {
    float3 diffContrib;
    float3 specContrib;
metal_shared(IN,Lamp0Color,diffContrib,specContrib);
    float3 result = SurfColor * (specContrib + diffContrib);
    return float4(result,1);
}

float4 metalPS_t(vertexOutput IN) : COLOR {
    float3 diffContrib;
    float3 specContrib;
metal_shared(IN,Lamp0Color,diffContrib,specContrib);
    float3 map = tex2D(ColorSampler,IN.UV).xyz;
    float3 result = SurfColor * map * (specContrib + diffContrib);
    return float4(result,1);
}

// same as above with the addition of quadratic falloff

float4 metalQPS(vertexOutput IN) : COLOR {
    float3 diffContrib;
    float3 specContrib;
    float3 Cl = (Lamp0Intensity/dot(IN.LightVec.xyz,IN.LightVec.xyz)) * Lamp0Color;
    metal_shared(IN,Cl,diffContrib,specContrib);
    float3 result = SurfColor * (specContrib + diffContrib);
    return float4(result,1);
}

float4 metalQPS_t(vertexOutput IN) : COLOR {
    float3 diffContrib;
    float3 specContrib;
    float3 Cl = (Lamp0Intensity/dot(IN.LightVec.xyz,IN.LightVec.xyz)) * Lamp0Color;
    metal_shared(IN,Cl,diffContrib,specContrib);
    float3 map = tex2D(ColorSampler,IN.UV).xyz;
    float3 result = SurfColor * map * (diffContrib+specContrib);
    return float4(result,1);
}

//////////////////////////////////////////////////////////////////////
// TECHNIQUES ////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

technique SimplePS {
    pass p0 {
        VertexShader = compile vs_2_0 std_dp_VS();
    ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
        PixelShader = compile ps_2_a metalPS();
    }
}

technique TexturedPS {
    pass p0 {
        VertexShader = compile vs_2_0 std_dp_VS();
    ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
        PixelShader = compile ps_2_a metalPS_t();
    }
}

// pixel shaded, quadratic falloff

technique SimpleFalloffPS {
    pass p0 {
        VertexShader = compile vs_2_0 std_dp_VS();
    ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
        PixelShader = compile ps_2_a metalQPS();
    }
}

technique TexturedFalloffPS {
    pass p0 {
        VertexShader = compile vs_2_0 std_dp_VS();
    ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
CullMode = None;
        PixelShader = compile ps_2_a metalQPS_t();
    }
}

/***************************** eof ***/

Logged
newborn
Customers
Community Member
*****
Posts: 2437


WWW
« Reply #1 on: October 14, 2007, 10:38:27 AM »

I haven't been able to run any of the pre-made nvidia shaders. I think TV3D has its own shader structure/limitations.

Logged

MenDAKE
Community Member
*
Posts: 402


« Reply #2 on: October 14, 2007, 12:13:54 PM »

Would you happen to know where I might be able to find a decent reflective metal shader? I'll need to get into shader programming at a later date but for now I just need something that basically works.
Logged
SylvainTV
Administrator
Community Member
*****
Posts: 4479


WWW
« Reply #3 on: October 14, 2007, 12:42:12 PM »

For shaders with tangent and binormals, you must set the Meshformat to 17 (or bumpinfo)
Logged

Regards

Sylvain Dupont
TrueVision3D Developer
sylvain@truevision3d.com

TV3D IRC at http://chat.truevision3d.com or on server irc.truevision3d.com #Truevision3D. Come talk with us !
MenDAKE
Community Member
*
Posts: 402


« Reply #4 on: October 14, 2007, 02:43:38 PM »

I am doing that. Can you think of what else might be causing this?
Logged
sybixsus
Customers
Community Member
*****
Posts: 1088


WWW
« Reply #5 on: October 14, 2007, 06:21:48 PM »

Could you post that screenshot? I've had no problems with getting standard material-style shaders working.
Logged
MenDAKE
Community Member
*
Posts: 402


« Reply #6 on: October 14, 2007, 07:18:38 PM »

See the attached example. Excuse the horrible model and texturing -- it's just a placeholder that also happened to be the best example of this shader problem. Notice how the texture looks fine without the shader, but as soon as the shader is applied the texture goes haywire. For example, you can see the "booster" texture is now at the bottom of the ship facing in the wrong direction. (There's still a booster glow in the back but that's a separate glow texture done with a rendersurface.)

I'm not doing any funny business with the shader. I simply apply it with code that looks like this:

Code:
TVShader tempShader = new TVShader();
tempShader = GraphicEngine.Scene.CreateShader("tempShader");
tempShader.CreateFromEffectFile(Paths.Shaders + "metal\\metal.fx");
tempShader.SetTechnique("TexturedPS");
mesh.SetMeshFormat(17);
mesh.SetShaderEx(tempShader, true, -1);

By the way, I get the same results in ModelViewer using any models and textures with this shader.
« Last Edit: October 14, 2007, 07:23:28 PM by MenDAKE » Logged
Hypnotron
Customers
Community Member
*****
Posts: 807


« Reply #7 on: October 15, 2007, 09:00:53 AM »

have you tried commenting out the FLIP_TEXTURE_Y define at the top?
Logged
sybixsus
Customers
Community Member
*****
Posts: 1088


WWW
« Reply #8 on: October 15, 2007, 09:05:47 AM »

Ok, got you now, you mean your UV coordinates are wrong.

What Hypnotron said will work, or unless you have any reason to think you'll want to change the texture coordinates at some point, just replace this :

Code:
#ifdef FLIP_TEXTURE_Y
    OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
    OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */

With this :

Code:
    OUT.UV = IN.UV.xy;

Logged
MenDAKE
Community Member
*
Posts: 402


« Reply #9 on: October 15, 2007, 09:22:11 AM »

Yep, that fixed the problem. Thank you all VERY much for your help.
Logged
Pages: [1]
  Print  
 
Jump to:  

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