Files
breeze/design/figure-preview/preview_capture.zi
T
waozi 313cffba7c
Cloudflare Pages / Build and verify the site (push) Failing after 2m27s
Review exact Blender poses and improve practice figure motion
2026-11-20 12:16:00 -03:00

362 lines
14 KiB
Ziran

// The stage a moment shows, and the fixed review frames the capture mode
// writes: the practice's backdrop with the figure on it, or Breeze's current
// flat figure when asked.
#import "figure_math"
#import "figure_joints"
#import "figure_model"
#import "figure_finish"
#import "preview_practices"
#import "preview_flat"
#import "preview_figure"
#import "kryon/scene"
#import "kryon/scene_gltf"
#import "sun_salutation_rules"
#import "workout_rules"
#import "std/file"
#import "std/png_linux"
#import "kryon/image_pixels"
#import "std/number_text"
#import "std/vec"
Raster :: #import "std/image";
// Each practice's backdrop, and the last one scaled to a picture.
PreviewBackdrops :: struct {
images: [4]Raster.Image
covered: Raster.Image
covered_practice: s32
}
PreviewBackdropsLoad :: (backdrops: *PreviewBackdrops, practices: string) -> bool {
ready := true
for practice: cast(s32)0..3 {
folder: [1100]u8
path: [1200]u8
name := PreviewJoin(folder[:], practices, "/shared/figure/backdrops/")
name = PreviewJoin(path[:], name, PreviewBackdropName(practice))
file: [1300]u8
loaded := LoadImagePixels(PreviewJoin(file[:], name, ".webp"))
PreviewTake(*backdrops.images[practice], *loaded)
ready = ready && Raster.ImageValid(*backdrops.images[practice])
}
return ready
}
PreviewTake :: (to: *Raster.Image, from: *Raster.Image) {
VecSwap(to.pixels, from.pixels)
to.width = from.width
to.height = from.height
VecFree(from.pixels)
}
PreviewBackdropsRelease :: (backdrops: *PreviewBackdrops) {
for practice: 0..3 {
VecFree(backdrops.images[practice].pixels)
}
VecFree(backdrops.covered.pixels)
}
// The practice's backdrop covering a WIDTH by HEIGHT picture in OUT, and over
// it, for MOMENT seen as VIEW, Breeze's current flat figure (FLAT) or the
// loaded FIGURE.
PreviewStage :: (backdrops: *PreviewBackdrops, practice: s32, moment: *PreviewMoment, view: PreviewView,
flat: bool, figure: *PreviewFigure, width: s32, height: s32, out: *Raster.Image, seconds: float64 = -1.0) -> bool {
covered := *backdrops.covered
if covered.width != width || covered.height != height || backdrops.covered_practice != practice {
if !FigureBackdropCover(covered, *backdrops.images[practice], width, height,
moment.ground.zoom, moment.ground.focus, 1.2) {
return false
}
backdrops.covered_practice = practice
}
if !flat && figure != null && figure.loaded {
PreviewFigureProps(figure, moment.workout_item)
if !PreviewFigurePaint(figure, *moment.body, view, width, height, seconds) ||
!FigureOverOnto(out, covered, *figure.pixels) {
return false
}
if moment.twin_visible {
if !PreviewFigureInFrame(figure) || !PreviewFigureAwareness(figure, *moment.twin, view, width, height,
moment.twin_effect.offset, moment.twin_effect.pivot, moment.twin_effect.roll, seconds) {
return false
}
return FigureOverOnto(out, out, *figure.awareness_pixels)
}
return true
}
if !flat {
if !VecResize(out.pixels, covered.pixels.count) {
return false
}
out.width = width
out.height = height
for index: 0..covered.pixels.count - 1 {
out.pixels[index] = covered.pixels[index]
}
return true
}
flat_figure: Raster.Image
defer VecFree(flat_figure.pixels)
camera := PreviewFlatOf(moment, view)
return PreviewFlatRender(camera, view, width, height, *flat_figure) && FigureOverOnto(out, covered, *flat_figure)
}
PreviewShot :: struct {
practice: s32
item: s32
seconds: float64
name: string
}
// Writes NAME-woman.png and NAME-man.png (the figure on the practice's
// backdrop) and NAME-current.png (Breeze's current flat figure) for each
// practice's hardest poses; PRACTICES holds the figures.
PreviewCapture :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directory: string, practices: string) -> s32 {
shots: [16]PreviewShot
count: s32
full := PreviewDefaultItem(library, PREVIEW_TAI_CHI)
shots[count] = .{PREVIEW_TAI_CHI, full, 9.0, "tai-chi-9s"}
count += 1
shots[count] = .{PREVIEW_TAI_CHI, full, PreviewLowest(library, PREVIEW_TAI_CHI, full), "tai-chi-deepest-stance"}
count += 1
// The Sun Salutation's folds and floor poses, each once it is reached.
names: [5]string = .["sun-forward-fold", "sun-lunge", "sun-plank", "sun-cobra", "sun-downward-dog"]
steps: [5]s32 = .[2, 3, 4, 6, 7]
for index: 0..4 {
shots[count] = .{PREVIEW_SUN_SALUTATION, 0, PreviewSunHeld(steps[index]), names[index]}
count += 1
}
shots[count] = .{PREVIEW_WORKOUT, WORKOUT_SQUATS, PreviewLowest(library, PREVIEW_WORKOUT, WORKOUT_SQUATS),
"workout-squat"}
count += 1
shots[count] = .{PREVIEW_WORKOUT, WORKOUT_LUNGES, PreviewLowest(library, PREVIEW_WORKOUT, WORKOUT_LUNGES),
"workout-lunge"}
count += 1
shots[count] = .{PREVIEW_ASTRAL, 0, 60.0, "astral-60s"}
count += 1
picture: Raster.Image
defer VecFree(picture.pixels)
figure: PreviewFigure
defer PreviewFigureRelease(*figure)
failed: s32
written: s32
suffixes: [3]string = .["-woman.png", "-man.png", "-current.png"]
for variant: cast(s32)0..2 {
if variant < 2 && !PreviewFigureUse(*figure, practices, variant == 1) {
failed += 1
continue
}
for index: 0..count - 1 {
shot := shots[index]
moment := PreviewSample(library, shot.practice, shot.item, shot.seconds)
// Lying figures need a landscape picture.
width: s32 = ifx shot.practice == PREVIEW_ASTRAL then 900 else 600
height: s32 = ifx shot.practice == PREVIEW_ASTRAL then 600 else 800
view := PreviewViewOf(*moment, 0.0, 0.0, 1.0, cast(float64)width / cast(float64)height)
stem: [1024]u8
named: [1024]u8
path: [1024]u8
name := PreviewJoin(stem[:], directory, "/")
name = PreviewJoin(named[:], name, shot.name)
final := PreviewJoin(path[:], name, suffixes[variant])
if !PreviewStage(backdrops, shot.practice, *moment, view, variant == 2, *figure, width, height, *picture) ||
(variant < 2 && !PreviewFigureInFrame(*figure)) || !WritePNGFile(final, *picture, false) {
print("figure capture: % could not be painted\n", final)
failed += 1
continue
}
written += 1
}
}
print("figure capture: % frames, gpu=%\n", written, figure.accelerated)
return failed
}
// Writes PRACTICE's ITEM played from FROM to TO seconds at 30 frames a
// second, hair swinging and eyes blinking, into DIRECTORY as 0000.png on.
PreviewVideo :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directory: string, practices: string,
practice: s32, item: s32, from: float64, to: float64, man: bool) -> s32 {
picture: Raster.Image
defer VecFree(picture.pixels)
figure: PreviewFigure
defer PreviewFigureRelease(*figure)
if !PreviewFigureUse(*figure, practices, man) {
return 1
}
frame: s64
while from + cast(float64)frame / 30.0 <= to {
at := from + cast(float64)frame / 30.0
moment := PreviewSample(library, practice, item, at)
width: s32 = ifx practice == PREVIEW_ASTRAL then 900 else 600
height: s32 = ifx practice == PREVIEW_ASTRAL then 600 else 800
view := PreviewViewOf(*moment, 0.0, 0.0, 1.0, cast(float64)width / cast(float64)height)
digits: [32]u8
stem: [1024]u8
named: [1024]u8
path: [1024]u8
name := PreviewJoin(stem[:], directory, "/")
number := FormatDecimal(cast(float64)(10000 + frame), 0, digits[:])
name = PreviewJoin(named[:], name, number[1:])
if !PreviewStage(backdrops, practice, *moment, view, false, *figure, width, height, *picture, at) ||
!PreviewFigureInFrame(*figure) || !WritePNGFile(PreviewJoin(path[:], name, ".png"), *picture, false) {
return 1
}
frame += 1
}
return 0
}
// Writes the woman (or the man, MAN) at each of SECONDS of PRACTICE's ITEM
// into DIRECTORY as SECONDS.png, for looking at chosen moments.
PreviewShots :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, directory: string, practices: string,
practice: s32, item: s32, seconds: []float64, man: bool, turn: float64 = 0.0) -> s32 {
picture: Raster.Image
defer VecFree(picture.pixels)
figure: PreviewFigure
defer PreviewFigureRelease(*figure)
if !PreviewFigureUse(*figure, practices, man) {
return 1
}
failed: s32
for at: seconds {
moment := PreviewSample(library, practice, item, at)
width: s32 = ifx practice == PREVIEW_ASTRAL then 900 else 600
height: s32 = ifx practice == PREVIEW_ASTRAL then 600 else 800
view := PreviewViewOf(*moment, turn, 0.0, 1.0, cast(float64)width / cast(float64)height)
digits: [32]u8
stem: [1024]u8
named: [1024]u8
path: [1024]u8
name := PreviewJoin(stem[:], directory, "/")
name = PreviewJoin(named[:], name, FormatDecimal(at, 1, digits[:]))
final := PreviewJoin(path[:], name, ".png")
if !PreviewStage(backdrops, practice, *moment, view, false, *figure, width, height, *picture) ||
!PreviewFigureInFrame(*figure) || !WritePNGFile(final, *picture, false) {
failed += 1
}
}
return failed
}
// One exact native pose in glTF model space for inspecting the bind rig in
// Blender. The runtime-rendered shots also include the native cloth pass.
PreviewRigPose :: (library: *PreviewLibrary, practices: string, practice: s32, item: s32,
seconds: float64, man: bool) -> s32 {
if practice < 0 || practice > 3 || item < 0 || item >= PreviewItemCount(library, practice) ||
seconds < 0.0 || seconds > PreviewDuration(library, practice, item) {
return 1
}
scene: Scene
SceneInit(*scene)
defer SceneFree(*scene)
model: FigureModel
defer FigureModelRelease(*model)
path: [1200]u8
file := ifx man then "/shared/figure/man.vrm" else "/shared/figure/woman.vrm"
if !FigureModelLoad(*model, *scene, PreviewJoin(path[:], practices, file)) {
return 1
}
moment := PreviewSample(library, practice, item, seconds)
if !FigureModelPose(*model, *scene, *moment.body, seconds) {
return 1
}
root := scene.nodes[model.instance.root_node]
print("root\t%\t%\t%\t%\t%\n", root.position.x, root.position.y, root.position.z, model.yaw, root.scale.x)
for role: 0..FIGURE_BONES - 1 {
node := model.bones[role]
if node < 0 {
continue
}
source := *model.model.nodes[node]
name := TextView(source.name[:])
at := model.rest_at[node]
rest := model.rest_turn[node]
m := model.instance.node_world[node].m
print("bone\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\t%\n",
name, at.x, at.y, at.z, rest.x, rest.y, rest.z, rest.w,
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11])
}
return 0
}
#scope_file
// The second of the item where her pelvis is lowest: the deepest stance.
PreviewLowest :: (library: *PreviewLibrary, practice: s32, item: s32) -> float64 {
duration := PreviewDuration(library, practice, item)
best: float64
lowest: float64 = 1000.0
seconds: float64
while seconds < duration {
moment := PreviewSample(library, practice, item, seconds)
if moment.body.pelvis.y < lowest {
lowest = moment.body.pelvis.y
best = seconds
}
seconds += 0.25
}
return best
}
// Half a second before the first round's STEP ends: its pose reached.
PreviewSunHeld :: (step: s32) -> float64 {
total: float64
for at: cast(s32)0..step {
state: SunSalutationPracticeState
state.repetitions = 2
state.step = at
state.start_seconds = SUN_SALUTATION_DEFAULT_START_SECONDS
state.end_seconds = SUN_SALUTATION_DEFAULT_END_SECONDS
total += cast(float64)SalutationStepSeconds(state)
}
return total - 0.5
}
PreviewStreamLibc :: #system_library "libc";
PreviewStreamWrite :: (descriptor: s32, data: *u8, size: usize) -> s64 #foreign PreviewStreamLibc "write";
#scope_export
// Stream complete review sequences through an inherited pipe to the encoder.
PreviewStream :: (backdrops: *PreviewBackdrops, library: *PreviewLibrary, practices: string,
practice: s32, item: s32, from: float64, to: float64, rate: float64,
width: s32, height: s32, descriptor: s32, man: bool) -> s32 {
if practice < 0 || practice > 3 || item < 0 || item >= PreviewItemCount(library, practice) ||
from < 0.0 || to < from || to > PreviewDuration(library, practice, item) + 0.001 ||
rate < 1.0 || rate > 60.0 || width < 160 || width > 1920 || height < 160 || height > 1920 || descriptor < 3 {
return 1
}
picture: Raster.Image
defer VecFree(picture.pixels)
figure: PreviewFigure
defer PreviewFigureRelease(*figure)
if !PreviewFigureUse(*figure, practices, man) {
return 1
}
frame: s64
while true {
at := FigureMin(to, from + cast(float64)frame / rate)
moment := PreviewSample(library, practice, item, at)
view := PreviewViewOf(*moment, 0.0, 0.0, 1.0, cast(float64)width / cast(float64)height)
if !PreviewStage(backdrops, practice, *moment, view, false, *figure, width, height, *picture, at) ||
!PreviewFigureInFrame(*figure) {
print("review stream: person % practice % item % failed at %s\n", ifx man then "man" else "woman", practice, item, at)
return 1
}
size := cast(s64)width * cast(s64)height * 4
sent: s64
while sent < size {
count := PreviewStreamWrite(descriptor, *picture.pixels[sent], cast(usize)(size - sent))
if count <= 0 {
return 1
}
sent += count
}
frame += 1
if at >= to {
break
}
}
print("review stream: person % practice % item % from % to % fps % frames %\n",
ifx man then "man" else "woman", practice, item, from, to, rate, frame)
return 0
}