skinnode.java
来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 563 行 · 第 1/2 页
JAVA
563 行
/*
* Copyright (c) 2003-2009 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme.animation;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import com.jme.math.Matrix4f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.ConnectionPoint;
import com.jme.scene.Geometry;
import com.jme.scene.Node;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
import com.jme.util.geom.VertMap;
/**
* SkinNode defines a scene node that contains skinned mesh data. A skinned mesh
* is defined by a Geometry object representing the "skin" that is attached to a
* skeleton (or a tree of Bones). The orientation, translation of these bones
* define the position of the skin vertices. These bones can then be driven by
* an animation system to provide the animation of the skin. SkinNode defines
* for each vertex of the skin the bone that affects it and the weight
* (BoneInfluence) of that affect. This allows multiple bones to share a single
* vertex (although the total weight must add up to 1).
*
* @author Joshua Slack
* @author Mark Powell
*/
public class SkinNode extends Node implements Savable, BoneChangeListener {
private static final long serialVersionUID = 1L;
protected static Vector3f vertex = new Vector3f();
protected static Vector3f normal = new Vector3f();
protected boolean needsRefresh = true;
protected Node skins = null;
protected Bone skeleton = null;
protected ArrayList<BoneInfluence>[][] cache = null;
protected ArrayList<ConnectionPoint> connectionPoints;
protected transient boolean newSkeletonAssigned = false;
protected transient Matrix4f bindMatrix = new Matrix4f();
private final Vector3f tmpTranslation = new Vector3f();
private final Quaternion tmpRotation = new Quaternion();
private final Vector3f tmpScale = new Vector3f();
private boolean externalControl = false;
/**
* Empty Constructor to be used internally only.
*/
public SkinNode() {
setLastFrustumIntersection(Camera.FrustumIntersect.Inside);
}
/**
* Constructor creates a new SkinNode object with the supplied name.
*
* @param name
* the name of this SkinNode
*/
public SkinNode(String name) {
super(name);
}
/**
* getSkin returns the skins (Geometry objects) that the SkinNode is currently controlling.
*
* @return the skins contained in this SkinNode
*/
public Node getSkins() {
return skins;
}
/**
* setSkin sets the skin that the SkinNode will affect.
*
* @param skins
* the skins that this SkinNode will affect.
*/
public void setSkins(Node skins) {
this.skins = skins;
attachChild(skins);
}
/**
* addSkins sets the skin that the SkinNode will affect.
*
* @param skin
* an additional skin that this SkinNode will affect.
*/
public void addSkin(Geometry skin) {
if (skins == null) {
skins = new Node("Skins");
attachChild(skins);
}
this.skins.attachChild(skin);
}
/**
* addBoneInfluence defines how a vertex will be affected by a bone. This is
* given with four values, the geometry child the vertex is found, the index
* to the vertex in the geometry, the index of the bone that has been or
* will be set via setBones or addBone and the weight that this indexed bone
* affects the vertex.
*
* @param geomIndex
* the geometry child that contains the vertex to be affected.
* @param vert
* the index to the vertex.
* @param bone
* the bone that affects the vertex.
* @param weight
* the weight that the bone will affect the vertex.
*/
public void addBoneInfluence(int geomIndex, int vert, Bone bone,
float weight) {
if (weight == 0) return;
if (cache == null)
recreateCache();
ArrayList<BoneInfluence> infs = cache[geomIndex][vert];
if (infs == null) {
infs = new ArrayList<BoneInfluence>(1);
cache[geomIndex][vert] = infs;
}
BoneInfluence i = new BoneInfluence(bone, weight);
i.boneId = bone.getName();
if (!infs.contains(i))
infs.add(i);
}
public void setAnimation(BoneAnimation anim) {
if(skeleton != null && skeleton.getAnimationController() != null) {
skeleton.getAnimationController().setActiveAnimation(anim);
}
}
public void setAnimation(int index) {
if(skeleton != null && skeleton.getAnimationController() != null) {
skeleton.getAnimationController().setActiveAnimation(index);
}
}
public void setAnimation(String name) {
if(skeleton != null && skeleton.getAnimationController() != null) {
skeleton.getAnimationController().setActiveAnimation(name);
}
}
public String getAnimationString() {
if(skeleton != null && skeleton.getAnimationController() != null ) {
return skeleton.getAnimationController().getActiveAnimation().getName();
}
return null;
}
public void addBoneInfluence(int geomIndex, int vert, String boneId,
float weight) {
if (weight == 0) return;
if (cache == null) {
recreateCache();
}
if (vert > cache[geomIndex].length) {
System.out.println("vert: " + vert);
System.out.println("cache[" + geomIndex + "].length: " + cache[geomIndex].length);
for (int i=0;i<cache.length;i++) {
System.out.println("cache[" + i + "].length: " + cache[i].length);
}
}
ArrayList<BoneInfluence> infs = cache[geomIndex][vert];
if (infs == null) {
infs = new ArrayList<BoneInfluence>(1);
cache[geomIndex][vert] = infs;
}
BoneInfluence i = new BoneInfluence(null, weight);
i.boneId = boneId;
if (!infs.contains(i))
infs.add(i);
}
public ConnectionPoint addConnectionPoint(String name, Bone b) {
ConnectionPoint cp = new ConnectionPoint(name, b);
if(connectionPoints == null) {
connectionPoints = new ArrayList<ConnectionPoint>();
}
connectionPoints.add(cp);
this.attachChild(cp);
return cp;
}
public ArrayList<ConnectionPoint> getConnectionPoints() {
return connectionPoints;
}
/**
* recreateCache initializes the cache of BoneInfluences for use by the skin
* node.
*/
@SuppressWarnings("unchecked")
public void recreateCache() {
cache = new ArrayList[skins.getQuantity()][];
for (int x = 0; x < cache.length; x++) {
cache[x] = new ArrayList[skins.getChild(x).getVertexCount()];
}
}
/**
* updateGeometricState overrides Spatials updateGeometric state to update
* the assigned skeleton bone influences, if changed.
*
* @param time
* the time that has passed between calls.
* @param initiator
* true if this is the top level being called.
*/
public void updateGeometricState(float time, boolean initiator) {
if (newSkeletonAssigned) {
assignSkeletonBoneInfluences();
}
if (!externalControl && skins != null && needsRefresh) {
updateSkin();
skins.updateModelBound();
needsRefresh = false;
}
super.updateGeometricState(time, initiator);
}
/**
* normalizeWeights insures that all vertex BoneInfluences equal 1. The total
* BoneInfluence on a single vertex should be 1 otherwise the position of the
* vertex will be multiplied.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?