
  #define DIFFUSE_LIGHTING_SCALE 1.5
 
 #define TIME_SCALE 1.2
 #define SPEC_SCALE 1.0
 #define SPEC_BIAS 0.0
  
 #define FRESNEL_SCALE 2.0
 #define FRESNEL_BIAS 0.00
 #define FRESENEL_POWER 10
 
 #define BLEND_BIAS 0.25
  
  float4 ambientColor  : register (c0);
  float4 diffuseColor  : register (c1);
  float4 worldLight    : register (c2);
  float4 specularColor : register (c3); //power in c3.w
  float4 blendPct      : register (c4); //bump map blend in c4.z, cube map blend in c4.w
  
  struct PS_INPUT
  {
    float4 Color   : COLOR0;
    float4 Omni    : COLOR1;
    float  Fog     : FOG;

    float4 Tex0    : TEXCOORD0; // base0=xy, spec=zw - textures swapped
    float4 Tex1    : TEXCOORD1; // bump=xy, base1=zw - textures swapped
    float3 Light   : TEXCOORD2;
    float3 EyeVec  : TEXCOORD3;	
	
   float3 normal :NORMAL;
  };
  
  struct PS_OUTPUT
  {
    float4 Color : COLOR0;
  };
  
  #if CUSTOM == 1
  
  sampler2D sBase:  register (s0);
  sampler2D sBump:  register (s1);
  sampler2D sBase1:  register (s2);
  
  PS_OUTPUT ps20_RZDiffuseAlphaSpecBumpMapShadowMap (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	    	
		
    float4 bumpPixel  = tex2D (sBump, Input.Tex0.xy);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
    float shadowMap = tex2D (sBase1, Input.Tex1.xy).a;  // sample base tex
            
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
	
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
	
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a * shadowMap;
    Out.Color += (saturate(pow(NdotH, specularColor.a)) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * basePixel.a * Input.Color * shadowMap;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;	
    Out.Color.rgb *= Input.Color.rgb;
	
    return (Out);
  }
  
  #elif CUSTOM == 2
  
  sampler2D sBase:  register (s0);
  sampler2D sBump:  register (s1);
  sampler2D sMixMap:  register (s2);
  sampler2D sBase2:  register (s3);
  
  
  PS_OUTPUT ps20_RZDiffuseAlphaSpecBumpMixAltDiff (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	
    float4 bumpPixel  = tex2D (sBump, Input.Tex0.zw);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
    float4 mixPixel  = tex2D (sMixMap, Input.Tex1.xy);  // sample base tex
    float4 base2Pixel  = tex2D (sBase2, Input.Tex0.xy);  // sample base tex
    
    
    float blendPerc =  saturate((Input.Color.r - mixPixel.r)/BLEND_BIAS);
    basePixel = lerp(basePixel,base2Pixel,blendPerc);
	        
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
    
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
      
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (pow(NdotH, specularColor.a) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * basePixel.a * Input.Color.a;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;
				
    return (Out);
  }
  
  #elif CUSTOM == 4
  
  sampler2D sBase:  register (s0);
  sampler2D sSpec:  register (s1);
  sampler2D sBump:  register (s2);
  sampler2D sBase1: register (s3);
  
  PS_OUTPUT ps20_RZBumpSpecMapT0T1Custom (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	
    float4 bumpPixel  = tex2D (sBump, Input.Tex1.xy);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
    float4 specPixel  = tex2D (sSpec, Input.Tex0.zw);  // sample spec tex (gloss)
    float4 base1Pixel = tex2D (sBase1, Input.Tex1.zw);  // sample base tex
    
    basePixel.rgb = basePixel.rgb * base1Pixel.rgb;
    
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));
	
    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
        
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);      
        
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (pow(NdotH, specularColor.a) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * specPixel * Input.Color.a;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;
    Out.Color.rgb *= Input.Color.rgb;
		
    return (Out);
    
  }

  #elif CUSTOM == 11
  
  sampler2D sBase:  register (s0);
  sampler2D sBase1:  register (s1);
  sampler2D sBump:  register (s2);
  
  PS_OUTPUT ps20_RZDiffuseAOBumpAlphaSpec (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	    	
		
    float4 bumpPixel  = tex2D (sBump, Input.Tex1.xy);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
    float4 base1Pixel = tex2D (sBase1, Input.Tex0.zw);  // sample base tex
    
    basePixel.rgb = basePixel.rgb * base1Pixel.rgb;
        
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
    
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
    
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (saturate(pow(NdotH, specularColor.a)) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * basePixel.a * Input.Color;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;	
    Out.Color.rgb *= Input.Color.rgb;
	
    return (Out);
  }
  
  #elif CUSTOM == 12
  
  sampler2D sBase:  register (s0);
  sampler2D sBump:  register (s1);
  
  #define PARALLAX_SCALE 0.05
  #define PARALLAX_BIAS 0.00
  
  PS_OUTPUT ps20_RZblinnDiffuseBumpParallaxAlphaSpec (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
	
    float parallaxHeight  = tex2D (sBump, Input.Tex0.zw).a;
    parallaxHeight = halfway.xy * ((parallaxHeight * PARALLAX_SCALE) + PARALLAX_BIAS);
	
    Input.Tex0.xy += parallaxHeight;	
	
    float4 bumpPixel  = tex2D (sBump, Input.Tex0.zw);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy) * Input.Color;  // sample base tex
	        
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
    
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
    
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (saturate(pow(NdotH, specularColor.a)) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * basePixel.a * Input.Color.a;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;	
    Out.Color.rgb *= Input.Color.rgb;
		
    return (Out);
  }
    
  #elif CUSTOM == 13
  
  sampler2D sBase:  register (s0);
  sampler2D sBump:  register (s1);
  
  
  PS_OUTPUT ps20_RZDiffuseBumpAlphaSpec (PS_INPUT Input)
  {
    PS_OUTPUT Out;
	
    float4 bumpPixel  = tex2D (sBump, Input.Tex0.zw);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
	        
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
    
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
      
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (pow(NdotH, specularColor.a) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * basePixel.a * Input.Color.a;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;
    Out.Color.rgb *= Input.Color.rgb;
				
    return (Out);
  }
  
  #elif CUSTOM == 14
  
  sampler2D sBase:  register (s0);
  sampler2D sSpec:  register (s1);
  sampler2D sBump:  register (s2);
  
  PS_OUTPUT ps20_RZDiffuseBumpSpecMaps (PS_INPUT Input)
  {
    PS_OUTPUT Out;
			
    float4 bumpPixel  = tex2D (sBump, Input.Tex1.xy);  // sample and unbias normal
    bumpPixel.xyz = bumpPixel.xyz * 2.0 - 1.0;
    float4 basePixel  = tex2D (sBase, Input.Tex0.xy);  // sample base tex
    float4 specPixel  = tex2D (sSpec, Input.Tex0.zw);  // sample base tex
                      
    float3 normLight = normalize(Input.Light);
    float3 normEyeVec = normalize(Input.EyeVec);
    float3 halfway =  normalize (normLight -  normEyeVec); 
    float NdotL = saturate(dot(bumpPixel.xyz,normLight));
    float NdotH = saturate(dot(bumpPixel.xyz,halfway));

    float specLowLightScale = saturate(worldLight.g * -TIME_SCALE + TIME_SCALE); //invert sun height, and make anything below 33% = 1, and 100% height = 0
    specLowLightScale = (SPEC_SCALE * specLowLightScale) + SPEC_BIAS;
    
    // Calculate fresnel
    float fresnel = min(FRESNEL_BIAS + FRESNEL_SCALE * pow(1 - abs(dot(normEyeVec, bumpPixel.xyz)), FRESENEL_POWER), FRESNEL_BIAS + FRESNEL_SCALE);
    
    Out.Color = saturate(NdotL * DIFFUSE_LIGHTING_SCALE) * diffuseColor * Input.Color.a;
    Out.Color += (pow(NdotH, specularColor.a) + fresnel * specLowLightScale) * saturate(NdotL * 3) * specularColor * specPixel * Input.Color.a;
    Out.Color += ambientColor + Input.Omni;
    Out.Color.a = 1;
    Out.Color *= basePixel;
    Out.Color.rgb *= Input.Color.rgb;
		
    return (Out);
  }
  
  #endif
  
  
  