#include "atmosphere.cg" #include "lighting.cg" #include "shadowmapping.cg" // mod function shamelessly copied from GPU Gems. Original author Li-Yi Wei float2 mod(const float2 a, const float2 b) { // emulate integer mod operations in a float-only environment return floor(frac(a/b) * b); } // Vertex to pixel shader structure struct v2p { float4 Position : TEXCOORD4; //POSITION; float3 normal : TEXCOORD5; float3 RayleighColor : TEXCOORD2; float3 MieColor : TEXCOORD3; float3 tc0 : TEXCOORD0; float3 Direction : TEXCOORD1; float extinction : TEXCOORD6; //float2 vpos : VPOS; }; void main( // In in v2p IN, // Out out float4 outColor : COLOR0, #ifdef CSM_MRT out float4 outShadow : COLOR1, #endif // Constants uniform sampler2D baseMap : TEXUNIT0, uniform sampler2D indexMap : TEXUNIT1, uniform float3 lightDirection, uniform float3 lightColor, uniform float3 lightAmbient, uniform float3 eyePosW, uniform float atmosRayleigh, uniform float atmosMie, uniform float3 Ke, uniform float3 Ka, uniform float3 Kd, uniform float3 Ks, uniform float shininess, #ifdef CSM uniform sampler2D shadowArray : TEXUNIT7, uniform float4x4 smTexMatArray[SM_MAX_SPLITS], uniform float smSplits, #endif uniform float sunny, uniform float exposure, uniform float scale ) { float3 skyColor; #ifdef CSM float shadow; // Output shadowing and normals shadow=GetShadowFactor(IN.Position, IN.normal,shadowArray, smTexMatArray, smSplits, lightDirection)*sunny; //outShadow.rgb = IN.normal; #else const float shadow=sunny; #endif // Get sky gradient color skyColor.rgb=GetSkyColor(lightDirection,IN.Direction,IN.RayleighColor,IN.MieColor,atmosRayleigh,atmosMie,lightColor,lightAmbient); // Get base texture color // this is where the modifications go. Making this the wang tiling version of standard. float2 mappingScale1 = float2(1.0f,1.0f)/scale; float2 mappingScale2 = float2((128.0f),(128.0f)); float2 mappingAddress = IN.tc0 * mappingScale1; // In.tc0, IN.Position.xz float4 whichTile = tex2D(indexMap, float2(0.5f,0.5f)/mappingScale2 + floor(mappingAddress)/mappingScale2); //float4 whichTile = tex2D(indexMap,mappingAddress); // truncate values to make sure edges are crisp whichTile = floor(whichTile*3.0f + 0.25f)/4.0f; // number of input tiles (should be 4?) float2 tileScale = float2(4,4); // get correct mipmapping? Don't know if this code works in Racer. float2 tileScaledTex = mappingAddress * 0.25f; float4 baseCol=tex2D(baseMap,whichTile.wy + frac(mappingAddress)*float2(0.25,0.25), ddx(tileScaledTex),ddy(tileScaledTex)); // debug tinting of each tile to make it more obvious what's going on // baseCol = whichTile.wyzz*0.5 + 0.5*baseCol; // Lighting float3 ambient,diffuse,specular; LightingSun(Ke,Ka,Kd,Ks,shininess,lightDirection,lightColor,lightAmbient,IN.Position.xyz,IN.normal,eyePosW, ambient,diffuse,specular); // Store shadowed color in COLOR0, lit color additions (spec+diff) in COLOR1 float3 sdColor=baseCol*diffuse+specular; float3 ambientColor=baseCol*(ambient+Ke); // HDR toning of sky (for LDR shaders) //skyColor.rgb=1.0-exp(-exposure*skyColor.rgb); #ifdef CSM_MRT // Mix sky with texture color based on atmospheric influence outColor.rgb=lerp(skyColor,ambientColor,IN.extinction); //outShadow.rgb=lerp(skyColor*(IN.extinction),sdColor,IN.extinction); outShadow.rgb=sdColor*IN.extinction; outShadow.a=shadow; //outColor.rgb=outShadow.a; //outShadow.rgba=0; // Blending outColor.a=1; //baseCol.a; //outShadow.a=1; //baseCol.a; #else // Mix sky with texture color based on atmospheric influence outColor.rgb=lerp(skyColor,ambientColor+sdColor*shadow,IN.extinction); // Blending outColor.a=baseCol.a; #endif }