Skip to content

Commit

Permalink
feat: add loader for animation only files
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Nov 13, 2024
1 parent 8ea8e87 commit 00843eb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/actors/assimp/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@

(defmethod print-object ((obj ai::node-animation) stream)
(print-unreadable-object (obj stream :type T :identity T)
(format stream "~w :SIZE ~a"
(ai:node-name obj)
(array-total-size (ai:position-keys obj)))))
(format stream "~a ~w"
(array-total-size (ai:position-keys obj))
(ai:node-name obj))))

(defmethod print-object ((obj ai::quat-key) out)
(print-unreadable-object (obj out :type t)
Expand All @@ -80,6 +80,10 @@
(print-unreadable-object (obj out :type t)
(format out "~$" (ai:key-time obj))))

(defmethod print-object ((obj ai::animation) out)
(print-unreadable-object (obj out :type t)
(format out "~w" (ai:name obj))))

;;--------------------------------------------------
;; Loaders
;;--------------------------------------------------
Expand Down Expand Up @@ -349,11 +353,6 @@

;;--------------------------------------------------

(defun remove-nil-plist (plist)
(loop :for (p v) :on plist :by #'cddr
:when v
:append (list p v)))

(defun assimp-load-meshes (file)
"returns a list of meshes, each one being a plist. Everything should
be cached."
Expand Down Expand Up @@ -406,3 +405,27 @@
(assimp-load-meshes)
(first)
(getf :buf)))

(s:-> assimp-load-animation (string) ai::animation)
(defun assimp-load-animation (file)
"Loads animation from .FBX, assumes it only has one animation."
(s:lret ((animation
(s:~> file
(ai:import-into-lisp
:properties
'(:import-fbx-preserve-pivots nil
:import-fbx-read-textures nil
:import-no-skeleton-meshes t))
(ai:animations)
(aref 0))))
(setf (ai:name animation) (s:path-basename file))))

(defgeneric add-animation (scene file)
(:documentation "Adds first animation on given FILE to SCENE.")
(:method ((scene ai:scene) (paths list))
(map nil (s:op (add-animation scene _)) paths))
(:method ((scene ai:scene) (animation ai::animation))
(setf (ai:animations scene) (vector-add (ai:animations scene) animation)))
(:method ((scene ai:scene) (filepath string))
(log4cl:log-info "Loading animation from: ~a" filepath)
(add-animation scene (assimp-load-animation filepath))))
14 changes: 14 additions & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,17 @@
(coerce 1/1000000 'double-float)))))
#-sbcl (* (get-internal-real-time)
(coerce (/ internal-time-units-per-second) 'double-float)))

(defun remove-nil-plist (plist)
"Returns a new property list with nil properties removed."
(loop :for (p v) :on plist :by #'cddr
:when v
:append (list p v)))

(defun vector-add (vector new-element)
"Returns a new instance of VECTOR with NEW-ELEMENT added."
(s:lret ((arr (make-array (1+ (length vector)))))
(loop :for v :across vector
:for i :from 0
:do (setf (aref arr i) v)
:finally (setf (aref arr (length vector)) new-element))))

0 comments on commit 00843eb

Please sign in to comment.