Compare commits
No commits in common. "5d3b7051a4a9c3c13eeb492960673834c569f24d" and "9677c18b576cef6e4b3423b3d15a0448b407dedc" have entirely different histories.
5d3b7051a4
...
9677c18b57
92
src/lib.rs
92
src/lib.rs
|
@ -9,24 +9,17 @@ use std::{
|
|||
|
||||
pub trait PointCache {
|
||||
type Frame: Frame;
|
||||
fn read_frames(
|
||||
fn read(
|
||||
infile: File,
|
||||
) -> Result<
|
||||
Box<dyn Iterator<Item = Result<<Self as PointCache>::Frame, Box<dyn Error>>>>,
|
||||
Box<dyn Error>,
|
||||
>;
|
||||
fn read_animation_info(infile: File) -> Result<AnimationInfo, Box<dyn Error>>;
|
||||
fn map_to<F: FnMut(<Self as PointCache>::Frame) -> Result<MDDFrame, Box<dyn Error>>>(
|
||||
infile: File,
|
||||
outfile: File,
|
||||
op: F,
|
||||
) -> Result<(), Box<dyn Error>>;
|
||||
fn read(infile: File) -> Result<Animation<Self>, Box<dyn Error>> {
|
||||
Ok(Animation {
|
||||
frames: Self::read_frames(infile.try_clone()?)?,
|
||||
info: Self::read_animation_info(infile)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Frame {
|
||||
|
@ -45,32 +38,6 @@ pub struct Point {
|
|||
pub z: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct AnimationInfo {
|
||||
pub total_frames: usize,
|
||||
pub total_points: usize,
|
||||
pub start_frame: usize,
|
||||
pub end_frame: usize,
|
||||
// MDD file format supports varying FPS , but Blender's exporter does not
|
||||
// FPS is only correct if it's not varying, see individual frame times for more granularity
|
||||
pub fps: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EmptyAnimationError;
|
||||
|
||||
impl Display for EmptyAnimationError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "EmptyAnimationError")
|
||||
}
|
||||
}
|
||||
impl Error for EmptyAnimationError {}
|
||||
|
||||
pub struct Animation<PC: PointCache + ?Sized> {
|
||||
pub frames: Box<dyn Iterator<Item = Result<<PC>::Frame, Box<dyn Error>>>>,
|
||||
pub info: AnimationInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MDDFrame {
|
||||
pub frame_idx: usize,
|
||||
|
@ -125,18 +92,15 @@ impl Error for ExaustedPointsUnexpectedly {}
|
|||
|
||||
impl PointCache for MDDSeekableFile {
|
||||
type Frame = MDDFrame;
|
||||
fn read_frames(
|
||||
fn read(
|
||||
mut infile: File,
|
||||
) -> Result<
|
||||
Box<dyn Iterator<Item = Result<<Self as PointCache>::Frame, Box<dyn Error>>>>,
|
||||
Box<dyn Error>,
|
||||
> {
|
||||
) -> Result<Box<dyn Iterator<Item = Result<MDDFrame, Box<dyn Error>>>>, Box<dyn Error>> {
|
||||
let (total_frames, total_points) = MDDSeekableFile::read_header(&mut infile)?;
|
||||
|
||||
let header_size: u64 = (size_of::<i32>() * 2).try_into()?;
|
||||
let f32_size: u64 = size_of::<f32>().try_into()?;
|
||||
let mut buff = [0u8; size_of::<f32>()];
|
||||
let frames = Box::new(
|
||||
let data = Box::new(
|
||||
(0..((total_frames * total_points) as u64)).map::<Result<_, Box<dyn Error>>, _>(
|
||||
move |count| {
|
||||
let frame_idx: usize = (count / (total_points as u64)).try_into()?;
|
||||
|
@ -167,7 +131,7 @@ impl PointCache for MDDSeekableFile {
|
|||
},
|
||||
),
|
||||
);
|
||||
Ok(frames)
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn map_to<F>(mut infile: File, mut outfile: File, mut op: F) -> Result<(), Box<dyn Error>>
|
||||
|
@ -181,7 +145,7 @@ impl PointCache for MDDSeekableFile {
|
|||
let f32_size: u64 = size_of::<f32>().try_into()?;
|
||||
let mut count = 0;
|
||||
|
||||
for frame in Self::read_frames(infile)? {
|
||||
for frame in Self::read(infile)? {
|
||||
let frame = op(frame?)?;
|
||||
let frame_idx: u64 = count / (total_points as u64);
|
||||
let time_offset: u64 = f32_size * frame_idx;
|
||||
|
@ -201,32 +165,6 @@ impl PointCache for MDDSeekableFile {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_animation_info(mut infile: File) -> Result<AnimationInfo, Box<dyn Error>> {
|
||||
let (total_frames, total_points) = Self::read_header(&mut infile)?;
|
||||
let mut frames = Self::read_frames(infile)?;
|
||||
let mut fps: f32 = -1.0;
|
||||
let mut prev_frame = frames.next().ok_or(EmptyAnimationError)??;
|
||||
let start_frame = prev_frame.frame_idx;
|
||||
// TODO: sometimes the time difference between frames is 0, not sure why
|
||||
// Need to rely on later frames to avoid divide by zero
|
||||
for next_frame in frames {
|
||||
let next_frame = next_frame?;
|
||||
if fps < 0.0 && (next_frame.time - prev_frame.time) > 0.0 {
|
||||
fps = 1.0 / (next_frame.time - prev_frame.time);
|
||||
}
|
||||
prev_frame = next_frame;
|
||||
}
|
||||
let end_frame = prev_frame.frame_idx;
|
||||
|
||||
Ok(AnimationInfo {
|
||||
total_frames: total_frames as usize,
|
||||
total_points: total_points as usize,
|
||||
start_frame,
|
||||
end_frame,
|
||||
fps,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
|
@ -252,25 +190,9 @@ mod tests {
|
|||
})?;
|
||||
println!("\nReading back output.mdd");
|
||||
let infile = File::open("output.mdd")?;
|
||||
for frame in MDDSeekableFile::read_frames(infile)? {
|
||||
for frame in MDDSeekableFile::read(infile)? {
|
||||
println!("{:?}", frame);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn animation_info() -> Result<(), Box<dyn Error>> {
|
||||
let infile = File::open("test_cube.mdd")?;
|
||||
let animation = MDDSeekableFile::read(infile)?;
|
||||
assert_eq!(
|
||||
animation.info,
|
||||
AnimationInfo {
|
||||
total_frames: 4,
|
||||
total_points: 1,
|
||||
start_frame: 0,
|
||||
end_frame: 3,
|
||||
fps: 24.0
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,41 +10,87 @@
|
|||
@tool
|
||||
extends SceneTree
|
||||
|
||||
var bundle_name := "vertex_animations.scn"
|
||||
var VertexAnimation = preload("VertexAnimation.gd")
|
||||
var VertexAnimations = preload("VertexAnimations.gd")
|
||||
|
||||
var bundle_name := "bundle.res"
|
||||
|
||||
func _init():
|
||||
var script := GDScript.new()
|
||||
var basenames := OS.get_cmdline_user_args()
|
||||
script.source_code = make_all_headers(basenames, true)
|
||||
script.reload()
|
||||
var res: Resource = script.new()
|
||||
|
||||
var animations = VertexAnimations.new()
|
||||
animations.name = "VertexAnimations"
|
||||
res.basenames = basenames
|
||||
for basename in basenames:
|
||||
animations.animations.append(add_animation(basename))
|
||||
add_images(res, basename)
|
||||
|
||||
|
||||
var shader := ShaderInclude.new()
|
||||
for basename in basenames:
|
||||
shader.code += make_shader(basename)
|
||||
|
||||
script.source_code = make_all_headers(basenames, false)
|
||||
|
||||
|
||||
# Cannot use built-in scripts
|
||||
# See: https://github.com/godotengine/godot/issues/85876
|
||||
script.resource_path = "res://bundle/bundle.gd"
|
||||
ResourceSaver.save(script, "res://bundle/bundle.gd")
|
||||
res.shader = shader
|
||||
res.shader.resource_name = "shader"
|
||||
shader.resource_path = "res://bundle/bundle.gdshaderinc"
|
||||
ResourceSaver.save(shader, "res://bundle/bundle.gdshaderinc", ResourceSaver.FLAG_CHANGE_PATH)
|
||||
ResourceSaver.save(res, "res://bundle/" + bundle_name, ResourceSaver.FLAG_CHANGE_PATH)
|
||||
|
||||
var packed = PackedScene.new()
|
||||
packed.pack(animations)
|
||||
|
||||
ResourceSaver.save(packed, "res://" + bundle_name, ResourceSaver.FLAG_CHANGE_PATH)
|
||||
|
||||
# Was never added to the scene tree, must manually free
|
||||
animations.free()
|
||||
quit()
|
||||
|
||||
|
||||
func add_animation(basename: String):
|
||||
var animation_info: Dictionary = JSON.parse_string(FileAccess.open(basename + ".json", FileAccess.READ).get_as_text())
|
||||
var animation = VertexAnimation.new()
|
||||
animation.name = basename
|
||||
animation.start_frame = animation_info["frame_start"]
|
||||
animation.end_frame = animation_info["frame_end"]
|
||||
animation.fps = animation_info["fps"]
|
||||
func make_all_shaders(basenames):
|
||||
for basename in basenames:
|
||||
pass
|
||||
|
||||
func make_shader(basename: String):
|
||||
var code = "\nuniform sampler2D image_" + basename + ": repeat_disable;\n"
|
||||
code += "uniform sampler2D image_mask_" + basename + ": repeat_disable;\n"
|
||||
code += "uniform bool enabled_" + basename + " = false;\n"
|
||||
code += "uniform int start_time_" + basename + ";\n"
|
||||
return code
|
||||
|
||||
func make_all_headers(basenames, include_setters):
|
||||
var header := "extends Resource\n"
|
||||
header += "@export var basenames: PackedStringArray\n"
|
||||
header += "@export var shader: ShaderInclude\n"
|
||||
for basename in basenames:
|
||||
header += make_header(basename)
|
||||
if include_setters:
|
||||
header += make_setters(basename)
|
||||
return header + attach_code(basenames)
|
||||
|
||||
func attach_code(basenames):
|
||||
var code := "\nfunc attach(mat: ShaderMaterial):\n"
|
||||
for basename in basenames:
|
||||
code += " mat.set_shader_parameter('image_"+basename+"', image_"+basename+")\n"
|
||||
code += " mat.set_shader_parameter('image_mask_"+basename+"', image_mask_"+basename+")\n"
|
||||
return code
|
||||
|
||||
func make_header(basename: String):
|
||||
var header := "@export var image_" + basename + ": Texture2D\n"
|
||||
header += "@export var image_mask_" + basename + ": Texture2D\n"
|
||||
return header
|
||||
|
||||
func make_setters(basename: String):
|
||||
var body := """
|
||||
func set_image_"""+basename+"""(image):\n image_""" + basename + """ = image
|
||||
func set_image_mask_"""+basename+"""(image):\n image_mask_""" + basename + """ = image
|
||||
"""
|
||||
return body
|
||||
|
||||
|
||||
func add_images(res, basename: String):
|
||||
var img := Image.new()
|
||||
img.load(basename + ".exr")
|
||||
animation.data = ImageTexture.create_from_image(img)
|
||||
var tex := ImageTexture.create_from_image(img)
|
||||
res.call("set_image_"+basename, tex)
|
||||
img = Image.new()
|
||||
img.load(basename + "_mask.exr")
|
||||
animation.mask = ImageTexture.create_from_image(img)
|
||||
|
||||
return animation
|
||||
tex = ImageTexture.create_from_image(img)
|
||||
res.call("set_image_mask_"+basename, tex)
|
||||
|
|
|
@ -2,19 +2,21 @@
|
|||
// First image contains index of every active vertex in the bigger image file, vertices that don't move have a value of -1 in the mask image.
|
||||
|
||||
// Openexr library is too complex to go off documentation alone and no projects on github using library. Seek out test cases
|
||||
// Reference for outputting an image
|
||||
// https://github.com/vfx-rs/openexr-rs/blob/25826b4f89bc768b565ba150d6f9c76876ad6bc3/src/core/output_file.rs#L275
|
||||
|
||||
// TODO make images less than 16k
|
||||
|
||||
use openexr::core::channel_list::Channel;
|
||||
use openexr::core::frame_buffer::{FrameBuffer, Slice};
|
||||
use openexr::core::header::Header;
|
||||
use openexr::core::output_file::OutputFile;
|
||||
use openexr::core::{Compression, PixelType};
|
||||
use pointcache::{AnimationInfo, MDDFrame, MDDSeekableFile, Point, PointCache};
|
||||
use std::io::Write;
|
||||
use openexr::core::{PixelType, Compression};
|
||||
use pointcache::{MDDFrame, MDDSeekableFile, Point, PointCache};
|
||||
use std::{error::Error, fs::File};
|
||||
|
||||
// https://github.com/vfx-rs/openexr-rs/blob/25826b4f89bc768b565ba150d6f9c76876ad6bc3/src/core/output_file.rs#L275
|
||||
|
||||
pub const MAX_IMAGE_DIM: usize = 16384;
|
||||
// TODO lower this once you figure out what the fuck
|
||||
pub const MOVEMENT_EPSILON: f32 = 0.000000001;
|
||||
|
||||
fn non_color_channel() -> Channel {
|
||||
|
@ -22,12 +24,11 @@ fn non_color_channel() -> Channel {
|
|||
type_: PixelType::Float.into(),
|
||||
x_sampling: 1,
|
||||
y_sampling: 1,
|
||||
// The docs say this is only used by a particular compression algorithm, but I don't think it hurts to have it here
|
||||
p_linear: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Using f32 instead of Uint to store integers because Godot does not support Uint
|
||||
// Godot doesn't seem to support Uint images, using f32 instead
|
||||
// Currently only using R channel, can squeeze more into 16k texture if use more channels
|
||||
fn write_mask_image(
|
||||
filename: &str,
|
||||
|
@ -107,22 +108,18 @@ fn write_point_image(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
// Not used because blender always starts frame frame zero so this info is useless
|
||||
fn _write_animation_info(filename: &str, info: AnimationInfo) -> Result<(), Box<dyn Error>> {
|
||||
let data = format!(
|
||||
"{{ \"total_frames\": {}, \"total_points\": {}, \"start_frame\": {}, \"end_frame\": {}, \"fps\": {} }}\n",
|
||||
info.total_frames, info.total_points, info.start_frame, info.end_frame, info.fps
|
||||
);
|
||||
let mut file = File::create(filename)?;
|
||||
file.write_all(data.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum PointState {
|
||||
Unseen,
|
||||
OneValue { point_idx: usize, point: Point },
|
||||
Varying { new_idx: usize },
|
||||
OneValue {
|
||||
point_idx: usize,
|
||||
point: Point,
|
||||
},
|
||||
Varying {
|
||||
point: Point,
|
||||
new_idx: usize,
|
||||
count: usize,
|
||||
},
|
||||
}
|
||||
use PointState::*;
|
||||
impl PointState {
|
||||
|
@ -138,7 +135,6 @@ impl PointState {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let basename = std::env::args()
|
||||
.nth(1)
|
||||
|
@ -146,12 +142,12 @@ fn main() {
|
|||
|
||||
let mdd_filename = basename.clone() + ".mdd";
|
||||
|
||||
let mdd_file = File::open(mdd_filename).expect("Mdd filename is not valid");
|
||||
let mut mdd_file = File::open(mdd_filename).expect("Mdd filename is not valid");
|
||||
|
||||
let info = MDDSeekableFile::read_animation_info(mdd_file.try_clone().unwrap()).unwrap();
|
||||
let (total_frames, total_points) = MDDSeekableFile::read_header(&mut mdd_file).unwrap();
|
||||
|
||||
let mut mask_last: Vec<PointState> = vec![Unseen; info.total_points];
|
||||
let pc = MDDSeekableFile::read_frames(mdd_file.try_clone().unwrap()).unwrap();
|
||||
let mut mask_last: Vec<PointState> = vec![Unseen; total_points as usize];
|
||||
let pc = MDDSeekableFile::read(mdd_file.try_clone().unwrap()).unwrap();
|
||||
let mut total_varying = 0;
|
||||
for frame in pc {
|
||||
let MDDFrame {
|
||||
|
@ -166,22 +162,41 @@ fn main() {
|
|||
assert_eq!(old_point_idx, point_idx);
|
||||
if PointState::point_changed(&point, &old_point) {
|
||||
mask_last[point_idx] = Varying {
|
||||
point,
|
||||
new_idx: total_varying,
|
||||
count: 1,
|
||||
};
|
||||
total_varying += 1;
|
||||
}
|
||||
}
|
||||
Varying { .. } => {}
|
||||
Varying {
|
||||
point: old_point,
|
||||
new_idx,
|
||||
count,
|
||||
} => {
|
||||
if PointState::point_changed(&point, &old_point) {
|
||||
mask_last[point_idx] = Varying {
|
||||
point,
|
||||
new_idx,
|
||||
count: count + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mask: Vec<f32> = mask_last.iter().map(PointState::mask_value).collect();
|
||||
|
||||
write_mask_image(&(basename.clone() + "_mask.exr"), &mask, info.total_points).unwrap();
|
||||
write_mask_image(
|
||||
&(basename.clone() + "_mask.exr"),
|
||||
&mask,
|
||||
total_points as usize,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut pixels: Vec<Point> = Vec::with_capacity(total_varying * (info.total_frames));
|
||||
let mut pixels: Vec<Point> = Vec::with_capacity(total_varying * (total_frames as usize));
|
||||
|
||||
let pc = MDDSeekableFile::read_frames(mdd_file.try_clone().unwrap()).unwrap();
|
||||
let pc = MDDSeekableFile::read(mdd_file.try_clone().unwrap()).unwrap();
|
||||
let pc: Vec<_> = pc.collect();
|
||||
let max_point_idx = pc
|
||||
.iter()
|
||||
|
@ -190,7 +205,7 @@ fn main() {
|
|||
.cloned()
|
||||
.max()
|
||||
.unwrap();
|
||||
assert_eq!(max_point_idx + 1, info.total_points);
|
||||
assert_eq!(max_point_idx + 1, total_points as usize);
|
||||
|
||||
let pc = pc.into_iter();
|
||||
|
||||
|
@ -205,7 +220,7 @@ fn main() {
|
|||
}
|
||||
}));
|
||||
|
||||
assert!(pixels.len() == total_varying * (info.total_frames));
|
||||
assert!(pixels.len() == total_varying * (total_frames as usize));
|
||||
|
||||
if total_varying == 0 {
|
||||
eprintln!("No varying frames, not outputting .exr image");
|
||||
|
@ -213,15 +228,12 @@ fn main() {
|
|||
}
|
||||
|
||||
write_point_image(
|
||||
&(basename.clone() + ".exr"),
|
||||
&(basename + ".exr"),
|
||||
&pixels,
|
||||
total_varying,
|
||||
info.total_frames,
|
||||
total_frames as usize,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
println!(
|
||||
"Total {} varying points of {} total points",
|
||||
total_varying, info.total_points
|
||||
);
|
||||
println!("Total {} varying points of {} total points", total_varying, total_points);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue