-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt to upstream gltf schema changes
- Loading branch information
Showing
2 changed files
with
79 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,64 @@ | ||
(in-package #:org.shirakumo.fraf.trial.gltf) | ||
|
||
(defun add-convex-shape (gltf vertices faces) | ||
(let* ((primitive (gltf:make-mesh-primitive gltf vertices faces '(:position))) | ||
(mesh (gltf:make-indexed 'gltf:mesh gltf :primitives (vector primitive)))) | ||
(gltf:make-indexed 'gltf:mesh-shape gltf :mesh mesh :kind "mesh" :convex-p T))) | ||
|
||
(defun push-convex-shape (base-node shape) | ||
(let* ((collider (make-instance 'gltf:collider :collision-filter (gltf:collision-filter (gltf:collider base-node)) | ||
:physics-material (gltf:physics-material (gltf:collider base-node)) | ||
:shape shape | ||
:gltf (gltf:gltf base-node))) | ||
(child (gltf:make-indexed 'gltf:node base-node :collider collider :virtual-p T))) | ||
(gltf:push-child child base-node))) | ||
(defun add-convex-mesh (gltf primitives &optional name) | ||
(flet ((make-primitive (geometry) | ||
(gltf:make-mesh-primitive gltf (car geometry) (cdr geometry) '(:position)))) | ||
(gltf:make-indexed 'gltf:mesh gltf | ||
:name name | ||
:primitives (map 'vector #'make-primitive primitives)))) | ||
|
||
(defmethod optimize-model (file (type (eql :glb)) &rest args) | ||
(apply #'optimize-model file :gltf args)) | ||
|
||
(defun shape-optimizable-p (shape) | ||
(and (typep shape 'gltf:mesh-shape) | ||
(not (gltf:convex-p shape)))) | ||
|
||
(defmethod optimize-model (file (type (eql :gltf)) &rest args &key (output file) &allow-other-keys) | ||
(let ((decomposition-args (remf* args :output)) | ||
(shape-table (make-hash-table :test 'eql)) | ||
(mesh-table (make-hash-table :test 'eql)) | ||
(work-done-p NIL)) | ||
(trial:with-tempfile (tmp :type (pathname-type file)) | ||
(gltf:with-gltf (gltf file) | ||
;; Rewrite mesh shapes to multiple new shapes. | ||
;; TODO: if original mesh has no other refs anywhere, remove it | ||
(loop for shape across (gltf:shapes gltf) | ||
do (when (and (shape-optimizable-p shape) | ||
;; Only bother decomposing it if it's actually referenced anywhere. | ||
(loop for node across (gltf:nodes gltf) | ||
thereis (and (gltf:collider node) (eql shape (gltf:shape (gltf:collider node)))))) | ||
(let* ((primitives (gltf:primitives (gltf:mesh shape))) | ||
(mesh (load-primitive (aref primitives 0))) | ||
(verts (reordered-vertex-data mesh '(location))) | ||
(hulls (apply #'trial::decompose-to-convex verts (faces mesh) decomposition-args))) | ||
(setf (gethash shape shape-table) | ||
(loop for (verts . faces) across hulls | ||
collect (add-convex-shape gltf verts faces)))))) | ||
;; Rewrite nodes with refs to mesh colliders to have child nodes for | ||
;; all decomposed hulls. | ||
(loop for node across (gltf:nodes gltf) | ||
do (when (and (gltf:collider node) (shape-optimizable-p (gltf:shape (gltf:collider node)))) | ||
(loop for shape in (gethash (gltf:shape (gltf:collider node)) shape-table) | ||
do (push-convex-shape node shape)) | ||
(setf (gltf:collider node) NIL) | ||
;; Clear the extension, too. Ideally this would be done by the library already. | ||
(remhash "collider" (gethash "KHR_physics_rigid_bodies" (gltf:extensions node))) | ||
(setf work-done-p T))) | ||
for collider = (gltf:collider node) | ||
do (when collider | ||
(let* ((collider (gltf:collider node)) | ||
(geometry (gltf:geometry collider))) | ||
(when (gltf:node geometry) | ||
(let ((new (gethash (gltf:mesh (gltf:node geometry)) mesh-table))) | ||
(unless new | ||
(let* ((node (gltf:node geometry)) | ||
(mesh (gltf:mesh node)) | ||
(meshes (loop for primitive across (gltf:primitives mesh) | ||
for mesh = (load-primitive primitive) | ||
collect (cons (reordered-vertex-data mesh '(location)) (faces mesh)))) | ||
(meshes (cond ((gltf:convex-p geometry) | ||
(v:info :trial.gltf "Re-hulling ~a" mesh) | ||
(loop for (vertices) in meshes | ||
collect (multiple-value-bind (vertices faces) (org.shirakumo.fraf.quickhull:convex-hull vertices) | ||
(cons vertices faces)))) | ||
(T | ||
(v:info :trial.gltf "Decomposing ~a" mesh) | ||
(loop for (vertices . faces) in meshes | ||
nconc (coerce (apply #'trial::decompose-to-convex vertices faces decomposition-args) 'list))))) | ||
(new-mesh (add-convex-mesh gltf meshes (format NIL "~a/~:[decomposed~;rehulled~]" | ||
(gltf:name mesh) (gltf:convex-p geometry))))) | ||
(v:info :trial.gltf "Creating new mesh ~a" new-mesh) | ||
(setf new (gltf:make-indexed 'gltf:node node :mesh new-mesh | ||
:name (gltf:name new-mesh) | ||
:virtual-p T | ||
:matrix (gltf:matrix node) | ||
:rotation (gltf:rotation node) | ||
:scale (gltf:scale node) | ||
:translation (gltf:translation node))) | ||
(setf (gethash mesh mesh-table) new))) | ||
(v:info :trial.gltf "Updating ~a to point to ~a" node new) | ||
(setf (gltf:node geometry) new) | ||
(setf (gltf:convex-p geometry) T) | ||
(when (gltf:extensions node) | ||
(remhash "KHR_physics_rigid_bodies" (gltf:extensions node))) | ||
(setf work-done-p T)))))) | ||
(when work-done-p | ||
(gltf:serialize gltf tmp))) | ||
(when work-done-p | ||
(when (and work-done-p output) | ||
;; FIXME: this does not work correctly if gltf serialises to multiple files. | ||
(org.shirakumo.filesystem-utils:rename-file* tmp output))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters