CGFX Shader: Normalmap problems in Maya


#1

Hi So I started scripting cgfx shaders in Maya and somehow I cant get the Normal map to work for my cgfx shaders. I even loaded in a shader wich I didn’t write myself.
No success…

also the lod function for the cube map gives me weird results.

Script:



// CGFX Matrices Section 
float4x4 WorldViewProjection     : WorldViewProjection   < string UIWidget = "None"; >;
float4x4 WorldInverseTranspose   : WorldInverseTranspose < string UIWidget = "None"; >;
float4x4 ViewInverse             : ViewInverse           < string UIWidget = "None"; >;
float4x4 World                   : World                 < string UIWidget = "None"; >;

// Maya Description 
string description = "Phong";

//////////////////////////////////////////////////////////////
// Parameters section 

float3 shaderColor : Diffuse
<
	string UIName = "Shader Color";
> = {1.0f, 1.0f, 1.0f};

float3 lightDirection : Direction
<
	string UIName = "Light Direction";
> = {0.0f, 1.0f, 0.0f};

float3 ambientColor : Diffuse
<
	string UIName = "Ambient Color";
> = {0.1f, 0.15f, 0.15f};

float fresnelPower
<
	string UIWidget = "slider";
	string UIName = "Fresnel Power";
	float UIMin = 0.0f;
	float UIMax = 10.0f;
	float UIStep = 0.01f;
	
> = 3.0f;

float specularPower
<
	string UIWidget = "slider";
	string UIName = "Specular Power";
	float UIMin = 0.05f;
	float UIMax = 100.0f;
	float UIStep = 0.01f;
	
> = 30.0f;

float3 specularColor : Diffuse
<
	string UIName = "Specular Color";
> = {1.0f, 1.0f, 1.0f};

texture diffuseTexture
<
	string name = "default_color.dds";
	string UIName = "Diffuse Texture";
	string TextureType = "2D";
>;

sampler2D diffuseMap = sampler_state
{
	Texture = <diffuseTexture>;
	AddressU = Wrap;
	AddressV = Wrap;
};

texture specTexture
<
	string name = "default_color.dds";
	string UIName = "Specualr Texture";
	string TextureType = "2D";
>;

sampler2D specMap = sampler_state
{
	Texture = <specTexture>;
	AddressU = Wrap;
	AddressV = Wrap;
};

[B]texture normalTexture
<
	string name = "default_color.dds";
	string UIName = "Normal Texture";
	string TextureType = "2D";
>;

sampler2D normalMap = sampler_state
{
	Texture = <normalTexture>;
	AddressU = Wrap;
	AddressV = Wrap;
};
[/B]



//////////////////////////////////////////////////////////////
// Structs section 
// input from application
struct a2v {
	float4 position  : POSITION;
	float2 texCoord	 : TEXCOORD0;
	float4 normal	 : NORMAL;
	float4 binormal	 : BINORMAL;
	float4 tangent	 : TANGENT;
};

// output to fragment program
struct v2f {
        float4 position         : POSITION;
		float2 texCoord		    : TEXCOORD0;
		float3 worldNormal	    : TEXCOORD1;
		float3 worldBinormal	: TEXCOORD2;
		float3 worldTangent     : TEXCOORD3;
		float3 eyeVector	    : TEXCOORD4;
};



//////////////////////////////////////////////////////////////
// Vertex Shader 
v2f vShader(a2v In)
{
    v2f Out;
    Out.position = mul(WorldViewProjection, In.position);
	
	Out.worldNormal   = mul(WorldInverseTranspose, In.normal).xyz;
	Out.worldBinormal 	= (mul(WorldInverseTranspose, In.binormal).xyz);
	Out.worldTangent  	= (mul(WorldInverseTranspose, In.tangent).xyz);
	 
	
	float3 worldSpacePos = mul(World, In.position);
	float3 worldCameraPos = float3(ViewInverse[0].w, ViewInverse[1].w, ViewInverse[2].w);
	Out.eyeVector = worldCameraPos - worldSpacePos;
	 
	Out.texCoord = In.texCoord;
	 
    return Out;
}

//////////////////////////////////////////////////////////////
// Pixel Shader 
float4 pShader(v2f In) : COLOR
{
	float4 outColor;
	
	float3 worldNormal = tex2D(normalMap, In.texCoord) * 2 -1;
	worldNormal = normalize((worldNormal.x * In.worldTangent) + (worldNormal.y * In.worldBinormal) + (worldNormal.z * In.worldNormal));
	
	float3 lightDir = normalize(-lightDirection);
	float3 eyeVector = normalize(In.eyeVector);
	
	float4 colorMap = tex2D(diffuseMap, In.texCoord);
	float4 specularMap  = tex2D(specMap, In.texCoord);
	
	
	float3 reflectionVector = -reflect(eyeVector, worldNormal);
	
	float3 specular = pow(saturate(dot(reflectionVector, lightDir)), specularPower) * specularColor;
	
	float3 lambert = saturate(dot(lightDir, worldNormal));
	
	float3 fresnel = pow(1- saturate(dot(eyeVector, worldNormal)) ,fresnelPower) * lambert;
	float3 totalSpec = (fresnel * specularMap.r) + (specular * specularMap.g);
	outColor.rgb = (lambert + ambientColor) * (shaderColor * colorMap.rgb) + totalSpec;
	
	outColor.a = 1.0f;
	
	
	
	return outColor;
}

//////////////////////////////////////////////////////////////
// Techniques
technique Simple
{
    pass one
    {
		VertexShader = compile vp40 vShader();
		PixelShader = compile fp40 pShader();
    }
}

hope someone can help me here, because I’m kind of stuck here. no use writing shaders if I can’t apply normalmaps :frowning:

cheers :wink:


#2

Have you an AMD graphic card? Try to put:

VertexShader = compile arbvp1 vShader();
PixelShader = compile arbfp1 pShader();

Instead of:

VertexShader = compile vp40 vShader();
PixelShader = compile fp40 pShader();

Hope this help.

There is a link to a shader I’ve made:

http://www.fevrierdorian.com/blog/public/billets/2010_08_04_CgFX_part5/cgfx_tuto_009.7z

Tutorial is here (french):

http://www.fevrierdorian.com/blog/post/2010/08/28/CgFX-Des-shaders-temps-réel-dans-le-viewport-Maya!-Part-5


#3

Thank you

This is my graphc card:

ATI Radeon HD 5700 Series

I have tried this new tecnique but this doesnt work neither.
I think its because of the graphic card though.


#4

You graphic card should be ok.

You’re example is a little complex. You should try a simpler one. :slight_smile:


#5

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.