use of com.jme3.animation.SkeletonControl in project jmonkeyengine by jMonkeyEngine.
the class TestCustomAnim method simpleInitApp.
@Override
public void simpleInitApp() {
AmbientLight al = new AmbientLight();
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(Vector3f.UNIT_XYZ.negate());
rootNode.addLight(dl);
Box box = new Box(1, 1, 1);
VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
indicesHW.setUsage(Usage.CpuOnly);
weightsHW.setUsage(Usage.CpuOnly);
box.setBuffer(weightsHW);
box.setBuffer(indicesHW);
// Setup bone weight buffer
FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4);
VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
box.setBuffer(weightsBuf);
// Setup bone index buffer
ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4);
VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
box.setBuffer(indicesBuf);
// Create bind pose buffers
box.generateBindPose(true);
// Create skeleton
bone = new Bone("root");
bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
bone.setUserControl(true);
skeleton = new Skeleton(new Bone[] { bone });
// Assign all verticies to bone 0 with weight 1
for (int i = 0; i < box.getVertexCount() * 4; i += 4) {
// assign vertex to bone index 0
indices.array()[i + 0] = 0;
indices.array()[i + 1] = 0;
indices.array()[i + 2] = 0;
indices.array()[i + 3] = 0;
// set weight to 1 only for first entry
weights.array()[i + 0] = 1;
weights.array()[i + 1] = 0;
weights.array()[i + 2] = 0;
weights.array()[i + 3] = 0;
}
// Maximum number of weights per bone is 1
box.setMaxNumWeights(1);
// Create model
Geometry geom = new Geometry("box", box);
geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
Node model = new Node("model");
model.attachChild(geom);
// Create skeleton control
SkeletonControl skeletonControl = new SkeletonControl(skeleton);
model.addControl(skeletonControl);
rootNode.attachChild(model);
}
use of com.jme3.animation.SkeletonControl in project jmonkeyengine by jMonkeyEngine.
the class SkeletonControl method controlRenderHardware.
private void controlRenderHardware() {
offsetMatrices = skeleton.computeSkinningMatrices();
for (Material m : materials) {
MatParam currentParam = m.getParam("BoneMatrices");
if (currentParam != null) {
if (currentParam.getValue() != offsetMatrices) {
// Check to see if other SkeletonControl
// is operating on this material, in that case, user
// is sharing materials between models which is NOT allowed
// when hardware skinning used.
Logger.getLogger(SkeletonControl.class.getName()).log(Level.SEVERE, "Material instances cannot be shared when hardware skinning is used. " + "Ensure all models use unique material instances.");
}
}
m.setParam("BoneMatrices", VarType.Matrix4Array, offsetMatrices);
}
}
use of com.jme3.animation.SkeletonControl in project jmonkeyengine by jMonkeyEngine.
the class AnimationHelper method applyAnimations.
/**
* The method applies skeleton animations to the given node.
* @param node
* the node where the animations will be applied
* @param skeleton
* the skeleton of the node
* @param animationMatchMethod
* the way animation should be matched with skeleton
*/
public void applyAnimations(Node node, Skeleton skeleton, AnimationMatchMethod animationMatchMethod) {
node.addControl(new SkeletonControl(skeleton));
blenderContext.setNodeForSkeleton(skeleton, node);
List<BlenderAction> actions = this.getActions(skeleton, animationMatchMethod);
if (actions.size() > 0) {
List<Animation> animations = new ArrayList<Animation>();
for (BlenderAction action : actions) {
BoneTrack[] tracks = action.toTracks(skeleton, blenderContext);
if (tracks != null && tracks.length > 0) {
Animation boneAnimation = new Animation(action.getName(), action.getAnimationTime());
boneAnimation.setTracks(tracks);
animations.add(boneAnimation);
Long animatedNodeOMA = ((Number) blenderContext.getMarkerValue(ObjectHelper.OMA_MARKER, node)).longValue();
blenderContext.addAnimation(animatedNodeOMA, boneAnimation);
}
}
if (animations.size() > 0) {
AnimControl control = new AnimControl(skeleton);
HashMap<String, Animation> anims = new HashMap<String, Animation>(animations.size());
for (int i = 0; i < animations.size(); ++i) {
Animation animation = animations.get(i);
anims.put(animation.getName(), animation);
}
control.setAnimations(anims);
node.addControl(control);
// make sure that SkeletonControl is added AFTER the AnimControl
SkeletonControl skeletonControl = node.getControl(SkeletonControl.class);
if (skeletonControl != null) {
node.removeControl(SkeletonControl.class);
node.addControl(skeletonControl);
}
}
}
}
use of com.jme3.animation.SkeletonControl in project jmonkeyengine by jMonkeyEngine.
the class KinematicRagdollControl method createSpatialData.
@Override
protected void createSpatialData(Spatial model) {
targetModel = model;
Node parent = model.getParent();
Vector3f initPosition = model.getLocalTranslation().clone();
Quaternion initRotation = model.getLocalRotation().clone();
initScale = model.getLocalScale().clone();
model.removeFromParent();
model.setLocalTranslation(Vector3f.ZERO);
model.setLocalRotation(Quaternion.IDENTITY);
model.setLocalScale(1);
//HACK ALERT change this
//I remove the skeletonControl and readd it to the spatial to make sure it's after the ragdollControl in the stack
//Find a proper way to order the controls.
SkeletonControl sc = model.getControl(SkeletonControl.class);
if (sc == null) {
throw new IllegalArgumentException("The root node of the model should have a SkeletonControl. Make sure the control is there and that it's not on a sub node.");
}
model.removeControl(sc);
model.addControl(sc);
// put into bind pose and compute bone transforms in model space
// maybe dont reset to ragdoll out of animations?
scanSpatial(model);
if (parent != null) {
parent.attachChild(model);
}
model.setLocalTranslation(initPosition);
model.setLocalRotation(initRotation);
model.setLocalScale(initScale);
if (added) {
addPhysics(space);
}
logger.log(Level.FINE, "Created physics ragdoll for skeleton {0}", skeleton);
}
use of com.jme3.animation.SkeletonControl in project jmonkeyengine by jMonkeyEngine.
the class Spatial method read.
public void read(JmeImporter im) throws IOException {
InputCapsule ic = im.getCapsule(this);
name = ic.readString("name", null);
worldBound = (BoundingVolume) ic.readSavable("world_bound", null);
cullHint = ic.readEnum("cull_mode", CullHint.class, CullHint.Inherit);
batchHint = ic.readEnum("batch_hint", BatchHint.class, BatchHint.Inherit);
queueBucket = ic.readEnum("queue", RenderQueue.Bucket.class, RenderQueue.Bucket.Inherit);
shadowMode = ic.readEnum("shadow_mode", ShadowMode.class, ShadowMode.Inherit);
localTransform = (Transform) ic.readSavable("transform", Transform.IDENTITY);
localLights = (LightList) ic.readSavable("lights", null);
localLights.setOwner(this);
ArrayList<MatParamOverride> localOverridesList = ic.readSavableArrayList("overrides", null);
if (localOverridesList == null) {
localOverrides = new SafeArrayList<>(MatParamOverride.class);
} else {
localOverrides = new SafeArrayList(MatParamOverride.class, localOverridesList);
}
worldOverrides = new SafeArrayList<>(MatParamOverride.class);
//changed for backward compatibility with j3o files generated before the AnimControl/SkeletonControl split
//the AnimControl creates the SkeletonControl for old files and add it to the spatial.
//The SkeletonControl must be the last in the stack so we add the list of all other control before it.
//When backward compatibility won't be needed anymore this can be replaced by :
//controls = ic.readSavableArrayList("controlsList", null));
controls.addAll(0, ic.readSavableArrayList("controlsList", null));
userData = (HashMap<String, Savable>) ic.readStringSavableMap("user_data", null);
}
Aggregations