Skip to content

Commit

Permalink
Untested: implement gltf convex decomposition optimisation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Nov 21, 2023
1 parent b65cf55 commit b102af3
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion formats/gltf.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
(mesh (make-instance (if skin 'skinned-mesh 'static-mesh)
:name name :vertex-form (gltf:mode primitive)
:vertex-attributes attributes)))
(when (gltf:material primitive)
(when (and model (gltf:material primitive))
(setf (material mesh)
(find-material (or (gltf:name (gltf:material primitive))
(gltf:idx (gltf:material primitive)))
Expand Down Expand Up @@ -473,3 +473,34 @@
(enter (load-environment-light (gltf:light node)) scene))
(recurse (gltf:nodes node) scene)))
model)))

(defun add-convex-shape (base-node vertices faces)
(let* ((primitive (gltf:make-mesh-primitive base-node vertices faces '(:position)))
(mesh (gltf:make-indexed 'gltf:mesh base-node :primitives (vector primitive)))
(shape (gltf:make-indexed 'gltf:convex-shape base-node :mesh mesh :kind "convex"))
(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)))
(vector-push-extend child (gltf:children base-node))))

(defun precompile (file &rest args &key (output file) &allow-other-keys)
(let ((decomposition-args (remf* args :output)))
(gltf:with-gltf (gltf file)
(let ((work-done-p NIL))
(loop for node across (gltf:nodes gltf)
do (when (and (gltf:collider node)
(typep (gltf:shape (gltf:collider node)) 'gltf:trimesh-shape))
(let* ((shape (gltf:shape (gltf:collider node)))
(primitives (gltf:primitives (gltf:mesh shape)))
(mesh (load-mesh (aref primitives 0) NIL "" NIL))
(verts (reordered-vertex-data mesh '(location))))
(loop for hull across (apply #'org.shirakumo.fraf.convex-covering:decompose verts (faces mesh) decomposition-args)
do (add-convex-shape node
(org.shirakumo.fraf.convex-covering:vertices hull)
(org.shirakumo.fraf.convex-covering:faces hull)))
(setf (gltf:collider node) NIL)
(setf work-done-p T))))
(when work-done-p
(gltf:serialize gltf output))))))

0 comments on commit b102af3

Please sign in to comment.