spine2d 0.4.0

Pure Rust runtime for Spine 4.3 (unofficial)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use spine2d::{
    AnimationState, AnimationStateData, Atlas, Bone, MixInterpolation, Physics, Skeleton,
    SkeletonData, TrackEntryHandle,
};
use std::{collections::HashMap, env, fs, path::Path, sync::Arc};

fn usage() -> ! {
    eprintln!(
        "Usage:\n  render_dump <atlas.atlas> <skeleton.(json|skel)> [--y-down 0|1] <commands...>\n\nCommands:\n  --set-skin <name|none>\n  --physics <none|reset|update|pose>\n  --mix <from> <to> <duration>\n  --set <track> <animation> <loop 0|1>\n  --add <track> <animation> <loop 0|1> <delay>\n  --set-empty <track> <mixDuration>\n  --add-empty <track> <mixDuration> <delay>\n  --entry-alpha <alpha>\n  --entry-event-threshold <threshold>\n  --entry-alpha-attachment-threshold <threshold>\n  --entry-mix-attachment-threshold <threshold>\n  --entry-mix-draw-order-threshold <threshold>\n  --entry-additive <0|1>\n  --entry-mix-interpolation <linear|smooth|slow-fast|fast-slow|circle>\n  --entry-reverse <0|1>\n  --entry-shortest-rotation <0|1>\n  --entry-reset-rotation-directions\n  --step <dt>\n"
    );
    std::process::exit(2);
}

fn read_to_string(path: &Path) -> Result<String, String> {
    fs::read_to_string(path).map_err(|e| format!("failed to read {}: {e}", path.display()))
}

#[cfg(feature = "binary")]
fn read_bytes(path: &Path) -> Result<Vec<u8>, String> {
    fs::read(path).map_err(|e| format!("failed to read {}: {e}", path.display()))
}

fn load_skeleton_data(path: &Path) -> Result<Arc<SkeletonData>, String> {
    let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("");
    if ext.eq_ignore_ascii_case("skel") {
        #[cfg(feature = "binary")]
        {
            let bytes = read_bytes(path)?;
            return SkeletonData::from_skel_bytes(&bytes)
                .map_err(|e| format!("failed to parse {}: {e}", path.display()));
        }
        #[cfg(not(feature = "binary"))]
        {
            return Err("loading .skel requires `--features binary`".to_string());
        }
    }

    #[cfg(feature = "json")]
    {
        let json = read_to_string(path)?;
        SkeletonData::from_json_str(&json)
            .map_err(|e| format!("failed to parse {}: {e}", path.display()))
    }
    #[cfg(not(feature = "json"))]
    {
        let _ = path;
        Err("loading .json requires `--features json`".to_string())
    }
}

fn parse_physics(s: &str) -> Result<Physics, String> {
    match s {
        "none" => Ok(Physics::None),
        "reset" => Ok(Physics::Reset),
        "update" => Ok(Physics::Update),
        "pose" => Ok(Physics::Pose),
        _ => Err(format!("invalid --physics {s}")),
    }
}

fn physics_name(p: Physics) -> &'static str {
    match p {
        Physics::None => "none",
        Physics::Reset => "reset",
        Physics::Update => "update",
        Physics::Pose => "pose",
    }
}

fn parse_mix_interpolation(s: &str) -> Result<MixInterpolation, String> {
    match s {
        "linear" => Ok(MixInterpolation::Linear),
        "smooth" => Ok(MixInterpolation::Smooth),
        "slow-fast" => Ok(MixInterpolation::SlowFast),
        "fast-slow" => Ok(MixInterpolation::FastSlow),
        "circle" => Ok(MixInterpolation::Circle),
        _ => Err(format!("invalid --entry-mix-interpolation {s}")),
    }
}

fn parse_bool_flag(s: &str) -> bool {
    s.parse::<i32>().unwrap_or(0) != 0
}

fn require_last_entry(last_entry: Option<TrackEntryHandle>, command: &str) -> TrackEntryHandle {
    last_entry.unwrap_or_else(|| {
        eprintln!("{command} requires a preceding --set/--add command");
        std::process::exit(2);
    })
}

fn json_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c => out.push(c),
        }
    }
    out
}

fn clamp_u8_from_f32(v: f32) -> u8 {
    if !v.is_finite() {
        return 0;
    }
    let x = (v.clamp(0.0, 1.0) * 255.0) as i32;
    x.clamp(0, 255) as u8
}

fn pack_aarrggbb(rgba: [f32; 4]) -> u32 {
    let r = clamp_u8_from_f32(rgba[0]) as u32;
    let g = clamp_u8_from_f32(rgba[1]) as u32;
    let b = clamp_u8_from_f32(rgba[2]) as u32;
    let a = clamp_u8_from_f32(rgba[3]) as u32;
    (a << 24) | (r << 16) | (g << 8) | b
}

fn atlas_uses_pma(atlas: &Atlas) -> bool {
    atlas.get_pages().iter().any(|page| page.pma)
}

fn main() {
    let mut args = env::args().skip(1).collect::<Vec<_>>();
    if args.len() < 3 {
        usage();
    }

    let atlas_path = Path::new(&args[0]).to_path_buf();
    let skeleton_path = Path::new(&args[1]).to_path_buf();
    args.drain(0..2);

    let mut y_down = false;
    let mut i = 0usize;
    while i < args.len() {
        if args[i] == "--y-down" {
            if i + 1 >= args.len() {
                usage();
            }
            y_down = parse_bool_flag(args[i + 1].as_str());
            i += 2;
        } else {
            i += 1;
        }
    }
    Bone::set_y_down(y_down);

    let mut skin: Option<String> = None;
    let mut physics = Physics::None;
    let mut total_time = 0.0f32;
    let mut last_entry: Option<TrackEntryHandle> = None;

    let mut i = 0usize;
    while i < args.len() {
        match args[i].as_str() {
            "--y-down" if i + 1 < args.len() => {
                i += 2;
            }
            _ => break,
        }
    }

    let atlas_text = read_to_string(&atlas_path).unwrap_or_else(|e| {
        eprintln!("{e}");
        std::process::exit(2);
    });
    let atlas = atlas_text.parse::<Atlas>().unwrap_or_else(|e| {
        eprintln!("failed to parse {}: {e}", atlas_path.display());
        std::process::exit(2);
    });

    let data = load_skeleton_data(&skeleton_path).unwrap_or_else(|e| {
        eprintln!("{e}");
        std::process::exit(2);
    });

    let mut skeleton = Skeleton::new(data.clone());
    skeleton.setup_pose();

    let mut state = AnimationState::new(AnimationStateData::new(data));

    while i < args.len() {
        match args[i].as_str() {
            "--set-skin" if i + 1 < args.len() => {
                let name = args[i + 1].as_str();
                skin = if name == "none" {
                    None
                } else {
                    Some(name.to_string())
                };
                skeleton.set_skin(skin.as_deref());
                skeleton.setup_pose_slots();
                skeleton.update_cache();
                i += 2;
            }
            "--physics" if i + 1 < args.len() => {
                physics = parse_physics(args[i + 1].as_str()).unwrap_or_else(|e| {
                    eprintln!("{e}");
                    std::process::exit(2);
                });
                i += 2;
            }
            "--mix" if i + 3 < args.len() => {
                let duration = args[i + 3].parse::<f32>().unwrap_or(0.0);
                state
                    .get_data()
                    .set_mix(args[i + 1].as_str(), args[i + 2].as_str(), duration);
                i += 4;
            }
            "--set" if i + 3 < args.len() => {
                let track = args[i + 1].parse::<usize>().unwrap_or(0);
                let looped = parse_bool_flag(args[i + 3].as_str());
                last_entry = Some(state.set_animation(track, args[i + 2].as_str(), looped));
                i += 4;
            }
            "--add" if i + 4 < args.len() => {
                let track = args[i + 1].parse::<usize>().unwrap_or(0);
                let looped = parse_bool_flag(args[i + 3].as_str());
                let delay = args[i + 4].parse::<f32>().unwrap_or(0.0);
                last_entry = Some(state.add_animation(track, args[i + 2].as_str(), looped, delay));
                i += 5;
            }
            "--set-empty" if i + 2 < args.len() => {
                let track = args[i + 1].parse::<usize>().unwrap_or(0);
                let mix_duration = args[i + 2].parse::<f32>().unwrap_or(0.0);
                last_entry = Some(state.set_empty_animation(track, mix_duration));
                i += 3;
            }
            "--add-empty" if i + 3 < args.len() => {
                let track = args[i + 1].parse::<usize>().unwrap_or(0);
                let mix_duration = args[i + 2].parse::<f32>().unwrap_or(0.0);
                let delay = args[i + 3].parse::<f32>().unwrap_or(0.0);
                last_entry = Some(state.add_empty_animation(track, mix_duration, delay));
                i += 4;
            }
            "--entry-alpha" if i + 1 < args.len() => {
                let alpha = args[i + 1].parse::<f32>().unwrap_or(0.0);
                require_last_entry(last_entry, "--entry-alpha").set_alpha(&mut state, alpha);
                i += 2;
            }
            "--entry-event-threshold" if i + 1 < args.len() => {
                let threshold = args[i + 1].parse::<f32>().unwrap_or(0.0);
                require_last_entry(last_entry, "--entry-event-threshold")
                    .set_event_threshold(&mut state, threshold);
                i += 2;
            }
            "--entry-alpha-attachment-threshold" if i + 1 < args.len() => {
                let threshold = args[i + 1].parse::<f32>().unwrap_or(0.0);
                require_last_entry(last_entry, "--entry-alpha-attachment-threshold")
                    .set_alpha_attachment_threshold(&mut state, threshold);
                i += 2;
            }
            "--entry-mix-attachment-threshold" if i + 1 < args.len() => {
                let threshold = args[i + 1].parse::<f32>().unwrap_or(0.0);
                require_last_entry(last_entry, "--entry-mix-attachment-threshold")
                    .set_mix_attachment_threshold(&mut state, threshold);
                i += 2;
            }
            "--entry-mix-draw-order-threshold" if i + 1 < args.len() => {
                let threshold = args[i + 1].parse::<f32>().unwrap_or(0.0);
                require_last_entry(last_entry, "--entry-mix-draw-order-threshold")
                    .set_mix_draw_order_threshold(&mut state, threshold);
                i += 2;
            }
            "--entry-additive" if i + 1 < args.len() => {
                let additive = parse_bool_flag(args[i + 1].as_str());
                require_last_entry(last_entry, "--entry-additive")
                    .set_additive(&mut state, additive);
                i += 2;
            }
            "--entry-mix-interpolation" if i + 1 < args.len() => {
                let interpolation =
                    parse_mix_interpolation(args[i + 1].as_str()).unwrap_or_else(|e| {
                        eprintln!("{e}");
                        std::process::exit(2);
                    });
                require_last_entry(last_entry, "--entry-mix-interpolation")
                    .set_mix_interpolation(&mut state, interpolation);
                i += 2;
            }
            "--entry-reverse" if i + 1 < args.len() => {
                let reverse = parse_bool_flag(args[i + 1].as_str());
                require_last_entry(last_entry, "--entry-reverse").set_reverse(&mut state, reverse);
                i += 2;
            }
            "--entry-shortest-rotation" if i + 1 < args.len() => {
                let shortest_rotation = parse_bool_flag(args[i + 1].as_str());
                require_last_entry(last_entry, "--entry-shortest-rotation")
                    .set_shortest_rotation(&mut state, shortest_rotation);
                i += 2;
            }
            "--entry-reset-rotation-directions" => {
                require_last_entry(last_entry, "--entry-reset-rotation-directions")
                    .reset_rotation_directions(&mut state);
                i += 1;
            }
            "--step" if i + 1 < args.len() => {
                let dt = args[i + 1].parse::<f32>().unwrap_or(0.0);
                state.update(dt);
                state.apply(&mut skeleton);
                skeleton.update(dt);
                skeleton.update_world_transform_with_physics(physics);
                total_time += dt;
                i += 2;
            }
            _ => usage(),
        }
    }

    let draw_list = spine2d::build_draw_list_with_atlas(&skeleton, &atlas);

    let mut page_index_by_name: HashMap<&str, usize> = HashMap::new();
    for (i, page) in atlas.get_pages().iter().enumerate() {
        page_index_by_name.insert(page.name.as_str(), i);
    }

    // Manual JSON writing keeps this example dependency-free and avoids `serde_json` feature
    // coupling, while still providing a stable oracle format.
    let skin_json = skin
        .as_ref()
        .map(|s| format!("\"{}\"", json_escape(s)))
        .unwrap_or_else(|| "null".to_string());
    println!(
        "{{\"mode\":\"scenario\",\"y_down\":{},\"pma\":{},\"physics\":\"{}\",\"skin\":{},\"anim\":\"<scenario>\",\"time\":{},\"draws\":[",
        if y_down { 1 } else { 0 },
        if atlas_uses_pma(&atlas) { 1 } else { 0 },
        physics_name(physics),
        skin_json,
        total_time
    );

    for (draw_i, draw) in draw_list.draws.iter().enumerate() {
        if draw_i != 0 {
            print!(",");
        }

        let page_index = page_index_by_name
            .get(draw.texture_path.as_str())
            .copied()
            .map(|i| i as i32)
            .unwrap_or(-1);
        let blend = match draw.blend {
            spine2d::BlendMode::Normal => "normal",
            spine2d::BlendMode::Additive => "additive",
            spine2d::BlendMode::Multiply => "multiply",
            spine2d::BlendMode::Screen => "screen",
        };

        let indices = &draw_list.indices[draw.first_index..(draw.first_index + draw.index_count)];

        // Compute the vertex range used by this draw (conservative: scan indices).
        let mut min_v = u32::MAX;
        let mut max_v = 0u32;
        for &idx in indices {
            min_v = min_v.min(idx);
            max_v = max_v.max(idx);
        }
        let start_v = min_v as usize;
        let end_v = (max_v as usize).saturating_add(1);
        let vertices = &draw_list.vertices[start_v..end_v];

        print!(
            "{{\"page\":{page_index},\"texture\":\"{}\",\"blend\":\"{blend}\",\"num_vertices\":{},\"num_indices\":{},",
            json_escape(&draw.texture_path),
            vertices.len(),
            indices.len()
        );

        print!("\"positions\":[");
        for (i, v) in vertices.iter().enumerate() {
            if i != 0 {
                print!(",");
            }
            print!("{},{}", v.position[0], v.position[1]);
        }
        print!("],");

        print!("\"uvs\":[");
        for (i, v) in vertices.iter().enumerate() {
            if i != 0 {
                print!(",");
            }
            print!("{},{}", v.uv[0], v.uv[1]);
        }
        print!("],");

        print!("\"colors\":[");
        for (i, v) in vertices.iter().enumerate() {
            if i != 0 {
                print!(",");
            }
            print!("{}", pack_aarrggbb(v.color));
        }
        print!("],");

        print!("\"dark_colors\":[");
        for (i, v) in vertices.iter().enumerate() {
            if i != 0 {
                print!(",");
            }
            print!("{}", pack_aarrggbb(v.dark_color));
        }
        print!("],");

        print!("\"indices\":[");
        for (i, idx) in indices.iter().enumerate() {
            if i != 0 {
                print!(",");
            }
            print!("{}", idx - min_v);
        }
        print!("]}}");
    }

    println!("]}}");
}