📝

GLSL|Transform

2024.03.17

SHARE

TABLE OF CONTENTS

    📝

    GLSL|Transform

    2024.03.17

    GLSLで使用するトランスフォームのコードについてメモしています。

    rotate 2d

    mat2 rotate2d(float rad) {
      float c = cos(rad);
      float s = sin(rad);
      return mat2(
        c, s,
        -s, c
      );
    }

    rotate 3d

    mat4 rotationMatrix(vec3 axis, float angle) {
        axis = normalize(axis);
        float s = sin(angle);
        float c = cos(angle);
        float oc = 1.0 - c;
        return mat4(oc * axis.x * axis.x + c,           oc * axis.x * axis.y - axis.z * s,  oc * axis.z * axis.x + axis.y * s,  0.0,
                    oc * axis.x * axis.y + axis.z * s,  oc * axis.y * axis.y + c,           oc * axis.y * axis.z - axis.x * s,  0.0,
                    oc * axis.z * axis.x - axis.y * s,  oc * axis.y * axis.z + axis.x * s,  oc * axis.z * axis.z + c,           0.0,
                    0.0,                                0.0,                                0.0,                                1.0);
    }
    
    vec3 rotate(vec3 v, vec3 axis, float angle) {
    	mat4 m = rotationMatrix(axis, angle);
    	return (m * vec4(v, 1.0)).xyz;
    }

    translate

    vec2 translate(vec2 p, vec2 r, float t, float e){;
      return vec2(
        r.x * cos(uTime * t + p.x * p.y * e),
        r.y * sin(uTime * t + p.x * p.y * e)
      );
    }

    ©2025 SHOYA KAJITA.