|
| 1 | +--- |
| 2 | +title: One-to-many Asset Processing |
| 3 | +authors: ["@andriyDev"] |
| 4 | +pull_requests: [] |
| 5 | +--- |
| 6 | + |
| 7 | +In previous versions, asset processing was always one-to-one: a processor would be given a single |
| 8 | +asset to process and write to a single file. |
| 9 | + |
| 10 | +Now, an asset processor can write to multiple files! When implementing the `Process` trait, you can |
| 11 | +call `writer_context.write_multiple` and provide the path relative to the original asset. So for |
| 12 | +example, here we have a processor that reads all the lines in a file and writes them each to their |
| 13 | +own file: |
| 14 | + |
| 15 | +```rust |
| 16 | +struct LineSplitterProcess; |
| 17 | + |
| 18 | +impl Process for LineSplitterProcess { |
| 19 | + type Settings = (); |
| 20 | + |
| 21 | + async fn process( |
| 22 | + &self, |
| 23 | + context: &mut ProcessContext<'_>, |
| 24 | + meta: AssetMeta<(), Self>, |
| 25 | + writer_context: WriterContext<'_>, |
| 26 | + ) -> Result<(), ProcessError> { |
| 27 | + let bytes = context.asset_bytes(); |
| 28 | + if bytes.is_empty() { |
| 29 | + return Err(ProcessError::AssetTransformError("empty asset".into())); |
| 30 | + } |
| 31 | + for (i, line) in bytes.lines().map(Result::unwrap).enumerate() { |
| 32 | + let mut writer = writer_context |
| 33 | + .write_multiple(Path::new(&format!("Line{i}.line"))) |
| 34 | + .await?; |
| 35 | + writer.write_all(line.as_bytes()).await.map_err(|err| { |
| 36 | + ProcessError::AssetWriterError { |
| 37 | + path: context.path().clone_owned(), |
| 38 | + err: err.into(), |
| 39 | + } |
| 40 | + })?; |
| 41 | + writer.finish::<TextLoader>(TextSettings::default()).await?; |
| 42 | + } |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Then if you have an asset like `shakespeare.txt`, you can load these separate files as |
| 49 | +`shakespeare.txt/Line0.line`, `shakespeare.txt/Line1.line`, etc. These separate files can have |
| 50 | +different file extensions, be loaded as completely separate asset types, or be entirely produced |
| 51 | +from scratch within the asset processor! |
| 52 | + |
| 53 | +We plan to use this to break apart large glTF files into smaller, easier-to-load pieces - |
| 54 | +particularly for producing meshlets. |
0 commit comments