|
32 | 32 |
|
33 | 33 | package jme3test.model.anim; |
34 | 34 |
|
35 | | -import com.jme3.animation.Bone; |
36 | | -import com.jme3.animation.Skeleton; |
37 | | -import com.jme3.animation.SkeletonControl; |
| 35 | +import java.nio.ByteBuffer; |
| 36 | +import java.nio.FloatBuffer; |
| 37 | + |
| 38 | +import com.jme3.anim.Armature; |
| 39 | +import com.jme3.anim.Joint; |
| 40 | +import com.jme3.anim.SkinningControl; |
38 | 41 | import com.jme3.app.SimpleApplication; |
39 | 42 | import com.jme3.light.AmbientLight; |
40 | 43 | import com.jme3.light.DirectionalLight; |
41 | 44 | import com.jme3.math.Quaternion; |
| 45 | +import com.jme3.math.Transform; |
42 | 46 | import com.jme3.math.Vector3f; |
43 | 47 | import com.jme3.scene.Geometry; |
44 | 48 | import com.jme3.scene.Node; |
|
47 | 51 | import com.jme3.scene.VertexBuffer.Type; |
48 | 52 | import com.jme3.scene.VertexBuffer.Usage; |
49 | 53 | import com.jme3.scene.shape.Box; |
50 | | -import java.nio.ByteBuffer; |
51 | | -import java.nio.FloatBuffer; |
52 | 54 |
|
53 | 55 | public class TestCustomAnim extends SimpleApplication { |
54 | 56 |
|
55 | | - private Bone bone; |
56 | | - private Skeleton skeleton; |
57 | | - private Quaternion rotation = new Quaternion(); |
58 | | - |
59 | | - public static void main(String[] args) { |
60 | | - TestCustomAnim app = new TestCustomAnim(); |
61 | | - app.start(); |
62 | | - } |
63 | | - |
64 | | - @Override |
65 | | - public void simpleInitApp() { |
66 | | - |
67 | | - AmbientLight al = new AmbientLight(); |
68 | | - rootNode.addLight(al); |
69 | | - |
70 | | - DirectionalLight dl = new DirectionalLight(); |
71 | | - dl.setDirection(Vector3f.UNIT_XYZ.negate()); |
72 | | - rootNode.addLight(dl); |
73 | | - |
74 | | - Box box = new Box(1, 1, 1); |
75 | | - |
76 | | - VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight); |
77 | | - VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex); |
78 | | - indicesHW.setUsage(Usage.CpuOnly); |
79 | | - weightsHW.setUsage(Usage.CpuOnly); |
80 | | - box.setBuffer(weightsHW); |
81 | | - box.setBuffer(indicesHW); |
82 | | - |
83 | | - // Setup bone weight buffer |
84 | | - FloatBuffer weights = FloatBuffer.allocate( box.getVertexCount() * 4 ); |
85 | | - VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight); |
86 | | - weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights); |
87 | | - box.setBuffer(weightsBuf); |
88 | | - |
89 | | - // Setup bone index buffer |
90 | | - ByteBuffer indices = ByteBuffer.allocate( box.getVertexCount() * 4 ); |
91 | | - VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex); |
92 | | - indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices); |
93 | | - box.setBuffer(indicesBuf); |
94 | | - |
95 | | - // Create bind pose buffers |
96 | | - box.generateBindPose(); |
97 | | - |
98 | | - // Create skeleton |
99 | | - bone = new Bone("root"); |
100 | | - bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ); |
101 | | - bone.setUserControl(true); |
102 | | - skeleton = new Skeleton(new Bone[]{ bone }); |
103 | | - |
104 | | - // Assign all verticies to bone 0 with weight 1 |
105 | | - for (int i = 0; i < box.getVertexCount() * 4; i += 4){ |
106 | | - // assign vertex to bone index 0 |
107 | | - indices.array()[i+0] = 0; |
108 | | - indices.array()[i+1] = 0; |
109 | | - indices.array()[i+2] = 0; |
110 | | - indices.array()[i+3] = 0; |
111 | | - |
112 | | - // set weight to 1 only for first entry |
113 | | - weights.array()[i+0] = 1; |
114 | | - weights.array()[i+1] = 0; |
115 | | - weights.array()[i+2] = 0; |
116 | | - weights.array()[i+3] = 0; |
117 | | - } |
118 | | - |
119 | | - // Maximum number of weights per bone is 1 |
120 | | - box.setMaxNumWeights(1); |
121 | | - |
122 | | - // Create model |
123 | | - Geometry geom = new Geometry("box", box); |
124 | | - geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m")); |
125 | | - Node model = new Node("model"); |
126 | | - model.attachChild(geom); |
127 | | - |
128 | | - // Create skeleton control |
129 | | - SkeletonControl skeletonControl = new SkeletonControl(skeleton); |
130 | | - model.addControl(skeletonControl); |
131 | | - |
132 | | - rootNode.attachChild(model); |
133 | | - } |
134 | | - |
135 | | - @Override |
136 | | - public void simpleUpdate(float tpf){ |
137 | | - // Rotate around X axis |
138 | | - Quaternion rotate = new Quaternion(); |
139 | | - rotate.fromAngleAxis(tpf, Vector3f.UNIT_X); |
140 | | - |
141 | | - // Combine rotation with previous |
142 | | - rotation.multLocal(rotate); |
143 | | - |
144 | | - // Set new rotation into bone |
145 | | - bone.setUserTransforms(Vector3f.ZERO, rotation, Vector3f.UNIT_XYZ); |
146 | | - |
147 | | - // After changing skeleton transforms, must update world data |
148 | | - skeleton.updateWorldVectors(); |
149 | | - } |
| 57 | + private Joint bone; |
| 58 | + private Armature armature; |
| 59 | + private Quaternion rotation = new Quaternion(); |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + TestCustomAnim app = new TestCustomAnim(); |
| 63 | + app.start(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void simpleInitApp() { |
| 68 | + |
| 69 | + AmbientLight al = new AmbientLight(); |
| 70 | + rootNode.addLight(al); |
| 71 | + |
| 72 | + DirectionalLight dl = new DirectionalLight(); |
| 73 | + dl.setDirection(Vector3f.UNIT_XYZ.negate()); |
| 74 | + rootNode.addLight(dl); |
| 75 | + |
| 76 | + Box box = new Box(1, 1, 1); |
| 77 | + |
| 78 | + VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight); |
| 79 | + VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex); |
| 80 | + indicesHW.setUsage(Usage.CpuOnly); |
| 81 | + weightsHW.setUsage(Usage.CpuOnly); |
| 82 | + box.setBuffer(weightsHW); |
| 83 | + box.setBuffer(indicesHW); |
| 84 | + |
| 85 | + // Setup bone weight buffer |
| 86 | + FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4); |
| 87 | + VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight); |
| 88 | + weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights); |
| 89 | + box.setBuffer(weightsBuf); |
| 90 | + |
| 91 | + // Setup bone index buffer |
| 92 | + ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4); |
| 93 | + VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex); |
| 94 | + indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices); |
| 95 | + box.setBuffer(indicesBuf); |
| 96 | + |
| 97 | + // Create bind pose buffers |
| 98 | + box.generateBindPose(); |
| 99 | + |
| 100 | + // Create skeleton |
| 101 | + bone = new Joint("root"); |
| 102 | + bone.setLocalTransform(new Transform(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ)); |
| 103 | + armature = new Armature(new Joint[] { bone }); |
| 104 | + |
| 105 | + // Assign all verticies to bone 0 with weight 1 |
| 106 | + for (int i = 0; i < box.getVertexCount() * 4; i += 4) { |
| 107 | + // assign vertex to bone index 0 |
| 108 | + indices.array()[i + 0] = 0; |
| 109 | + indices.array()[i + 1] = 0; |
| 110 | + indices.array()[i + 2] = 0; |
| 111 | + indices.array()[i + 3] = 0; |
| 112 | + |
| 113 | + // set weight to 1 only for first entry |
| 114 | + weights.array()[i + 0] = 1; |
| 115 | + weights.array()[i + 1] = 0; |
| 116 | + weights.array()[i + 2] = 0; |
| 117 | + weights.array()[i + 3] = 0; |
| 118 | + } |
| 119 | + |
| 120 | + // Maximum number of weights per bone is 1 |
| 121 | + box.setMaxNumWeights(1); |
| 122 | + |
| 123 | + // Create model |
| 124 | + Geometry geom = new Geometry("box", box); |
| 125 | + geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m")); |
| 126 | + Node model = new Node("model"); |
| 127 | + model.attachChild(geom); |
| 128 | + |
| 129 | + // Create skeleton control |
| 130 | + SkinningControl skinningControl = new SkinningControl(armature); |
| 131 | + model.addControl(skinningControl); |
| 132 | + |
| 133 | + rootNode.attachChild(model); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void simpleUpdate(float tpf) { |
| 138 | + // Rotate around X axis |
| 139 | + Quaternion rotate = new Quaternion(); |
| 140 | + rotate.fromAngleAxis(tpf, Vector3f.UNIT_X); |
| 141 | + |
| 142 | + // Combine rotation with previous |
| 143 | + rotation.multLocal(rotate); |
| 144 | + |
| 145 | + // Set new rotation into bone |
| 146 | + bone.setLocalTransform(new Transform(Vector3f.ZERO, rotation, Vector3f.UNIT_XYZ)); |
| 147 | + |
| 148 | + // After changing skeleton transforms, must update world data |
| 149 | + armature.update(); |
| 150 | + } |
150 | 151 |
|
151 | 152 | } |
0 commit comments