Skip to content

Commit

Permalink
Prepare iron to c port
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Feb 1, 2024
1 parent 06bda08 commit b96f2c5
Show file tree
Hide file tree
Showing 27 changed files with 1,616 additions and 1,511 deletions.
86 changes: 57 additions & 29 deletions Sources/iron/Animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ class Animation {
// Lerp
static m1 = Mat4.identity();
static m2 = Mat4.identity();
static vpos = new Vec4();
static vpos2 = new Vec4();
static vscl = new Vec4();
static vscl2 = new Vec4();
static q1 = new Quat();
static q2 = new Quat();
static q3 = new Quat();
static vp = new Vec4();
static vs = new Vec4();
static vpos = Vec4.create();
static vpos2 = Vec4.create();
static vscl = Vec4.create();
static vscl2 = Vec4.create();
static q1 = Quat.create();
static q2 = Quat.create();
static q3 = Quat.create();
static vp = Vec4.create();
static vs = Vec4.create();

static create(): AnimationRaw {
let raw = new AnimationRaw();
Scene.animations.push(raw);
return raw;
}

static play = (raw: AnimationRaw, action = "", onComplete: ()=>void = null, blendTime = 0.0, speed = 1.0, loop = true) => {
static playSuper = (raw: AnimationRaw, action = "", onComplete: ()=>void = null, blendTime = 0.0, speed = 1.0, loop = true) => {
if (blendTime > 0) {
raw.blendTime = blendTime;
raw.blendCurrent = 0.0;
Expand All @@ -59,21 +59,42 @@ class Animation {
raw.speed = speed;
raw.loop = loop;
raw.paused = false;

if (raw.ext.constructor.play != null) {
raw.ext.constructor.play(raw.ext, action, onComplete, blendTime, speed, loop);
}
}

static blend = (raw: AnimationRaw, action1: string, action2: string, factor: f32) => {
if (raw.ext.constructor.blend != null) {
raw.ext.constructor.blend(raw.ext, action1, action2, factor);
static play = (raw: AnimationRaw, action = "", onComplete: ()=>void = null, blendTime = 0.0, speed = 1.0, loop = true) => {
if (raw.ext != null) {
if (raw.ext.constructor == ObjectAnimationRaw) {
ObjectAnimation.play(raw.ext, action, onComplete, blendTime, speed, loop);
}
///if arm_skin
else if (raw.ext.constructor == BoneAnimationRaw) {
BoneAnimation.play(raw.ext, action, onComplete, blendTime, speed, loop);
}
///end
}
else {
Animation.playSuper(raw, action, onComplete, blendTime, speed, loop);
}
}

static blendSuper = (raw: AnimationRaw, action1: string, action2: string, factor: f32) => {
raw.blendTime = 1.0; // Enable blending
raw.blendFactor = factor;
}

static blend = (raw: AnimationRaw, action1: string, action2: string, factor: f32) => {
if (raw.ext != null) {
///if arm_skin
if (raw.ext.constructor == BoneAnimationRaw) {
BoneAnimation.blend(raw.ext, action1, action2, factor);
}
///end
}
else {
Animation.blendSuper(raw, action1, action2, factor);
}
}

static pause = (raw: AnimationRaw) => {
raw.paused = true;
}
Expand All @@ -97,8 +118,15 @@ class Animation {
}

static update = (raw: AnimationRaw, delta: f32) => {
if (raw.ext.constructor.update != null) {
raw.ext.constructor.update(raw, delta);
if (raw.ext != null) {
if (raw.ext.constructor == ObjectAnimationRaw) {
ObjectAnimation.update(raw.ext, delta);
}
///if arm_skin
else if (raw.ext.constructor == BoneAnimationRaw) {
BoneAnimation.update(raw.ext, delta);
}
///end
}
else {
Animation.updateSuper(raw, delta);
Expand Down Expand Up @@ -157,7 +185,7 @@ class Animation {
}
}

static updateAnimSampled = (raw: AnimationRaw, anim: TAnimation, m: Mat4) => {
static updateAnimSampled = (raw: AnimationRaw, anim: TAnimation, m: TMat4) => {
if (anim == null) return;
let track = anim.tracks[0];
let sign = raw.speed > 0 ? 1 : -1;
Expand All @@ -168,21 +196,21 @@ class Animation {
let t2 = track.frames[ti + sign] * raw.frameTime;
let s: f32 = (t - t1) / (t2 - t1); // Linear

Animation.m1.setF32(track.values, ti * 16); // Offset to 4x4 matrix array
Animation.m2.setF32(track.values, (ti + sign) * 16);
Mat4.setF32(Animation.m1, track.values, ti * 16); // Offset to 4x4 matrix array
Mat4.setF32(Animation.m2, track.values, (ti + sign) * 16);

// Decompose
Animation.m1.decompose(Animation.vpos, Animation.q1, Animation.vscl);
Animation.m2.decompose(Animation.vpos2, Animation.q2, Animation.vscl2);
Mat4.decompose(Animation.m1, Animation.vpos, Animation.q1, Animation.vscl);
Mat4.decompose(Animation.m2, Animation.vpos2, Animation.q2, Animation.vscl2);

// Lerp
Animation.vp.lerp(Animation.vpos, Animation.vpos2, s);
Animation.vs.lerp(Animation.vscl, Animation.vscl2, s);
Animation.q3.lerp(Animation.q1, Animation.q2, s);
Vec4.lerp(Animation.vp, Animation.vpos, Animation.vpos2, s);
Vec4.lerp(Animation.vs, Animation.vscl, Animation.vscl2, s);
Quat.lerp(Animation.q3, Animation.q1, Animation.q2, s);

// Compose
m.fromQuat(Animation.q3);
m.scale(Animation.vs);
Mat4.fromQuat(m, Animation.q3);
Mat4.scale(m, Animation.vs);
m._30 = Animation.vp.x;
m._31 = Animation.vp.y;
m._32 = Animation.vp.z;
Expand Down
2 changes: 1 addition & 1 deletion Sources/iron/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class App {
if (App.onResize != null) App.onResize();
else {
if (Scene.camera != null) {
Scene.camera.buildProjection();
CameraObject.buildProjection(Scene.camera);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/iron/Armature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Armature {
type TAction = {
name: string;
bones: TObj[];
mats: Mat4[];
mats: TMat4[];
}

///end
125 changes: 79 additions & 46 deletions Sources/iron/BaseObject.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,133 @@

class BaseObject {
class TBaseObject {
uid: i32;
urandom: f32;
raw: TObj = null;
name: string = "";
transform: Transform;
transform: TransformRaw;
traits: any[] = [];
parent: BaseObject = null;
children: BaseObject[] = [];
parent: TBaseObject = null;
children: TBaseObject[] = [];
animation: AnimationRaw = null;
visible = true; // Skip render, keep updating
culled = false; // BaseObject was culled last frame
culled = false; // TBaseObject was culled last frame
isEmpty = false;
ext: any; // MeshObject | CameraObject | LightObject | SpeakerObject
ext: any; // TMeshObject | TCameraObject | TLightObject | TSpeakerObject
}

class BaseObject {
static uidCounter = 0;

constructor() {
this.uid = BaseObject.uidCounter++;
this.transform = new Transform(this);
this.isEmpty = this.constructor == BaseObject;
if (this.isEmpty && Scene.ready) Scene.empties.push(this);
static create(): TBaseObject {
let raw = new TBaseObject();
raw.uid = BaseObject.uidCounter++;
raw.transform = Transform.create(raw);
raw.isEmpty = raw.constructor == TBaseObject;
if (raw.isEmpty && Scene.ready) Scene.empties.push(raw);
return raw;
}

setParent = (parentObject: BaseObject, parentInverse = false, keepTransform = false) => {
if (parentObject == this || parentObject == this.parent) return;
static setParent = (raw: TBaseObject, parentObject: TBaseObject, parentInverse = false, keepTransform = false) => {
if (parentObject == raw || parentObject == raw.parent) return;

if (this.parent != null) {
array_remove(this.parent.children, this);
if (keepTransform) this.transform.applyParent();
this.parent = null; // rebuild matrix without a parent
this.transform.buildMatrix();
if (raw.parent != null) {
array_remove(raw.parent.children, raw);
if (keepTransform) Transform.applyParent(raw.transform);
raw.parent = null; // rebuild matrix without a parent
Transform.buildMatrix(raw.transform);
}

if (parentObject == null) {
parentObject = Scene.sceneParent;
}
this.parent = parentObject;
this.parent.children.push(this);
if (parentInverse) this.transform.applyParentInverse();
raw.parent = parentObject;
raw.parent.children.push(raw);
if (parentInverse) Transform.applyParentInverse(raw.transform);
}

removeSuper = () => {
if (this.isEmpty && Scene.ready) array_remove(Scene.empties, this);
if (this.animation != null) Animation.remove(this.animation);
while (this.children.length > 0) this.children[0].remove();
if (this.parent != null) {
array_remove(this.parent.children, this);
this.parent = null;
static removeSuper = (raw: TBaseObject) => {
if (raw.isEmpty && Scene.ready) array_remove(Scene.empties, raw);
if (raw.animation != null) Animation.remove(raw.animation);
while (raw.children.length > 0) BaseObject.remove(raw.children[0]);
if (raw.parent != null) {
array_remove(raw.parent.children, raw);
raw.parent = null;
}
}

remove = this.removeSuper;
static remove = (raw: TBaseObject) => {
if (raw.ext != null) {
if (raw.ext.constructor == TMeshObject) {
MeshObject.remove(raw.ext);
}
else if (raw.ext.constructor == TCameraObject) {
CameraObject.remove(raw.ext);
}
else if (raw.ext.constructor == TLightObject) {
LightObject.remove(raw.ext);
}
///if arm_audio
else if (raw.ext.constructor == TSpeakerObject) {
SpeakerObject.remove(raw.ext);
}
///end
}
else {
BaseObject.removeSuper(raw);
}
}

getChild = (name: string): BaseObject => {
if (this.name == name) return this;
static getChild = (raw: TBaseObject, name: string): TBaseObject => {
if (raw.name == name) return raw;
else {
for (let c of this.children) {
let r = c.getChild(name);
for (let c of raw.children) {
let r = BaseObject.getChild(c, name);
if (r != null) return r;
}
}
return null;
}

getChildren = (recursive = false): BaseObject[] => {
if (!recursive) return this.children;
static getChildren = (raw: TBaseObject, recursive = false): TBaseObject[] => {
if (!recursive) return raw.children;

let retChildren = this.children.slice();
for (let child of this.children) {
retChildren = retChildren.concat(child.getChildren(recursive));
let retChildren = raw.children.slice();
for (let child of raw.children) {
retChildren = retChildren.concat(BaseObject.getChildren(child, recursive));
}
return retChildren;
}

///if arm_skin
getParentArmature = (name: string): BoneAnimationRaw => {
static getParentArmature = (raw: TBaseObject, name: string): BoneAnimationRaw => {
for (let a of Scene.animations) if (a.armature != null && a.armature.name == name) return a.ext;
return null;
}
///end

setupAnimationSuper = (oactions: TSceneFormat[] = null) => {
static setupAnimationSuper = (raw: TBaseObject, oactions: TSceneFormat[] = null) => {
// Parented to bone
///if arm_skin
if (this.raw.parent_bone != null) {
if (raw.raw.parent_bone != null) {
App.notifyOnInit(() => {
let banim = this.getParentArmature(this.parent.name);
if (banim != null) BoneAnimation.addBoneChild(banim, this.raw.parent_bone, this);
let banim = BaseObject.getParentArmature(raw, raw.parent.name);
if (banim != null) BoneAnimation.addBoneChild(banim, raw.raw.parent_bone, raw);
});
}
///end
// BaseObject actions
// TBaseObject actions
if (oactions == null) return;
this.animation = ObjectAnimation.create(this, oactions).base;
raw.animation = ObjectAnimation.create(raw, oactions).base;
}

setupAnimation = this.setupAnimationSuper;
static setupAnimation = (raw: TBaseObject, oactions: TSceneFormat[] = null) => {
if (raw.ext != null) {
if (raw.ext.constructor == TMeshObject) {
MeshObject.setupAnimation(raw.ext, oactions);
}
}
else {
BaseObject.setupAnimationSuper(raw, oactions);
}
}
}
Loading

0 comments on commit b96f2c5

Please sign in to comment.