Advertisement
Guest User

Untitled

a guest
Apr 28th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.20 KB | None | 0 0
  1. struct VertexInput {
  2.     int2    pointCoord : Position;
  3. };
  4.            
  5. struct VertexToGeometry {
  6.     int2    pointCoord : RawPosition;
  7.     uint4   tileStatus : TileStatus;                // status of the tile at the up-right of the point
  8.     uint4   neighbourTileStatus : TileStatus2;      // status of the tiles up, right, bottom and left of the current one
  9.     bool4   drawCornerBorders : DrawBorders3;       // true if a border should be drawn in the topleft/topright/bottomright/bottomleft corner inside the tile
  10. };
  11.            
  12. struct GeometryToFragment {
  13.     float4  position : SV_Position;
  14.     uint    tileTextureOffset : TextureOffset;          // offset inside tileTextures
  15.     float2  localTexCoords : LocalTextureCoords;        // texture coords within the tile type texture
  16.     bool4   drawOutsideBorders : DrawBorders;           // true if a border should be drawn at the top/right/bottom/left of the tile
  17.     bool4   drawDiagonalBorders : DrawBorders2;         // true if a border should be drawn in the topleft/topright/bottomright/bottomleft diagonal inside the tile
  18.     bool4   drawCornerBorders : DrawBorders3;           // true if a border should be drawn in the topleft/topright/bottomright/bottomleft corner inside the tile
  19. };
  20.            
  21. SamplerState linearSampler {
  22.     Filter = MIN_MAG_MIP_LINEAR;
  23.     AddressU = Wrap;
  24.     AddressV = Wrap;
  25. };
  26.            
  27. // uniform variables
  28. Texture2D<uint4>            tileStatusTexture;      // each tile has an uint4 with types at top,right,bottom,left
  29. Texture2DArray              tileTextures;           // array of textures types
  30. row_major float4x4          globalMatrix;           // global worldview matrix
  31. int2                        pointCoordDelta;
  32.  
  33. static const float          bordersWidth = 0.1;                     // width of the borders between tiles
  34. static const float4         bordersColor = float4(0, 0, 0, 1);      // color of the borders between tiles
  35. static const float          randomDivergenceDistance = 0.05;        // max random distance a point can move
  36.  
  37. bool                        highlightActivated;
  38. int2                        highlightTile;
  39. static const float          highlightWidth = 0.05;
  40. static const float4         highlightColor = float4(1, 0, 0, 1);
  41.            
  42.            
  43. float2 rand_1_05(in float2 uv) {
  44.     return frac(sin(dot(uv, float2(12.9898, 78.233) * 2.0)) * 43758.5453);
  45. }
  46.            
  47. uint getTileStatus(int2 tileCoords) {
  48.     return tileStatusTexture.Load(int3(tileCoords, 0)).z;
  49. }
  50.            
  51. VertexToGeometry vertexShaderMain(in VertexInput inputData) {
  52.     VertexToGeometry retValue;
  53.     retValue.pointCoord = inputData.pointCoord + pointCoordDelta;
  54.     retValue.tileStatus = tileStatusTexture.Load(int3(inputData.pointCoord, 0));
  55.     retValue.neighbourTileStatus.x = tileStatusTexture.Load(int3(inputData.pointCoord + int2(0,  1), 0)).z;
  56.     retValue.neighbourTileStatus.y = tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 0), 0)).w;
  57.     retValue.neighbourTileStatus.z = tileStatusTexture.Load(int3(inputData.pointCoord + int2(0, -1), 0)).x;
  58.     retValue.neighbourTileStatus.w = tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 0), 0)).y;
  59.  
  60.     retValue.drawCornerBorders.x =  tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1,  1), 0)).y != retValue.tileStatus.w &&
  61.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1,  1), 0)).z != retValue.tileStatus.w &&
  62.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1,  1), 0)).y != retValue.tileStatus.x &&
  63.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1,  1), 0)).z != retValue.tileStatus.x;
  64.     retValue.drawCornerBorders.y =  tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1,  1), 0)).z != retValue.tileStatus.x &&
  65.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1,  1), 0)).w != retValue.tileStatus.x &&
  66.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1,  1), 0)).z != retValue.tileStatus.y &&
  67.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1,  1), 0)).w != retValue.tileStatus.y;
  68.     retValue.drawCornerBorders.z =  tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).w != retValue.tileStatus.y &&
  69.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).x != retValue.tileStatus.y &&
  70.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).w != retValue.tileStatus.z &&
  71.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).x != retValue.tileStatus.z;
  72.     retValue.drawCornerBorders.w =  tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).x != retValue.tileStatus.z &&
  73.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).y != retValue.tileStatus.z &&
  74.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).x != retValue.tileStatus.w &&
  75.                                     tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).y != retValue.tileStatus.w;
  76.     return retValue;
  77. }
  78.            
  79. [maxvertexcount(12)]
  80. void geometryShaderMain(point VertexToGeometry inputData[1],
  81.                         inout TriangleStream<GeometryToFragment> outputStream)
  82. {
  83.     GeometryToFragment middlePoint;
  84.     middlePoint.position = float4(0.5, 0.5, 0, 1);
  85.     middlePoint.localTexCoords = middlePoint.position.xy;
  86.     middlePoint.position.xy += inputData[0].pointCoord;
  87.     middlePoint.position.xy += fmod(rand_1_05(middlePoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
  88.     middlePoint.position = mul(middlePoint.position, globalMatrix);
  89.  
  90.     middlePoint.drawOutsideBorders = inputData[0].tileStatus != inputData[0].neighbourTileStatus;
  91.     middlePoint.drawCornerBorders = inputData[0].drawCornerBorders;
  92.     middlePoint.drawDiagonalBorders.x = inputData[0].tileStatus.w != inputData[0].tileStatus.x;
  93.     middlePoint.drawDiagonalBorders.y = inputData[0].tileStatus.x != inputData[0].tileStatus.y;
  94.     middlePoint.drawDiagonalBorders.z = inputData[0].tileStatus.y != inputData[0].tileStatus.z;
  95.     middlePoint.drawDiagonalBorders.w = inputData[0].tileStatus.z != inputData[0].tileStatus.w;
  96.                
  97.                
  98.     [unroll]
  99.     for (int i = 0; i < 4; ++i) {
  100.         middlePoint.tileTextureOffset = inputData[0].tileStatus[i];
  101.  
  102.         GeometryToFragment currentPoint = middlePoint;
  103.         currentPoint.position = float4(0, 0, 0, 1);
  104.         if (i == 1 || i == 2)
  105.             currentPoint.position.x = 1;
  106.         if (i == 0 || i == 1)
  107.             currentPoint.position.y = 1;
  108.         currentPoint.localTexCoords = currentPoint.position.xy;
  109.         currentPoint.position.xy += inputData[0].pointCoord;
  110.         currentPoint.position.xy += fmod(rand_1_05(currentPoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
  111.         currentPoint.position = mul(currentPoint.position, globalMatrix);
  112.         outputStream.Append(currentPoint);
  113.                    
  114.         GeometryToFragment nextPoint = currentPoint;
  115.         nextPoint.position = float4(0, 0, 0, 1);
  116.         if (i == 0 || i == 1)
  117.             nextPoint.position.x = 1;
  118.         if (i == 0 || i == 3)
  119.             nextPoint.position.y = 1;
  120.         nextPoint.localTexCoords = nextPoint.position.xy;
  121.         nextPoint.position.xy += inputData[0].pointCoord;
  122.         nextPoint.position.xy += fmod(rand_1_05(nextPoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
  123.         nextPoint.position = mul(nextPoint.position, globalMatrix);
  124.         outputStream.Append(nextPoint);
  125.                    
  126.         outputStream.Append(middlePoint);
  127.         outputStream.RestartStrip();
  128.     }
  129. }
  130.            
  131. float4 pixelShaderMain(in GeometryToFragment infos) : SV_Target {
  132.     // TODO
  133.     /*if (infos.highlighted &&
  134.             (infos.localTexCoords.y > 1 - highlightWidth || infos.localTexCoords.y < highlightWidth ||
  135.             infos.localTexCoords.x > 1 - highlightWidth || infos.localTexCoords.x < highlightWidth))
  136.         return highlightColor;*/
  137.                
  138.     // horizontal borders
  139.     if (infos.drawOutsideBorders.x && infos.localTexCoords.y > 1 - bordersWidth)
  140.         return bordersColor;
  141.     if (infos.drawOutsideBorders.w && infos.localTexCoords.x < bordersWidth)
  142.         return bordersColor;
  143.     if (infos.drawOutsideBorders.z && infos.localTexCoords.y < bordersWidth)
  144.         return bordersColor;
  145.     if (infos.drawOutsideBorders.y && infos.localTexCoords.x > 1 - bordersWidth)
  146.         return bordersColor;
  147.                
  148.     // diagonal borders
  149.     if (infos.drawDiagonalBorders.x && infos.localTexCoords.x < 0.5 && abs(1 - infos.localTexCoords.y - infos.localTexCoords.x) <  bordersWidth * 1.414)
  150.         return bordersColor;
  151.     if (infos.drawDiagonalBorders.y && infos.localTexCoords.x >= 0.5 && abs(infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
  152.         return bordersColor;
  153.     if (infos.drawDiagonalBorders.z && infos.localTexCoords.x >= 0.5 && abs(1 - infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
  154.         return bordersColor;
  155.     if (infos.drawDiagonalBorders.w && infos.localTexCoords.x < 0.5 && abs(infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
  156.         return bordersColor;
  157.                
  158.     // corners
  159.     if (infos.drawCornerBorders.x && distance(infos.localTexCoords, float2(0, 1)) < bordersWidth)
  160.         return bordersColor;
  161.     if (infos.drawCornerBorders.y && distance(infos.localTexCoords, float2(1, 1)) < bordersWidth)
  162.         return bordersColor;
  163.     if (infos.drawCornerBorders.z && distance(infos.localTexCoords, float2(1, 0)) < bordersWidth)
  164.         return bordersColor;
  165.     if (infos.drawCornerBorders.w && distance(infos.localTexCoords, float2(0, 0)) < bordersWidth)
  166.         return bordersColor;
  167.                
  168.     // we are not in a border (most common case), sampling texture
  169.     float4 outputColor = tileTextures.Sample(linearSampler, float3(infos.localTexCoords, infos.tileTextureOffset));
  170.  
  171.     // adding noise
  172.     outputColor.rgb *= 1 + fmod(rand_1_05(infos.position.xy).x, 0.08) - 0.04;
  173.     return outputColor;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement