Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Created August 23, 2014 08:10
Show Gist options
  • Save capnslipp/02536fd06afe9428c381 to your computer and use it in GitHub Desktop.
Save capnslipp/02536fd06afe9428c381 to your computer and use it in GitHub Desktop.
Fragment-phase texture point-filtering, as a Unity GLSL shader
/// @creator: Slipp Douglas Thompson
/// @license: WTFPL
/// @why: Too trivial to care.
/// @intersource: https://gist.github.com/capnslipp/02536fd06afe9428c381
Shader "Texture Stepped" {
Properties {
_MainTex ("Base (RGBA)", 2D) = "gray" {}
_Step ("Step (XY)", Vector) = (32, 32, 0, 0)
}
SubShader {
Tags {}
Lighting Off
ZWrite On
Pass {
GLSLPROGRAM
/// @fix: explicit TRANSFORM_TEX(); Unity's preprocessor chokes when attempting to use the TRANSFORM_TEX() macro in UnityCG.glslinc
/// (as of Unity 4.5.0f6; issue dates back to 2011 or earlier: http://forum.unity3d.com/threads/glsl-transform_tex-and-tiling.93756/)
vec2 transformTex(vec4 texCoord, vec4 texST) {
return (texCoord.xy * texST.xy + texST.zw);
}
#ifdef VERTEX
uniform vec4 _MainTex_ST;
#ifdef SHADER_API_GLES3
out vec2 vTextureCoords;
#else
varying vec2 vTextureCoords;
#endif
void main()
{
vTextureCoords = transformTex(gl_MultiTexCoord0, _MainTex_ST);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#endif
#ifdef FRAGMENT
uniform sampler2D _MainTex;
uniform ivec2 _Step;
#ifdef SHADER_API_GLES3
in vec2 vTextureCoords;
#else
varying vec2 vTextureCoords;
#endif
void main()
{
vec2 step_v2 = vec2(_Step);
vec2 uv = floor(vTextureCoords * step_v2) / step_v2;
gl_FragColor = texture2D(_MainTex, uv);
}
#endif
ENDGLSL
}
}
Fallback Off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment