Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.63 KB | None | 0 0
  1. ## Includes
  2.  
  3. Includes = {
  4. "constants.fxh"
  5. "standardfuncsgfx.fxh"
  6. "pdxmap.fxh"
  7. "shadow.fxh"
  8. }
  9.  
  10.  
  11. ## Samplers
  12.  
  13. VertexShader =
  14. {
  15. Samplers =
  16. {
  17. HeightMap =
  18. {
  19. AddressV = "Wrap"
  20. MagFilter = "Point"
  21. AddressU = "Wrap"
  22. Index = 0
  23. MipFilter = "Linear"
  24. MinFilter = "Linear"
  25. }
  26. }
  27. }
  28.  
  29. PixelShader =
  30. {
  31. Samplers =
  32. {
  33. TerrainDiffuse =
  34. {
  35. AddressV = "Wrap"
  36. MagFilter = "Linear"
  37. AddressU = "Wrap"
  38. Index = 0
  39. MipFilter = "Linear"
  40. MinFilter = "Linear"
  41. }
  42.  
  43. HeightNormal =
  44. {
  45. AddressV = "Wrap"
  46. MagFilter = "Linear"
  47. AddressU = "Wrap"
  48. Index = 1
  49. MipFilter = "Linear"
  50. MinFilter = "Linear"
  51. }
  52.  
  53. TerrainColorTint =
  54. {
  55. AddressV = "Wrap"
  56. MagFilter = "Linear"
  57. AddressU = "Wrap"
  58. Index = 2
  59. MipFilter = "Linear"
  60. MinFilter = "Linear"
  61. }
  62.  
  63. TerrainColorTintSecond =
  64. {
  65. AddressV = "Wrap"
  66. MagFilter = "Linear"
  67. AddressU = "Wrap"
  68. Index = 3
  69. MipFilter = "Linear"
  70. MinFilter = "Linear"
  71. }
  72.  
  73. TerrainNormal =
  74. {
  75. AddressV = "Wrap"
  76. MagFilter = "Linear"
  77. AddressU = "Wrap"
  78. Index = 4
  79. MipFilter = "Point"
  80. MinFilter = "Linear"
  81. }
  82.  
  83. TerrainIDMap =
  84. {
  85. AddressV = "Clamp"
  86. MagFilter = "Point"
  87. AddressU = "Clamp"
  88. Index = 5
  89. MipFilter = "None"
  90. MinFilter = "Point"
  91. }
  92.  
  93. ProvinceSecondaryColorMap =
  94. {
  95. AddressV = "Clamp"
  96. MagFilter = "Point"
  97. AddressU = "Clamp"
  98. Index = 6
  99. MipFilter = "Point"
  100. MinFilter = "Point"
  101. }
  102.  
  103. IndirectionMap =
  104. {
  105. AddressV = "Clamp"
  106. MagFilter = "Point"
  107. AddressU = "Clamp"
  108. Index = 7
  109. MipFilter = "Point"
  110. MinFilter = "Point"
  111. }
  112.  
  113. FoWTexture =
  114. {
  115. AddressV = "Wrap"
  116. MagFilter = "Linear"
  117. AddressU = "Wrap"
  118. Index = 8
  119. MipFilter = "Linear"
  120. MinFilter = "Linear"
  121. }
  122.  
  123. FoWDiffuse =
  124. {
  125. AddressV = "Wrap"
  126. MagFilter = "Linear"
  127. AddressU = "Wrap"
  128. Index = 9
  129. MipFilter = "Linear"
  130. MinFilter = "Linear"
  131. }
  132.  
  133. OccupationMask =
  134. {
  135. AddressV = "Wrap"
  136. MagFilter = "Linear"
  137. AddressU = "Wrap"
  138. Index = 10
  139. MipFilter = "Linear"
  140. MinFilter = "Linear"
  141. }
  142.  
  143. ProvinceColorMap =
  144. {
  145. AddressV = "Clamp"
  146. MagFilter = "Point"
  147. AddressU = "Clamp"
  148. Index = 11
  149. MipFilter = "Point"
  150. MinFilter = "Point"
  151. }
  152.  
  153. ShadowMap =
  154. {
  155. AddressV = "Wrap"
  156. MagFilter = "Linear"
  157. AddressU = "Wrap"
  158. Index = 12
  159. MipFilter = "Linear"
  160. MinFilter = "Linear"
  161. }
  162.  
  163. TITexture =
  164. {
  165. AddressV = "Wrap"
  166. MagFilter = "Linear"
  167. AddressU = "Wrap"
  168. Index = 13
  169. MipFilter = "Linear"
  170. MinFilter = "Linear"
  171. }
  172.  
  173. MudTexture =
  174. {
  175. AddressV = "Clamp"
  176. MagFilter = "Linear"
  177. AddressU = "Clamp"
  178. Index = 14
  179. MipFilter = "Linear"
  180. MinFilter = "Linear"
  181. }
  182.  
  183. MudDiffuse =
  184. {
  185. AddressV = "Wrap"
  186. MagFilter = "Linear"
  187. AddressU = "Wrap"
  188. Index = 15
  189. MipFilter = "Linear"
  190. MinFilter = "Linear"
  191. }
  192. }
  193. }
  194.  
  195. ## Vertex Structs
  196.  
  197. VertexStruct VS_INPUT_TERRAIN_NOTEXTURE
  198. {
  199. float4 position : POSITION;
  200. float2 height : TEXCOORD0;
  201. };
  202.  
  203. VertexStruct VS_OUTPUT_TERRAIN
  204. {
  205. float4 position : PDX_POSITION;
  206. float2 uv : TEXCOORD0;
  207. float2 uv2 : TEXCOORD1;
  208. float3 prepos : TEXCOORD2;
  209. float4 vShadowProj : TEXCOORD3;
  210. float4 vScreenCoord : TEXCOORD4;
  211. };
  212.  
  213. ## Constant Buffers
  214.  
  215. ## Shared Code
  216.  
  217. Code
  218. [[
  219. static const float3 GREYIFY = float3( 0.212671, 0.715160, 0.072169 );
  220. static const float NUM_TILES = 4.0f;
  221. static const float TEXELS_PER_TILE = 512.0f;
  222. static const float ATLAS_TEXEL_POW2_EXPONENT= 11.0f;
  223. static const float TERRAIN_WATER_CLIP_HEIGHT = 3.0f;
  224. static const float TERRAIN_UNDERWATER_CLIP_HEIGHT = 3.0f;
  225.  
  226. #ifdef TERRAIN_SHADER
  227. #ifdef COLOR_SHADER
  228. #define TERRAIN_AND_COLOR_SHADER
  229. #endif
  230. #endif
  231.  
  232. float mipmapLevel( float2 uv )
  233. {
  234. #ifdef PDX_OPENGL
  235.  
  236. #ifdef NO_SHADER_TEXTURE_LOD
  237. return 1.0f;
  238. #else
  239.  
  240. #ifdef PIXEL_SHADER
  241. float dx = fwidth( uv.x * TEXELS_PER_TILE );
  242. float dy = fwidth( uv.y * TEXELS_PER_TILE );
  243. float d = max( dot(dx, dx), dot(dy, dy) );
  244. return 0.5 * log2( d );
  245. #else
  246. return 3.0f;
  247. #endif //PIXEL_SHADER
  248.  
  249. #endif // NO_SHADER_TEXTURE_LOD
  250.  
  251. #else
  252. float2 dx = ddx( uv * TEXELS_PER_TILE );
  253. float2 dy = ddy( uv * TEXELS_PER_TILE );
  254. float d = max( dot(dx, dx), dot(dy, dy) );
  255. return 0.5f * log2( d );
  256. #endif //PDX_OPENGL
  257. }
  258.  
  259. float4 sample_terrain( float IndexU, float IndexV, float2 vTileRepeat, float vMipTexels, float lod, float vTiles )
  260. {
  261. vTileRepeat = frac( vTileRepeat );
  262.  
  263. #ifdef NO_SHADER_TEXTURE_LOD
  264. vTileRepeat *= 0.98;
  265. vTileRepeat += 0.01;
  266. #endif
  267.  
  268. float vTexelsPerTile = vMipTexels / vTiles;
  269.  
  270. vTileRepeat *= ( vTexelsPerTile - 1.0f ) / vTexelsPerTile;
  271. return float4( ( float2( IndexU, IndexV ) + vTileRepeat ) / vTiles + 0.5f / vMipTexels, 0.0f, lod );
  272. }
  273.  
  274. void calculate_index( float4 IDs, out float4 IndexU, out float4 IndexV, out float vAllSame )
  275. {
  276. IDs *= 255.0f;
  277. vAllSame = saturate( IDs.z - 98.0f ); // we've added 100 to first if all IDs are same
  278. IDs -= vAllSame * 100.0f;
  279.  
  280. IndexV = trunc( ( IDs + 0.5f ) / NUM_TILES );
  281. IndexU = trunc( IDs - ( IndexV * NUM_TILES ) + 0.5f );
  282. }
  283.  
  284. #ifdef PIXEL_SHADER
  285.  
  286. float3 calculate_secondary( float2 uv, float3 vColor, float2 vPos )
  287. {
  288. float4 vSample = GetProvinceColorSampled( uv, IndirectionMap, ProvinceIndirectionMapSize, ProvinceSecondaryColorMap, ProvinceColorMapSize );
  289. float4 vMask = tex2D( OccupationMask, vPos / 8.0f ).rgba;
  290. return lerp( vColor, vSample.rgb, saturate( vSample.a * vMask.a ) );
  291. }
  292.  
  293. float3 calculate_secondary_compressed( float2 uv, float3 vColor, float2 vPos )
  294. {
  295. float4 vMask = tex2D( OccupationMask, vPos / 8.0 ).rgba;
  296.  
  297. // Point sample the color of this province.
  298. float4 vSecondary = GetProvinceColorSampled( uv, IndirectionMap, ProvinceIndirectionMapSize, ProvinceSecondaryColorMap, ProvinceColorMapSize );
  299.  
  300. const int nDivisor = 6;
  301. int3 vTest = int3(vSecondary.rgb * 255.0);
  302.  
  303. int3 RedParts = int3( vTest / ( nDivisor * nDivisor ) );
  304. vTest -= RedParts * ( nDivisor * nDivisor );
  305.  
  306. int3 GreenParts = int3( vTest / nDivisor );
  307. vTest -= GreenParts * nDivisor;
  308.  
  309. int3 BlueParts = int3( vTest );
  310.  
  311. float3 vSecondColor =
  312. float3( RedParts.x, GreenParts.x, BlueParts.x ) * vMask.b
  313. + float3( RedParts.y, GreenParts.y, BlueParts.y ) * vMask.g
  314. + float3( RedParts.z, GreenParts.z, BlueParts.z ) * vMask.r;
  315.  
  316. vSecondary.a -= 0.5 * saturate( saturate( frac( vPos.x / 2.0 ) - 0.7 ) * 10000.0 );
  317. vSecondary.a = saturate( saturate( vSecondary.a ) * 3.0 ) * vMask.a;
  318. return vColor * ( 1.0 - vSecondary.a ) + ( vSecondColor / float(nDivisor) ) * vSecondary.a;
  319. }
  320.  
  321. bool GetFoWAndTI( float3 PrePos, out float4 vFoWColor, out float4 vMonsoonColor, out float TI, out float4 vTIColor )
  322. {
  323. vFoWColor = GetFoWColor( PrePos, FoWTexture);
  324. vMonsoonColor = GetFoWColor( PrePos, MudTexture);
  325. TI = GetTI( vFoWColor );
  326. vTIColor = GetTIColor( PrePos, TITexture );
  327. return ( TI - 0.99f ) * 1000.0f <= 0.0f;
  328. }
  329.  
  330. float3 CalcNormalForLighting( float3 InputNormal, float3 TerrainNormal )
  331. {
  332. TerrainNormal = normalize( TerrainNormal );
  333.  
  334. //Calculate normal
  335. float3 zaxis = InputNormal;
  336. float3 xaxis = cross( zaxis, float3( 0, 0, 1 ) ); //tangent
  337. xaxis = normalize( xaxis );
  338. float3 yaxis = cross( xaxis, zaxis ); //bitangent
  339. yaxis = normalize( yaxis );
  340. return xaxis * TerrainNormal.x + zaxis * TerrainNormal.y + yaxis * TerrainNormal.z;
  341. }
  342. #endif // PIXEL_SHADER
  343. ]]
  344.  
  345. ## Vertex Shaders
  346.  
  347. VertexShader =
  348. {
  349. MainCode VertexShader
  350. [[
  351. VS_OUTPUT_TERRAIN main( const VS_INPUT_TERRAIN_NOTEXTURE VertexIn )
  352. {
  353. VS_OUTPUT_TERRAIN VertexOut;
  354.  
  355. #ifdef USE_VERTEX_TEXTURE
  356. float2 mapPos = VertexIn.position.xy * QuadOffset_Scale_IsDetail.z + QuadOffset_Scale_IsDetail.xy;
  357. float heightScale = vBorderLookup_HeightScale_UseMultisample_SeasonLerp.y * 255.0;
  358.  
  359. VertexOut.uv = float2( ( mapPos.x + 0.5f ) / MAP_SIZE_X, ( mapPos.y + 0.5f ) / MAP_SIZE_Y );
  360. VertexOut.uv2.x = ( mapPos.x + 0.5f ) / MAP_SIZE_X;
  361. VertexOut.uv2.y = ( mapPos.y + 0.5f - MAP_SIZE_Y ) / -MAP_SIZE_Y;
  362. VertexOut.uv2.xy *= float2( MAP_POW2_X, MAP_POW2_Y ); //POW2
  363.  
  364. float2 heightMapUV = VertexOut.uv;
  365. heightMapUV.y = 1.0 - heightMapUV.y;
  366.  
  367. #ifdef PDX_OPENGL
  368. float vHeight = tex2D( HeightMap, heightMapUV ).x * heightScale;
  369. #else
  370. float vHeight = tex2Dlod0( HeightMap, heightMapUV ).x * heightScale;
  371. #endif // PDX_OPENGL
  372.  
  373. VertexOut.prepos = float3( mapPos.x, vHeight, mapPos.y );
  374. VertexOut.position = mul( ViewProjectionMatrix, float4( VertexOut.prepos, 1.0f ) );
  375. #else // !USE_VERTEX_TEXTURE
  376. float2 pos = VertexIn.position.xy * QuadOffset_Scale_IsDetail.z + QuadOffset_Scale_IsDetail.xy;
  377. float vSatPosZ = saturate( VertexIn.position.z ); // VertexIn.position.z can have a value [0-4], if != 0 then we shall displace vertex
  378. float vUseAltHeight = vSatPosZ * vSnap[ int( VertexIn.position.z - 1.0f ) ]; // the snap values are set to either 0 or 1 before each draw call to enable/disable snapping due to LOD
  379. pos += vUseAltHeight
  380. * float2( 1.0f - VertexIn.position.w, VertexIn.position.w ) // VertexIn.position.w determines offset direction
  381. * QuadOffset_Scale_IsDetail.z; // and of course we need to scale it to the same LOD
  382.  
  383. VertexOut.uv = float2( ( pos.x + 0.5f ) / MAP_SIZE_X, ( pos.y + 0.5f ) / MAP_SIZE_Y );
  384. VertexOut.uv2.x = ( pos.x + 0.5f ) / MAP_SIZE_X;
  385. VertexOut.uv2.y = ( pos.y + 0.5f - MAP_SIZE_Y ) / -MAP_SIZE_Y;
  386. VertexOut.uv2.xy *= float2( MAP_POW2_X, MAP_POW2_Y ); //POW2
  387.  
  388. float vHeight = VertexIn.height.x * vUseAltHeight - VertexIn.height.x;
  389. vHeight = VertexIn.height.y * vUseAltHeight - vHeight;
  390.  
  391. vHeight *= 0.01f;
  392. VertexOut.prepos = float3( pos.x, vHeight, pos.y );
  393. VertexOut.position = mul( ViewProjectionMatrix, float4( VertexOut.prepos, 1.0f ) );
  394. #endif // USE_VERTEX_TEXTURE
  395.  
  396. VertexOut.vShadowProj = mul( ShadowMapTextureMatrix, float4( VertexOut.prepos, 1.0f ) );
  397.  
  398. // Output the screen-space texture coordinates
  399. float fHalfW = VertexOut.position.w * 0.5;
  400. VertexOut.vScreenCoord.x = ( VertexOut.position.x * 0.5 + fHalfW );
  401. VertexOut.vScreenCoord.y = ( fHalfW - VertexOut.position.y * 0.5 );
  402. #ifdef PDX_OPENGL
  403. VertexOut.vScreenCoord.y = -VertexOut.vScreenCoord.y;
  404. #endif
  405. VertexOut.vScreenCoord.z = VertexOut.position.w;
  406. VertexOut.vScreenCoord.w = VertexOut.position.w;
  407.  
  408. return VertexOut;
  409. }
  410. ]]
  411. }
  412.  
  413. ## Pixel Shaders
  414.  
  415. PixelShader =
  416. {
  417. MainCode PixelShaderUnderwater
  418. [[
  419. float4 main( VS_OUTPUT_TERRAIN Input ) : PDX_COLOR
  420. {
  421. clip( WATER_HEIGHT - Input.prepos.y + TERRAIN_WATER_CLIP_HEIGHT );
  422. float3 normal = normalize( tex2D( HeightNormal,Input.uv2 ).rbg - 0.5f );
  423. float3 diffuseColor = tex2D( TerrainDiffuse, Input.uv2 * float2(( MAP_SIZE_X / 32.0f ), ( MAP_SIZE_Y / 32.0f ) ) ).rgb;
  424. float3 waterColorTint = tex2D( TerrainColorTint, Input.uv2 ).rgb;
  425.  
  426. float vMin = 17.0f;
  427. float vMax = 18.5f;
  428. float vWaterFog = saturate( 1.0f - ( Input.prepos.y - vMin ) / ( vMax - vMin ) );
  429.  
  430. diffuseColor = lerp( diffuseColor, waterColorTint, vWaterFog );
  431. float vFog = saturate( Input.prepos.y * Input.prepos.y * Input.prepos.y * WATER_HEIGHT_RECP_SQUARED * WATER_HEIGHT_RECP );
  432. float3 vOut = CalculateMapLighting( diffuseColor, normal * vFog );
  433.  
  434. return float4( vOut, 1.0f );
  435. }
  436. ]]
  437.  
  438. MainCode PixelShaderTerrain
  439. [[
  440. float3 ApplyMonsoon( float3 vColor, float3 vPos, inout float3 vNormal, float3 vMudNormal, float4 vFoWColor, float TerrainDiffuseAlpha, float3 vMonsoonDiffuse )
  441. {
  442. float vFade = saturate( vPos.y - 18.0f );
  443. float vNormalFade = saturate( saturate( vNormal.y + 0.9f ) * 10.0f );
  444.  
  445. float vNoise = tex2D( FoWDiffuse, ( vPos.xz + 0.5f ) / 100.0f ).r;
  446. float FoWDiffuseColor = tex2D( FoWDiffuse, ( vPos.xz + 0.5f ) / 10.0f ).r;
  447.  
  448. float vIsMonsoon = lerp( vFoWColor.b, vFoWColor.g, vFoWOpacity_Time.z ) * 0.70;
  449.  
  450. vNoise += saturate( vPos.y - 220.0f )*( saturate( (vNormal.y-0.9f) * 1000.0f )*vIsMonsoon );
  451. vNoise = saturate( vNoise );
  452.  
  453. float vHeavy = saturate( saturate( vNoise - ( 1.0f - vIsMonsoon ) ) * 5.0f );
  454. float vLight = saturate( ( saturate( vNoise + 0.5f ) - ( 1.0f - vIsMonsoon ) ) * 1.4f );
  455.  
  456. float vHeightFade = 1.0f - saturate( vPos.y - 24.0f );
  457.  
  458. float3 vMonsoonColor = vMonsoonDiffuse * ( 0.9f + 0.1f * FoWDiffuseColor);
  459. float vMonsoonStrength = saturate( vHeavy + vLight ) * vFade * vNormalFade * ( saturate( vIsMonsoon * 2.25f ) ) * vHeightFade * 0.8;
  460. vColor = lerp( vColor, vMonsoonColor, vMonsoonStrength );
  461. vNormal = lerp( vNormal, vMudNormal, saturate( vMonsoonStrength * 2.0 ) );
  462. return vColor;
  463. }
  464.  
  465. float4 main( VS_OUTPUT_TERRAIN Input ) : PDX_COLOR
  466. {
  467. #ifndef MAP_IGNORE_CLIP_HEIGHT
  468. clip( Input.prepos.y + TERRAIN_WATER_CLIP_HEIGHT - WATER_HEIGHT );
  469. #endif
  470. float fTI;
  471. float4 vFoWColor, vTIColor, vMonsoonColor;
  472. if( !GetFoWAndTI( Input.prepos, vFoWColor, vMonsoonColor, fTI, vTIColor ) )
  473. {
  474. return float4( vTIColor.rgb, 1.0f );
  475. }
  476.  
  477. float2 vOffsets = float2( -0.5f / MAP_SIZE_X, -0.5f / MAP_SIZE_Y );
  478.  
  479. float vAllSame;
  480. float4 IndexU, IndexV;
  481. calculate_index( tex2D( TerrainIDMap, Input.uv + vOffsets.xy ), IndexU, IndexV, vAllSame );
  482.  
  483. float2 vTileRepeat = Input.uv2 * TERRAIN_TILE_FREQ;
  484. vTileRepeat.x *= MAP_SIZE_X/MAP_SIZE_Y;
  485.  
  486. float lod = clamp( trunc( mipmapLevel( vTileRepeat ) - 0.5f ), 0.0f, 6.0f );
  487. float vMipTexels = pow( 2.0f, ATLAS_TEXEL_POW2_EXPONENT - lod );
  488. float3 vHeightNormalSample = normalize( tex2D( HeightNormal, Input.uv2 ).rbg - 0.5f );
  489.  
  490. float4 vTerrainSamplePosition = sample_terrain( IndexU.w, IndexV.w, vTileRepeat, vMipTexels, lod, NUM_TILES );
  491. float4 vTerrainDiffuseSample = tex2Dlod( TerrainDiffuse, vTerrainSamplePosition );
  492.  
  493. float4 vMudSamplePosition = sample_terrain( IndexU.w, IndexV.w, vTileRepeat, vMipTexels, lod, 1.0f );
  494. float4 vMudDiffuseSample = tex2Dlod( MudDiffuse, vMudSamplePosition );
  495. float4 vMudNormalSamplePosition = sample_terrain( 2.0f, 3.0f, vTileRepeat, vMipTexels, lod, NUM_TILES);
  496.  
  497. #ifdef TERRAIN_SHADER
  498. #ifdef NO_SHADER_TEXTURE_LOD
  499. float3 vTerrainNormalSample = float3( 0, 1, 0 );
  500. float3 vMudNormalSample = vTerrainNormalSample;
  501. #else
  502. float3 vTerrainNormalSample = tex2Dlod( TerrainNormal, vTerrainSamplePosition ).rbg - 0.5f;
  503. float3 vMudNormalSample = tex2Dlod( TerrainNormal, vMudNormalSamplePosition ).rgb - 0.5f;
  504. #endif //NO_SHADER_TEXTURE_LOD
  505. #endif
  506. #ifdef COLOR_SHADER
  507. float4 vColorMapSample = GetProvinceColorSampled( Input.uv, IndirectionMap, ProvinceIndirectionMapSize, ProvinceColorMap, ProvinceColorMapSize );
  508. #endif
  509.  
  510. if ( vAllSame < 1.0f && vBorderLookup_HeightScale_UseMultisample_SeasonLerp.z < 8.0f )
  511. {
  512. float4 TerrainSampleX = sample_terrain( IndexU.x, IndexV.x, vTileRepeat, vMipTexels, lod, NUM_TILES );
  513. float4 TerrainSampleY = sample_terrain( IndexU.y, IndexV.y, vTileRepeat, vMipTexels, lod, NUM_TILES );
  514. float4 TerrainSampleZ = sample_terrain( IndexU.z, IndexV.z, vTileRepeat, vMipTexels, lod, NUM_TILES );
  515. float4 ColorRD = tex2Dlod( TerrainDiffuse, TerrainSampleX );
  516. float4 ColorLU = tex2Dlod( TerrainDiffuse, TerrainSampleY );
  517. float4 ColorRU = tex2Dlod( TerrainDiffuse, TerrainSampleZ );
  518.  
  519. float2 vFracVector = float2( Input.uv.x * MAP_SIZE_X - 0.5f, Input.uv.y * MAP_SIZE_Y - 0.5f );
  520. float2 vFrac = frac( vFracVector );
  521.  
  522. const float vAlphaFactor = 10.0f;
  523. float4 vTestFrac = float4( vFrac.x, 1.0f - vFrac.x, vFrac.x, 1.0f - vFrac.x );
  524. float4 vTestRemainder = float4(
  525. 1.0f + ColorLU.a * vAlphaFactor,
  526. 1.0f + ColorRU.a * vAlphaFactor,
  527. 1.0f + vTerrainDiffuseSample.a * vAlphaFactor,
  528. 1.0f + ColorRD.a * vAlphaFactor );
  529. float4 vTest = vTestFrac * vTestRemainder;
  530. float2 yWeights = float2( ( vTest.x + vTest.y ) * vFrac.y, ( vTest.z + vTest.w ) * ( 1.0f - vFrac.y ) );
  531. float3 vBlendFactors = float3( vTest.x / ( vTest.x + vTest.y ),
  532. vTest.z / ( vTest.z + vTest.w ),
  533. yWeights.x / ( yWeights.x + yWeights.y ) );
  534. vTerrainDiffuseSample = lerp(
  535. lerp( ColorRU, ColorLU, vBlendFactors.x ),
  536. lerp( ColorRD, vTerrainDiffuseSample, vBlendFactors.y ),
  537. vBlendFactors.z );
  538.  
  539. #ifdef TERRAIN_SHADER
  540. #ifndef NO_SHADER_TEXTURE_LOD
  541. float3 terrain_normalRD = tex2Dlod( TerrainNormal, TerrainSampleX ).rbg - 0.5f;
  542. float3 terrain_normalLU = tex2Dlod( TerrainNormal, TerrainSampleY ).rbg - 0.5f;
  543. float3 terrain_normalRU = tex2Dlod( TerrainNormal, TerrainSampleZ ).rbg - 0.5f;
  544.  
  545. vTerrainNormalSample =
  546. ( ( 1.0f - vBlendFactors.x ) * terrain_normalRU + vBlendFactors.x * terrain_normalLU ) * ( 1.0f - vBlendFactors.z ) +
  547. ( ( 1.0f - vBlendFactors.y ) * terrain_normalRD + vBlendFactors.y * vTerrainNormalSample ) * vBlendFactors.z;
  548. #endif
  549. #endif
  550. }
  551.  
  552. float3 TerrainColor = lerp( tex2D( TerrainColorTint, Input.uv2 ), tex2D( TerrainColorTintSecond, Input.uv2 ), vBorderLookup_HeightScale_UseMultisample_SeasonLerp.w ).rgb;
  553. float3 vOut;
  554. #ifdef TERRAIN_SHADER
  555. #ifdef TERRAIN_AND_COLOR_SHADER
  556. const float fTestThreshold = 0.82f;
  557. if( vColorMapSample.a < fTestThreshold )
  558. #endif
  559. {
  560. vHeightNormalSample = CalcNormalForLighting( vHeightNormalSample, vTerrainNormalSample );
  561.  
  562. vTerrainDiffuseSample.rgb = GetOverlay( vTerrainDiffuseSample.rgb, TerrainColor, 0.75f );
  563. vTerrainDiffuseSample.rgb = ApplySnow( vTerrainDiffuseSample.rgb, Input.prepos, vHeightNormalSample, vFoWColor, FoWDiffuse );
  564. vTerrainDiffuseSample.rgb = ApplyMonsoon( vTerrainDiffuseSample.rgb, Input.prepos, vHeightNormalSample, vMudNormalSample, vMonsoonColor, vTerrainDiffuseSample.a, vMudDiffuseSample.rgb );
  565. vTerrainDiffuseSample.rgb = calculate_secondary_compressed( Input.uv, vTerrainDiffuseSample.rgb, Input.prepos.xz );
  566.  
  567. vOut = CalculateMapLighting( vTerrainDiffuseSample.rgb, vHeightNormalSample );
  568. }
  569. #endif // end TERRAIN_SHADER
  570. #ifdef COLOR_SHADER
  571. #ifdef TERRAIN_AND_COLOR_SHADER
  572. else
  573. #endif
  574. {
  575. vTerrainDiffuseSample.rgb = GetOverlay( vTerrainDiffuseSample.rgb, TerrainColor, 0.5f );
  576.  
  577. // Adjusts the normal, to also use a bit of the texture normal, and not only the heighmap of the mesh
  578. float2 vBlend_normal = float2( 0.2f, 0.8f );
  579.  
  580. float3 vHeightNormalSample_adj = normalize( tex2D( HeightNormal, Input.uv2 ).rbg - 0.5f ) * vBlend_normal.x + ( 1.0f - vBlend_normal.x );
  581. float3 vTerrainNormalSample_adj = ( tex2Dlod( TerrainNormal, vTerrainSamplePosition ).rbg - 0.5f ) * vBlend_normal.y + ( 1.0f - vBlend_normal.y );
  582.  
  583. float2 vBlend_normal2 = float2( 0.2f, 2.0f );
  584. vHeightNormalSample_adj = CalcNormalForLighting( vHeightNormalSample * vBlend_normal2.x, normalize( vTerrainNormalSample_adj ) * vBlend_normal2.y );
  585.  
  586. // Handle snow, if you don't want snow, add comments to line 592-593
  587. float3 vHeightNormalSample_snow = normalize( tex2D( HeightNormal, Input.uv2 ).rbg - 0.5f );
  588. float3 vTerrainNormalSample_snow = tex2Dlod( TerrainNormal, vTerrainSamplePosition ).rbg - 0.5f;
  589. vHeightNormalSample_snow = CalcNormalForLighting( vHeightNormalSample_snow, normalize( vTerrainNormalSample_snow ) );
  590.  
  591. // Applies snow
  592. vTerrainDiffuseSample.rgb = ApplySnow( vTerrainDiffuseSample.rgb, Input.prepos, vHeightNormalSample_snow, vFoWColor, FoWDiffuse );
  593. vTerrainDiffuseSample.rgb = calculate_secondary_compressed( Input.uv, vTerrainDiffuseSample.rgb, Input.prepos.xz );
  594.  
  595. // "flux" parameters, turns the map ever so slightly yellow / warm
  596. float3 vBlend_flux = float3( 1.02f, 1.0f, 0.98f );
  597.  
  598. // Gets the modified color for the mapmode
  599. float3 vColorMapSample_blended = vColorMapSample;
  600.  
  601. // Samples the green channel in the terrain, and adjusts accordingly
  602. float vTerrainDiffuseSample_green = min( vTerrainDiffuseSample.g * 2.0f + 0.2f, 1.0f);
  603.  
  604. // Converts the terrain to greyscale
  605. float vTerrainDiffuseSample_grey = min( ( 0.2989f * vTerrainDiffuseSample.r + 0.5870f * vTerrainDiffuseSample.g + 0.1140f * vTerrainDiffuseSample.b), 0.7f );
  606.  
  607. // Makes a blend adjustment between country color and greyscale color
  608. float2 vBlend_saturation = float2( 0.57f, 0.30f );
  609.  
  610. // Calculates the combined mapmode and terraincolor
  611. vOut = float3( min( ( vColorMapSample_blended.r * vBlend_saturation.x * vTerrainDiffuseSample_green + vTerrainDiffuseSample_grey * vBlend_saturation.y ) * vBlend_flux.r , 1.0f ) ,
  612. min( ( vColorMapSample_blended.g * vBlend_saturation.x * vTerrainDiffuseSample_green + vTerrainDiffuseSample_grey * vBlend_saturation.y ) * vBlend_flux.g , 1.0f ) ,
  613. min( ( vColorMapSample_blended.b * vBlend_saturation.x * vTerrainDiffuseSample_green + vTerrainDiffuseSample_grey * vBlend_saturation.y ) * vBlend_flux.b , 1.0f ) );
  614.  
  615. // Combines the calculated colors with the normal shading, by taking percentages of each part and adding
  616. float2 vBlend_normal3 = float2( 0.75f, 0.35f );
  617. float3 vOut2 = CalculateMapLighting( vOut, vHeightNormalSample_adj );
  618. vOut = vOut * vBlend_normal3.x + vOut2 * vBlend_normal3.y;
  619.  
  620. // Clamps the color to not entirely blinding white in certain countries
  621. vOut = float3( min( vOut.r, 0.80f ) ,
  622. min( vOut.g, 0.80f ) ,
  623. min( vOut.b, 0.80f ) );
  624.  
  625. // No clue what this line does, was in the original shader so kept it
  626. vOut = calculate_secondary( Input.uv, vOut, Input.prepos.xz );
  627. }
  628. #endif // end COLOR_SHADER
  629.  
  630. // Grab the shadow term
  631. float fShadowTerm = GetShadowScaled( SHADOW_WEIGHT_MAP, Input.vScreenCoord, ShadowMap );
  632. vOut *= fShadowTerm;
  633.  
  634. vOut = ApplyDistanceFog( vOut, Input.prepos, vFoWColor, FoWDiffuse );
  635. return float4( lerp( ComposeSpecular( vOut, 0.0f ), vTIColor.rgb, fTI ), 1.0f );
  636. }
  637. ]]
  638.  
  639. MainCode PixelShaderTerrainUnlit
  640. [[
  641. float4 main( VS_OUTPUT_TERRAIN Input ) : PDX_COLOR
  642. {
  643. // Grab the shadow term
  644. float fShadowTerm = CalculateShadow( Input.vShadowProj, ShadowMap );
  645. return float4( fShadowTerm, fShadowTerm, fShadowTerm, 1.0f );
  646. }
  647. ]]
  648. }
  649.  
  650.  
  651. ## Blend States
  652.  
  653. BlendState BlendState
  654. {
  655. AlphaTest = no
  656. BlendEnable = no
  657. WriteMask = "RED|GREEN|BLUE|ALPHA"
  658. }
  659.  
  660. ## Rasterizer States
  661.  
  662. ## Depth Stencil States
  663.  
  664. ## Effects
  665.  
  666. Effect terrainunlit
  667. {
  668. VertexShader = "VertexShader"
  669. PixelShader = "PixelShaderTerrainUnlit"
  670. }
  671.  
  672. Effect terrain
  673. {
  674. VertexShader = "VertexShader"
  675. PixelShader = "PixelShaderTerrain"
  676. }
  677.  
  678. Effect underwater
  679. {
  680. VertexShader = "VertexShader"
  681. PixelShader = "PixelShaderUnderwater"
  682. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement