Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created January 16, 2018 23:24
Show Gist options
  • Save ayamflow/c06bc0c8a64f985dd431bd0ac5b557cd to your computer and use it in GitHub Desktop.
Save ayamflow/c06bc0c8a64f985dd431bd0ac5b557cd to your computer and use it in GitHub Desktop.
Rotate UV in GLSL
vec2 rotateUV(vec2 uv, float rotation)
{
float mid = 0.5;
return vec2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}
vec2 rotateUV(vec2 uv, float rotation, vec2 mid)
{
return vec2(
cos(rotation) * (uv.x - mid.x) + sin(rotation) * (uv.y - mid.y) + mid.x,
cos(rotation) * (uv.y - mid.y) - sin(rotation) * (uv.x - mid.x) + mid.y
);
}
vec2 rotateUV(vec2 uv, float rotation, float mid)
{
return vec2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}
#pragma glslify: export(rotateUV)
@Orsbert
Copy link

Orsbert commented Aug 22, 2022

<3

@Dakumisu
Copy link

<3

@tlztonio
Copy link

tlztonio commented Nov 5, 2022

@MrSquirrel26
Copy link

<3

@LeonBaudouin
Copy link

<3

@bogosorted
Copy link

<3

@SPLEEN96
Copy link

<3

@flanb
Copy link

flanb commented Apr 4, 2023

❤️

@dmnsgn
Copy link

dmnsgn commented Oct 17, 2023

Compute cos()/sin() once.
vec2 rotateUV(vec2 uv, float rotation)
{
    float mid = 0.5;
    float cosAngle = cos(rotation);
    float sinAngle = sin(rotation);
    return vec2(
        cosAngle * (uv.x - mid) + sinAngle * (uv.y - mid) + mid,
        cosAngle * (uv.y - mid) - sinAngle * (uv.x - mid) + mid
    );
}

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

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

Compute point once:

vec2 rotateUV(vec2 uv, float rotation)
{
    float cosAngle = cos(rotation);
    float sinAngle = sin(rotation);
    vec2 p = uv - vec2(0.5);
    return vec2(
        cosAngle * p.x + sinAngle * p.y + 0.5,
        cosAngle * p.y - sinAngle * p.x + 0.5
    );
}

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

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

#pragma glslify: export(rotateUV)

💙

@Ahlecss
Copy link

Ahlecss commented Mar 29, 2024

<3

@PolyBytes
Copy link

<3

@BerryOnGit
Copy link

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment