Stochastic Filter for Hiding Texture Tiling

Shader code
//Adapted from this pastebin
//https://pastebin.com/sDrnzYxB
//hash for randomness
vec2 hash2D2D (vec2 s)
{
	//magic numbers
	vec2 i = vec2(0.0);
	return fract(sin(modf(vec2(dot(s, vec2(127.1,311.7)), dot(s, vec2(269.5,183.3))) / 3.14159, i))*43758.5453);
}

//stochastic sampling
vec4 tex2DStochastic(sampler2D tex, vec2 uv, vec2 r, vec2 dx, vec2 dy, int samples, bool aniso)
{
	//triangle vertices and blend weights
	//BW_vx[0...2].xyz = triangle verts
	//BW_vx[3].xy = blend weights (z is unused)
	mat4 BW_vx;

	//uv transformed into triangular grid space with UV scaled by approximation of 2*sqrt(3)
	vec2 skewUV = mat2(vec2(1.0 , 0.0) , vec2(-0.57735027, 1.15470054 )) * (uv * 3.464);

	//vertex IDs and barycentric coords
	vec2 vxID = vec2 (floor(skewUV));
	vec3 barry = vec3 (fract(skewUV), 0);
	barry.z = 1.0-barry.x-barry.y;

	BW_vx = ((barry.z > 0.0) ?
		mat4(vec4(vxID, 0, 0), vec4(vxID + vec2(0, 1), 0, 0), vec4(vxID + vec2(1, 0), 0, 0), vec4(barry, 0)) :
		mat4(vec4(vxID + vec2 (1, 1), 0, 0), vec4(vxID + vec2 (1, 0), 0, 0), vec4(vxID + vec2 (0, 1), 0, 0), vec4(-barry.z, 1.0-barry.y, 1.0-barry.x, 0.0)));

	//blend samples with calculated weights
    return textureGrad(tex, uv + hash2D2D(BW_vx[0].xy), dx, dy) * BW_vx[3].x +
        textureGrad(tex, uv + hash2D2D(BW_vx[1].xy), dx, dy) * BW_vx[3].y +
        textureGrad(tex, uv + hash2D2D(BW_vx[2].xy), dx, dy) * BW_vx[3].z;
}
Tags
repeat, sampler, seams, stochastic, texture, tiling
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from granitrocky

Anisotropic Filter

N64 3 Point Filtering

Related shaders

Stochastic Procedural Texture Shader

Screen Tiling

Triplanar Stochastic Terrain Shader

Subscribe
Notify of
guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
RembotGames
2 months ago

this will be really handy in the future – thank you for your contribution!

Jutraim
Jutraim
1 month ago

You have redundant arguments in tex2DStochastic function. What does ‘vec2 r’ ‘bool aniso’ or ‘int samples’ even there for? They’re unused. Dx and Dy should not be an input as well since they don’t seem to change anything. Maybe I am dumb, can you explain why you made these choices?

elvisish
26 days ago
Reply to  Jutraim

Probably during testing, they don’t cause any problems and you can just remove them?