Initial Commit
This commit is contained in:
36 files changed
+8436
No files matched your search
@@ -0,0 +1,148 @@
|
||||
#! Contains reading animations: a stack's time, a curve node's channels and the
|
||||
#! values they hold where no curve is given, and a curve's keys, each a time
|
||||
#! in FBX's ticks, 46,186,158,000 a second, read as seconds, and a value. How a
|
||||
#! key goes on to the next is given by attributes, each of flags and four
|
||||
#! numbers, a cubic's slopes first, each holding for as many keys in turn as
|
||||
#! its count says.
|
||||
|
||||
module malunal::fbx;
|
||||
|
||||
# Reads a stack: the time it spans.
|
||||
def scene::read_stack(&mut this, object: u32, id: i64, name: strview): u64 {
|
||||
var made := animation_stack{ id = id, name = string::from(name, nothing{}), node = object };
|
||||
made.start = this.number_of(object, "LocalStart", this.number_of(object, "ReferenceStart", 0)) / ticks_per_second;
|
||||
made.stop = this.number_of(object, "LocalStop", this.number_of(object, "ReferenceStop", 0)) / ticks_per_second;
|
||||
this.stacks.append(move(made));
|
||||
return this.stacks.length - 1;
|
||||
}
|
||||
|
||||
# Reads a curve node: a channel of each of its `d|` properties, of its value.
|
||||
def scene::read_curve_node(&mut this, object: u32, id: i64, name: strview): u64 {
|
||||
var made := curve_node{ id = id, name = string::from(name, nothing{}), node = object };
|
||||
if this.document.child(object, "Properties70") is some(properties) {
|
||||
val list := &this.document.nodes[properties as u64].children;
|
||||
for i in 0..list.length {
|
||||
val each := &this.document.nodes[list[i] as u64];
|
||||
continue if each.name.span != "P" || each.properties.length < 5;
|
||||
val named := each.properties[0].text();
|
||||
continue if !named.starts_with("d|");
|
||||
made.channels.append(curve_channel{ name = string::from(named.substr(2, named.length - 2), nothing{}), value = each.properties[4].number() as f32 });
|
||||
}
|
||||
}
|
||||
this.curve_nodes.append(move(made));
|
||||
return this.curve_nodes.length - 1;
|
||||
}
|
||||
|
||||
# Gives a curve node's channel of a connection's property, as `d|X`, its
|
||||
# curve, adding the channel where it hasn't one.
|
||||
def scene::attach_curve(&mut this, node: u64, curve: u32, property: strview): void {
|
||||
val name := property.starts_with("d|") ? property.substr(2, property.length - 2) : property;
|
||||
val value := this.curves[curve as u64].value;
|
||||
val held := &mut this.curve_nodes[node];
|
||||
for i in 0..held.channels.length {
|
||||
if held.channels[i].name.span == name {
|
||||
held.channels[i].curve = curve;
|
||||
return;
|
||||
}
|
||||
}
|
||||
held.channels.append(curve_channel{ name = string::from(name, nothing{}), value = value, curve = curve });
|
||||
}
|
||||
|
||||
# Reads a curve: its keys, and how each goes on to the next.
|
||||
def scene::read_curve(&mut this, object: u32, id: i64, name: strview): u64 {
|
||||
var made := curve{ id = id, name = string::from(name, nothing{}), node = object };
|
||||
made.value = this.document.number_of(object, "Default", 0) as f32;
|
||||
if this.document.child(object, "KeyTime") is some(times) {
|
||||
val values := this.document.integers(times);
|
||||
made.times.reserve(values.length);
|
||||
for i in 0..values.length {
|
||||
made.times.append(values[i] as f64 / ticks_per_second);
|
||||
}
|
||||
}
|
||||
if this.document.child(object, "KeyValueFloat") is some(values_node) {
|
||||
val values := this.document.reals(values_node);
|
||||
made.values.reserve(values.length);
|
||||
for i in 0..values.length {
|
||||
made.values.append(values[i] as f32);
|
||||
}
|
||||
}
|
||||
val keys := made.times.length < made.values.length ? made.times.length : made.values.length;
|
||||
var flags := vector::<i64>::new();
|
||||
var data := vector::<f64>::new();
|
||||
var counts := vector::<i64>::new();
|
||||
if this.document.child(object, "KeyAttrFlags") is some(found) {
|
||||
flags = this.document.integers(found);
|
||||
}
|
||||
if this.document.child(object, "KeyAttrDataFloat") is some(found) {
|
||||
data = this.document.reals(found);
|
||||
}
|
||||
if this.document.child(object, "KeyAttrRefCount") is some(found) {
|
||||
counts = this.document.integers(found);
|
||||
}
|
||||
var key: u64 = 0;
|
||||
for a in 0..flags.length {
|
||||
break if key >= keys;
|
||||
var held := keys - key;
|
||||
if a < counts.length && counts[a] >= 0 {
|
||||
held = counts[a] as u64;
|
||||
}
|
||||
val bits := flags[a];
|
||||
var how := key_interpolation::linear;
|
||||
if (bits & 0x2) != 0 {
|
||||
how = key_interpolation::constant;
|
||||
} else if (bits & 0x8) != 0 {
|
||||
how = key_interpolation::cubic;
|
||||
}
|
||||
val right: f32 = a * 4 + 1 < data.length ? data[a * 4] as f32 : 0;
|
||||
val next_left: f32 = a * 4 + 1 < data.length ? data[a * 4 + 1] as f32 : 0;
|
||||
var taken: u64 = 0;
|
||||
while taken < held && key < keys {
|
||||
made.interpolations.append(how);
|
||||
made.right_slopes.append(right);
|
||||
made.next_left_slopes.append(next_left);
|
||||
key++;
|
||||
taken++;
|
||||
}
|
||||
}
|
||||
while key < keys {
|
||||
made.interpolations.append(key_interpolation::linear);
|
||||
made.right_slopes.append(0);
|
||||
made.next_left_slopes.append(0);
|
||||
key++;
|
||||
}
|
||||
this.curves.append(move(made));
|
||||
return this.curves.length - 1;
|
||||
}
|
||||
|
||||
#! The value of a curve at a time: of its keys, held where they're constant,
|
||||
#! between them linearly, or along a cubic of their slopes; its first before
|
||||
#! its first key and its last after its last; its value where it has none.
|
||||
pub def curve::at(&this, time: f64): f32 {
|
||||
val keys := this.times.length < this.values.length ? this.times.length : this.values.length;
|
||||
return this.value if keys == 0;
|
||||
return this.values[0] if time <= this.times[0];
|
||||
return this.values[keys - 1] if time >= this.times[keys - 1];
|
||||
var k: u64 = 0;
|
||||
while k + 1 < keys && this.times[k + 1] <= time {
|
||||
k++;
|
||||
}
|
||||
val a := this.values[k];
|
||||
val b := this.values[k + 1];
|
||||
val span_of := this.times[k + 1] - this.times[k];
|
||||
return a if span_of <= 0;
|
||||
val t := ((time - this.times[k]) / span_of) as f32;
|
||||
val how := k < this.interpolations.length ? this.interpolations[k] : key_interpolation::linear;
|
||||
if how == key_interpolation::constant {
|
||||
return a;
|
||||
}
|
||||
if how == key_interpolation::cubic {
|
||||
# Hermite's, the slopes per second scaled to the span between the keys.
|
||||
val d := span_of as f32;
|
||||
val m0 := this.right_slopes[k] * d;
|
||||
val m1 := this.next_left_slopes[k] * d;
|
||||
val t2 := t * t;
|
||||
val t3 := t2 * t;
|
||||
return (2 * t3 - 3 * t2 + 1) * a + (t3 - 2 * t2 + t) * m0 + (-2 * t3 + 3 * t2) * b + (t3 - t2) * m1;
|
||||
}
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
Reference in new issue
Block a user