transformation_visualizer/animation.odin

130 lines
3.9 KiB
Odin

package main
import rl "libraries/raylib"
import "core:math"
import "core:math/ease"
import "core:math/linalg"
import "core:slice"
import "core:fmt"
ANIMATION_COLOR_ORIGIN :: rl.RED
ANIMATION_COLOR_END :: rl.BLUE
RectangleCorners :: struct {tl, tr, br, bl: v2}
rectangle_corners_from_rect :: proc "contextless" (rect: rect2) -> RectangleCorners {
return {
{rect.x, rect.y},
{rect.x + rect.width, rect.y},
{rect.x + rect.width, rect.y + rect.height},
{rect.x, rect.y + rect.height}
}
}
rectangle_corners_transform_mat3 :: proc "contextless" (corners: RectangleCorners, transform: mat3) -> RectangleCorners {
return {
(v3{**corners.tl, 1.0} * transform).xy,
(v3{**corners.tr, 1.0} * transform).xy,
(v3{**corners.br, 1.0} * transform).xy,
(v3{**corners.bl, 1.0} * transform).xy,
}
}
rectangle_corners_lerp :: proc "contextless" (a, b: RectangleCorners, t: f32) -> RectangleCorners {
return {
linalg.lerp(a.tl, b.tl, t),
linalg.lerp(a.tr, b.tr, t),
linalg.lerp(a.bl, b.bl, t),
linalg.lerp(a.br, b.br, t),
}
}
rectangle_corners_transform_mat2 :: proc "contextless" (corners: RectangleCorners, transform: mat2) -> RectangleCorners {
return {corners.tl * transform, corners.tr * transform, corners.br * transform, corners.bl * transform}
}
draw_animation_rectangle_corners :: proc(corners: RectangleCorners, color: rl.Color) {
rect := rect_from_corners(corners.tl, corners.tr, corners.br, corners.bl)
rl.DrawRectangleRec(rect, {**color.rgb, color.a >> 1})
rl.DrawRectangleLinesEx(rect, 2, color)
}
/*
Maybe it is possible to have everything 3d be mathed the same way
as in 2d. But I'm not sure if that's actually how it works.
*/
AnimationShapes :: struct {
rectangle: RectangleCorners,
}
Animation :: struct {
matrices: []f32,
matrix_index: f32,
matrix_count: int,
matrix_type: AnimationMatrixType,
shapes: AnimationShapes,
ease: ease.Ease,
type: AnimationType,
}
AnimationMatrixType :: enum {
Mat2,
Mat3,
Mat4,
}
@private @rodata animation_matrix_type_size := [AnimationMatrixType]int {
.Mat2 = size_of(mat2),
.Mat3 = size_of(mat3),
.Mat4 = size_of(mat4),
}
AnimationType :: enum {
Rectangle,
}
animation_init :: proc(animation: ^Animation, matrices: []f32) {
animation^ = {
ease = .Cubic_Out,
type = .Rectangle,
matrices = matrices,
shapes = {
rectangle = rectangle_corners_from_rect(rect2{-16, -16, 32, 32})
},
}
}
animation_is_finished :: proc(animation: Animation) -> bool {
return animation.matrix_index == f32(animation.matrix_count)
}
animation_draw :: proc(animation: Animation, global_transform: mat3) {
switch animation.type {
case .Rectangle:
shape := animation.shapes.rectangle
transform := linalg.identity(mat3)
color_origin := cast([4]f32)(ANIMATION_COLOR_ORIGIN)
color_end := cast([4]f32)(ANIMATION_COLOR_END)
draw_animation_rectangle_corners(rectangle_corners_transform_mat3(shape, global_transform), ANIMATION_COLOR_ORIGIN)
matrices := slice.reinterpret([]mat3, animation.matrices)
for i in 0..<animation.matrix_count {
animation_progress := f32(i + 1) / f32(animation.matrix_count)
previous := rectangle_corners_transform_mat3(shape, transform)
transform *= matrices[i]
next := rectangle_corners_transform_mat3(shape, transform)
color_next := linalg.lerp(color_origin, color_end, animation_progress)
draw_animation_rectangle_corners(rectangle_corners_transform_mat3(next, global_transform), cast(rl.Color)color_next)
if f32(i) == math.floor(animation.matrix_index) {
progress := ease.ease(animation.ease, fract(animation.matrix_index))
color_previous := linalg.lerp(color_origin, color_end, animation.matrix_index / f32(animation.matrix_count))
color := linalg.lerp(color_previous, color_next, progress)
animated_corners := rectangle_corners_lerp(previous, next, progress)
animated_corners_world := rectangle_corners_transform_mat3(animated_corners, global_transform)
draw_animation_rectangle_corners(animated_corners_world, cast(rl.Color)color)
}
}
}
}