Skip to content

Instantly share code, notes, and snippets.

void clip( float clipValue, float alphaThreshold, int type )
{
// switch operation because were only value checking type
switch( type )
{
case 0: // 0 means less than
if( clipValue < alphaThreshold ) discard;
break;
case 1: // 1 means greater than
@otanodesignco
otanodesignco / radialshear.glsl
Created October 1, 2024 04:18
Radial shear ported from unity shader graph node to glsl
vec2 radialShear( vec2 uv, vec2 center, vec2 strength, vec2 offset )
{
vec2 delta = uv - center;
float delta2 = dot( delta.xy, delta.xy );
vec2 deltaOffset = delta2 * strength;
return uv + vec2( delta.y, -delta.x) * deltaOffset + offset;
}
@otanodesignco
otanodesignco / twirl.glsl
Created October 1, 2024 04:17
Twirl function for glsl ported from unity shader graph node
vec2 twirl( vec2 uv, vec2 center, float strength, vec2 offset )
{
vec2 delta = uv - center;
float angle = strength * length(delta);
float x = cos(angle) * delta.x - sin(angle) * delta.y;
float y = sin(angle) * delta.x + cos(angle) * delta.y;
return vec2(x + center.x + offset.x, y + center.y + offset.y);
}
@otanodesignco
otanodesignco / simplenoise.glsl
Created October 1, 2024 04:14
simple noise ported from unity to glsl
float random(vec2 n) {
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}
float interpolate( float a, float b, float t )
{
return ( 1.0 - t ) * a + ( t * b );
}
float valueNoise( vec2 uv )
@otanodesignco
otanodesignco / negate.glsl
Created August 8, 2023 06:14
Negate function to simulate unity negate node in glsl
float negate( float value )
{
return -1.0 * value;
}
@otanodesignco
otanodesignco / vertex.glsl
Created August 3, 2023 06:50
How to transform normals to world space for lighting
out vec3 vWorldSpaceNormal;
void main()
{
vec4 worldSpaceNormal = modelMatrix * vec4( normal, 0.0 );
vWorldSpaceNormal = normalize( worldSpaceNormal.xyz );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
@otanodesignco
otanodesignco / vertex.glsl
Created August 2, 2023 07:23
glsl view position of the camera based on the model position in three.js
/* calculate view position of the camera */
out vec3 vViewDirection;
void main()
{
vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
@otanodesignco
otanodesignco / vertex.glsl
Created August 2, 2023 07:20
calculate surface normals in three.js
/* calculate surface normals shader material three.js */
out vec3 vSurfaceNormal;
void main()
{
vSurfaceNormal = normalize( normalMatrix * normal );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1. );
@otanodesignco
otanodesignco / fragment.glsl
Last active August 1, 2023 06:42
Calculate the screen space uv to map a texture over the mesh regardless of the mesh position or orientation
/* Fragment shader
//
// Screen space
//
*/
uniform vec2 uResolution; // screen width and height
void main()
{
out vec2 vWorldUv;
void main()
{
vec4 objectSpace = modelMatrix * vec4( position, 1.0 );
vWorldUv = vec2( objectSpace.x, objectSpace.z );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );