Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct VertexInput {
- int2 pointCoord : Position;
- };
- struct VertexToGeometry {
- int2 pointCoord : RawPosition;
- uint4 tileStatus : TileStatus; // status of the tile at the up-right of the point
- uint4 neighbourTileStatus : TileStatus2; // status of the tiles up, right, bottom and left of the current one
- bool4 drawCornerBorders : DrawBorders3; // true if a border should be drawn in the topleft/topright/bottomright/bottomleft corner inside the tile
- };
- struct GeometryToFragment {
- float4 position : SV_Position;
- uint tileTextureOffset : TextureOffset; // offset inside tileTextures
- float2 localTexCoords : LocalTextureCoords; // texture coords within the tile type texture
- bool4 drawOutsideBorders : DrawBorders; // true if a border should be drawn at the top/right/bottom/left of the tile
- bool4 drawDiagonalBorders : DrawBorders2; // true if a border should be drawn in the topleft/topright/bottomright/bottomleft diagonal inside the tile
- bool4 drawCornerBorders : DrawBorders3; // true if a border should be drawn in the topleft/topright/bottomright/bottomleft corner inside the tile
- };
- SamplerState linearSampler {
- Filter = MIN_MAG_MIP_LINEAR;
- AddressU = Wrap;
- AddressV = Wrap;
- };
- // uniform variables
- Texture2D<uint4> tileStatusTexture; // each tile has an uint4 with types at top,right,bottom,left
- Texture2DArray tileTextures; // array of textures types
- row_major float4x4 globalMatrix; // global worldview matrix
- int2 pointCoordDelta;
- static const float bordersWidth = 0.1; // width of the borders between tiles
- static const float4 bordersColor = float4(0, 0, 0, 1); // color of the borders between tiles
- static const float randomDivergenceDistance = 0.05; // max random distance a point can move
- bool highlightActivated;
- int2 highlightTile;
- static const float highlightWidth = 0.05;
- static const float4 highlightColor = float4(1, 0, 0, 1);
- float2 rand_1_05(in float2 uv) {
- return frac(sin(dot(uv, float2(12.9898, 78.233) * 2.0)) * 43758.5453);
- }
- uint getTileStatus(int2 tileCoords) {
- return tileStatusTexture.Load(int3(tileCoords, 0)).z;
- }
- VertexToGeometry vertexShaderMain(in VertexInput inputData) {
- VertexToGeometry retValue;
- retValue.pointCoord = inputData.pointCoord + pointCoordDelta;
- retValue.tileStatus = tileStatusTexture.Load(int3(inputData.pointCoord, 0));
- retValue.neighbourTileStatus.x = tileStatusTexture.Load(int3(inputData.pointCoord + int2(0, 1), 0)).z;
- retValue.neighbourTileStatus.y = tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 0), 0)).w;
- retValue.neighbourTileStatus.z = tileStatusTexture.Load(int3(inputData.pointCoord + int2(0, -1), 0)).x;
- retValue.neighbourTileStatus.w = tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 0), 0)).y;
- retValue.drawCornerBorders.x = tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 1), 0)).y != retValue.tileStatus.w &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 1), 0)).z != retValue.tileStatus.w &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 1), 0)).y != retValue.tileStatus.x &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, 1), 0)).z != retValue.tileStatus.x;
- retValue.drawCornerBorders.y = tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 1), 0)).z != retValue.tileStatus.x &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 1), 0)).w != retValue.tileStatus.x &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 1), 0)).z != retValue.tileStatus.y &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, 1), 0)).w != retValue.tileStatus.y;
- retValue.drawCornerBorders.z = tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).w != retValue.tileStatus.y &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).x != retValue.tileStatus.y &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).w != retValue.tileStatus.z &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2( 1, -1), 0)).x != retValue.tileStatus.z;
- retValue.drawCornerBorders.w = tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).x != retValue.tileStatus.z &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).y != retValue.tileStatus.z &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).x != retValue.tileStatus.w &&
- tileStatusTexture.Load(int3(inputData.pointCoord + int2(-1, -1), 0)).y != retValue.tileStatus.w;
- return retValue;
- }
- [maxvertexcount(12)]
- void geometryShaderMain(point VertexToGeometry inputData[1],
- inout TriangleStream<GeometryToFragment> outputStream)
- {
- GeometryToFragment middlePoint;
- middlePoint.position = float4(0.5, 0.5, 0, 1);
- middlePoint.localTexCoords = middlePoint.position.xy;
- middlePoint.position.xy += inputData[0].pointCoord;
- middlePoint.position.xy += fmod(rand_1_05(middlePoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
- middlePoint.position = mul(middlePoint.position, globalMatrix);
- middlePoint.drawOutsideBorders = inputData[0].tileStatus != inputData[0].neighbourTileStatus;
- middlePoint.drawCornerBorders = inputData[0].drawCornerBorders;
- middlePoint.drawDiagonalBorders.x = inputData[0].tileStatus.w != inputData[0].tileStatus.x;
- middlePoint.drawDiagonalBorders.y = inputData[0].tileStatus.x != inputData[0].tileStatus.y;
- middlePoint.drawDiagonalBorders.z = inputData[0].tileStatus.y != inputData[0].tileStatus.z;
- middlePoint.drawDiagonalBorders.w = inputData[0].tileStatus.z != inputData[0].tileStatus.w;
- [unroll]
- for (int i = 0; i < 4; ++i) {
- middlePoint.tileTextureOffset = inputData[0].tileStatus[i];
- GeometryToFragment currentPoint = middlePoint;
- currentPoint.position = float4(0, 0, 0, 1);
- if (i == 1 || i == 2)
- currentPoint.position.x = 1;
- if (i == 0 || i == 1)
- currentPoint.position.y = 1;
- currentPoint.localTexCoords = currentPoint.position.xy;
- currentPoint.position.xy += inputData[0].pointCoord;
- currentPoint.position.xy += fmod(rand_1_05(currentPoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
- currentPoint.position = mul(currentPoint.position, globalMatrix);
- outputStream.Append(currentPoint);
- GeometryToFragment nextPoint = currentPoint;
- nextPoint.position = float4(0, 0, 0, 1);
- if (i == 0 || i == 1)
- nextPoint.position.x = 1;
- if (i == 0 || i == 3)
- nextPoint.position.y = 1;
- nextPoint.localTexCoords = nextPoint.position.xy;
- nextPoint.position.xy += inputData[0].pointCoord;
- nextPoint.position.xy += fmod(rand_1_05(nextPoint.position.xy), randomDivergenceDistance * 2) - float2(randomDivergenceDistance, 0);
- nextPoint.position = mul(nextPoint.position, globalMatrix);
- outputStream.Append(nextPoint);
- outputStream.Append(middlePoint);
- outputStream.RestartStrip();
- }
- }
- float4 pixelShaderMain(in GeometryToFragment infos) : SV_Target {
- // TODO
- /*if (infos.highlighted &&
- (infos.localTexCoords.y > 1 - highlightWidth || infos.localTexCoords.y < highlightWidth ||
- infos.localTexCoords.x > 1 - highlightWidth || infos.localTexCoords.x < highlightWidth))
- return highlightColor;*/
- // horizontal borders
- if (infos.drawOutsideBorders.x && infos.localTexCoords.y > 1 - bordersWidth)
- return bordersColor;
- if (infos.drawOutsideBorders.w && infos.localTexCoords.x < bordersWidth)
- return bordersColor;
- if (infos.drawOutsideBorders.z && infos.localTexCoords.y < bordersWidth)
- return bordersColor;
- if (infos.drawOutsideBorders.y && infos.localTexCoords.x > 1 - bordersWidth)
- return bordersColor;
- // diagonal borders
- if (infos.drawDiagonalBorders.x && infos.localTexCoords.x < 0.5 && abs(1 - infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
- return bordersColor;
- if (infos.drawDiagonalBorders.y && infos.localTexCoords.x >= 0.5 && abs(infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
- return bordersColor;
- if (infos.drawDiagonalBorders.z && infos.localTexCoords.x >= 0.5 && abs(1 - infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
- return bordersColor;
- if (infos.drawDiagonalBorders.w && infos.localTexCoords.x < 0.5 && abs(infos.localTexCoords.y - infos.localTexCoords.x) < bordersWidth * 1.414)
- return bordersColor;
- // corners
- if (infos.drawCornerBorders.x && distance(infos.localTexCoords, float2(0, 1)) < bordersWidth)
- return bordersColor;
- if (infos.drawCornerBorders.y && distance(infos.localTexCoords, float2(1, 1)) < bordersWidth)
- return bordersColor;
- if (infos.drawCornerBorders.z && distance(infos.localTexCoords, float2(1, 0)) < bordersWidth)
- return bordersColor;
- if (infos.drawCornerBorders.w && distance(infos.localTexCoords, float2(0, 0)) < bordersWidth)
- return bordersColor;
- // we are not in a border (most common case), sampling texture
- float4 outputColor = tileTextures.Sample(linearSampler, float3(infos.localTexCoords, infos.tileTextureOffset));
- // adding noise
- outputColor.rgb *= 1 + fmod(rand_1_05(infos.position.xy).x, 0.08) - 0.04;
- return outputColor;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement