Sign in to follow this  
mental_raygun

Cgfx shader adding a 2nd cubemap?

Recommended Posts

Hi, I just found this nice Cgfx-Shader that comes with the maya cgfx plugin and would like to change the 'color texture' into a 2nd cubemap or adding a 2nd cubemap below the color texture. I tried different times to copy some lines from the cubmaps functions, but I can't get through tbe pixelshader declarations that my changes get accepted.. Maybe anyone can take a look at? thank you.
/*********************************************************************NVMH3****
Path:  NVSDK\Common\media\cgfx
File:  $Id: //sw/devrel6/SDK/MEDIA/CgFX/plasticFresnel_tex_CgDX9.fx#1 $

Copyright NVIDIA Corporation 2002...

Comments:
    A phong-shaded plastic-style surface + fresnel mirror term.

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

string Category = "Effects\\Cg\\BRDF";
string keywords = "dx9,fresnel,pointlight";
string description = "Plastic w/fresnel reflection";

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

float4x4 worldIT : WorldIT;
float4x4 wvp : WorldViewProjection;
float4x4 world : World;
float4x4 viewIT : ViewIT;

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

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

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
    float4 HPosition	: POSITION;
    float4 TexCoord	: TEXCOORD0;
    float3 WorldLightVec : TEXCOORD1;
    float3 WorldNormal	: TEXCOORD2;
    float3 WorldEyeVec	: TEXCOORD3;
};

/* Output pixel values */
struct pixelOutput {
  float4 col : COLOR;
};

/*********** vertex shader ******/

vertexOutput mainVS(appdata IN,
    uniform float4x4 WorldViewProj,
    uniform float4x4 WorldIT,
    uniform float4x4 World,
    uniform float4x4 ViewIT,
    uniform float3 LightPos
) {
    vertexOutput OUT;
    OUT.WorldNormal = mul(WorldIT, IN.Normal).xyz;
    float4 Po = float4(IN.Position.x,IN.Position.y,IN.Position.z,1.0);
    float3 Pw = mul(World, Po).xyz;
    OUT.WorldLightVec = normalize(LightPos - Pw);
    OUT.TexCoord = IN.UV;
    OUT.WorldEyeVec = normalize(ViewIT[3].xyz - Pw);
    OUT.HPosition = mul(WorldViewProj, Po);
    return OUT;
}

/********* pixel shader ********/

pixelOutput mainPS(vertexOutput IN,
    uniform samplerCUBE EnvironmentMap, 
    uniform sampler2D ColorMap, 
    uniform float SpecExpon,
    uniform float4 AmbiColor,
    uniform float4 SurfColor,
    uniform float4 LightColor,
    uniform float Kd,
    uniform float KrMax,
    uniform float KrMin,
    uniform float FresExpon
) {
    pixelOutput OUT; 
    float4 map = tex2D(ColorMap,IN.TexCoord.xy);
    float3 Ln = normalize(IN.WorldLightVec);
    float3 Nn = normalize(IN.WorldNormal);
    float3 Vn = normalize(IN.WorldEyeVec);
    float3 Hn = normalize(Vn + Ln);
    float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
    float4 diffContrib = (Kd*litV.y * LightColor + AmbiColor);
    float4 specContrib = litV.y*litV.z * LightColor;
    float3 reflVect = reflect(Vn,Nn);
    float fres = KrMin + (KrMax-KrMin) *
		pow((1.0-abs(dot(Vn,Nn))),FresExpon);
    float4 reflColor = texCUBE(EnvironmentMap,float4(reflVect,1.0));

    float4 result = (SurfColor * map * diffContrib) +
		    specContrib +
		    (fres * reflColor);
    OUT.col = result;
    return OUT;
}

/////////////////////////////////////////////////////


float4 lightPos : Position
<
    string Object = "PointLight";
    string Space = "World";
> = {100.0f, 100.0f, 100.0f, 0.0f};

float4 lightColor : Specular <
    string Desc = "Light Color";
> = {0.7f, 0.7f, 0.7f, 1.0f};

float4 ambiColor : Ambient
<
    string Desc = "Ambient Light Color";
> = {0.07f, 0.07f, 0.07f, 1.0f};

float4 surfColor : Diffuse
<
    string Desc = "Surface Color";
> = {0.2f, 0.1f, 0.6f, 1.0f};

float specExpon : Power
<
    string gui = "slider";
    float uimin = 1.0;
    float uimax = 128.0;
    float uistep = 1.0;
    string Desc = "specular power";
    float min = 1.0;
    float max = 128.0;
> = 12.0;

float diffStrength
<
    string gui = "slider";
    float uimin = 0.0;
    float uimax = 1.0;
    float uistep = 0.05;
    string Desc = "Compared to hilights";
    float min = 0.0;
    float max = 1.0;
> = 0.8;

float reflStrength
<
    string gui = "slider";
    float uimin = 0.1;
    float uimax = 1.0;
    float uistep = 0.05;
    string Desc = "Edge reflection";
    float min = 0.1;
    float max = 1.0;
> = 1.0;

float reflMin
<
    string gui = "slider";
    float uimin = 0.0;
    float uimax = 1.0;
    float uistep = 0.05;
    string Desc = "Center reflection";
    float min = 0.0;
    float max = 1.0;
> = 0.1;

float fresnel
<
    string gui = "slider";
    float uimin = 1.0;
    float uimax = 6.5;
    float uistep = 0.5;
    string Desc = "Schlick Exponent";
    float min = 1.0;
    float max = 6.5;
> = 4.0;

texture colorTexture : DiffuseMap
<
    string File = "default_color.dds";
    string TextureType = "2D";
>;

texture cubeMap : EnvMap
<
	string File = "default_reflection.dds";
	string TextureType = "Cube";
>;

sampler2D colorMapSampler = sampler_state
{
	Texture = <colorTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

samplerCUBE environmentMapSampler = sampler_state
{
	Texture = <cubeMap>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

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

technique dx9textured
{
	pass p0 
	{		
		// same vertex shader for both DX9 techniques
        VertexShader = compile vs_2_0 mainVS(wvp,worldIT,world,viewIT,lightPos);

		ZEnable = true;
		ZWriteEnable = true;
		CullMode = None;
        PixelShader = compile ps_2_0 mainPS(environmentMapSampler,colorMapSampler, specExpon,ambiColor,surfColor,lightColor,diffStrength,reflStrength,reflMin,fresnel);
	}
}

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this