diff --git a/design/figure-preview/README.md b/design/figure-preview/README.md index d38714dfd..aa7461878 100644 --- a/design/figure-preview/README.md +++ b/design/figure-preview/README.md @@ -6,14 +6,14 @@ 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. -Each practice plays on its own painted backdrop: a misty mountain garden -for Tai Chi, a sunrise meadow for Sun Salutation, a light room for the -Workout and the dim bedroom for astral projection. 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 on paper, and can show -Breeze's current flat figure for the same moment. It also tunes the wet -bleed, the dried edges, the ink line and the granulation. Drag over her to -turn around her; scroll to zoom. +Each practice plays on its own backdrop in Inner Breeze's illustration +style: a lake terrace for Tai Chi, a sunrise hill for Sun Salutation, a +woodland deck for the Workout and a moonlit veranda for astral projection. +The panel picks the practice and item, plays, pauses, scrubs and slows the +clock. It switches the finish off to show the bare surface on paper, and +can show Breeze's current flat figure for the same moment. It also tunes the +seam softening, the dried edges, the line, the texture and the colour +strength. 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 portable shaders, and the practices commit that has the figure, @@ -48,10 +48,18 @@ 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. +## Profiling + +The window prints a timing report to its standard output (the launcher's +journal) every 240 painted frames: median and 95th percentile milliseconds +for the whole frame and for each step (the practice's pose solve, the +figure's pose, skinning, cloth, render and readback, finish and ground +shadow, the backdrop, compositing, the astral double, the texture upload +and the panel's controls). `./build/app/figure_preview --profile PRACTICE` +(`tai-chi`, `sun-salutation`, `workout` or `astral`) paints 240 frames +without a window and prints the same report; add `still` to hold one pose. + ## 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 index a29d28787..7298a1f70 100644 --- a/design/figure-preview/figure_preview.zi +++ b/design/figure-preview/figure_preview.zi @@ -15,6 +15,7 @@ #import "preview_practices" #import "preview_capture" #import "preview_flat" +#import "preview_profile" #import "kryon/scene" #import "kryon/session" #import "kryon/geometry" @@ -69,7 +70,7 @@ PreviewState :: struct { } preview_state: PreviewState = .{playing = true, speed = 1.0, watercolor = true, zoom = 1.0, - bleed = 0.6, edge = 0.6, contour = 0.75, granulation = 0.55, colour = 0.6}; + bleed = 0.15, edge = 0.25, contour = 0.6, granulation = 0.25, colour = 0.6}; preview_media: FigureMedia; preview_backdrops: PreviewBackdrops; preview_library: PreviewLibrary; @@ -90,6 +91,8 @@ preview_page: s32; preview_time_text: [2][32]u8; preview_status_text: [2][96]u8; preview_frame_ms: float64; +preview_last_draw: s64; +preview_painted: bool; preview_practices_path: [1024]u8; preview_practices_length: s64; @@ -107,6 +110,19 @@ main :: (argc: s32, argv: **u8) -> s32 { return 1 } preview_state.item = PreviewDefaultItem(*preview_library, preview_state.practice) + if argc >= 3 && TextFromCString(argv[1]) == "--profile" { + practice: s32 = 0 + names: [4]string = .["tai-chi", "sun-salutation", "workout", "astral"] + for index: cast(s32)0..3 { + if TextFromCString(argv[2]) == names[index] { + practice = index + } + } + // A third argument "still" holds one pose, to tell the renderer's own + // cost from rebuilding the moving surface. + still := argc >= 4 && TextFromCString(argv[3]) == "still" + return PreviewProfileRun(*preview_media, *preview_backdrops, *preview_library, practice, 240, 600, 760, still) + } if argc >= 3 && TextFromCString(argv[1]) == "--capture" { return PreviewCapture(*preview_media, *preview_backdrops, *preview_library, TextFromCString(argv[2])) } @@ -160,7 +176,14 @@ PreviewDraw :: (session: Session, viewport: Rectangle) -> s32 { state.zoom = FigureClamp(state.zoom * Pow(0.88, cast(float64)input.wheel), 0.25, 3.0) } + drawn := PreviewNow() + if preview_last_draw != 0 { + PreviewRecord(PREVIEW_PHASE_FRAME, cast(float64)(drawn - preview_last_draw) / 1000000.0) + } + preview_last_draw = drawn + preview_painted = false PreviewPaintFigure(session, area) + controls := PreviewNow() x := panel.x + 20.0 width := panel.width - 40.0 @@ -261,6 +284,10 @@ PreviewDraw :: (session: Session, viewport: Rectangle) -> s32 { 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}) } + if preview_painted { + PreviewRecord(PREVIEW_PHASE_CONTROLS, PreviewLap(*controls)) + PreviewProfileFrame("live window") + } return 0 } @@ -286,7 +313,9 @@ PreviewPaintFigure :: (session: Session, area: Rectangle) { } key := PreviewKey(width, height) if key != preview_painted_key || preview_textures[preview_shown].id == 0 { + clock := PreviewNow() moment := PreviewSample(*preview_library, state.practice, state.item, state.seconds) + PreviewRecord(PREVIEW_PHASE_SOLVE, PreviewLap(*clock)) view := PreviewViewOf(*moment, state.turn, state.lift, state.zoom) if state.current { camera := PreviewFlatOf(*moment, view) @@ -295,6 +324,7 @@ PreviewPaintFigure :: (session: Session, area: Rectangle) { preview_failed = !PreviewCompose(*preview_body, *preview_twin, *preview_media, *preview_backdrops, state.practice, *moment, view, PreviewStyle(), width, height, *preview_picture) } + unused PreviewLap(*clock) if !preview_failed { next := 1 - preview_shown texture := preview_textures[next] @@ -314,6 +344,8 @@ PreviewPaintFigure :: (session: Session, area: Rectangle) { preview_shown = next } preview_painted_key = key + PreviewRecord(PREVIEW_PHASE_UPLOAD, PreviewLap(*clock)) + preview_painted = true } } shown := preview_textures[preview_shown] diff --git a/design/figure-preview/preview_capture.zi b/design/figure-preview/preview_capture.zi index 9f77aec18..176eb4f26 100644 --- a/design/figure-preview/preview_capture.zi +++ b/design/figure-preview/preview_capture.zi @@ -7,6 +7,7 @@ #import "figure_wash" #import "preview_practices" #import "preview_flat" +#import "preview_profile" #import "kryon/scene" #import "sun_salutation_rules" #import "workout_rules" @@ -16,9 +17,12 @@ #import "std/vec" Raster :: #import "std/image"; -// Each practice's painted backdrop. +// Each practice's painted backdrop, and the last one scaled to a picture. PreviewBackdrops :: struct { images: [4]Raster.Image + covered: Raster.Image + covered_practice: s32 + covered_vivid: float64 } PreviewBackdropsLoad :: (backdrops: *PreviewBackdrops, practices: string) -> bool { @@ -47,6 +51,7 @@ PreviewBackdropsRelease :: (backdrops: *PreviewBackdrops) { for practice: 0..3 { VecFree(backdrops.images[practice].pixels) } + VecFree(backdrops.covered.pixels) } // The body, and an astral twin over it, into OUT: on the practice's @@ -58,36 +63,99 @@ PreviewCompose :: (body: *FigurePainter, twin: *FigurePainter, media: *FigureMed if !FigurePaint(body, media, *moment.body, view, style, still, width, height) { return false } + times := body.times + PreviewRecord(PREVIEW_PHASE_POSE, times.pose) + PreviewRecord(PREVIEW_PHASE_SKIN, times.skin) + PreviewRecord(PREVIEW_PHASE_CLOTH, times.cloth) + PreviewRecord(PREVIEW_PHASE_RENDER, times.render) + PreviewRecord(PREVIEW_PHASE_FINISH, times.finish) + PreviewRecord(PREVIEW_PHASE_SHADOW, times.shadow) + clock := PreviewNow() + // The backdrop is scaled once for each size, practice and colour + // strength, then the figure is laid over it. backdrop := *backdrops.images[practice] - if style.watercolor && Raster.ImageValid(backdrop) { - // The lying astral body needs the bedroom floor raised to her. - zoom := ifx practice == PREVIEW_ASTRAL then 1.5 else 1.0 - focus := ifx practice == PREVIEW_ASTRAL then 0.85 else 0.62 - if !FigureBackdropCover(out, backdrop, width, height, zoom, focus, FigureVivid(style.wash)) { - return false - } - } else { - if !VecResize(out.pixels, cast(s64)width * height * 4) { - return false - } - out.width = width - out.height = height - paper: [4]u8 = .[243, 237, 225, 255] - for index: 0..out.pixels.count - 1 { - out.pixels[index] = paper[index % 4] + covered := *backdrops.covered + painted := style.watercolor && Raster.ImageValid(backdrop) + vivid := ifx painted then FigureVivid(style.wash) else -1.0 + if covered.width != width || covered.height != height || backdrops.covered_practice != practice || + backdrops.covered_vivid != vivid { + if painted { + // The lying astral body needs the floor raised to her. + zoom := ifx practice == PREVIEW_ASTRAL then 1.7 else 1.0 + focus := ifx practice == PREVIEW_ASTRAL then 1.0 else 0.62 + if !FigureBackdropCover(covered, backdrop, width, height, zoom, focus, vivid) { + return false + } + } else { + if !VecResize(covered.pixels, cast(s64)width * height * 4) { + return false + } + covered.width = width + covered.height = height + paper: [4]u8 = .[243, 237, 225, 255] + for index: 0..covered.pixels.count - 1 { + covered.pixels[index] = paper[index % 4] + } } + backdrops.covered_practice = practice + backdrops.covered_vivid = vivid } - if !FigureOver(out, *body.pixels, 1.0) { + PreviewRecord(PREVIEW_PHASE_BACKDROP, PreviewLap(*clock)) + if !FigureOverOnto(out, covered, *body.pixels) { return false } + PreviewRecord(PREVIEW_PHASE_OVER, PreviewLap(*clock)) + PreviewRecord(PREVIEW_PHASE_TWIN, 0.0) if !moment.twin_visible { return true } glow := style glow.luminous = true glow.opacity = 0.58 - return FigurePaint(twin, media, *moment.twin, view, glow, moment.twin_effect, width, height) && + doubled := FigurePaint(twin, media, *moment.twin, view, glow, moment.twin_effect, width, height) && FigureOver(out, *twin.pixels, 1.0) + PreviewRecord(PREVIEW_PHASE_TWIN, PreviewLap(*clock)) + return doubled +} + +// Paints PRACTICE's default item for FRAMES frames a thirtieth of a second +// apart (or all at its first moment, STILL) at WIDTH by HEIGHT, without a +// window, and prints the timing. +PreviewProfileRun :: (media: *FigureMedia, backdrops: *PreviewBackdrops, library: *PreviewLibrary, + practice: s32, frames: s32, width: s32, height: s32, still: bool) -> s32 { + body: FigurePainter + twin: FigurePainter + picture: Raster.Image + defer { + FigurePainterRelease(*body) + FigurePainterRelease(*twin) + VecFree(picture.pixels) + } + item := PreviewDefaultItem(library, practice) + duration := PreviewDuration(library, practice, item) + seconds: float64 + for frame: 0..frames - 1 { + start := PreviewNow() + clock := start + moment := PreviewSample(library, practice, item, seconds) + PreviewRecord(PREVIEW_PHASE_SOLVE, PreviewLap(*clock)) + view := PreviewViewOf(*moment, 0.0, 0.0, 1.0) + if !PreviewCompose(*body, *twin, media, backdrops, practice, *moment, view, FigureStyleDefault(), width, + height, *picture) { + print("figure profile: frame % could not be painted\n", frame) + return 1 + } + PreviewRecord(PREVIEW_PHASE_UPLOAD, 0.0) + PreviewRecord(PREVIEW_PHASE_CONTROLS, 0.0) + PreviewRecord(PREVIEW_PHASE_FRAME, PreviewLap(*start)) + PreviewProfileFrame("headless") + seconds += ifx still then 0.0 else 1.0 / 30.0 + if seconds >= duration { + seconds = 0.0 + } + } + print("figure profile: gpu=% %\n", body.accelerated, body.diagnostic) + return 0 } PreviewShot :: struct { diff --git a/design/figure-preview/preview_profile.zi b/design/figure-preview/preview_profile.zi new file mode 100644 index 000000000..228c204a1 --- /dev/null +++ b/design/figure-preview/preview_profile.zi @@ -0,0 +1,78 @@ +// Frame-time profiling for the figure preview: each phase of the last 240 +// frames, reported as median and 95th percentile milliseconds. The live +// window prints a report every 240 frames; `--profile` measures without one. +#import "std/math" +Timer :: #import "std/timer_linux"; + +PREVIEW_PHASE_FRAME :: 0 +PREVIEW_PHASE_SOLVE :: 1 +PREVIEW_PHASE_POSE :: 2 +PREVIEW_PHASE_SKIN :: 3 +PREVIEW_PHASE_CLOTH :: 4 +PREVIEW_PHASE_RENDER :: 5 +PREVIEW_PHASE_FINISH :: 6 +PREVIEW_PHASE_SHADOW :: 7 +PREVIEW_PHASE_BACKDROP :: 8 +PREVIEW_PHASE_OVER :: 9 +PREVIEW_PHASE_TWIN :: 10 +PREVIEW_PHASE_UPLOAD :: 11 +PREVIEW_PHASE_CONTROLS :: 12 +PREVIEW_PHASES :: 13 +PREVIEW_PROFILE_FRAMES :: 240 + +PreviewProfile :: struct { + samples: [PREVIEW_PHASES][PREVIEW_PROFILE_FRAMES]float64 + frames: s64 +} + +preview_profile: PreviewProfile; + +PreviewNow :: () -> s64 { + return Timer.MonotonicNanoseconds() +} + +// Milliseconds since CLOCK, which moves on to now. +PreviewLap :: (clock: *s64) -> float64 { + now := Timer.MonotonicNanoseconds() + spent := cast(float64)(now - clock.*) / 1000000.0 + clock.* = now + return spent +} + +PreviewRecord :: (phase: s32, milliseconds: float64) { + preview_profile.samples[phase][preview_profile.frames % PREVIEW_PROFILE_FRAMES] = milliseconds +} + +// Ends a frame; LABEL names the run, and a full window of frames prints. +PreviewProfileFrame :: (label: string) { + preview_profile.frames += 1 + if preview_profile.frames % PREVIEW_PROFILE_FRAMES == 0 { + PreviewProfileReport(label) + } +} + +PreviewProfileReport :: (label: string) { + count := ifx preview_profile.frames < PREVIEW_PROFILE_FRAMES then preview_profile.frames else PREVIEW_PROFILE_FRAMES + if count == 0 { + return + } + names: [PREVIEW_PHASES]string = .["frame", "solve", "pose", "skin", "cloth", "render+readback", "finish", + "shadow", "backdrop", "over", "twin", "upload", "controls"] + print("figure preview timing, %, % frames (median / p95 ms):", label, count) + for phase: 0..PREVIEW_PHASES - 1 { + sorted: [PREVIEW_PROFILE_FRAMES]float64 + for at: 0..count - 1 { + value := preview_profile.samples[phase][at] + spot := at + while spot > 0 && sorted[spot - 1] > value { + sorted[spot] = sorted[spot - 1] + spot -= 1 + } + sorted[spot] = value + } + median := sorted[count / 2] + tail := sorted[(count * 95) / 100] + print(" %=%/%", names[phase], cast(s64)(median * 10.0 + 0.5), cast(s64)(tail * 10.0 + 0.5)) + } + print(" (tenths of a ms)\n") +} diff --git a/design/figure-preview/ziran.lock b/design/figure-preview/ziran.lock index b0abc4911..e7a8ac217 100644 --- a/design/figure-preview/ziran.lock +++ b/design/figure-preview/ziran.lock @@ -19,15 +19,6 @@ "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"}, @@ -46,6 +37,15 @@ "source": true, "url": "https://github.com/webmproject/libwebp.git" }, + { + "commit": "8e44f3f86262924f513b28cc807e67b1138e5e0b", + "dependencies": {"audio": "p59deb49c7f103127", "avif": "pb203529d803e73a4", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"}, + "dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "2611e727e226a2af22dfb7f10bf224bd2f1a3d30", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"}, + "id": "p54dafccfa152a22e", + "name": "kryon", + "ref": "8e44f3f86262924f513b28cc807e67b1138e5e0b", + "url": "https://github.com/kryonlabs/kryon.git" + }, { "commit": "1aadfad932c98c069a1204261b1856f81f3bc199", "dependencies": {}, @@ -90,15 +90,6 @@ "source": true, "url": "https://aomedia.googlesource.com/aom" }, - { - "commit": "3e7c7bffd2e0021277977e7b15b4ced4683d53c7", - "dependencies": {"kryon": "p1e41122208040e0f"}, - "dependency_refs": {"kryon": "199b51c97c2aba67367308c3dc98126692b476a8"}, - "id": "p79c25fede49784ba", - "name": "practices", - "ref": "3e7c7bffd2e0021277977e7b15b4ced4683d53c7", - "url": "https://github.com/waozixyz/practices.git" - }, { "commit": "2611e727e226a2af22dfb7f10bf224bd2f1a3d30", "dependencies": {"av1": "p5d7d12505a42f7b3", "dav1d": "pb24767b9eb34d961", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db"}, @@ -117,15 +108,6 @@ "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": {}, @@ -134,16 +116,25 @@ "ref": "ddbe48383984d56acd9e1ab6a090c54ca6b735a6", "source": true, "url": "https://github.com/xiph/opus.git" + }, + { + "commit": "e0950d9379e93dbc3526130588bc6f4bffa170d2", + "dependencies": {"kryon": "p54dafccfa152a22e"}, + "dependency_refs": {"kryon": "8e44f3f86262924f513b28cc807e67b1138e5e0b"}, + "id": "peccdcd876aad0aba", + "name": "practices", + "ref": "e0950d9379e93dbc3526130588bc6f4bffa170d2", + "url": "https://github.com/waozixyz/practices.git" } ], "root": { - "dependencies": {"kryon": "p1e41122208040e0f", "practices": "p79c25fede49784ba"}, - "dependency_refs": {"kryon": "199b51c97c2aba67367308c3dc98126692b476a8", "practices": "3e7c7bffd2e0021277977e7b15b4ced4683d53c7"}, + "dependencies": {"kryon": "p54dafccfa152a22e", "practices": "peccdcd876aad0aba"}, + "dependency_refs": {"kryon": "8e44f3f86262924f513b28cc807e67b1138e5e0b", "practices": "e0950d9379e93dbc3526130588bc6f4bffa170d2"}, "name": "figure_preview" }, "toolchain": { - "commit": "2313b49cb005610853faeec46447fc38578a2a15", - "ref": "2313b49cb005610853faeec46447fc38578a2a15", + "commit": "7fbc8152b0ab18b641a73d16d93ccda1e1c7f863", + "ref": "7fbc8152b0ab18b641a73d16d93ccda1e1c7f863", "url": "https://github.com/ziranlang/ziran.git" } } diff --git a/design/figure-preview/ziran.toml b/design/figure-preview/ziran.toml index 6a02badb5..c2cc05598 100644 --- a/design/figure-preview/ziran.toml +++ b/design/figure-preview/ziran.toml @@ -6,13 +6,13 @@ links = ["SDL2", "pangocairo-1.0", "pango-1.0", "cairo", "fontconfig", "freetype [toolchain] git = "https://github.com/ziranlang/ziran.git" -ref = "2313b49cb005610853faeec46447fc38578a2a15" +ref = "7fbc8152b0ab18b641a73d16d93ccda1e1c7f863" [dependencies.kryon] git = "https://github.com/kryonlabs/kryon.git" -ref = "199b51c97c2aba67367308c3dc98126692b476a8" +ref = "8e44f3f86262924f513b28cc807e67b1138e5e0b" # The shared figure and its media (anatomy, textures, Tai Chi course). [dependencies.practices] git = "https://github.com/waozixyz/practices.git" -ref = "3e7c7bffd2e0021277977e7b15b4ced4683d53c7" +ref = "e0950d9379e93dbc3526130588bc6f4bffa170d2" diff --git a/plans/2026-11-17-astral-projection.md b/plans/2026-11-17-astral-projection.md index e42623ce0..05bb57d7e 100644 --- a/plans/2026-11-17-astral-projection.md +++ b/plans/2026-11-17-astral-projection.md @@ -2,37 +2,32 @@ The owner wants one perfected character used everywhere: Tai Chi, Sun Salutation, Workout and astral projection, native on Ziran and Kryon -without raylib, looking like a hand-painted watercolor illustration. The -character is the practices package's `figure/` (pinned practices 3e7c7bff: -slender female MakeHuman CC0 surface in loose cloth shells, dual-quaternion rig, -generated painted textures and face, three-wash shaders, painterly finish, -painted backdrops, Ziran tests). The style target is practices -`figure/design/reference/style-target.webp`. The live tuning tool is -`design/figure-preview/` (see its README). The three.js studies in -`design/astral-projection/three/` are look references only. +without raylib: a slim, cute young Chinese woman with sleek black hair, an +attractive Chinese-style practice outfit, matched to Inner Breeze's own +storybook illustrations with only a little texture, and vivid colour. The +character is the practices package's `figure/`; its style target is +practices `figure/design/reference/style-target.webp`, made from the Tai Chi +banner. The live tuning tool is `design/figure-preview/` (see its README). +The three.js studies in `design/astral-projection/three/` are look +references only. -- Kryon fixes, owned by the Kryon fix task, then repin the preview and the - figure's tests: the desktop loop stops showing frames that arrive without - input (hands-off playback freezes until the pointer moves); images - compare by texture id only, so `BitmapImageUpdate` never repaints (the - preview alternates two textures); `SceneIndexedMesh` stores scene-wide - indices that the renderers offset again (the figure keeps one surface as - the scene's first mesh); a sampler texture uploaded during a frame takes - another sampler's unit (the figure paints its first picture twice). - Remove the workarounds after the repin. +- Kryon, owned by the Kryon lanes: the first-frame sampler-unit clobber in + `scene_gpu.zi` (the figure paints its first picture twice) and the + per-frame geometry rebuild (every moving frame re-expands all triangles + into a new vertex buffer and re-uploads it, about 5–6 ms on llvmpipe); + then repin and remove the workaround. Images still compare by texture id + only, so 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: - - Seen from the front at practice sizes the hair still reads as a close - cap; the style target's loose volume and stray strands need cards or - painted strands that do not read as sideburns. - - The skirt swings rigidly from the waist and only by the pelvis's tilt: - in a deep squat and a forward fold it bulges. Secondary sway and a - simple collision with the thighs would help. - - The bell sleeves do not hang below the arm as in the style target. + - The top's lower part swings rigidly from the waist and only by the + pelvis's tilt; secondary sway and a simple collision with the thighs + would help in deep squats and folds. - The face painting is projected from the front; in profile its side fades to plain skin. + - The cream piping at the top's hem and cuffs in the style target is not + painted yet. - The asset bake (cloth shells, UVs, texture swatches) is Python in `scripts/prepare-practice-human.py`; port it to Ziran with the anatomy reader. diff --git a/scripts/prepare-practice-human.py b/scripts/prepare-practice-human.py index 8e498843a..4d194e1b3 100644 --- a/scripts/prepare-practice-human.py +++ b/scripts/prepare-practice-human.py @@ -14,8 +14,8 @@ ROOT = Path(__file__).resolve().parents[1] # originals kept in OUTPUT/source (provenance.json there): each swatch is # moved to the style target's palette, made to tile without a seam and # sized for phones. -SWATCHES = {'skin': ('#efbe96', 0.45), 'tunic': ('#7aa46a', 0.9), 'trousers': ('#4e5090', 0.85), - 'hair': ('#7a4a2a', 1.0), 'sash': ('#ecd6a2', 0.6)} +SWATCHES = {'skin': ('#f3d2b8', 0.45), 'top': ('#3f9a7e', 0.9), 'trousers': ('#24345e', 0.85), + 'hair': ('#1e1e28', 1.0)} BACKDROPS = ('tai-chi', 'sun-salutation', 'workout', 'astral') def prepare_textures(folder): @@ -39,15 +39,24 @@ def prepare_textures(folder): Image.fromarray(np.clip(tiled, 0, 255).astype(np.uint8)).save(folder/f'{name}.webp', quality=88, method=6) # The face painting takes the skin swatch's colour on its cheeks and # brow, keeping its own eyes, brows, lips and blush. + # The trim keeps its own colours: jade silk with cream frog knots. + Image.open(source/'trim.webp').convert('RGB').resize((256, 256), Image.LANCZOS).save( + folder/'trim.webp', quality=90, method=6) face = np.asarray(Image.open(source/'face.webp').convert('RGB').resize((512, 512), Image.LANCZOS), dtype=np.float64) - skin = face[118:155, 230:285].reshape(-1, 3).mean(axis=0) + skin = face[135:170, 228:286].reshape(-1, 3).mean(axis=0) goal = np.array([int(SWATCHES['skin'][0][at:at+2], 16) for at in (1, 3, 5)], dtype=np.float64) face = face*goal/skin + # The paper beside her small chin becomes skin too, where MakeHuman's + # wider jaw reaches past the painted one; her hair, eyes and lips stay. + light = face @ np.array([0.30, 0.59, 0.11]) + warmth = face[:, :, 0]-face[:, :, 2] + paper = np.clip((light-205)/20, 0, 1)*np.clip((48-warmth)/14, 0, 1) + face = face*(1-paper[:, :, None])+goal*paper[:, :, None] # Outside the painted face (its paper, hair and ears) the painting # becomes plain skin, so wherever the mesh's face reaches past it the # skin goes on. y, x = np.mgrid[0:512, 0:512] - oval = np.sqrt(((x-256)/150)**2+((y-262)/190)**2) + oval = np.sqrt(((x-257)/140)**2+((y-250)/175)**2) keep = np.clip((1.0-oval)/0.18, 0, 1)[:, :, None] face = face*keep+goal*(1-keep) Image.fromarray(np.clip(face, 0, 255).astype(np.uint8)).save(folder/'face.webp', quality=90, method=6) @@ -80,11 +89,10 @@ 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. -# She is slender: MakeHuman's weight slider at 0.25, muscle at 0.35, height -# at 0.85 and ideal proportions, mixed as MakeHuman mixes its macro targets +# MakeHuman's base mesh is its gender-neutral average. She is a slim young +# Chinese woman: MakeHuman's own CC0 young-adult female macro shape, mostly +# its Asian target, with the weight slider at 0.15, muscle at 0.30, height +# at 0.65 and ideal proportions, mixed as MakeHuman mixes its macro targets # (each weight-and-muscle pair by the product of their shares). def apply_target(name, share): target = ROOT / f'design/astral-projection/three/assets/makehuman-{name}.target.gz' @@ -93,9 +101,9 @@ def apply_target(name, share): index, dx, dy, dz = line.split() all_vertices[int(index)] += np.array([float(dx), float(dy), float(dz)])*share -for race in ('african', 'asian', 'caucasian'): - apply_target(f'{race}-female-young', 1/3) -BODY_WEIGHT, BODY_MUSCLE, BODY_HEIGHT, BODY_PROPORTIONS = 0.25, 0.35, 0.65, 1.0 +for race, share in (('asian', 0.9), ('caucasian', 0.1)): + apply_target(f'{race}-female-young', share) +BODY_WEIGHT, BODY_MUSCLE, BODY_HEIGHT, BODY_PROPORTIONS = 0.15, 0.30, 0.65, 1.0 for muscle, muscle_share in (('minmuscle', 1-BODY_MUSCLE/0.5), ('averagemuscle', BODY_MUSCLE/0.5)): for weight, weight_share in (('minweight', 1-BODY_WEIGHT/0.5), ('averageweight', BODY_WEIGHT/0.5)): pair = muscle_share*weight_share @@ -363,19 +371,19 @@ def hairline(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.012, 0.002, -0.022, -0.045]) + [0.056, 0.055, 0.047, 0.030, 0.010, 0.000, -0.022, -0.045]) 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 neckline(points): - """Positive below the wrap top's collar: level over the shoulders, then - a V down the breastbone in front.""" + """Positive below the mandarin top's round neckline, a little lower in + front, where its stand-up collar begins.""" points = np.atleast_2d(points) x, y, z = points[:, 0], points[:, 1], points[:, 2] - v = 0.075*np.clip(1-np.abs(x)/0.085, 0, 1)*np.clip((z-0.02)/0.04, 0, 1) - return (neck_height-0.04-v)-y + front = 0.012*np.clip(1-np.abs(x)/0.07, 0, 1)*np.clip((z-0.02)/0.04, 0, 1) + return (neck_height-0.008-front)-y def region_of(points): points = np.atleast_2d(points) @@ -493,23 +501,23 @@ extras = [] hair_faces = triangles[np.all(regions[triangles] == 3, axis=1)] used = np.unique(hair_faces) depth = hairline(positions[used]) -# Neat swept-back hair: close over the ears and at the hairline, fuller -# towards the back of the crown where it gathers into the bun. +# Sleek black hair combed back close to the head, a little fuller +# towards the crown where it gathers into the bun. top = np.clip((positions[used, 1]-eye_height-0.03)/0.07, 0, 1) back = np.clip((joint('head')[2]+0.01-positions[used, 2])/0.08, 0, 1) sides = np.clip(np.abs(positions[used, 0])/0.07, 0, 1) above = np.clip((positions[used, 1]-eye_height)/0.03, 0, 1) -thickness = (0.005+0.008*top+0.010*top*back+0.008*sides*above)*np.clip(depth/0.04, 0, 1)**0.9 +thickness = (0.004+0.007*top+0.006*top*back+0.004*sides*above)*np.clip(depth/0.03, 0, 1)**0.8 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 -# A low, round bun at the back of the head. -nape = positions[used][np.abs(positions[used, 1]-(eye_height+0.045)) < 0.01] -bun_center = np.array([0.0, eye_height+0.045, nape[:, 2].min()-0.016]) -bun_radii = np.array([0.046, 0.040, 0.036]) +# A neat high bun on the back of the crown. +crown_ring = positions[used][np.abs(positions[used, 1]-(eye_height+0.095)) < 0.008] +bun_center = np.array([0.0, eye_height+0.095, crown_ring[:, 2].min()+0.004]) +bun_radii = np.array([0.040, 0.034, 0.038]) 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)]) @@ -535,6 +543,7 @@ for prefix in ('l', 'r'): extras.append((eye, vertex_normals(eye, eye_faces), np.zeros(len(eye)), eye_faces)) head_index = [at for at, item in enumerate(bones) if item['role'] == 3][0] body_count = len(positions) +crown = positions[:, 1].max() for vertices, vertex_normal, vertex_region, faces_of in extras: base = len(positions) positions = np.vstack([positions, vertices]) @@ -544,11 +553,12 @@ for vertices, vertex_normal, vertex_region, faces_of in extras: regions = np.concatenate([regions, np.asarray(vertex_region, dtype=np.uint8)]) triangles = np.vstack([triangles, np.asarray(faces_of, dtype=np.int64)+base]) -# Loose clothes: shells over the body, skinned by the same rig. The wrap -# top hangs from the bust and the shoulder blades, its skirt hangs over the -# hips and both thighs, a sash with two tails covers the seam at the waist, -# the sleeves open into bells and the trousers balloon to a gathered hem. -# Regions: 0 skin, 1 tunic, 2 trousers, 3 hair, 4 sash. +# Her Tai Chi silk suit: shells over the body, skinned by the same rig. The +# mandarin top is fitted over the chest and falls straight from the bust and +# shoulder blades to below the hips, slit at the sides, with a stand-up +# collar and a placket of frog-knot buttons; the long sleeves and the +# trousers fall a little wider towards the wrist and ankle. +# Regions: 0 skin, 1 top, 2 trousers, 3 hair, 4 trim (the placket's frog buttons). role_of = np.array([item['role'] for item in bones]) def share(rows, shares, roles): @@ -562,7 +572,7 @@ arm = share(joints, weights, [4, 5, 6, 7]) trunk_points = (np.arange(len(positions)) < body_count) & (arm < 0.15) hips_joint = (joint('l-upper-leg')+joint('r-upper-leg'))/2 armpit_y = (joint('l-shoulder')[1]+joint('r-shoulder')[1])/2-0.09 -hem_y = hips_joint[1]-0.15 +hem_y = hips_joint[1]-0.05 def slab(height, mask=trunk_points, half=0.012): return positions[mask & (np.abs(positions[:, 1]-height) < half)] @@ -624,11 +634,14 @@ dense_body = np.zeros((body_count, len(bones))) for slot in range(4): np.add.at(dense_body, (np.arange(body_count), joints[:body_count, slot]), weights[:body_count, slot]) -def nearest_weights(points, near=8): +def nearest_weights(points, near=8, low=None, high=None): """Skin weights for cloth away from the surface: the weights of the - nearest trunk and leg vertices, closer ones counting more.""" - candidates = np.where(trunk_points[:body_count] & (positions[:body_count, 1] > hem_y-0.12) & - (positions[:body_count, 1] < waist_y+0.15))[0] + nearest trunk and leg vertices between LOW and HIGH, closer ones counting + more.""" + low = hem_y-0.12 if low is None else low + high = waist_y+0.15 if high is None else high + candidates = np.where(trunk_points[:body_count] & (positions[:body_count, 1] > low) & + (positions[:body_count, 1] < high))[0] result = np.zeros((len(points), len(bones))) for at, point in enumerate(points): distance = np.linalg.norm(positions[candidates]-point, axis=1) @@ -663,18 +676,18 @@ def facing_out(points, faces, centre_of): def trunk_axis(points): return np.column_stack((np.zeros(len(points)), points[:, 1], trunk_z(points[:, 1]))) -cloth = [] # (points, normals, joints, weights, region, faces, source positions or None, hang) +cloth = [] # (points, normals, joints, weights, region, faces, source positions or None, hang, UVs or None) -# The top: the body's own tunic surface lifted off it, above the waist and -# along the sleeves. +# The mandarin top: the body's own surface lifted a little off it, fitted +# over the chest and along the long sleeves. top_faces = triangles[np.all(regions[triangles] == 1, axis=1) & np.all(triangles < body_count, axis=1)] top_faces = top_faces[(positions[top_faces][:, :, 1].min(axis=1) > waist_y-0.03) | (arm[top_faces].mean(axis=1) > 0.5)] used = np.unique(top_faces) p, n = positions[used], normals[used] bust_y = slab((waist_y+armpit_y)/2, half=0.06)[:, 1][np.argmax(slab((waist_y+armpit_y)/2, half=0.06)[:, 2])] sleeve = smooth((arm[used]-0.2)/0.6) -lift = np.interp(p[:, 1], [waist_y, bust_y, neck_height-0.04], [0.016, 0.010, 0.005])*(1-sleeve) -lift += (0.008+0.040*arm_progress(p)**1.6)*sleeve +lift = np.interp(p[:, 1], [waist_y, bust_y, neck_height-0.04], [0.010, 0.007, 0.004])*(1-sleeve) +lift += (0.006+0.014*arm_progress(p)**1.5)*sleeve q = p+n*lift[:, None] rel = np.column_stack((q[:, 0], q[:, 2]-trunk_z(p[:, 1]))) theta = np.arctan2(rel[:, 0], rel[:, 1]) @@ -685,65 +698,71 @@ q[:, 2] += np.cos(theta)*push remap = np.full(len(positions), -1, dtype=np.int64) remap[used] = np.arange(len(used)) faces_of = remap[top_faces] -cloth.append((q, vertex_normals(q, faces_of), joints[used], weights[used], 1, faces_of, p, np.zeros(len(q)))) +top_shell = q +cloth.append((q, vertex_normals(q, faces_of), joints[used], weights[used], 1, faces_of, p, np.zeros(len(q)), None)) -# The skirt of the top, from just above the waist to the upper thigh, with -# a gently uneven hem. -rows_y = np.linspace(waist_y+0.035, hem_y, 16) +# The top's hip-length lower part, from just above the waist to below the +# hip bones, slit at both sides. +rows_y = np.linspace(waist_y+0.035, hem_y, 12) fall = (rows_y[0]-rows_y)/(rows_y[0]-rows_y[-1]) skirt = [] for y, f in zip(rows_y, fall): - hem = 0.012*np.sin(3*angles+0.7)*f**4 - r = hanging_radius(angles, np.full(K, y))+0.016+0.028*f**1.5 - skirt.append(np.column_stack((np.sin(angles)*r, np.full(K, y)+hem, trunk_z(y)+np.cos(angles)*r))) + r = hanging_radius(angles, np.full(K, y))+0.010+0.012*f**1.5 + skirt.append(np.column_stack((np.sin(angles)*r, np.full(K, y), trunk_z(y)+np.cos(angles)*r))) skirt = np.vstack(skirt) -faces_of = facing_out(skirt, grid_faces(len(rows_y), K, True), trunk_axis) +faces_of = grid_faces(len(rows_y), K, True) +side = np.abs(np.sin(angles[faces_of[:, 0] % K]+np.pi/K)) > 0.97 +faces_of = facing_out(skirt, faces_of[~(side & (faces_of[:, 0]//K >= len(rows_y)//2))], trunk_axis) dense = nearest_weights(skirt).reshape(len(rows_y), K, len(bones)) for _ in range(2): dense = 0.5*dense+0.25*np.roll(dense, 1, axis=1)+0.25*np.roll(dense, -1, axis=1) -# The hand-over from the pelvis to the thighs spreads down the whole skirt, -# so a lifted leg draws the hem out gradually instead of tearing a band. +# The hand-over from the pelvis to the thighs spreads down the whole hem, +# so a lifted leg draws it out gradually instead of tearing a band. for _ in range(12): dense[1:-1] = 0.5*dense[1:-1]+0.25*dense[:-2]+0.25*dense[2:] dense[-1] = 0.5*dense[-1]+0.5*dense[-2] rows, shares = strongest(dense.reshape(-1, len(bones))) -cloth.append((skirt, vertex_normals(skirt, faces_of), rows, shares, 1, faces_of, None, np.repeat(fall, K))) +cloth.append((skirt, vertex_normals(skirt, faces_of), rows, shares, 1, faces_of, None, np.repeat(fall, K), None)) -# The sash round the waist and its two tails hanging in front. -band_y = waist_y+np.array([0.035, 0.012, -0.012, -0.035]) -band = np.vstack([np.column_stack((np.sin(angles)*r, np.full(K, y), trunk_z(y)+np.cos(angles)*r)) - for y, r in ((y, hanging_radius(angles, np.full(K, y))+0.016+bulge) - for y, bulge in zip(band_y, (0.005, 0.010, 0.010, 0.005)))]) -faces_of = facing_out(band, grid_faces(len(band_y), K, True), trunk_axis) -rows, shares = strongest(nearest_weights(band)) -cloth.append((band, vertex_normals(band, faces_of), rows, shares, 4, faces_of, None, np.zeros(len(band)))) -for tail, (turn, length) in enumerate(((0.30, 0.30), (0.46, 0.25))): - strip = [] - for step in np.linspace(0, 1, 9): - y = waist_y-0.02-step*length - f = np.clip((rows_y[0]-y)/(rows_y[0]-rows_y[-1]), 0, 1) - for side in (-1, 1): - angle = turn+0.12*step+side*0.022/0.15 - r = hanging_radius(np.array([angle]), np.array([y]))[0]+0.016+0.028*f**1.5+0.008+0.004*tail - strip.append([np.sin(angle)*r, y, trunk_z(y)+np.cos(angle)*r]) - strip = np.array(strip) - faces_of = facing_out(strip, grid_faces(9, 2, False), trunk_axis) - rows, shares = strongest(nearest_weights(strip)) - cloth.append((strip, vertex_normals(strip, faces_of), rows, shares, 4, faces_of, None, - np.repeat(0.25+0.75*np.linspace(0, 1, 9), 2))) +# The stand-up mandarin collar round the base of her neck, leaning in a +# little at its top. +collar_y = neck_height+np.array([-0.012, 0.006, 0.022]) +collar_angles = np.arange(48)*2*np.pi/48 +collar = [] +for y, give in zip(collar_y, (0.005, 0.004, 0.002)): + s = positions[:body_count][(arm[:body_count] < 0.15) & (np.abs(positions[:body_count, 1]-y) < 0.006)] + middle = (s[:, 2].min()+s[:, 2].max())/2 + reach = (np.column_stack((s[:, 0], s[:, 2]-middle)) @ np.column_stack((np.sin(collar_angles), np.cos(collar_angles))).T).max(axis=0) + collar.append(np.column_stack((np.sin(collar_angles)*(reach+give), np.full(48, y), middle+np.cos(collar_angles)*(reach+give)))) +collar = np.vstack(collar) +faces_of = facing_out(collar, grid_faces(3, 48, True), lambda m: np.column_stack((np.zeros(len(m)), m[:, 1], np.full(len(m), collar[:, 2].mean())))) +rows, shares = strongest(nearest_weights(collar, low=neck_height-0.05, high=neck_height+0.05)) +cloth.append((collar, vertex_normals(collar, faces_of), rows, shares, 1, faces_of, None, np.zeros(len(collar)), None)) -# The trousers: the legs' surface lifted off them, ballooning below the -# knee and gathered again at the ankle. +# The placket down her front with its frog-knot buttons, just over the top. +front = np.vstack([top_shell[np.abs(top_shell[:, 0]) < 0.01], skirt[np.abs(skirt[:, 0]) < 0.01]]) +front = front[np.argsort(front[:, 1])] +placket_y = np.arange(neck_height-0.012, hem_y+0.004, -0.01) +reach = np.array([front[np.abs(front[:, 1]-y) < 0.012][:, 2].max() for y in placket_y]) +placket = np.vstack([[[side*0.015, y, z+0.0025] for side in (-1, 1)] for y, z in zip(placket_y, reach)]) +faces_of = facing_out(placket, grid_faces(len(placket_y), 2, False), trunk_axis) +rows, shares = strongest(nearest_weights(placket, low=hem_y-0.12, high=neck_height+0.02)) +placket_hang = np.repeat(np.clip((rows_y[0]-placket_y)/(rows_y[0]-rows_y[-1]), 0, 1), 2) +placket_uv = np.column_stack((np.tile([0.0, 1.0], len(placket_y)), np.repeat((placket_y[0]-placket_y)/0.045, 2))) +cloth.append((placket, vertex_normals(placket, faces_of), rows, shares, 4, faces_of, None, placket_hang, placket_uv)) + +# The trousers: the legs' surface lifted off them, falling wide in soft +# silk and gathered again at the ankle. leg_faces = triangles[np.all(regions[triangles] == 2, axis=1) & np.all(triangles < body_count, axis=1)] used = np.unique(leg_faces) p, n = positions[used], normals[used] down = np.clip((hip_height-0.04-p[:, 1])/(hip_height-0.04-np.mean(ankles)), 0, 1) -lift = 0.010+0.030*smooth(down/0.7)-0.018*smooth((down-0.86)/0.14) +lift = 0.012+0.034*smooth(down/0.75)-0.026*smooth((down-0.85)/0.15) q = p+n*lift[:, None] remap = np.full(len(positions), -1, dtype=np.int64) remap[used] = np.arange(len(used)) faces_of = remap[leg_faces] -cloth.append((q, vertex_normals(q, faces_of), joints[used], weights[used], 2, faces_of, p, np.zeros(len(q)))) +cloth.append((q, vertex_normals(q, faces_of), joints[used], weights[used], 2, faces_of, p, np.zeros(len(q)), None)) # Painted textures follow the form: the trunk and the head are wrapped # about the vertical, each arm and leg about its own length, so brush @@ -783,16 +802,22 @@ def wrapped_uv(points, rows, shares): result[limb] = limb_uv(points[limb], joint(prefix+'-upper-leg'), joint(prefix+'-ankle'), 0.25) return result +# Black cloth shoes: below the trouser hems her feet take the sleek black +# of her hair. +shoes = (np.arange(len(positions)) < body_count) & (regions == 0) & (positions[:, 1] <= max(ankles)+1e-6) +regions[shoes] = 3 uvs = wrapped_uv(positions, joints, weights) hangs = np.zeros(len(positions)) -hair = regions == 3 +hair = (regions == 3) & ~shoes # Hair strokes run from the hairline back into the bun. swept = positions[hair]-bun_center uvs[hair] = np.column_stack((np.abs(np.arctan2(swept[:, 0], swept[:, 1]))/np.pi*0.25/HAIR_TILE, np.linalg.norm(swept, axis=1)/HAIR_TILE)) -for points, vertex_normal, rows, shares, region, faces_of, source, hanging in cloth: +for points, vertex_normal, rows, shares, region, faces_of, source, hanging, given_uv in cloth: base = len(positions) - if source is None: + if given_uv is not None: + uv = given_uv + elif source is None: uv = trunk_uv(points) else: uv = wrapped_uv(source, rows, shares) @@ -808,7 +833,7 @@ for points, vertex_normal, rows, shares, region, faces_of, source, hanging in cl # The face is painted by face.webp, projected from the front onto the # head: its eyes, mouth and the mesh's own match exactly, and the painting # fades out towards the ears, the hairline and under the chin. -FACE = dict(size=(1254, 1254), right_eye=(430, 576), left_eye=(826, 576), mouth=(630, 858)) +FACE = dict(size=(1254, 1254), right_eye=(480, 520), left_eye=(770, 520), mouth=(628, 782)) face_ready = FACE['right_eye'] != (0, 0) mouth_point = np.array([0.0, joint('mouth')[1]-0.027]) mesh_marks = np.array([joint('r-eye')[:2], joint('l-eye')[:2], mouth_point]) @@ -817,15 +842,33 @@ picture = np.column_stack((picture[:, 0]/FACE['size'][0], 1-picture[:, 1]/FACE[' mapping = np.linalg.solve(np.column_stack((mesh_marks, np.ones(3))), picture) details = np.column_stack((positions[:, :2], np.ones(len(positions)))) @ mapping x, y = positions[:, 0], positions[:, 1] -oval = ((x/0.078)**2+((y-eye_height+0.02)/0.115)**2)**0.5 +oval = ((x/0.078)**2+((y-eye_height+0.02)/0.100)**2)**0.5 mask = smooth((normals[:, 2]-0.12)/0.4)*smooth((1.0-oval)/0.25)*smooth(-hairline(positions)/0.012) mask *= (role_of[joints[:, 0]] == 3) & (regions == 0) & face_ready details[mask <= 0] = 0.0 # Cloth's detail is how freely it hangs from the waist: 0 at the waist, 1 -# at the skirt's hem and the sash tails' ends. +# at the top's hem. details[regions != 0] = np.column_stack((hangs, np.zeros(len(hangs))))[regions != 0] masks = np.rint(np.clip(mask, 0, 1)*255).astype(np.int64) +# The body under the top and the trousers never shows: drop it, keeping +# the skin near the cuffs, the collar and the trouser hems, where it can be +# seen inside. Fewer triangles to skin, upload and draw every frame. +corners = positions[triangles] +body_face = np.all(triangles < body_count, axis=1) +progress = arm_progress(corners.reshape(-1, 3)).reshape(-1, 3).max(axis=1) +face_arm = share(joints, weights, [4, 5, 6, 7])[triangles].mean(axis=1) +leg_down = np.clip((hip_height-0.04-corners[:, :, 1].min(axis=1))/(hip_height-0.04-np.mean(ankles)), 0, 1) +hidden_top = (regions[triangles[:, 0]] == 1) & ((face_arm < 0.5) | (progress < 0.8)) & \ + (corners[:, :, 1].max(axis=1) < neck_height-0.05) & ((face_arm >= 0.5) | (corners[:, :, 1].min(axis=1) > waist_y)) +hidden_legs = (regions[triangles[:, 0]] == 2) & (leg_down < 0.8) +triangles = triangles[~(body_face & (hidden_top | hidden_legs))] +kept = np.unique(triangles) +renumber = np.full(len(positions), -1, dtype=np.int64) +renumber[kept] = np.arange(len(kept)) +triangles = renumber[triangles] +positions, normals, uvs, details = positions[kept], normals[kept], uvs[kept], details[kept] +joints, weights, regions, masks = joints[kept], weights[kept], regions[kept], masks[kept] 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) @@ -848,12 +891,11 @@ output.mkdir(parents=True, exist_ok=True) (output/'anatomy.bin').write_bytes(blob) metadata = {'format':'PFIG0002', 'vertices':len(positions), 'triangles':len(triangles), 'bones':len(bones), 'bytes':len(blob), 'sha256':hashlib.sha256(blob).hexdigest(), - 'regions':['skin', 'tunic', 'trousers', 'hair', 'sash'], + 'regions':['skin', 'top', 'trousers', 'hair', 'trim'], 'source':'https://github.com/makehumancommunity/makehuman/blob/a8bc2d54ff0ac92e78ff71431b1023eda42bf482/makehuman/data/3dobjs/base.obj', 'license':'CC0-1.0', 'bone_names':[f"{b['role']}/{b['side']}/{b['finger']}/{b['segment']}" for b in bones]} (output/'anatomy.json').write_text(json.dumps(metadata,indent=2)+'\n') print(f"Prepared {len(positions)} vertices, {len(triangles)} triangles, {len(bones)} bones; {len(blob)} bytes") -crown = positions[:body_count, 1].max() # The eyes sit halfway down the head. print(f"waist {waist_y:.3f} bust {bust_y:.3f} armpit {armpit_y:.3f} hem {hem_y:.3f}; " f"{1.7/(2*(crown-eye_height)):.2f} heads tall")