123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>Debugging</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="./coderay-asciidoctor.css"></head><body class="article toc2 toc-left"><div id="header"><h1>Debugging</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/debugging.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a></div><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#debug-shapes">Debug Shapes</a><ul class="sectlevel2"><li><a href="#coordinate-axes">Coordinate Axes</a></li><li><a href="#wireframe-grid">Wireframe Grid</a></li><li><a href="#wireframe-cube">Wireframe Cube</a></li><li><a href="#wireframe-sphere">Wireframe Sphere</a></li></ul></li><li><a href="#wireframe-for-physics">Wireframe for Physics</a></li><li><a href="#wireframe-for-animations">Wireframe for Animations</a></li><li><a href="#example-toggle-wireframe-on-model">Example: Toggle Wireframe on Model</a></li><li><a href="#example-toggle-wireframe-on-the-scene">Example: Toggle Wireframe on the scene</a></li><li><a href="#see-also">See also</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>When you deal with complex game engine features like animations or physics it is handy to get feedback from the engine how it interpreted the current state. Is the physical object’s collision shape really where you think it is? Is the skeleton of the animated character moving like you think it should? This document shows you how to activate visual debug aides.</p></div>
- <div class="paragraph"><p>What if you just want to quickly write code that loads models and brings them in their start position? You may not want to hunt for a sample model, convert it, add lights, and load materials. Instead you use “hasslefree simple shapes, and a “hasslefree unshaded material or wireframe: No model, no light source, no materials are needed to see them in your test scene.</p></div>
- <div class="paragraph"><p>If you ever have problems with objects appearing in the wrong spot, with the wrong scale, or wrong orientation, simply attach debug shapes to your scene to have a point of reference in 3D space – just like a giant ruler. If your code positions the debug shapes correctly, but models remain invisible when you apply the same code to them, you know that the problem must be either the model (where is its origin coordinate?), or the light (too dark? too bright? missing?), or the model’s material (missing?) – and not the positioning code.</p></div>
- <div class="paragraph"><p>Here are some different debug shapes:</p></div>
- <div style="text-align: center;" class="imageblock"><div class="content"><img src="../../jme3/advanced/debug-shapes.png" alt="debug-shapes.png" height="220"></div></div></div></div>
- <div class="sect2"><h3 id="debug-shapes">Debug Shapes</h3><div class="sect2"><h3 id="coordinate-axes">Coordinate Axes</h3><div class="paragraph"><p>The coordinate axes (com.jme3.scene.debug.Arrow) help you see the cardinal directions (X,Y,Z) from their center point. Scale the arrows to use them as a “ruler for a certain length.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> <span class="type">void</span> attachCoordinateAxes(Vector3f pos){
- Arrow arrow = <span class="keyword">new</span> Arrow(Vector3f.UNIT_X);
- arrow.setLineWidth(<span class="integer">4</span>); <span class="comment">// make arrow thicker</span>
- putShape(arrow, ColorRGBA.Red).setLocalTranslation(pos);
- arrow = <span class="keyword">new</span> Arrow(Vector3f.UNIT_Y);
- arrow.setLineWidth(<span class="integer">4</span>); <span class="comment">// make arrow thicker</span>
- putShape(arrow, ColorRGBA.Green).setLocalTranslation(pos);
- arrow = <span class="keyword">new</span> Arrow(Vector3f.UNIT_Z);
- arrow.setLineWidth(<span class="integer">4</span>); <span class="comment">// make arrow thicker</span>
- putShape(arrow, ColorRGBA.Blue).setLocalTranslation(pos);
- }
- <span class="directive">private</span> Geometry putShape(Mesh shape, ColorRGBA color){
- Geometry g = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">coordinate axis</span><span class="delimiter">"</span></span>, shape);
- Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat.getAdditionalRenderState().setWireframe(<span class="predefined-constant">true</span>);
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, color);
- g.setMaterial(mat);
- rootNode.attachChild(g);
- <span class="keyword">return</span> g;
- }</code></pre></div></div></div>
- <div class="sect2"><h3 id="wireframe-grid">Wireframe Grid</h3><div class="paragraph"><p>Use a wireframe grid (com.jme3.scene.debug.Grid) as a ruler or simple floor.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> Geometry attachGrid(Vector3f pos, <span class="type">float</span> size, ColorRGBA color){
- Geometry g = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">wireframe grid</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> Grid(size, size, <span class="float">0.2f</span>) );
- Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat.getAdditionalRenderState().setWireframe(<span class="predefined-constant">true</span>);
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, color);
- g.setMaterial(mat);
- g.center().move(pos);
- rootNode.attachChild(g);
- <span class="keyword">return</span> g;
- }</code></pre></div></div></div>
- <div class="sect2"><h3 id="wireframe-cube">Wireframe Cube</h3><div class="paragraph"><p>Use a wireframe cube (com.jme3.scene.debug.WireBox) as a stand-in object to see whether your code scales, positions, or orients, loaded models right.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> Geometry attachWireBox(Vector3f pos, <span class="type">float</span> size, ColorRGBA color){
- Geometry g = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">wireframe cube</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> WireBox(size, size, size));
- Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat.getAdditionalRenderState().setWireframe(<span class="predefined-constant">true</span>);
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, color);
- g.setMaterial(mat);
- g.setLocalTranslation(pos);
- rootNode.attachChild(g);
- <span class="keyword">return</span> g;
- }</code></pre></div></div></div>
- <div class="sect2"><h3 id="wireframe-sphere">Wireframe Sphere</h3><div class="paragraph"><p>Use a wireframe sphere (com.jme3.scene.debug.WireSphere) as a stand-in object to see whether your code scales, positions, or orients, loaded models right.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> Geometry attachWireSphere(Vector3f pos, <span class="type">float</span> size, ColorRGBA color){
- Geometry g = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">wireframe sphere</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> WireSphere(size));
- Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat.getAdditionalRenderState().setWireframe(<span class="predefined-constant">true</span>);
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, color);
- g.setMaterial(mat);
- g.setLocalTranslation(pos);
- rootNode.attachChild(g);
- <span class="keyword">return</span> g;
- }</code></pre></div></div></div></div>
- <div class="sect1"><h2 id="wireframe-for-physics">Wireframe for Physics</h2><div class="sectionbody"><div class="paragraph"><p>You can display a wireframe of the (usually invisible) collision shape around all physical objects. Use this for debugging when analyzing unexpected behaviour. Does not work with DETACHED physics, please switch to PARALLEL or SEQUENTIAL for debugging.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">physicsSpace.enableDebug(assetManager);</code></pre></div></div>
- <div class="paragraph"><p>With debugging enabled, colors are used to indicate various types of physical objects:</p></div>
- <div class="ulist"><ul><li><p>A magenta wire mesh indicates an active rigid body.</p></li><li><p>A blue wire mesh indicates a rigid body which is either new or inactive.</p></li><li><p>A yellow wire mesh indicates a ghost.</p></li><li><p>Two green arrows indicate a joint.</p></li><li><p>A pink wire mesh indicates a character.</p></li></ul></div></div></div>
- <div class="sect1"><h2 id="wireframe-for-animations">Wireframe for Animations</h2><div class="sectionbody"><div class="paragraph"><p>Making the skeleton visible inside animated models can be handy for debugging animations. The <code>control</code> object is an AnimControl, <code>player</code> is the loaded model.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> SkeletonDebugger skeletonDebug =
- <span class="keyword">new</span> SkeletonDebugger(<span class="string"><span class="delimiter">"</span><span class="content">skeleton</span><span class="delimiter">"</span></span>, control.getSkeleton());
- Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.Green);
- mat.getAdditionalRenderState().setDepthTest(<span class="predefined-constant">false</span>);
- skeletonDebug.setMaterial(mat);
- player.attachChild(skeletonDebug);</code></pre></div></div></div></div>
- <div class="sect1"><h2 id="example-toggle-wireframe-on-model">Example: Toggle Wireframe on Model</h2><div class="sectionbody"><div class="paragraph"><p>We assume that you have loaded a model with a material <code>mat</code>.</p></div>
- <div class="paragraph"><p>Then you can add a switch to toggle the model’s wireframe on and off, like this:</p></div>
- <div class="olist arabic"><ol class="arabic"><li><p>Create a key input trigger that switches between the two materials: E.g. we toggle when the T key is pressed:</p></li></ol></div>
- <div class="listingblock"><div class="content"><pre> inputManager.addMapping("toggle wireframe", new KeyTrigger(KeyInput.KEY_T));
- inputManager.addListener(actionListener, "toggle wireframe");</pre></div></div>
- <div class="olist arabic"><ol class="arabic"><li><p>Now add the toggle action to the action listener</p></li></ol></div>
- <div class="listingblock"><div class="content"><pre> private ActionListener actionListener = new ActionListener() {
- @Override
- public void onAction(String name, boolean pressed, float tpf) {
- // toggle wireframe
- if (name.equals("toggle wireframe") && !pressed) {
- wireframe = !wireframe; // toggle boolean
- mat.getAdditionalRenderState().setWireframe(wireframe);
- }
- // else ... other input tests.
- }
- };</pre></div></div>
- <div class="olist arabic"><ol class="arabic"><li><p>Alternatively you could traverse over the whole scene and toggle for all Geometry objects in there if you don’t want to create a new SceneProcessor</p></li></ol></div>
- <div class="listingblock"><div class="content"><pre> private ActionListener actionListener = new ActionListener() {
- boolean wireframe = false;
- @Override
- public void onAction(String name, boolean pressed, float tpf) {
- // toggle wireframe
- if (name.equals("toggle wireframe") && !pressed) {
- wireframe = !wireframe; // toggle boolean
- rootNode.depthFirstTraversal(new SceneGraphVisitor() {
- public void visit(Spatial spatial) {
- if (spatial instanceof Geometry)
- ((Geometry)spatial).getMaterial().getAdditionalRenderState().setWireframe(wireframe);
- }
- });
- }
- // else ... other input tests.
- }
- };</pre></div></div>
- <div class="dlist"><dl><dt class="hdlist1">TIP </dt><dd><p>To set the line width of wireframe display, use mesh.setLineWidth(lineWidth). Default line width is 1.</p></dd></dl></div></div></div>
- <div class="sect1"><h2 id="example-toggle-wireframe-on-the-scene">Example: Toggle Wireframe on the scene</h2><div class="sectionbody"><div class="paragraph"><p>To display the wireframe of the entire scene instead on one material at a time, first create the following Scene Processor</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">WireProcessor</span> <span class="directive">implements</span> SceneProcessor {
- RenderManager renderManager;
- Material wireMaterial;
- <span class="directive">public</span> WireProcessor(AssetManager assetManager) {
- wireMaterial = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">/Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- wireMaterial.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.Blue);
- wireMaterial.getAdditionalRenderState().setWireframe(<span class="predefined-constant">true</span>);
- }
- <span class="directive">public</span> <span class="type">void</span> initialize(RenderManager rm, ViewPort vp) {
- renderManager = rm;
- }
- <span class="directive">public</span> <span class="type">void</span> reshape(ViewPort vp, <span class="type">int</span> w, <span class="type">int</span> h) {
- <span class="keyword">throw</span> <span class="keyword">new</span> <span class="exception">UnsupportedOperationException</span>(<span class="string"><span class="delimiter">"</span><span class="content">Not supported yet.</span><span class="delimiter">"</span></span>);
- }
- <span class="directive">public</span> <span class="type">boolean</span> isInitialized() {
- <span class="keyword">return</span> renderManager != <span class="predefined-constant">null</span>;
- }
- <span class="directive">public</span> <span class="type">void</span> preFrame(<span class="type">float</span> tpf) {
- }
- <span class="directive">public</span> <span class="type">void</span> postQueue(RenderQueue rq) {
- renderManager.setForcedMaterial(wireMaterial);
- }
- <span class="directive">public</span> <span class="type">void</span> postFrame(FrameBuffer out) {
- renderManager.setForcedMaterial(<span class="predefined-constant">null</span>);
- }
- <span class="directive">public</span> <span class="type">void</span> cleanup() {
- renderManager.setForcedMaterial(<span class="predefined-constant">null</span>);
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>Then attach the scene processor to the <abbr title="Graphical User Interface">GUI</abbr> Viewport.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">getViewPort().addProcessor(<span class="keyword">new</span> WireProcessor());</code></pre></div></div></div></div>
- <div class="sect1"><h2 id="see-also">See also</h2><div class="sectionbody"><div class="ulist"><ul><li><p><a href="../../jme3/advanced/spatial.html">Spatial</a> – if you can’t see certain spatials, you can modify the culling behaviour to identify problems (such as inside-out custom meshes)</p></li></ul></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2016-07-06 19:47:04 UTC</div></div></body></html>
|