LYGIA Shader Library

rotate (lygia/space/rotate)

rotate a 2D space by a radian r

Dependencies:

Use:

rotate(<vec3|vec2> v, float r [, vec2 c])

Check it on Github



#ifndef FNC_ROTATE
#define FNC_ROTATE
vec2 rotate(in vec2 v, in float r, in vec2 c) {
    return rotate2d(r) * (v - c) + c;
}

vec2 rotate(in vec2 v, in float r) {
    #ifdef CENTER_2D
    return rotate(v, r, CENTER_2D);
    #else
    return rotate(v, r, vec2(.5));
    #endif
}

vec2 rotate(vec2 v, vec2 x_axis) {
    #ifdef CENTER_2D
    v -= CENTER_2D;
    #endif
    vec2 rta = vec2( dot(v, vec2(-x_axis.y, x_axis.x)), dot(v, x_axis) );
    #ifdef CENTER_2D
    rta += CENTER_2D;
    #endif
    return rta;
}

vec3 rotate(in vec3 v, in float r, in vec3 axis, in vec3 c) {
    return (rotate4d(axis, r) * vec4(v - c, 1.)).xyz + c;
}

vec3 rotate(in vec3 v, in float r, in vec3 axis) {
    #ifdef CENTER_3D
    return rotate(v, r, axis, CENTER_3D);
    #else
    return rotate(v, r, axis, vec3(0.));
    #endif
}

vec4 rotate(in vec4 v, in float r, in vec3 axis, in vec4 c) {
    return rotate4d(axis, r) * (v - c) + c;
}

vec4 rotate(in vec4 v, in float r, in vec3 axis) {
    #ifdef CENTER_4D
    return rotate(v, r, axis, CENTER_4D);
    #else
    return rotate(v, r, axis, vec4(0.));
    #endif
}

#if defined(FNC_QUATMULT)
vec3 rotate(QUAT q, vec3 v) {
    QUAT q_c = QUAT(-q.x, -q.y, -q.z, q.w);
    return quatMul(q, quatMul(vec4(v, 0), q_c)).xyz;
}

vec3 rotate(QUAT q, vec3 v, vec3 c) {
    vec3 dir = v - c;
    return c + rotate(q, dir);
}
#endif

#endif

Dependencies:

Use:

rotate(<float3|float2> st, float radians [, float2 center])

Check it on Github



#ifndef FNC_ROTATE
#define FNC_ROTATE
float2 rotate(in float2 st, in float radians, in float2 center) {
    return mul(rotate2d(radians), (st - center)) + center;
}

float2 rotate(in float2 st, in float radians) {
    #ifdef CENTER_2D
    return rotate(st, radians, CENTER_2D);
    #else
    return rotate(st, radians, float2(0.5, 0.5));
    #endif
}

float2 rotate(float2 st, float2 x_axis) {
    #ifdef CENTER_2D
    st -= CENTER_2D;
    #endif
    float2 rta = float2( dot(st, float2(-x_axis.y, x_axis.x)), dot(st, x_axis) );
    #ifdef CENTER_2D
    rta += CENTER_2D;
    #endif
    return rta;
}

float3 rotate(in float3 xyz, in float radians, in float3 axis, in float3 center) {
    return mul(rotate4d(axis, radians), float4(xyz - center, 1.)).xyz + center;
}

float3 rotate(in float3 xyz, in float radians, in float3 axis) {
    #ifdef CENTER_3D
    return rotate(xyz, radians, axis, CENTER_3D);
    #else
    return rotate(xyz, radians, axis, float3(0.0, 0.0, 0.0));
    #endif
}

float4 rotate(in float4 xyzw, in float radians, in float3 axis, in float4 center) {
    return mul(rotate4d(axis, radians), (xyzw - center)) + center;
}

float4 rotate(in float4 xyzw, in float radians, in float3 axis) {
    #ifdef CENTER_4D
    return rotate(xyzw, radians, axis, CENTER_4D);
    #else
    return rotate(xyzw, radians, axis, float4(0.0, 0.0, 0.0, 0.0));
    #endif
}
#endif

Dependencies:

Use:

rotate(<float3|float2> v, float r [, float2 c])

Check it on Github



#ifndef FNC_ROTATE
#define FNC_ROTATE
float2 rotate(float2 v, float r, float2 c) {
    return rotate2d(r) * (v - c) + c;
}

float2 rotate(float2 v, float r) {
    #ifdef CENTER_2D
    return rotate(v, r, CENTER_2D);
    #else
    return rotate(v, r, float2(.5));
    #endif
}

float2 rotate(float2 v, float2 x_axis) {
    #ifdef CENTER_2D
    v -= CENTER_2D;
    #endif
    float2 rta = float2( dot(v, float2(-x_axis.y, x_axis.x)), dot(v, x_axis) );
    #ifdef CENTER_2D
    rta += CENTER_2D;
    #endif
    return rta;
}

float3 rotate(float3 v, float r, float3 axis, float3 c) {
    return (rotate4d(axis, r) * float4(v - c, 1.)).xyz + c;
}

float3 rotate(float3 v, float r, float3 axis) {
    #ifdef CENTER_3D
    return rotate(v, r, axis, CENTER_3D);
    #else
    return rotate(v, r, axis, float3(0.));
    #endif
}

float4 rotate(float4 v, float r, float3 axis, float4 c) {
    return rotate4d(axis, r) * (v - c) + c;
}

float4 rotate(float4 v, float r, float3 axis) {
    #ifdef CENTER_4D
    return rotate(v, r, axis, CENTER_4D);
    #else
    return rotate(v, r, axis, float4(0.));
    #endif
}

#if defined(FNC_QUATMULT)
float3 rotate(QUAT q, float3 v) {
    QUAT q_c = QUAT(-q.x, -q.y, -q.z, q.w);
    return quatMul(q, quatMul(float4(v, 0), q_c)).xyz;
}

float3 rotate(QUAT q, float3 v, float3 c) {
    float3 dir = v - c;
    return c + rotate(q, dir);
}
#endif

#endif

Dependencies:

Check it on Github




fn rotate(st: vec2f, radians: f32) -> vec2f {
    return rotate2d(radians) * (st - 0.5) + 0.5;
}

Examples

Licenses

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository