Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Last active June 3, 2024 15:20
Show Gist options
  • Save cecilemuller/b1268001369d4e9b476caceede8da05e to your computer and use it in GitHub Desktop.
Save cecilemuller/b1268001369d4e9b476caceede8da05e to your computer and use it in GitHub Desktop.
Cross-hatching shader for Marmoset Toolbag 4
// Based on http://research.microsoft.com/en-us/um/people/hoppe/hatching.pdf
// and https://github.com/spite/cross-hatching
#include "data/shader/mat/state.frag"
USE_TEXTURE2D(tHatch0);
USE_TEXTURE2D(tHatch1);
USE_TEXTURE2D(tHatch2);
USE_TEXTURE2D(tHatch3);
USE_TEXTURE2D(tHatch4);
USE_TEXTURE2D(tHatch5);
void CrossHatching( inout FragmentState s )
{
vec2 vUv = s.vertexTexCoord.xy;
float rim = saturate(dot(s.normal, s.vertexEye));
rim *= rim;
vec4 c = vec4(1.0, 1.0, 1.0, 1.0);
float step = 1.0 / 6.0;
if ( rim <= step ){
c = mix( texture2D( tHatch5, vUv ), texture2D( tHatch4, vUv ), 6.0 * rim );
}
if ( rim > step && rim <= 2.0 * step ){
c = mix( texture2D( tHatch4, vUv ), texture2D( tHatch3, vUv) , 6.0 * ( rim - step ) );
}
if ( rim > 2.0 * step && rim <= 3.0 * step ){
c = mix( texture2D( tHatch3, vUv ), texture2D( tHatch2, vUv ), 6.0 * ( rim - 2.0 * step ) );
}
if ( rim > 3.0 * step && rim <= 4.0 * step ){
c = mix( texture2D( tHatch2, vUv ), texture2D( tHatch1, vUv ), 6.0 * ( rim - 3.0 * step ) );
}
if ( rim > 4.0 * step && rim <= 5.0 * step ){
c = mix( texture2D( tHatch1, vUv ), texture2D( tHatch0, vUv ), 6.0 * ( rim - 4.0 * step ) );
}
if ( rim > 5.0 * step ){
c = mix( texture2D( tHatch0, vUv ), vec4(1.0, 1.0, 1.0, 1.0), 6.0 * ( rim - 5.0 * step ) );
}
s.emissiveLight = c.rgb;
}
#ifdef Emissive
#undef Emissive
#endif
#define Emissive CrossHatching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment