| 
54 | 54 | 
 
  | 
55 | 55 | public class TestCustomAnim extends SimpleApplication {  | 
56 | 56 | 
 
  | 
57 |  | -	private Joint bone;  | 
58 |  | -	private Armature armature;  | 
59 |  | -	final 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 vertices 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 |  | -	}  | 
 | 57 | +    private Joint bone;  | 
 | 58 | +    private Armature armature;  | 
 | 59 | +    final 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 vertices 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 | +    }  | 
151 | 151 | 
 
  | 
152 | 152 | }  | 
0 commit comments