diff --git a/design/astral-projection/three/assets/makehuman-african-female-young.target.gz b/design/astral-projection/three/assets/makehuman-african-female-young.target.gz new file mode 100644 index 000000000..0a912e2e8 Binary files /dev/null and b/design/astral-projection/three/assets/makehuman-african-female-young.target.gz differ diff --git a/design/astral-projection/three/assets/makehuman-asian-female-young.target.gz b/design/astral-projection/three/assets/makehuman-asian-female-young.target.gz new file mode 100644 index 000000000..13ab25820 Binary files /dev/null and b/design/astral-projection/three/assets/makehuman-asian-female-young.target.gz differ diff --git a/design/astral-projection/three/assets/makehuman-caucasian-female-young.target.gz b/design/astral-projection/three/assets/makehuman-caucasian-female-young.target.gz new file mode 100644 index 000000000..f646bfc7d Binary files /dev/null and b/design/astral-projection/three/assets/makehuman-caucasian-female-young.target.gz differ diff --git a/design/astral-projection/three/assets/makehuman-source.json b/design/astral-projection/three/assets/makehuman-source.json index 57d9ac05c..be44242cb 100644 --- a/design/astral-projection/three/assets/makehuman-source.json +++ b/design/astral-projection/three/assets/makehuman-source.json @@ -6,5 +6,12 @@ "licence_evidence": "https://github.com/makehumancommunity/makehuman/blob/a8bc2d54ff0ac92e78ff71431b1023eda42bf482/LICENSE.md", "weights": "makehuman/data/rigs/default_weights.mhw", "weights_sha256": "0f3641d651ae3d00ad6b4ccee43142edb109d3bd909d27d9e4139ef1beed8625", - "weights_licence": "CC0-1.0 (declared in the file header)" + "weights_licence": "CC0-1.0 (declared in the file header)", + "shape": "young adult female: makehuman/data/targets/macrodetails/{african,asian,caucasian}-female-young.target, one third each", + "shape_files": { + "makehuman-african-female-young.target.gz": "92d61eeb3c164b421fd5a7c3537ee45e7e1a51de4d49bf312e19c3df2be1d8fc", + "makehuman-asian-female-young.target.gz": "095fe79694fa19e1fe98d93009ec116199bd524e081c640351a10eccf2cca1eb", + "makehuman-caucasian-female-young.target.gz": "118379f6e8ba9266247fdb8788a20e1df40a239f97ced0b9905bcbcc74f6e820" + }, + "shape_licence": "CC0-1.0 (declared in each target file header)" } diff --git a/design/figure-preview/README.md b/design/figure-preview/README.md new file mode 100644 index 000000000..266fa3427 --- /dev/null +++ b/design/figure-preview/README.md @@ -0,0 +1,53 @@ +# Figure preview + +A native window for judging and tuning the shared practice figure. It plays +each practice's own choreography on the character from the practices +package (`figure/`): Tai Chi forms, two rounds of Sun Salutation, the +Workout's twelve exercises and the six astral techniques. It runs on Ziran +and Kryon's SDL/Cairo desktop host, without raylib. + +The panel picks the practice and item, plays, pauses, scrubs and slows the +clock. It switches the watercolor finish off to show the bare surface, and +can show Breeze's current flat figure for the same moment. It also tunes +edge pooling, pencil contour and paper grain. Drag over her to turn around +her; scroll to zoom. + +This is its own small project so it can pin the Kryon that has +`scene_skin` and `watercolor`, and the practices commit that has the figure, +while Breeze's app still pins older ones. The poses are Breeze's own: the +build adds Breeze's Tai Chi, Sun Salutation and practice folders as module +folders, so nothing is copied. + +## Build and run + +From this folder, through the build queue: + +```sh +build-queue -- ziran build --project --target=c --exe \ + --module-path ../../src/practices/tai_chi \ + --module-path ../../src/practices/sun_salutation \ + --module-path ../../apps/practice \ + --entry figure_preview:main -o build/app figure_preview.zi +./build/app/figure_preview +``` + +The preview asks `ziran pkg path practices --locked` beside this manifest +for its media. `FIGURE_PRACTICES=/path/to/practices` uses a working +checkout instead, for trying figure changes before they are pushed (add +`--module-path ../../../../modules/practices/figure` to the build to compile +that checkout's figure code too). + +`./build/app/figure_preview --capture DIR` writes review frames to `DIR` and +exits without a window. Each frame is written three ways: the painted figure +(`NAME.png`), her bare surface (`NAME-bare.png`) and Breeze's current flat +figure for the same moment and camera (`NAME-current.png`). The frames cover +Tai Chi's deepest stance, Sun Salutation's fold, lunge, plank, cobra and +downward dog, the Workout's lowest squat and lunge, and the astral exits. + +## Known limits + +- At the pinned Kryon, the desktop loop stops showing frames that arrive + with no input, so hands-off playback freezes until the pointer moves over + the window. The bug is reported to Kryon with a minimal repro. +- Practices commits without `astral_projection/sessions/` play the astral + techniques with steps evenly 20 seconds apart, and the panel says so. diff --git a/design/figure-preview/figure_preview.zi b/design/figure-preview/figure_preview.zi new file mode 100644 index 000000000..3bf653f19 --- /dev/null +++ b/design/figure-preview/figure_preview.zi @@ -0,0 +1,411 @@ +// A live preview of the shared practice figure: pick a practice and one of +// its items, play, pause, scrub or slow it down, turn and zoom around her, +// and switch the watercolor finish off to judge the bare surface. The poses +// are the practices' own (preview_practices.zi); the character is the +// practices package's figure. +// +// figure_preview opens the window +// figure_preview --capture DIR writes the review frames to DIR and exits +// +// FIGURE_PRACTICES names a practices checkout; otherwise the preview asks +// `ziran pkg path practices --locked` beside its own manifest. +#import "figure_math" +#import "figure_joints" +#import "figure_paint" +#import "preview_practices" +#import "preview_capture" +#import "preview_flat" +#import "kryon/scene" +#import "kryon/session" +#import "kryon/geometry" +#import "kryon/frame_snapshot" +#import "kryon/style_sheet" +#import "kryon/bitmap_image" +#import "kryon/desktop_runtime" +#import "kryon/watercolor" +#import "kryon/drawing_props" +Desktop :: #import "kryon/desktop_loop"; +Box :: #import "kryon/box"; +Button :: #import "kryon/button"; +Dropdown :: #import "kryon/dropdown"; +Image :: #import "kryon/image"; +Segmented :: #import "kryon/segmented_control"; +Slider :: #import "kryon/slider"; +Text :: #import "kryon/text"; +Toggle :: #import "kryon/toggle"; +Raster :: #import "std/image"; +Timer :: #import "std/timer_linux"; +#import "std/c_string" +#import "std/byte_text_linux" +#import "std/math" +#import "std/number_text" +#import "std/process_capture_linux" +#import "std/vec" + +libc :: #system_library "libc"; +PreviewGetEnvironment :: (name: *u8) -> *u8 #foreign libc "getenv"; +PreviewWorkingDirectory :: (output: *u8, size: usize) -> *u8 #foreign libc "getcwd"; +PreviewChangeDirectory :: (path: *u8) -> s32 #foreign libc "chdir"; + +PreviewState :: struct { + practice: s32 + item: s32 + seconds: float64 + playing: bool + speed: float64 + watercolor: bool + // Shows Breeze's current flat figure instead, for comparison. + current: bool + turn: float64 + lift: float64 + zoom: float64 + dragging: bool + drag_x: float32 + drag_y: float32 + pooling: float32 + contour: float32 + grain: float32 +} + +preview_state: PreviewState = .{playing = true, speed = 1.0, watercolor = true, zoom = 1.0, + pooling = 0.6, contour = 0.7, grain = 0.3}; +preview_media: FigureMedia; +preview_library: PreviewLibrary; +preview_body: FigurePainter; +preview_twin: FigurePainter; +preview_picture: Raster.Image; +// Two textures, used in turn: Kryon redraws an image when its texture +// changes, not when the same texture's pixels are updated. +preview_textures: [2]Texture2D; +preview_shown: s32; +preview_owner: Session; +preview_painted_key: u64; +preview_failed: bool; +preview_clock: s64; +// Changing labels alternate between two buffers, so each frame's text is +// its own and never an edit of the last frame's. +preview_page: s32; +preview_time_text: [2][32]u8; +preview_status_text: [2][96]u8; +preview_frame_ms: float64; +preview_practices_path: [1024]u8; +preview_practices_length: s64; + +main :: (argc: s32, argv: **u8) -> s32 { + practices := PreviewPracticesPath() + if practices.count == 0 { + print("figure preview: no practices checkout; set FIGURE_PRACTICES or lock the package\n") + return 1 + } + media: [1100]u8 + preview_media = FigureMediaLoad(PreviewJoin(media[:], practices, "/shared/watercolor")) + if !FigureMediaReady(*preview_media) || !PreviewLibraryLoad(*preview_library, practices) { + print("figure preview: could not read the figure media or the practices in %\n", practices) + return 1 + } + preview_state.item = PreviewDefaultItem(*preview_library, preview_state.practice) + if argc >= 3 && TextFromCString(argv[1]) == "--capture" { + return PreviewCapture(*preview_media, *preview_library, TextFromCString(argv[2])) + } + return Desktop.RunTitled(PreviewDraw, 1360, 900, "Figure preview") +} + +PreviewDraw :: (session: Session, viewport: Rectangle) -> s32 { + input := CaptureFrameInput(session) + state := *preview_state + panel_width: float32 = 360.0 + area := Rectangle.{viewport.x, viewport.y, viewport.width - panel_width, viewport.height} + panel := Rectangle.{viewport.x + area.width, viewport.y, panel_width, viewport.height} + Box.Draw(session, Box.Props.{.key = 1, .bounds = area, .fill = .{243, 237, 225, 255}}) + Box.Draw(session, Box.Props.{.key = 2, .bounds = panel, .fill = .{232, 225, 211, 255}}) + + duration := PreviewDuration(*preview_library, state.practice, state.item) + now := Timer.MonotonicMilliseconds() + elapsed := ifx preview_clock == 0 then 0 else now - preview_clock + preview_clock = now + if elapsed > 0 { + preview_frame_ms = preview_frame_ms * 0.9 + cast(float64)elapsed * 0.1 + } + if state.playing { + // A stalled frame never jumps the figure ahead by more than a moment. + state.seconds += cast(float64)FigureClamp(cast(float64)elapsed, 0.0, 250.0) / 1000.0 * state.speed + if state.seconds >= duration { + state.seconds -= duration * cast(float64)cast(s64)(state.seconds / duration) + } + DesktopRequestFrame() + } + // Turn and tilt by dragging over her; zoom with the wheel. + pointer := input.pointer + inside := pointer.x >= area.x && pointer.x < area.x + area.width && pointer.y >= area.y && + pointer.y < area.y + area.height + if pointer.pressed && inside { + state.dragging = true + state.drag_x = pointer.x + state.drag_y = pointer.y + } + if state.dragging { + state.turn -= cast(float64)(pointer.x - state.drag_x) * 0.008 + state.lift = FigureClamp(state.lift + cast(float64)(pointer.y - state.drag_y) * 0.006, -1.2, 1.45) + state.drag_x = pointer.x + state.drag_y = pointer.y + DesktopRequestFrame() + if !pointer.down { + state.dragging = false + } + } + if inside && input.wheel != 0.0 { + state.zoom = FigureClamp(state.zoom * Pow(0.88, cast(float64)input.wheel), 0.25, 3.0) + } + + PreviewPaintFigure(session, area) + + x := panel.x + 20.0 + width := panel.width - 40.0 + y := panel.y + 18.0 + Text.Draw(session, Text.Props.{.key = 10, .bounds = .{x, y, width, 30.0}, .text = "Figure preview", + .size = 22}) + y += 42.0 + options: [4]Segmented.SegmentOption + options[0].label = "Tai Chi" + options[1].label = "Sun" + options[2].label = "Workout" + options[3].label = "Astral" + chosen := Segmented.Draw(session, Segmented.Props.{.key = 11, .bounds = .{x, y, width, 40.0}, + .selected_index = state.practice, .has_selection = true}, options[:]).selected_index + if chosen != state.practice && chosen >= 0 && chosen <= 3 { + state.practice = chosen + state.item = PreviewDefaultItem(*preview_library, chosen) + state.seconds = 0.0 + } + y += 52.0 + items: [PREVIEW_ITEM_LIMIT]Dropdown.Option + count := PreviewItemCount(*preview_library, state.practice) + for index: cast(s32)0..count - 1 { + items[index].label = PreviewItemName(*preview_library, state.practice, index) + } + picked := Dropdown.Draw(session, Dropdown.Props.{.key = 12, .bounds = .{x, y, width, 40.0}, + .selected_index = state.item}, items[0:count]).selected_index + if picked != state.item && picked >= 0 && picked < count { + state.item = picked + state.seconds = 0.0 + } + y += 54.0 + if Button.Draw(session, Button.Props.{.key = 13, .bounds = .{x, y, width * 0.48, 40.0}, + .label = ifx state.playing then "Pause" else "Play"}) != 0 { + state.playing = !state.playing + } + if Button.Draw(session, Button.Props.{.key = 14, .bounds = .{x + width * 0.52, y, width * 0.48, 40.0}, + .label = "Front view"}) != 0 { + state.turn = 0.0 + state.lift = 0.0 + state.zoom = 1.0 + } + y += 54.0 + preview_page = 1 - preview_page + at := Slider.Draw(session, Slider.Props.{.key = cast(u64)(15 + preview_page * 1000), .bounds = .{x, y, width, 64.0}, + .label = "Time", .value_text = PreviewSeconds(state.seconds, duration, preview_time_text[preview_page][:]), .value = cast(float32)state.seconds, + .min = 0.0, .max = cast(float32)duration}) + if at.changed { + state.seconds = cast(float64)at.value + state.playing = false + } + y += 72.0 + speed_digits: [32]u8 + speed := Slider.Draw(session, Slider.Props.{.key = 16, .bounds = .{x, y, width, 64.0}, .label = "Speed", + .value_text = FormatDecimal(state.speed, 2, speed_digits[:]), .value = cast(float32)state.speed, + .min = 0.05, .max = 2.0}) + if speed.changed { + state.speed = cast(float64)speed.value + } + y += 72.0 + state.watercolor = Toggle.Draw(session, Toggle.Props.{.key = 17, .bounds = .{x, y, width, 40.0}, + .label = "Watercolor finish", .value = state.watercolor}).value + y += 46.0 + state.current = Toggle.Draw(session, Toggle.Props.{.key = 23, .bounds = .{x, y, width, 40.0}, + .label = "Current flat figure", .value = state.current}).value + y += 50.0 + state.pooling = Slider.Draw(session, Slider.Props.{.key = 18, .bounds = .{x, y, width, 64.0}, + .label = "Edge pooling", .value = state.pooling, .max = 1.0}).value + y += 70.0 + state.contour = Slider.Draw(session, Slider.Props.{.key = 19, .bounds = .{x, y, width, 64.0}, + .label = "Pencil contour", .value = state.contour, .max = 1.0}).value + y += 70.0 + state.grain = Slider.Draw(session, Slider.Props.{.key = 20, .bounds = .{x, y, width, 64.0}, + .label = "Paper grain", .value = state.grain, .max = 1.0}).value + y += 76.0 + status := "CPU" + if preview_failed { + status = "The figure could not be painted" + } else if preview_body.accelerated { + status = "GPU" + } + rate: [16]u8 + first_line: [96]u8 + labelled := PreviewJoin(first_line[:], status, ", frame ms ") + status = PreviewJoin(preview_status_text[preview_page][:], labelled, FormatDecimal(preview_frame_ms, 0, rate[:])) + Text.Draw(session, Text.Props.{.key = 21, .bounds = .{x, y, width, 24.0}, .text = status, .size = 14}) + y += 26.0 + Text.Draw(session, Text.Props.{.key = 22, .bounds = .{x, y, width, 44.0}, + .text = "Drag over her to turn around her; scroll to zoom.", .size = 14}) + if state.practice == PREVIEW_ASTRAL && preview_library.astral_even { + y += 46.0 + Text.Draw(session, Text.Props.{.key = 24, .bounds = .{x, y, width, 44.0}, + .text = "These practices have no astral session timings yet; steps are 20 s apart.", .size = 14}) + } + return 0 +} + +PreviewStyle :: () -> FigureStyle { + style := FigureStyleDefault() + style.watercolor = preview_state.watercolor + style.finish.pooling_strength = cast(float64)preview_state.pooling + style.finish.contour_strength = cast(float64)preview_state.contour + style.finish.grain_strength = cast(float64)preview_state.grain + return style +} + +PreviewPaintFigure :: (session: Session, area: Rectangle) { + state := *preview_state + // Paint at most 760 pixels tall; the image scales smoothly to the area. + scale := FigureClamp(760.0 / cast(float64)area.height, 0.2, 1.0) + width := cast(s32)(cast(float64)area.width * scale) + height := cast(s32)(cast(float64)area.height * scale) + if width < 16 || height < 16 { + return + } + key := PreviewKey(width, height) + if key != preview_painted_key || preview_textures[preview_shown].id == 0 { + moment := PreviewSample(*preview_library, state.practice, state.item, state.seconds) + view := PreviewViewOf(*moment, state.turn, state.lift, state.zoom) + if state.current { + camera := PreviewFlatOf(*moment, view) + preview_failed = !PreviewFlatRender(camera, view, width, height, *preview_picture) + } else { + preview_failed = !PreviewCompose(*preview_body, *preview_twin, *preview_media, *moment, view, + PreviewStyle(), width, height, *preview_picture) + } + if !preview_failed { + next := 1 - preview_shown + texture := preview_textures[next] + if texture.id != 0 && (texture.width != width || texture.height != height || + preview_owner.slot != session.slot || preview_owner.epoch != session.epoch) { + BitmapImageRelease(preview_owner, texture) + texture = .{} + } + if texture.id == 0 { + texture = BitmapImageCreate(session, width, height, preview_picture.pixels[:]) + preview_owner = session + } else if !BitmapImageUpdate(session, texture, preview_picture.pixels[:]) { + preview_failed = true + } + preview_textures[next] = texture + if texture.id != 0 && !preview_failed { + preview_shown = next + } + preview_painted_key = key + } + } + shown := preview_textures[preview_shown] + if shown.id != 0 { + // A new key with each new picture: Kryon's retained frames compare + // an image by its node and texture, not by the pixels behind it. + Image.Draw(session, Image.Props.{.key = cast(u64)(3 + preview_shown * 1000), .bounds = area, + .texture = shown, .smooth = true, .alt_text = "The practice figure"}) + } +} + +// Everything the picture depends on. +PreviewKey :: (width: s32, height: s32) -> u64 { + state := *preview_state + digest: u64 = 1469598103934665603 + values: [13]float64 = .[cast(float64)state.practice, cast(float64)state.item, state.seconds, + state.turn, state.lift, state.zoom, ifx state.watercolor then 1.0 else 0.0, + ifx state.current then 1.0 else 0.0, + cast(float64)state.pooling, cast(float64)state.contour, cast(float64)state.grain, + cast(float64)width, cast(float64)height] + for value: values { + bits := cast(u64)(value * 1000003.0 + 0.5) + digest = digest * 1099511628211 + bits + } + return digest +} + +PreviewSeconds :: (seconds: float64, duration: float64, output: []u8) -> string { + first: [16]u8 + second: [16]u8 + a := FormatDecimal(seconds, 1, first[:]) + b := FormatDecimal(duration, 1, second[:]) + length: s64 + for byte: a { + if length < output.count { + output[length] = byte + length += 1 + } + } + joiner := " / " + for byte: joiner { + if length < output.count { + output[length] = byte + length += 1 + } + } + for byte: b { + if length < output.count { + output[length] = byte + length += 1 + } + } + return TextView(output[0:length]) +} + +PreviewPracticesPath :: () -> string { + variable: [32]u8 + unused CopyCString("FIGURE_PRACTICES", variable[:]) + named := PreviewGetEnvironment(*variable[0]) + if named != null { + text := TextFromCString(named) + if text.count > 0 && text.count < 1024 { + return PreviewKeepPath(text) + } + } + // Ask beside the manifest, so the lock names the pinned checkout, then + // come back: capture paths stay relative to where the preview started. + started: [4096]u8 + here := PreviewWorkingDirectory(*started[0], 4096) + unused Desktop.DesktopStartBeside("preview_practices.zi") + output: [1100]u8 + arguments: [5]string = .["ziran", "pkg", "path", "practices", "--locked"] + result := CaptureOutput(arguments[:], output[:]) + // A desktop launcher's PATH may not include the user's own commands. + home_name: [8]u8 + unused CopyCString("HOME", home_name[:]) + home := PreviewGetEnvironment(*home_name[0]) + installed: [1100]u8 + if (result.error || result.code != 0) && home != null { + arguments[0] = PreviewJoin(installed[:], TextFromCString(home), "/.local/bin/ziran") + result = CaptureOutput(arguments[:], output[:]) + } + if here != null { + unused PreviewChangeDirectory(*started[0]) + } + if result.error || result.code != 0 || result.length <= 1 { + return "" + } + length := result.length + while length > 0 && (output[length - 1] == 10 || output[length - 1] == 13) { + length -= 1 + } + return PreviewKeepPath(TextView(output[0:length])) +} + +#scope_file +PreviewKeepPath :: (text: string) -> string { + if text.count >= 1024 { + return "" + } + for index: 0..text.count - 1 { + preview_practices_path[index] = text[index] + } + preview_practices_length = text.count + return TextView(preview_practices_path[0:preview_practices_length]) +} diff --git a/design/figure-preview/preview_capture.zi b/design/figure-preview/preview_capture.zi new file mode 100644 index 000000000..c7a883a49 --- /dev/null +++ b/design/figure-preview/preview_capture.zi @@ -0,0 +1,214 @@ +// Composing a moment's picture, and the fixed review frames the capture +// mode writes: each practice's hardest poses for the figure, painted and +// bare, on paper. +#import "figure_math" +#import "figure_joints" +#import "figure_paint" +#import "preview_practices" +#import "preview_flat" +#import "kryon/scene" +#import "sun_salutation_rules" +#import "workout_rules" +#import "std/file" +#import "std/png_linux" +#import "std/vec" +Raster :: #import "std/image"; + +// The body, and an astral twin over it, into OUT. +PreviewCompose :: (body: *FigurePainter, twin: *FigurePainter, media: *FigureMedia, + moment: *PreviewMoment, view: FigureView, style: FigureStyle, width: s32, height: s32, + out: *Raster.Image) -> bool { + still: FigureEffect + if !FigurePaint(body, media, *moment.body, moment.look, view, style, still, width, height) { + return false + } + if out.width != width || out.height != height || out.pixels.count != cast(s64)width * height * 4 { + if !VecResize(out.pixels, cast(s64)width * height * 4) { + return false + } + out.width = width + out.height = height + } + for index: 0..out.pixels.count - 1 { + out.pixels[index] = body.pixels.pixels[index] + } + if !moment.twin_visible { + return true + } + glow := style + glow.luminous = true + glow.opacity = 0.58 + if !FigurePaint(twin, media, *moment.twin, PreviewLuminous(), view, glow, moment.twin_effect, + width, height) { + return false + } + for index: 0..cast(s64)width * height - 1 { + at := index * 4 + a := cast(float64)twin.pixels.pixels[at + 3] / 255.0 + if a <= 0.0 { + continue + } + under := cast(float64)out.pixels[at + 3] / 255.0 + alpha := a + under * (1.0 - a) + for channel: 0..2 { + top := cast(float64)twin.pixels.pixels[at + channel] + bottom := cast(float64)out.pixels[at + channel] + out.pixels[at + channel] = cast(u8)((top * a + bottom * under * (1.0 - a)) / alpha + 0.5) + } + out.pixels[at + 3] = cast(u8)(alpha * 255.0 + 0.5) + } + return true +} + +PreviewShot :: struct { + practice: s32 + item: s32 + seconds: float64 + name: string +} + +PreviewCapture :: (media: *FigureMedia, library: *PreviewLibrary, directory: string) -> s32 { + shots: [24]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, 131.0, "tai-chi-131s"} + 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_WORKOUT, WORKOUT_PUSH_UPS, 1.0, "workout-push-up"} + count += 1 + shots[count] = .{PREVIEW_WORKOUT, WORKOUT_JUMPING_JACKS, 0.0, "workout-ready"} + count += 1 + shots[count] = .{PREVIEW_ASTRAL, 0, 60.0, "astral-60s"} + count += 1 + shots[count] = .{PREVIEW_ASTRAL, 4, PreviewDoubleTime(library, 4), "astral-rope-exit"} + count += 1 + shots[count] = .{PREVIEW_ASTRAL, 5, PreviewDoubleTime(library, 5), "astral-roll-out"} + count += 1 + body: FigurePainter + twin: FigurePainter + picture: Raster.Image + defer { + FigurePainterRelease(*body) + FigurePainterRelease(*twin) + VecFree(picture.pixels) + } + failed: s32 + 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) + for variant: 0..2 { + style := FigureStyleDefault() + style.watercolor = variant == 0 + suffix := ".png" + if variant == 1 { + suffix = "-bare.png" + } else if variant == 2 { + suffix = "-current.png" + } + stem: [1024]u8 + named: [1024]u8 + path: [1024]u8 + name := PreviewJoin(stem[:], directory, "/") + name = PreviewJoin(named[:], name, shot.name) + final := PreviewJoin(path[:], name, suffix) + painted := false + if variant == 2 { + camera := PreviewFlatOf(*moment, view) + painted = PreviewFlatRender(camera, view, width, height, *picture) + } else { + painted = PreviewCompose(*body, *twin, media, *moment, view, style, width, height, *picture) + } + if !painted { + print("figure capture: % could not be painted\n", final) + failed += 1 + continue + } + PreviewOnPaper(*picture) + if !WritePNGFile(final, *picture, false) { + print("figure capture: could not write %\n", final) + failed += 1 + } + } + } + print("figure capture: % frames, gpu=% %\n", count * 3, body.accelerated, body.diagnostic) + return failed +} + +#scope_file +// Lays the picture on the preview's paper colour. +PreviewOnPaper :: (picture: *Raster.Image) { + paper: [3]float64 = .[243.0, 237.0, 225.0] + for index: 0..cast(s64)picture.width * picture.height - 1 { + at := index * 4 + a := cast(float64)picture.pixels[at + 3] / 255.0 + for channel: 0..2 { + value := cast(float64)picture.pixels[at + channel] * a + paper[channel] * (1.0 - a) + picture.pixels[at + channel] = cast(u8)(value + 0.5) + } + picture.pixels[at + 3] = 255 + } +} + +// 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 +} + +// The first second the astral twin is out beside the body, a little on. +PreviewDoubleTime :: (library: *PreviewLibrary, item: s32) -> float64 { + duration := PreviewDuration(library, PREVIEW_ASTRAL, item) + seconds: float64 + while seconds < duration { + if PreviewSample(library, PREVIEW_ASTRAL, item, seconds).twin_visible { + return FigureClamp(seconds + 4.0, 0.0, duration) + } + seconds += 1.0 + } + return duration * 0.5 +} + +// 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 +} diff --git a/design/figure-preview/preview_flat.zi b/design/figure-preview/preview_flat.zi new file mode 100644 index 000000000..d5a734262 --- /dev/null +++ b/design/figure-preview/preview_flat.zi @@ -0,0 +1,159 @@ +// Breeze's current flat figure (tai_chi_flat.zi) for the same moment, so the +// preview can show it beside the new character. The flat painter emits +// filled shapes and stroked lines in the picture plane; this lays them on +// pixels with four rows of coverage per pixel row. +#import "figure_math" +#import "figure_paint" +#import "kryon/scene" +#import "tai_chi_math" +#import "tai_chi_view" +#import "tai_chi_flat" +#import "std/math" +#import "std/vec" +Raster :: #import "std/image"; + +preview_flat_frame: TaiChiFlatFrame; +preview_flat_edges: [512]float64; +preview_flat_shape: [256]TaiChiVector2; + +// Draws preview_flat_frame, painted for CAMERA, into OUT as VIEW frames the +// scene. +PreviewFlatRender :: (camera: TaiChiCamera, view: FigureView, width: s32, height: s32, + out: *Raster.Image) -> bool { + frame := *preview_flat_frame + if width < 1 || height < 1 || !VecResize(out.pixels, cast(s64)width * height * 4) { + return false + } + out.width = width + out.height = height + for index: 0..out.pixels.count - 1 { + out.pixels[index] = 0 + } + flat: TaiChiFlatView + TaiChiFlatViewSet(*flat, camera) + target := TaiChiProject(*flat, TaiChiV3(view.center.x, view.center.y, view.center.z)) + scale := cast(float64)height / view.height + for index: cast(s32)0..frame.op_count - 1 { + op := frame.ops[index] + if op.kind == TAI_CHI_FLAT_FILL { + count := TaiChiMin(cast(float64)op.count, 256.0) + for at: cast(s32)0..cast(s32)count - 1 { + preview_flat_shape[at] = PreviewFlatPixel(frame.points[op.first + at], target, scale, width, height) + } + PreviewFlatFill(out, preview_flat_shape[0:cast(s32)count], op.color, op.alpha) + } else { + half := op.width * scale * 0.5 + if half < 0.5 { + half = 0.5 + } + for at: cast(s32)0..op.count - 2 { + a := PreviewFlatPixel(frame.points[op.first + at], target, scale, width, height) + b := PreviewFlatPixel(frame.points[op.first + at + 1], target, scale, width, height) + along := TaiChiUnit2(TaiChiSub2(b, a), TaiChiV2(1.0, 0.0)) + side := TaiChiScale2(TaiChiV2(-along.y, along.x), half) + preview_flat_shape[0] = TaiChiAdd2(a, side) + preview_flat_shape[1] = TaiChiAdd2(b, side) + preview_flat_shape[2] = TaiChiSub2(b, side) + preview_flat_shape[3] = TaiChiSub2(a, side) + PreviewFlatFill(out, preview_flat_shape[0:4], op.color, op.alpha) + } + } + } + return true +} + +#scope_file +PreviewFlatPixel :: (point: TaiChiVector2, target: TaiChiVector2, scale: float64, width: s32, + height: s32) -> TaiChiVector2 { + return TaiChiV2(cast(float64)width * 0.5 + (point.x - target.x) * scale, + cast(float64)height * 0.5 - (point.y - target.y) * scale) +} + +// Even-odd fill with sixteen coverage samples per pixel: four rows, and +// exact horizontal span ends within each. +PreviewFlatFill :: (out: *Raster.Image, shape: []TaiChiVector2, color: u32, alpha: float64) { + if shape.count < 3 || alpha <= 0.0 { + return + } + top: float64 = 1000000.0 + bottom: float64 = -1000000.0 + for point: shape { + top = TaiChiMin(top, point.y) + bottom = TaiChiMax(bottom, point.y) + } + first := cast(s32)TaiChiMax(0.0, top) + last := cast(s32)TaiChiMin(cast(float64)out.height - 1.0, bottom) + red := cast(float64)((color >> 16) & 255) + green := cast(float64)((color >> 8) & 255) + blue := cast(float64)(color & 255) + coverage: [4096]float64 + for row: first..last { + left: s32 = out.width + right: s32 = -1 + for sample: 0..3 { + y := cast(float64)row + (cast(float64)sample + 0.5) / 4.0 + crossings: s32 + for at: 0..shape.count - 1 { + a := shape[at] + b := shape[(at + 1) % shape.count] + if (a.y <= y && b.y > y) || (b.y <= y && a.y > y) { + if crossings < 512 { + preview_flat_edges[crossings] = a.x + (y - a.y) * (b.x - a.x) / (b.y - a.y) + crossings += 1 + } + } + } + // Sort the few crossings in place. + for i: 1..crossings - 1 { + value := preview_flat_edges[i] + j := i - 1 + while j >= 0 && preview_flat_edges[j] > value { + preview_flat_edges[j + 1] = preview_flat_edges[j] + j -= 1 + } + preview_flat_edges[j + 1] = value + } + pair: s32 + while pair + 1 < crossings { + start := TaiChiMax(0.0, preview_flat_edges[pair]) + end := TaiChiMin(cast(float64)out.width, preview_flat_edges[pair + 1]) + pair += 2 + if end <= start { + continue + } + low := cast(s32)start + high := cast(s32)TaiChiMin(cast(float64)out.width - 1.0, end) + left = cast(s32)TaiChiMin(cast(float64)left, cast(float64)low) + right = cast(s32)TaiChiMax(cast(float64)right, cast(float64)high) + for x: low..high { + if x >= 4096 { + break + } + cell := TaiChiMin(end, cast(float64)x + 1.0) - TaiChiMax(start, cast(float64)x) + if cell > 0.0 { + coverage[x] += cell * 0.25 + } + } + } + } + for x: left..right { + if x < 0 || x >= 4096 { + continue + } + a := TaiChiMin(1.0, coverage[x]) * alpha + coverage[x] = 0.0 + if a <= 0.0 { + continue + } + at := (cast(s64)row * out.width + x) * 4 + under := cast(float64)out.pixels[at + 3] / 255.0 + total := a + under * (1.0 - a) + source: [3]float64 = .[red, green, blue] + for channel: 0..2 { + value := (source[channel] * a + cast(float64)out.pixels[at + channel] * under * (1.0 - a)) / total + out.pixels[at + channel] = cast(u8)(value + 0.5) + } + out.pixels[at + 3] = cast(u8)(total * 255.0 + 0.5) + } + } +} diff --git a/design/figure-preview/preview_practices.zi b/design/figure-preview/preview_practices.zi new file mode 100644 index 000000000..be6918156 --- /dev/null +++ b/design/figure-preview/preview_practices.zi @@ -0,0 +1,384 @@ +// The four practices' own choreography, sampled at any second and turned +// into the shared figure's joints. The pose code is Breeze's own: the build +// adds src/practices/tai_chi, src/practices/sun_salutation and apps/practice +// as module folders, so the preview shows exactly what the app plays. +#import "figure_math" +#import "figure_joints" +#import "figure_paint" +#import "kryon/scene" +#import "tai_chi_math" +#import "tai_chi_course" +#import "tai_chi_timeline" +#import "tai_chi_solve" +#import "tai_chi_view" +#import "tai_chi_flat" +#import "tai_chi_rules" +#import "workout_rules" +#import "workout_moves" +#import "sun_salutation_rules" +#import "sun_salutation_pose" +#import "astral_projection_rules" +#import "astral_technique" +#import "sun_salutation_artwork" +#import "preview_flat" +#import "std/file" +#import "std/math" + +PREVIEW_TAI_CHI :: 0 +PREVIEW_SUN_SALUTATION :: 1 +PREVIEW_WORKOUT :: 2 +PREVIEW_ASTRAL :: 3 + +PREVIEW_ITEM_LIMIT :: 64 + +// What one moment of a practice shows: the figure, and for an astral exit +// the translucent twin beside the still physical body. +PreviewMoment :: struct { + body: FigureJoints + look: FigureLook + // The practice's own camera and framing, which the flat figure shares. + camera: TaiChiCamera + target: Vector3 + height: float64 + solved: TaiChiSolved + rig: TaiChiRig + flat_look: TaiChiFlatLook + twin_visible: bool + twin: FigureJoints + twin_effect: FigureEffect +} + +PreviewLibrary :: struct { + ready: bool + tai_chi_items: [PREVIEW_ITEM_LIMIT]s32 + tai_chi_count: s32 + timeline_item: s32 + timeline: TaiChiTimeline + astral: [6]AstralCourse + astral_ready: [6]bool + astral_even: bool +} + +preview_astral_ids: [6]string = .["bruce_foundation_touch", "bruce_full_body_current", + "bruce_elevator_trance", "bruce_spin_loosening", "bruce_rope_exit", "bruce_roll_out_exit"]; +preview_astral_names: [6]string = .["Foundation touch", "Full-body current", "Elevator trance", + "Spin loosening", "Rope exit", "Roll-out exit"]; +preview_workout_names: [12]string = .["Jumping jacks", "Wall sit", "Push-ups", "Crunches", + "Step-ups", "Squats", "Triceps dips", "Plank", "High knees", "Lunges", "Push-up rotation", + "Side plank"]; + +// PRACTICES is the practices package checkout (its taichi/ and +// astral_projection/ folders). +PreviewLibraryLoad :: (library: *PreviewLibrary, practices: string) -> bool { + path: [1024]u8 + course, found := ReadEntireFile(PreviewJoin(path[:], practices, "/taichi/course.tch")) + if !found || !TaiChiCourseParse(*tai_chi_course, course) { + return false + } + library.tai_chi_count = 0 + // Breakdown and follow-along items replay a demo's choreography slower. + for index: cast(s32)0..tai_chi_course.item_count - 1 { + id := TaiChiCourseString(tai_chi_course.items[index].id) + if PreviewEndsWith(id, "/breakdown") || PreviewEndsWith(id, "/follow") { + continue + } + if library.tai_chi_count < PREVIEW_ITEM_LIMIT { + library.tai_chi_items[library.tai_chi_count] = index + library.tai_chi_count += 1 + } + } + library.timeline_item = -1 + for technique: 0..5 { + name: [128]u8 + file := PreviewJoin(name[:], "/astral_projection/sessions/", preview_astral_ids[technique]) + full: [1024]u8 + relative := PreviewJoin(full[:], practices, file) + tail: [1100]u8 + data, ok := ReadEntireFile(PreviewJoin(tail[:], relative, ".json")) + library.astral_ready[technique] = ok && AstralCourseParse(data, *library.astral[technique]) + if !library.astral_ready[technique] { + // A practices checkout without the session timings still plays + // each technique, with its steps evenly twenty seconds apart. + PreviewAstralEven(*library.astral[technique]) + library.astral_ready[technique] = true + library.astral_even = true + } + } + library.ready = library.tai_chi_count > 0 + return library.ready +} + +PreviewItemCount :: (library: *PreviewLibrary, practice: s32) -> s32 { + if practice == PREVIEW_TAI_CHI { + return library.tai_chi_count + } + if practice == PREVIEW_SUN_SALUTATION { + return 1 + } + if practice == PREVIEW_WORKOUT { + return WORKOUT_EXERCISE_COUNT + } + return 6 +} + +PreviewItemName :: (library: *PreviewLibrary, practice: s32, item: s32) -> string { + if practice == PREVIEW_TAI_CHI && item >= 0 && item < library.tai_chi_count { + return TaiChiCourseString(tai_chi_course.items[library.tai_chi_items[item]].id) + } + if practice == PREVIEW_SUN_SALUTATION { + return "Two rounds, both legs" + } + if practice == PREVIEW_WORKOUT && item >= 0 && item < 12 { + return preview_workout_names[item] + } + if practice == PREVIEW_ASTRAL && item >= 0 && item < 6 { + return preview_astral_names[item] + } + return "" +} + +// The item the practice opens on: Tai Chi's full form, the Workout's squat. +PreviewDefaultItem :: (library: *PreviewLibrary, practice: s32) -> s32 { + if practice == PREVIEW_TAI_CHI { + for index: cast(s32)0..library.tai_chi_count - 1 { + if PreviewItemName(library, practice, index) == "full/normal" { + return index + } + } + } + if practice == PREVIEW_WORKOUT { + return WORKOUT_SQUATS + } + return 0 +} + +// Seconds the item plays before it starts again. +PreviewDuration :: (library: *PreviewLibrary, practice: s32, item: s32) -> float64 { + if practice == PREVIEW_TAI_CHI { + if !PreviewTimeline(library, item) { + return 1.0 + } + return library.timeline.duration + } + if practice == PREVIEW_SUN_SALUTATION { + total: float64 + for round: cast(s32)0..1 { + for step: cast(s32)0..SUN_SALUTATION_STEP_COUNT - 1 { + total += cast(float64)SalutationStepSeconds(PreviewSunState(round, step)) + } + } + return total + } + if practice == PREVIEW_WORKOUT { + return cast(float64)WORKOUT_WORK_MS / 1000.0 + } + if item >= 0 && item < 6 && library.astral_ready[item] { + return cast(float64)library.astral[item].duration + } + return 60.0 +} + +PreviewSample :: (library: *PreviewLibrary, practice: s32, item: s32, seconds: float64) -> PreviewMoment { + moment: PreviewMoment + if practice == PREVIEW_TAI_CHI { + moment.rig = TaiChiRigFor(0) + moment.flat_look = TaiChiFlatLookFor(0) + if PreviewTimeline(library, item) { + pose := TaiChiTimelineSample(*library.timeline, seconds) + moment.solved = TaiChiSolvePose(*pose, *moment.rig) + } + // The forms travel; the camera follows the pelvis over the floor. + moment.camera.yaw = 0.42 + moment.camera.tilt = 0.12 + moment.camera.center = TaiChiV3(moment.solved.pelvis.x, 0.0, moment.solved.pelvis.z) + PreviewAim(*moment, 2.3, moment.rig.height * 0.5) + } else if practice == PREVIEW_SUN_SALUTATION { + moment.rig = WorkoutRig() + state := PreviewSunAt(seconds) + pose := SunSalutationPoseAt(state, *moment.rig) + moment.solved = WorkoutSolve(*pose, *moment.rig) + moment.flat_look = SunSalutationWomanLook() + framing := SunSalutationPaintPose(*preview_flat_frame, 0, pose, SunSalutationViewAt(state), 600.0, 800.0) + moment.camera = framing.camera + PreviewAim(*moment, framing.view_height, framing.view_center) + } else if practice == PREVIEW_WORKOUT { + moment.rig = WorkoutRig() + pose := WorkoutPoseAt(item, cast(s64)(seconds * 1000.0), *moment.rig) + moment.solved = WorkoutSolve(*pose, *moment.rig) + moment.flat_look = TaiChiFlatLookNamed("jadeWoman") + framing := WorkoutFramingFor(item) + moment.camera = framing.camera + PreviewAim(*moment, framing.view_height, framing.view_center) + } else { + moment.rig = WorkoutRig() + moment.flat_look = TaiChiFlatLookNamed("jadeWoman") + moment.flat_look.top = 0x597888 + moment.flat_look.pants = 0x384d65 + if item >= 0 && item <= 5 && library.astral_ready[item] { + scene := AstralTechniqueAt(preview_astral_ids[item], *library.astral[item], cast(s64)(seconds * 1000.0)) + moment.solved = WorkoutSolve(*scene.physical, *moment.rig) + moment.camera = scene.camera + PreviewAim(*moment, 2.15, 0.35) + moment.twin_visible = scene.double_visible + if scene.double_visible { + awareness := WorkoutSolve(*scene.awareness, *moment.rig) + moment.twin = PreviewJoints(*awareness, *moment.rig) + moment.twin_effect.offset = FigureV3(scene.double_offset.x, scene.double_offset.y, + scene.double_offset.z) + moment.twin_effect.pivot = FigureV3(moment.solved.pelvis.x, moment.solved.pelvis.y, + moment.solved.pelvis.z) + moment.twin_effect.roll = scene.double_roll + } + } else { + resting := AstralRestingPose() + moment.solved = WorkoutSolve(*resting, *moment.rig) + } + } + moment.body = PreviewJoints(*moment.solved, *moment.rig) + moment.look = .{skin = moment.flat_look.skin, top = moment.flat_look.top, + trousers = moment.flat_look.pants, hair = moment.flat_look.hair} + return moment +} + +// The view of MOMENT turned TURN and lifted LIFT radians from the practice's +// own camera and zoomed ZOOM times, and the flat figure's camera to match. +PreviewViewOf :: (moment: *PreviewMoment, turn: float64, lift: float64, zoom: float64) -> FigureView { + view: FigureView + view.yaw = moment.camera.yaw + turn + view.pitch = FigureClamp(moment.camera.tilt + lift, -1.4, 1.5) + view.height = moment.height / zoom + view.center = moment.target + return view +} + +PreviewFlatOf :: (moment: *PreviewMoment, view: FigureView) -> TaiChiCamera { + camera := moment.camera + camera.yaw = view.yaw + camera.tilt = view.pitch + TaiChiFlatPaintLook(*preview_flat_frame, *moment.solved, *moment.rig, moment.flat_look, camera) + return camera +} + +PreviewLuminous :: () -> FigureLook { + return .{skin = 0xb2d8df, top = 0x7db7d5, trousers = 0x7a8fca, hair = 0x7ca1bc} +} + +PreviewJoints :: (solved: *TaiChiSolved, rig: *TaiChiRig) -> FigureJoints { + joints: FigureJoints + joints.height = rig.height + joints.pelvis = PreviewVector(solved.pelvis) + joints.spine = PreviewVector(solved.spine1) + joints.chest = PreviewVector(solved.chest) + joints.neck = PreviewVector(solved.neck_base) + joints.pelvis_turn = PreviewQuat(solved.pelvis_quat) + joints.chest_turn = PreviewQuat(solved.chest_quat) + joints.head_turn = PreviewQuat(solved.head_quat) + for side: 0..1 { + joints.shoulders[side] = PreviewVector(solved.shoulders[side]) + joints.elbows[side] = PreviewVector(solved.elbows[side]) + joints.wrists[side] = PreviewVector(solved.wrists[side]) + joints.hands[side] = PreviewQuat(solved.hand_quats[side]) + joints.fist[side] = solved.hand_shapes[side][1] + joints.hook[side] = solved.hand_shapes[side][2] + joints.hips[side] = PreviewVector(solved.hips[side]) + joints.knees[side] = PreviewVector(solved.knees[side]) + joints.ankles[side] = PreviewVector(solved.ankles[side]) + joints.toes[side] = PreviewVector(solved.toes[side]) + joints.feet[side] = PreviewQuat(solved.foot_quats[side]) + } + return joints +} + +PreviewAstralEven :: (course: *AstralCourse) { + course.count = 16 + course.duration = 330 + stem := "astral_text_preview_" + for index: cast(s32)0..15 { + cue := *course.cues[index] + cue.second = index * 20 + for at: 0..stem.count - 1 { + cue.key[at] = stem[at] + } + cue.key[stem.count] = cast(u8)(48 + index / 10) + cue.key[stem.count + 1] = cast(u8)(48 + index % 10) + cue.key[stem.count + 2] = 0 + } +} + +PreviewEndsWith :: (text: string, suffix: string) -> bool { + return text.count >= suffix.count && text[text.count - suffix.count:text.count] == suffix +} + +PreviewJoin :: (buffer: []u8, a: string, b: string) -> string { + if a.count + b.count > buffer.count { + return "" + } + for index: 0..a.count - 1 { + buffer[index] = a[index] + } + for index: 0..b.count - 1 { + buffer[a.count + index] = b[index] + } + return TextView(buffer[0:a.count + b.count]) +} + +#scope_file +PreviewVector :: (value: TaiChiVector3) -> Vector3 { + return .{value.x, value.y, value.z} +} + +PreviewQuat :: (value: TaiChiQuaternion) -> FigureQuat { + return .{value.x, value.y, value.z, value.w} +} + +// Frames the practice's camera as its painter does: VIEW_HEIGHT metres tall, +// centred UP metres above the camera's floor point along its up direction. +PreviewAim :: (moment: *PreviewMoment, view_height: float64, up: float64) { + flat: TaiChiFlatView + TaiChiFlatViewSet(*flat, moment.camera) + target := TaiChiAddScaled3(flat.center, flat.up, up) + moment.target = FigureV3(target.x, target.y, target.z) + moment.height = view_height +} + +PreviewTimeline :: (library: *PreviewLibrary, item: s32) -> bool { + if item < 0 || item >= library.tai_chi_count { + return false + } + if library.timeline_item == item { + return true + } + if !TaiChiTimelineForItem(*library.timeline, tai_chi_course.items[library.tai_chi_items[item]]) { + library.timeline_item = -1 + return false + } + library.timeline_item = item + return true +} + +PreviewSunState :: (round: s32, step: s32) -> SunSalutationPracticeState { + state: SunSalutationPracticeState + state.repetitions = 2 + state.repetition = round + state.step = step + state.start_seconds = SUN_SALUTATION_DEFAULT_START_SECONDS + state.end_seconds = SUN_SALUTATION_DEFAULT_END_SECONDS + return state +} + +// Two full rounds, the second leading with the other leg. +PreviewSunAt :: (seconds: float64) -> SunSalutationPracticeState { + left := seconds + for round: cast(s32)0..1 { + for step: cast(s32)0..SUN_SALUTATION_STEP_COUNT - 1 { + state := PreviewSunState(round, step) + length := cast(float64)SalutationStepSeconds(state) + if left < length || (round == 1 && step == SUN_SALUTATION_STEP_COUNT - 1) { + state.step_ticks = cast(s32)(FigureClamp(left, 0.0, length) * 60.0) + return state + } + left -= length + } + } + return PreviewSunState(0, 0) +} diff --git a/design/figure-preview/ziran.lock b/design/figure-preview/ziran.lock new file mode 100644 index 000000000..85a762460 --- /dev/null +++ b/design/figure-preview/ziran.lock @@ -0,0 +1,149 @@ +{ + "format": 1, + "packages": [ + { + "commit": "c96f5e3de9c4488f4e6c97f59f5245f22fda22f7", + "dependencies": {}, + "id": "p0dd67588478f9a3e", + "name": "libsamplerate", + "ref": "c96f5e3de9c4488f4e6c97f59f5245f22fda22f7", + "source": true, + "url": "https://github.com/libsndfile/libsamplerate.git" + }, + { + "commit": "829a65d769d935c4852f8159e964312c0957260a", + "dependencies": {}, + "id": "p16cd0434af3dff7b", + "name": "sdl3", + "ref": "829a65d769d935c4852f8159e964312c0957260a", + "source": true, + "url": "https://github.com/libsdl-org/SDL.git" + }, + { + "commit": "199b51c97c2aba67367308c3dc98126692b476a8", + "dependencies": {"audio": "p59deb49c7f103127", "avif": "pb203529d803e73a4", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "raylib": "pcc7aa76725f2fb2f", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"}, + "dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "2611e727e226a2af22dfb7f10bf224bd2f1a3d30", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "raylib": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"}, + "id": "p1e41122208040e0f", + "name": "kryon", + "ref": "199b51c97c2aba67367308c3dc98126692b476a8", + "url": "https://github.com/kryonlabs/kryon.git" + }, + { + "commit": "ec6ed304767e10c125ab2820f3b01d6fd773e147", + "dependencies": {"image_codec": "p69c0e968ae5735d9", "libwebp": "p3b3c785a0e17e605"}, + "dependency_refs": {"image_codec": "360553150917f352e49e1beb78917370792ccaef", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339"}, + "id": "p2af20e0c835b1c63", + "name": "webp", + "ref": "ec6ed304767e10c125ab2820f3b01d6fd773e147", + "url": "https://github.com/ziranlang/webp.git" + }, + { + "commit": "1844042d1ca751e62e61df7617dba19c98588339", + "dependencies": {}, + "id": "p3b3c785a0e17e605", + "name": "libwebp", + "ref": "1844042d1ca751e62e61df7617dba19c98588339", + "source": true, + "url": "https://github.com/webmproject/libwebp.git" + }, + { + "commit": "1aadfad932c98c069a1204261b1856f81f3bc199", + "dependencies": {}, + "id": "p57c0272208aae8db", + "name": "libavif", + "ref": "1aadfad932c98c069a1204261b1856f81f3bc199", + "source": true, + "url": "https://github.com/AOMediaCodec/libavif.git" + }, + { + "commit": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", + "dependencies": {"libopus": "pe1fe9bbe80bf1d6d", "libsamplerate": "p0dd67588478f9a3e"}, + "dependency_refs": {"libopus": "ddbe48383984d56acd9e1ab6a090c54ca6b735a6", "libsamplerate": "c96f5e3de9c4488f4e6c97f59f5245f22fda22f7"}, + "id": "p59deb49c7f103127", + "name": "audio", + "ref": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", + "url": "https://github.com/ziranlang/audio.git" + }, + { + "commit": "bd278217e2bac754d868b3d6502eac4257658d4b", + "dependencies": {"dav1d": "pb24767b9eb34d961", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa"}, + "dependency_refs": {"dav1d": "54706fc6bc0cdecab7e9593974a4039cc038fca7", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937"}, + "id": "p5d7d12505a42f7b3", + "name": "av1", + "ref": "bd278217e2bac754d868b3d6502eac4257658d4b", + "url": "https://github.com/ziranlang/av1.git" + }, + { + "commit": "b38054fcb0afd699c8442d050b323e361b90f027", + "dependencies": {"kryon": "p1e41122208040e0f"}, + "dependency_refs": {"kryon": "199b51c97c2aba67367308c3dc98126692b476a8"}, + "id": "p60c5d0ebd34a5724", + "name": "practices", + "ref": "b38054fcb0afd699c8442d050b323e361b90f027", + "url": "https://github.com/waozixyz/practices.git" + }, + { + "commit": "360553150917f352e49e1beb78917370792ccaef", + "dependencies": {}, + "id": "p69c0e968ae5735d9", + "name": "image-codec", + "ref": "360553150917f352e49e1beb78917370792ccaef", + "url": "https://github.com/ziranlang/image-codec.git" + }, + { + "commit": "b141d9c45db236335351c8a4b2a14b3ade27c937", + "dependencies": {}, + "id": "p74ab3cb6eb3b86fa", + "name": "libaom", + "ref": "b141d9c45db236335351c8a4b2a14b3ade27c937", + "source": true, + "url": "https://aomedia.googlesource.com/aom" + }, + { + "commit": "2611e727e226a2af22dfb7f10bf224bd2f1a3d30", + "dependencies": {"av1": "p5d7d12505a42f7b3", "dav1d": "pb24767b9eb34d961", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db"}, + "dependency_refs": {"av1": "bd278217e2bac754d868b3d6502eac4257658d4b", "dav1d": "54706fc6bc0cdecab7e9593974a4039cc038fca7", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199"}, + "id": "pb203529d803e73a4", + "name": "avif", + "ref": "2611e727e226a2af22dfb7f10bf224bd2f1a3d30", + "url": "https://github.com/ziranlang/avif.git" + }, + { + "commit": "54706fc6bc0cdecab7e9593974a4039cc038fca7", + "dependencies": {}, + "id": "pb24767b9eb34d961", + "name": "dav1d", + "ref": "54706fc6bc0cdecab7e9593974a4039cc038fca7", + "source": true, + "url": "https://github.com/videolan/dav1d.git" + }, + { + "commit": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "dependencies": {}, + "id": "pcc7aa76725f2fb2f", + "name": "raylib", + "ref": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", + "source": true, + "url": "https://github.com/raysan5/raylib.git" + }, + { + "commit": "ddbe48383984d56acd9e1ab6a090c54ca6b735a6", + "dependencies": {}, + "id": "pe1fe9bbe80bf1d6d", + "name": "libopus", + "ref": "ddbe48383984d56acd9e1ab6a090c54ca6b735a6", + "source": true, + "url": "https://github.com/xiph/opus.git" + } + ], + "root": { + "dependencies": {"kryon": "p1e41122208040e0f", "practices": "p60c5d0ebd34a5724"}, + "dependency_refs": {"kryon": "199b51c97c2aba67367308c3dc98126692b476a8", "practices": "b38054fcb0afd699c8442d050b323e361b90f027"}, + "name": "figure_preview" + }, + "toolchain": { + "commit": "2313b49cb005610853faeec46447fc38578a2a15", + "ref": "2313b49cb005610853faeec46447fc38578a2a15", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/design/figure-preview/ziran.toml b/design/figure-preview/ziran.toml new file mode 100644 index 000000000..721b9f6d1 --- /dev/null +++ b/design/figure-preview/ziran.toml @@ -0,0 +1,18 @@ +[package] +name = "figure_preview" +entry = "figure_preview.zi" +module_roots = ["."] +links = ["SDL2", "pangocairo-1.0", "pango-1.0", "cairo", "fontconfig", "freetype", "gio-2.0", "gobject-2.0", "glib-2.0", "X11", "EGL", "GLESv2", "GLX", "z", "m", "pthread"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "2313b49cb005610853faeec46447fc38578a2a15" + +[dependencies.kryon] +git = "https://github.com/kryonlabs/kryon.git" +ref = "199b51c97c2aba67367308c3dc98126692b476a8" + +# The shared figure and its media (anatomy, textures, Tai Chi course). +[dependencies.practices] +git = "https://github.com/waozixyz/practices.git" +ref = "b38054fcb0afd699c8442d050b323e361b90f027" diff --git a/plans/2026-11-17-astral-projection.md b/plans/2026-11-17-astral-projection.md index a1a0bbc71..462b44b43 100644 --- a/plans/2026-11-17-astral-projection.md +++ b/plans/2026-11-17-astral-projection.md @@ -1,47 +1,53 @@ # Open practice figure work — 2026-11-17 -The owner asked for the anatomical watercolor figures in the real Breeze app -(Ziran + Kryon, no raylib) for Tai Chi, Workout and astral practice. The -three.js studies in `design/astral-projection/three/` are the look reference -only. The shared figure media is in practices `shared/watercolor/`: -MakeHuman CC0 surface, MakeHuman CC0 default rig weights folded onto 46 -bones, clean plane-cut hems, and paper/pigment/wash textures. +The owner wants one perfected character used everywhere: Tai Chi, Sun +Salutation, Workout and astral projection, native on Ziran and Kryon +without raylib. The character is the practices package's `figure/` (pinned +practices b38054fc: female MakeHuman CC0 surface, dual-quaternion rig, +watercolor painter, floor shadow, Ziran tests). The live tuning tool is +`design/figure-preview/` (see its README). The three.js studies in +`design/astral-projection/three/` are look references only. -- Shared Kryon repin. Breeze pins Kryon 3f724419, which has no - `kryon/scene_skin` or `kryon/watercolor`. The repin must move plot, entity, - game2d, css and apps to the same Kryon, and move - `src/platform/audio_runtime.zi` and `radio_native.zi` from Kryon's removed - mixer/sink/decoders to Ziran std audio. It is coordinated with the - raylib-free desktop work (`plans/2026-11-15-breeze-desktop-without-raylib.md`). - The coordinator decides who owns that switch. -- Connect the painters after the repin, all under `DESKTOP_HOST`: - - Tai Chi: `TaiChiPaintFlat` call sites in `tai_chi_draw.zi`, through - `PracticeWatercolorDraw`. - - Workout: its solved pose through the same painter. - - Astral: `AstralSceneDraw`/`AstralPreviewDraw` through - `AstralWatercolorDraw` (`apps/practice/astral_watercolor_view.zi`), which - composes the still physical body, translucent double, cord, rope and - attention marks from `astral_technique.zi`. - Keep existing timelines, cues, narration, pause/resume and history. -- Native after frames on private Xvfb, matching `build/practice-compare/before/` - (Tai Chi 9 s, full form 131 s, astral 60 s, Workout ready). Then labelled - `side-by-side/` images, own inspection, and Gwenview for the owner. Retake - the full-form before frame, which shows the voice-guidance offer instead of - the figure. -- Local suites on the integrated build: `make astral-projection-test` (now - includes `tests/astral_technique_test.sh`), Tai Chi and Workout tests, - `tests/practice_human_test.sh`, build laws, package/size checks, chooser - previews, captions and cue boundaries. Bundled delivery of - `assets/practices/shared/watercolor/` from the common Practices pack is - configured in `scripts/prepare-package-assets.py`; verify it in the - built app. -- Performance: the painter re-renders and re-finishes the body on every pose - change. Measure on a real phone GPU; shell GL here is llvmpipe. -- Owner review of the native characters, watercolor look, Tai Chi motion and - the six astral techniques. -- Narration: the initial audit found all 88 cue texts in each of ten locales - but only 407/880 female clips (cs 6, de 86, en 86, es 78, fr 37, ja 5, - ko 6, pt 88, ru 10, zh 5). Complete and listen through the owning +- Kryon desktop presentation: at Kryon 199b51c9 the desktop loop keeps + drawing requested frames, but stops showing them once there is no input + (a 24-line repro was sent to the coordinator for the Kryon lane). The + preview's hands-off playback freezes until the pointer moves. Repin the + preview once Kryon is fixed. The same Kryon compares images by texture id + only, so `BitmapImageUpdate` on one texture never repaints; the preview + alternates two textures. +- Owner review of the live preview and of + `build/practice-compare/side-by-side/` (current flat figure beside the new + character, same moments and cameras), then tune from that feedback. +- Character work still open: + - The top and trousers hug the body; the flat figure's loose tunic needs + cloth geometry (a skirted tunic shell over the hips). + - Relaxed and open hands still read as claws in some Tai Chi palms. + - The face is plain at practice sizes. + - Each practice's look colours come from its flat look; settle one + palette. +- Astral session timings: practices' `astral_projection/sessions/*.json` + are another task's uncommitted files. Until they are pushed, a pinned + preview plays the six techniques with steps evenly 20 seconds apart. +- Breeze adoption, after the shared Kryon repin (lane -7, plot, entity, + game2d, css and apps on one Kryon; Breeze audio on Ziran std audio): + - Make Breeze's `practices` dependency a package pinned at or after + b38054fc. + - Move `PreviewJoints` (solver joints to `FigureJoints`) beside the + solver. + - Paint Tai Chi, Sun Salutation, Workout and astral with `FigurePaint` + under `DESKTOP_HOST`, keeping timelines, cues, narration, pause/resume + and history. Bundle `assets/practices/shared/watercolor/` from the + common Practices pack (the `scripts/prepare-package-assets.py` hunk is + ready). +- Native app after-frames for those painters, matching + `build/practice-compare/before/`. Retake the full-form before frame: it + shows the voice-guidance offer instead of the figure. +- Performance: the painter re-skins and re-finishes on every pose change + (watercolor finish and shadow on the CPU). Measure on a real phone GPU; + shell GL here is llvmpipe. +- Narration: the initial audit found all 88 cue texts in each of ten + locales but only 407/880 female clips (cs 6, de 86, en 86, es 78, fr 37, + ja 5, ko 6, pt 88, ru 10, zh 5). Complete and listen through the owning narration task; legacy cues do not fill the new exercise keys. -- Refresh the three.js caption snapshot if the practices task changes session - cues or text. Keep `build/astral-compare/before/` as the baseline. +- Refresh the three.js caption snapshot if the practices task changes + session cues or text. Keep `build/astral-compare/before/` as the baseline. diff --git a/scripts/prepare-practice-human.py b/scripts/prepare-practice-human.py index c310057be..0d30c449b 100644 --- a/scripts/prepare-practice-human.py +++ b/scripts/prepare-practice-human.py @@ -23,6 +23,15 @@ for line in gzip.open(source_file, 'rt'): elif line.startswith('f '): groups[group].append([int(item.split('/')[0]) - 1 for item in line.split()[1:]]) all_vertices = np.asarray(vertices, dtype=np.float64) +# MakeHuman's base mesh is its gender-neutral average. Her body and face take +# MakeHuman's own CC0 young-adult female macro shape: the three ethnic +# female-young targets in equal shares, as MakeHuman mixes them by default. +for race in ('african', 'asian', 'caucasian'): + target = ROOT / f'design/astral-projection/three/assets/makehuman-{race}-female-young.target.gz' + for line in gzip.open(target, 'rt'): + if line.strip() and not line.startswith('#'): + index, dx, dy, dz = line.split() + all_vertices[int(index)] += np.array([float(dx), float(dy), float(dz)])/3 ids = sorted({index for face in groups['body'] for index in face}) lookup = {old: index for index, old in enumerate(ids)} body = all_vertices[ids] @@ -158,6 +167,56 @@ for index, influence in enumerate(gathered): joints[index, slot] = target weights[index, slot] = weight/total +# MakeHuman hands the groin and buttock from pelvis to thigh within a few +# millimetres, which tears the surface open in a forward fold or a deep +# stance. Diffuse that hand-over across the surface around the hips, and the +# shoulder's chest-to-arm hand-over the same way for arms raised overhead; +# other bones' shares, the hands and the fingers stay as rigged. +face_triangles = np.array([[lookup[face[0]], lookup[face[at]], lookup[face[at+1]]] + for face in groups['body'] for at in range(1, len(face)-1)], dtype=np.int64) +pairs = np.concatenate([face_triangles[:, [0, 1]], face_triangles[:, [1, 2]], face_triangles[:, [2, 0]]]) +pairs = np.unique(np.concatenate([pairs, pairs[:, ::-1]]), axis=0) +degree = np.bincount(pairs[:, 0], minlength=len(ids)).astype(np.float64) +dense = np.zeros((len(ids), len(bones))) +for slot in range(4): + np.add.at(dense, (np.arange(len(ids)), joints[:, slot]), weights[:, slot]) +arm_share = dense[:, [at for at, item in enumerate(bones) if 5 <= item['role'] <= 7]].sum(axis=1) + +def diffuse(members, zone, steps): + shares = dense[:, members] + mass = shares.sum(axis=1) + active = zone & (mass > 0.02) + for _ in range(steps): + total = np.zeros_like(shares) + np.add.at(total, pairs[:, 0], shares[pairs[:, 1]]) + mean = total/np.maximum(degree, 1)[:, None] + mixed = shares*0.5+mean*0.5 + scale = mass/np.maximum(mixed.sum(axis=1), 1e-12) + shares[active] = mixed[active]*scale[active, None] + dense[:, members] = shares + +hips_y = (joint('l-upper-leg')[1]+joint('r-upper-leg')[1])/2 +hip_zone = (positions[:, 1] > hips_y-0.24) & (positions[:, 1] < hips_y+0.07) & (arm_share < 0.01) +thighs = [4+18, 4+21+18] +diffuse([0]+thighs, hip_zone, 120) +# The thighs never reach above the hip crease: a lifted or spread leg must +# not drag the waist out with it. +fade = np.clip((hips_y+0.07-positions[:, 1])/0.09, 0, 1) +for thigh in thighs: + removed = dense[:, thigh]*(1-fade) + dense[:, thigh] -= removed + dense[:, 0] += removed +for side in (0, 1): + shoulder = joint(('l' if side == 0 else 'r')+'-shoulder') + near = np.linalg.norm(positions-shoulder, axis=1) < 0.16 + diffuse([1, 2, 4+side*21], near & (arm_share < 0.01), 40) +for index in range(len(ids)): + strongest = np.argsort(-dense[index])[:4] + shares = dense[index, strongest] + shares[shares < 0.004] = 0.0 + joints[index] = strongest + weights[index] = shares/shares.sum() + # The middle waist belongs to the trunk even when a hand passes near it. waist = (positions[:, 1] > joint('pelvis')[1]+0.06) & (positions[:, 1] < joint('spine-3')[1]) & (np.abs(positions[:, 0]) < 0.25) arm_roles = np.array([4 <= item['role'] <= 7 for item in bones]) @@ -186,6 +245,21 @@ head = joint('head') cuffs = [abs(joint(prefix+'-hand')[0])-0.025 for prefix in ('l', 'r')] ankles = [joint(prefix+'-ankle')[1]+0.02 for prefix in ('l', 'r')] +eye_height = joint('l-eye')[1] + +def hairline(points): + """Positive on the hair: a hairline high on the forehead, lower over + the temples, behind the ears and down to the nape; the ears stay bare.""" + points = np.atleast_2d(points) + x, y, z = points[:, 0], points[:, 1], points[:, 2] + turn = np.degrees(np.arctan2(np.abs(x), z-0.02)) + height = eye_height+np.interp(turn, [0, 20, 45, 65, 85, 105, 125, 180], + [0.064, 0.062, 0.052, 0.034, 0.008, -0.014, -0.045, -0.080]) + value = y-height + ears = (y < eye_height+0.002) & (np.abs(x) > 0.070) + value = np.where(ears, np.minimum(value, 0.070-np.abs(x)), value) + return np.where(y < 1.44, -1.0, value) + def region_of(points): points = np.atleast_2d(points) x, y, z = points[:, 0], points[:, 1], points[:, 2] @@ -199,23 +273,26 @@ def region_of(points): # Hands hang below the hips in the bind pose; only arms reach this wide. regions[(np.abs(x) > cuff) & (y > 0.5)] = 0 regions[y < ankle] = 0 - regions[(y > head[1]+0.055) | ((y > head[1]-0.015) & (z < head[2]-0.015))] = 3 + regions[hairline(points) > 0] = 3 return regions # Clean painted hems: split each triangle that crosses a region boundary # exactly along the boundary's plane, then give every region its own copy # of the shared hem vertices. Surface and skin stay continuous. -cuts = [(1, hip_height-0.04), (1, neck_height-0.04), (1, neck_height+0.025), - (0, 0.115), (0, -0.115), (1, head[1]+0.055), (1, head[1]-0.015), (2, head[2]-0.015)] -cuts += [(0, cuff) for cuff in cuffs] + [(0, -cuff) for cuff in cuffs] + [(1, ankle) for ankle in ankles] +def plane(axis, value): + return lambda points: np.atleast_2d(points)[:, axis]-value + +planes = [(1, hip_height-0.04), (1, neck_height-0.04), (1, neck_height+0.025), (0, 0.115), (0, -0.115)] +planes += [(0, cuff) for cuff in cuffs] + [(0, -cuff) for cuff in cuffs] + [(1, ankle) for ankle in ankles] +cuts = [(('plane',)+item, plane(*item)) for item in planes] + [(('hairline',), hairline)] points, skin_joints, skin_weights = list(positions), list(joints), list(weights) surface_normals = list(normals) edges = {} -def hem_point(a, b, axis, value): - key = (min(a, b), max(a, b), axis, value) +def hem_point(a, b, name, cut): + key = (min(a, b), max(a, b), name) if key not in edges: - da, db = points[a][axis]-value, points[b][axis]-value + da, db = float(cut(points[a])[0]), float(cut(points[b])[0]) t = da/(da-db) points.append(points[a]+(points[b]-points[a])*t) normal = surface_normals[a]*(1-t)+surface_normals[b]*t @@ -232,11 +309,11 @@ def hem_point(a, b, axis, value): return edges[key] faces = [list(face) for face in triangles] -for axis, value in cuts: +for name, cut in cuts: split = [] for face in faces: corners = np.array([points[at] for at in face]) - distance = corners[:, axis]-value + distance = cut(corners) if len(set(region_of(corners))) == 1 or not (np.any(distance > 1e-7) and np.any(distance < -1e-7)): split.append(face) continue @@ -245,12 +322,12 @@ for axis, value in cuts: # One corner lies on the plane: cut the opposite edge once. on = int(np.where(side == 0)[0][0]) a, b, c = face[on], face[(on+1) % 3], face[(on+2) % 3] - middle = hem_point(b, c, axis, value) + middle = hem_point(b, c, name, cut) split += [[a, b, middle], [a, middle, c]] continue lone = int([at for at in range(3) if list(side).count(side[at]) == 1][0]) a, b, c = face[lone], face[(lone+1) % 3], face[(lone+2) % 3] - ab, ac = hem_point(a, b, axis, value), hem_point(a, c, axis, value) + ab, ac = hem_point(a, b, name, cut), hem_point(a, c, name, cut) split += [[a, ab, ac], [ab, b, c], [ab, c, ac]] faces = split points = np.array(points) @@ -279,6 +356,89 @@ joints = np.array(skin_joints)[source] weights = np.array(skin_weights)[source] uvs = np.column_stack((positions[:, 0]*3+0.5, positions[:, 1]*3)) triangles = np.array(faces, dtype=np.int64) +# The head. MakeHuman's own eyeballs sit in the sockets; a hair shell rises +# from nothing at the hairline to a centimetre over the crown; a low bun sits +# at the back. All follow the head bone alone. Beyond the four washes the +# regions are 4 iris, 5 eye white, 6 lips and 7 brows. +def outward_faces(vertices, faces, center): + faces = np.array(faces, dtype=np.int64) + normal = np.cross(vertices[faces[:, 1]]-vertices[faces[:, 0]], vertices[faces[:, 2]]-vertices[faces[:, 0]]) + flip = np.einsum('ij,ij->i', normal, vertices[faces].mean(axis=1)-center) < 0 + faces[flip] = faces[flip][:, ::-1] + return faces + +def vertex_normals(vertices, faces): + result = np.zeros_like(vertices) + face = np.cross(vertices[faces[:, 1]]-vertices[faces[:, 0]], vertices[faces[:, 2]]-vertices[faces[:, 0]]) + for corner in range(3): + np.add.at(result, faces[:, corner], face) + return result/np.maximum(np.linalg.norm(result, axis=1)[:, None], 1e-12) + +extras = [] +hair_faces = triangles[np.all(regions[triangles] == 3, axis=1)] +used = np.unique(hair_faces) +depth = hairline(positions[used]) +crown = np.clip((positions[used, 1]-eye_height-0.02)/0.08, 0, 1) +sides = np.clip(np.abs(positions[used, 0])/0.07, 0, 1) +thickness = (0.008+0.006*crown+0.005*sides)*np.clip(depth/0.025, 0, 1)**0.6 +shell = positions[used]+normals[used]*thickness[:, None] +remap = np.full(len(positions), -1, dtype=np.int64) +remap[used] = np.arange(len(used)) +shell_faces = remap[hair_faces] +extras.append((shell, vertex_normals(shell, shell_faces), np.full(len(shell), 3), shell_faces)) +rings, segments = 10, 18 +bun_center = np.array([0.0, eye_height+0.078, -0.056]) +bun_radii = np.array([0.040, 0.036, 0.033]) +units = np.array([[np.sin(np.pi*ring/rings)*np.cos(2*np.pi*at/segments), np.cos(np.pi*ring/rings), + np.sin(np.pi*ring/rings)*np.sin(2*np.pi*at/segments)] + for ring in range(rings+1) for at in range(segments)]) +bun = bun_center+units*bun_radii +bun_faces = [] +for ring in range(rings): + for at in range(segments): + a, b = ring*segments+at, ring*segments+(at+1) % segments + c, d = a+segments, b+segments + if ring > 0: + bun_faces.append([a, b, d]) + if ring < rings-1: + bun_faces.append([a, d, c]) +bun_faces = outward_faces(bun, bun_faces, bun_center) +extras.append((bun, vertex_normals(bun, bun_faces), np.full(len(bun), 3), bun_faces)) +for prefix in ('l', 'r'): + indices = sorted({index for face in groups[f'helper-{prefix}-eye'] for index in face}) + eye_lookup = {old: at for at, old in enumerate(indices)} + eye = (all_vertices[indices]-origin)*scale + center = eye.mean(axis=0) + eye_faces = outward_faces(eye, [[eye_lookup[face[0]], eye_lookup[face[at]], eye_lookup[face[at+1]]] + for face in groups[f'helper-{prefix}-eye'] for at in range(1, len(face)-1)], center) + gaze = joint(f'{prefix}-eye-target')-joint(f'{prefix}-eye') + gaze /= np.linalg.norm(gaze) + facing = (eye-center)/np.linalg.norm(eye-center, axis=1)[:, None] @ gaze + extras.append((eye, vertex_normals(eye, eye_faces), np.where(facing > np.cos(np.radians(32)), 4, 5), eye_faces)) +# Brows arch over the eyes and the lips fill the mouth; both are painted on +# the skin's own vertices, so their edges stay soft. +x, y, z = positions[:, 0], positions[:, 1], positions[:, 2] +for prefix in ('l', 'r'): + eye = joint(prefix+'-eye') + across = (x-eye[0])/0.023 + middle = eye[1]+0.017+0.004*(1-across**2)-0.002*np.sign(eye[0])*across + brow = (np.abs(across) < 1) & (np.abs(y-middle) < 0.0024*(1.25-0.6*np.abs(across))) & (z > eye[2]-0.005) + regions[brow & (regions == 0)] = 7 +mouth = joint('mouth') +# MakeHuman's mouth joint is the jaw's pivot, level with the nose tip. +lip_height = mouth[1]-0.027 +lips = ((x/0.021)**2+((y-lip_height)/0.0088)**2 < 1) & (z > 0.145) +regions[lips & (regions == 0)] = 6 +head_index = [at for at, item in enumerate(bones) if item['role'] == 3][0] +for vertices, vertex_normal, vertex_region, faces_of in extras: + base = len(positions) + positions = np.vstack([positions, vertices]) + normals = np.vstack([normals, vertex_normal]) + joints = np.vstack([joints, np.tile(np.array([head_index, 0, 0, 0], dtype=np.uint8), (len(vertices), 1))]) + weights = np.vstack([weights, np.tile(np.array([1.0, 0.0, 0.0, 0.0]), (len(vertices), 1))]) + regions = np.concatenate([regions, np.asarray(vertex_region, dtype=np.uint8)]) + triangles = np.vstack([triangles, np.asarray(faces_of, dtype=np.int64)+base]) +uvs = np.column_stack((positions[:, 0]*3+0.5, positions[:, 1]*3)) quantized = np.rint(weights*65535).astype(np.int64) quantized[:, 0] += 65535-quantized.sum(axis=1) assert np.all(quantized >= 0) and np.all(quantized <= 65535)