Techniques to reduce terrain texture repeating effect

After playing around with shader and studying some other different techniques explained here:

finally, I am satisfied with the result.



And here is the source code:

  vec2 rotateUV(vec2 uv, float rotation) {
      float mid = 0.0;
      return vec2(
      cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
      cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid

  float sum(vec3 v) { return v.x +v.y +v.z; }

  // The MIT License
  // Copyright © 2017 Inigo Quilez
  // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  // A technique to avoid texture tile repetition when using one small
  // texture to cover a huge area. Basically, it creates 8 different
  // offsets for the texture and picks two to interpolate between.
  // Unlike previous methods that tile space (
  // or, this one uses a random
  // low frequency texture (cache friendly) to pick the actual
  // texture's offset.
  // Also, this one mipmaps to something (ugly, but that's better than
  // not having mipmaps at all like in previous methods)
  // More info here:
  vec4 textureNoTile( sampler2D samp, in vec2 uv) {
      vec4 color;
      #ifdef NOISEMAP
          // sample variation pattern
          vec4 noise = texture(m_NoiseMap, m_NoiseFactor * uv); // cheap (cache friendly) lookup

          // compute index
          float index = noise.x * 8.0;
  //          float ia = floor(l); // my method
  //          float ib = ia + 1.0;
          float indexA = floor(index + 0.5);
          float indexB = floor(index);

          float fraction = fract(index);
          fraction = min(fraction, 1.0 - fraction) * 2.0;

          // compute offsets for the different virtual patterns
          vec2 offsetA = sin(vec2(3.0, 7.0) * indexA); // can replace with any other hash
          vec2 offsetB = sin(vec2(3.0, 7.0) * indexB); // can replace with any other hash

          // compute rotations for the different virtual patterns
          const float TWO_PI = 2.0 * 3.141592f;
          float angleA = (offsetA.x * offsetA.y) * TWO_PI;
          float angleB = (offsetB.x * offsetB.y) * TWO_PI;

          // compute derivatives for mip-mapping
          vec2 dx = dFdx(uv);
          vec2 dy = dFdy(uv);

          // sample the two closest virtual patterns
          vec3 colorA = textureGrad(samp, rotateUV(uv + offsetA, angleA), dx, dy).xyz;
          vec3 colorB = textureGrad(samp, rotateUV(uv + offsetB, angleB), dx, dy).xyz;

          // interpolate between the two virtual patterns
          color = vec4(mix(colorA, colorB, smoothstep(0.2, 0.8, fraction - 0.1 * sum(colorA - colorB))), 1.0);
          color = texture2D(samp, uv);

      return color;

If you have any suggestions please feel free to comment :slightly_smiling_face:

Thanks everyone for the help!


looks great!

1 Like