From 1ae900023e740a9f257e1df8ebd344e75a0ef8db Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Tue, 29 Oct 2013 20:15:30 +0000 Subject: [PATCH] Refactoring: making bone constraints to be applied from the root bone to the children and refreshing bone world vector after baking bone's constraint. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10856 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../scene/plugins/blender/BlenderContext.java | 28 ++++++++---- .../blender/constraints/BoneConstraint.java | 6 +++ .../blender/constraints/SimulationNode.java | 44 +++++++++++++++---- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/BlenderContext.java b/engine/src/blender/com/jme3/scene/plugins/blender/BlenderContext.java index cb64b0619..711a60ee2 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/BlenderContext.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/BlenderContext.java @@ -115,8 +115,8 @@ public class BlenderContext { /** A map og helpers that perform loading. */ private Map helpers = new HashMap(); /** Markers used by loading classes to store some custom data. This is made to avoid putting this data into user properties. */ - private Map> markers = new HashMap>(); - + private Map> markers = new HashMap>(); + /** * This method sets the blender file version. * @@ -387,6 +387,16 @@ public class BlenderContext { objectConstraints.addAll(constraints); } + /** + * Returns constraints applied to the feature of the given OMA. + * @param ownerOMA + * the constraints' owner OMA + * @return a list of constraints or null if no constraints are applied to the feature + */ + public List getConstraints(Long ownerOMA) { + return constraints.get(ownerOMA); + } + /** * @return all available constraints */ @@ -418,7 +428,7 @@ public class BlenderContext { * @return the animation data or null if none exists */ public AnimationData getAnimData(Long ownerOMA) { - return this.animData.get(ownerOMA); + return animData.get(ownerOMA); } /** @@ -430,7 +440,7 @@ public class BlenderContext { * the skeleton specified by the given OMA */ public void setSkeleton(Long skeletonOMA, Skeleton skeleton) { - this.skeletons.put(skeletonOMA, skeleton); + skeletons.put(skeletonOMA, skeleton); } /** @@ -465,7 +475,7 @@ public class BlenderContext { * @return the skeleton specified by the given OMA */ public Skeleton getSkeleton(Long skeletonOMA) { - return this.skeletons.get(skeletonOMA); + return skeletons.get(skeletonOMA); } /** @@ -478,7 +488,7 @@ public class BlenderContext { * the mesh's context */ public void setMeshContext(Long meshOMA, MeshContext meshContext) { - this.meshContexts.put(meshOMA, meshContext); + meshContexts.put(meshOMA, meshContext); } /** @@ -490,7 +500,7 @@ public class BlenderContext { * @return mesh's context */ public MeshContext getMeshContext(Long meshOMA) { - return this.meshContexts.get(meshOMA); + return meshContexts.get(meshOMA); } /** @@ -503,7 +513,7 @@ public class BlenderContext { * the bones's context */ public void setBoneContext(Long boneOMA, BoneContext boneContext) { - this.boneContexts.put(boneOMA, boneContext); + boneContexts.put(boneOMA, boneContext); } /** @@ -564,7 +574,7 @@ public class BlenderContext { } return blenderKey.getDefaultMaterial(); } - + /** * Adds a custom marker for scene's feature. * diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/BoneConstraint.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/BoneConstraint.java index 3bdbef447..284cb7da4 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/BoneConstraint.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/BoneConstraint.java @@ -64,4 +64,10 @@ import com.jme3.scene.plugins.blender.file.Structure; } return true; } + + @Override + public void apply(int frame) { + super.apply(frame); + blenderContext.getBoneContext(ownerOMA).getBone().updateWorldVectors(); + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/SimulationNode.java b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/SimulationNode.java index ce72dda87..bf7480adf 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/constraints/SimulationNode.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/constraints/SimulationNode.java @@ -274,12 +274,13 @@ public class SimulationNode { track.setTime(time, 1, animControl, animChannel, vars); skeleton.updateWorldVectors(); } + - // ... and then apply constraints ... - for (Constraint constraint : constraints) { - constraint.apply(frame); - if (constraint.getAlteredOmas() != null) { - alteredOmas.addAll(constraint.getAlteredOmas()); + // ... and then apply constraints from the root bone to the last child ... + for (Bone rootBone : skeleton.getRoots()) { + if(skeleton.getBoneIndex(rootBone) > 0) { + //ommit the 0 - indexed root bone as it is the bone added by importer + this.applyConstraints(rootBone, alteredOmas, frame); } } @@ -353,6 +354,32 @@ public class SimulationNode { } } + /** + * Applies constraints to the given bone and its children. + * The goal is to apply constraint from root bone to the last child. + * @param bone + * the bone whose constraints will be applied + * @param alteredOmas + * the set of OMAS of the altered bones (is populated if necessary) + * @param frame + * the current frame of the animation + */ + private void applyConstraints(Bone bone, Set alteredOmas, int frame) { + BoneContext boneContext = blenderContext.getBoneContext(bone); + List constraints = this.findConstraints(boneContext.getBoneOma(), blenderContext); + if (constraints != null && constraints.size() > 0) { + for (Constraint constraint : constraints) { + constraint.apply(frame); + if (constraint.getAlteredOmas() != null) { + alteredOmas.addAll(constraint.getAlteredOmas()); + } + } + } + for (Bone child : bone.getChildren()) { + this.applyConstraints(child, alteredOmas, frame); + } + } + /** * Simulates the node. */ @@ -401,9 +428,10 @@ public class SimulationNode { */ private List findConstraints(Long ownerOMA, BlenderContext blenderContext) { List result = new ArrayList(); - for (Constraint constraint : blenderContext.getAllConstraints()) { - if (constraint.ownerOMA.longValue() == ownerOMA.longValue()) { - if (constraint.isImplemented()) { + List constraints = blenderContext.getConstraints(ownerOMA); + if(constraints != null) { + for (Constraint constraint : constraints) { + if (constraint.isImplemented() && constraint.validate()) { result.add(constraint); } else { LOGGER.log(Level.WARNING, "Constraint named: ''{0}'' of type ''{1}'' is not implemented and will NOT be applied!", new Object[] { constraint.name, constraint.getConstraintTypeName() });