|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2025 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package jme3test.animation; |
| 31 | + |
| 32 | +import com.jme3.anim.AnimComposer; |
| 33 | +import com.jme3.anim.SkinningControl; |
| 34 | +import com.jme3.app.SimpleApplication; |
| 35 | +import com.jme3.bounding.BoundingBox; |
| 36 | +import com.jme3.bounding.BoundingVolume; |
| 37 | +import com.jme3.font.BitmapText; |
| 38 | +import com.jme3.input.KeyInput; |
| 39 | +import com.jme3.input.controls.ActionListener; |
| 40 | +import com.jme3.input.controls.KeyTrigger; |
| 41 | +import com.jme3.light.AmbientLight; |
| 42 | +import com.jme3.light.DirectionalLight; |
| 43 | +import com.jme3.material.Material; |
| 44 | +import com.jme3.math.ColorRGBA; |
| 45 | +import com.jme3.math.Vector3f; |
| 46 | +import com.jme3.scene.Geometry; |
| 47 | +import com.jme3.scene.Node; |
| 48 | +import com.jme3.scene.debug.WireBox; |
| 49 | + |
| 50 | +/** |
| 51 | + * Demonstrates the toggleable per-frame bounding-volume update feature of |
| 52 | + * {@link SkinningControl}. |
| 53 | + * |
| 54 | + * <p>The Elephant model plays its "legUp" animation in a loop. A wireframe |
| 55 | + * box shows the world bounding volume of the model. Press {@code B} to |
| 56 | + * toggle {@link SkinningControl#setUpdateBounds(boolean) updateBounds}: |
| 57 | + * |
| 58 | + * <ul> |
| 59 | + * <li><b>updateBounds OFF (default):</b> The bounding box stays fixed at |
| 60 | + * the bind pose — it will not grow when the leg extends upward.</li> |
| 61 | + * <li><b>updateBounds ON:</b> The bounding box tracks the animated pose |
| 62 | + * correctly, expanding and contracting as the leg moves.</li> |
| 63 | + * </ul> |
| 64 | + * |
| 65 | + * @see SkinningControl#setUpdateBounds(boolean) |
| 66 | + */ |
| 67 | +public class TestAnimatedModelBound extends SimpleApplication { |
| 68 | + |
| 69 | + /** Root node of the loaded model. */ |
| 70 | + private Node modelRoot; |
| 71 | + /** SkinningControl whose updateBounds flag we toggle. */ |
| 72 | + private SkinningControl skinningControl; |
| 73 | + /** Wireframe box visualizing the world bounding volume each frame. */ |
| 74 | + private Geometry boundGeom; |
| 75 | + /** Label shown in the top-left corner. */ |
| 76 | + private BitmapText statusText; |
| 77 | + |
| 78 | + public static void main(String[] args) { |
| 79 | + TestAnimatedModelBound app = new TestAnimatedModelBound(); |
| 80 | + app.start(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void simpleInitApp() { |
| 85 | + // ---------- lighting ---------- |
| 86 | + rootNode.addLight(new AmbientLight(new ColorRGBA(0.3f, 0.3f, 0.3f, 1f))); |
| 87 | + DirectionalLight sun = new DirectionalLight( |
| 88 | + new Vector3f(-1f, -1f, -1f).normalizeLocal(), |
| 89 | + ColorRGBA.White); |
| 90 | + rootNode.addLight(sun); |
| 91 | + |
| 92 | + // ---------- camera ---------- |
| 93 | + cam.setLocation(new Vector3f(0f, 2f, 8f)); |
| 94 | + cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y); |
| 95 | + flyCam.setMoveSpeed(5f); |
| 96 | + |
| 97 | + // ---------- model ---------- |
| 98 | + modelRoot = (Node) assetManager.loadModel("Models/Elephant/Elephant.mesh.xml"); |
| 99 | + float scale = 0.04f; |
| 100 | + modelRoot.scale(scale); |
| 101 | + rootNode.attachChild(modelRoot); |
| 102 | + |
| 103 | + skinningControl = modelRoot.getControl(SkinningControl.class); |
| 104 | + // updateBounds is OFF by default — the bounding box will stay static. |
| 105 | + skinningControl.setHardwareSkinningPreferred(false); // easier to visualize with SW skinning |
| 106 | + |
| 107 | + AnimComposer composer = modelRoot.getControl(AnimComposer.class); |
| 108 | + composer.setCurrentAction("legUp"); |
| 109 | + |
| 110 | + // ---------- bounding-box visualizer ---------- |
| 111 | + // Create an unshaded wireframe geometry; we reposition it every frame. |
| 112 | + Material wireMat = new Material(assetManager, |
| 113 | + "Common/MatDefs/Misc/Unshaded.j3md"); |
| 114 | + wireMat.setColor("Color", ColorRGBA.Yellow); |
| 115 | + wireMat.getAdditionalRenderState().setWireframe(true); |
| 116 | + |
| 117 | + boundGeom = new Geometry("boundingBox", new WireBox(1f, 1f, 1f)); |
| 118 | + boundGeom.setMaterial(wireMat); |
| 119 | + rootNode.attachChild(boundGeom); |
| 120 | + |
| 121 | + // ---------- HUD ---------- |
| 122 | + guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); |
| 123 | + statusText = new BitmapText(guiFont); |
| 124 | + statusText.setSize(guiFont.getCharSet().getRenderedSize()); |
| 125 | + statusText.setLocalTranslation(10f, |
| 126 | + settings.getHeight() - 10f, 0f); |
| 127 | + guiNode.attachChild(statusText); |
| 128 | + updateStatusText(); |
| 129 | + |
| 130 | + // ---------- key binding ---------- |
| 131 | + inputManager.addMapping("ToggleBounds", |
| 132 | + new KeyTrigger(KeyInput.KEY_B)); |
| 133 | + inputManager.addListener(new ActionListener() { |
| 134 | + @Override |
| 135 | + public void onAction(String name, boolean isPressed, float tpf) { |
| 136 | + if (isPressed) { |
| 137 | + boolean current = skinningControl.isUpdateBounds(); |
| 138 | + skinningControl.setUpdateBounds(!current); |
| 139 | + updateStatusText(); |
| 140 | + } |
| 141 | + } |
| 142 | + }, "ToggleBounds"); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void simpleUpdate(float tpf) { |
| 147 | + // Update the wireframe box to match the current world bounding volume. |
| 148 | + modelRoot.updateGeometricState(); |
| 149 | + BoundingVolume wb = modelRoot.getWorldBound(); |
| 150 | + if (wb instanceof BoundingBox) { |
| 151 | + BoundingBox bbox = (BoundingBox) wb; |
| 152 | + Vector3f center = bbox.getCenter(); |
| 153 | + ((WireBox) boundGeom.getMesh()).updatePositions( |
| 154 | + bbox.getXExtent(), bbox.getYExtent(), bbox.getZExtent()); |
| 155 | + boundGeom.setLocalTranslation(center); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** Refreshes the HUD label that shows the current updateBounds state. */ |
| 160 | + private void updateStatusText() { |
| 161 | + boolean on = skinningControl.isUpdateBounds(); |
| 162 | + statusText.setText( |
| 163 | + "Press B to toggle updateBounds\n" |
| 164 | + + "updateBounds: " + (on ? "ON — bounding box tracks the animated pose" |
| 165 | + : "OFF — bounding box stays at bind pose (default)")); |
| 166 | + } |
| 167 | +} |
0 commit comments