Advertisement
Guest User

Untitled

a guest
Feb 24th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. package de.b00n.daltura.client.scene;
  2.  
  3. import com.jme3.bounding.BoundingBox;
  4. import com.jme3.material.Material;
  5. import com.jme3.scene.Geometry;
  6. import com.jme3.scene.Node;
  7. import com.jme3.scene.Spatial;
  8. import com.jme3.scene.shape.Box;
  9. import com.jme3.util.TangentBinormalGenerator;
  10. import de.b00n.daltura.client.scene.appstates.cam.MiniMapState;
  11. import java.util.HashMap;
  12. import jme3tools.optimize.GeometryBatchFactory;
  13.  
  14. /**
  15.  * The idea is to have only one representating child, to use with the minimap, for each child of this regular node.
  16.  * So basically:
  17.  * <ul>
  18.  * <li>Check if node has a minimum extent length in x and z of {@link  #MINIMAP_EXTENT_LEN_RECOGNITION}</li>
  19.  * <li>Traverse Children and try finding "roofs" to use, if no roof can be found create a box with {@link #fallbackMaterial} and the size of the boundingbox of the parent</li>
  20.  * </ul>
  21.  * The created node will be detached/attached at the same moment as the regular nodes added to this node.
  22.  *
  23.  * @author sascha
  24.  */
  25. public class MiniMapSynchedNode extends Node {
  26.     public static final float MINIMAP_EXTENT_LEN_RECOGNITION = MiniMapState.MINI_MAP_CAMERA_DISTANCE * .005f;
  27.     private HashMap<Spatial, Spatial> mapSpatial = new HashMap<>();
  28.     private Node miniMapNode;
  29.     private final Material fallbackMaterial;
  30.  
  31.     MiniMapSynchedNode(Node miniMapParent, Material fallbackMaterial, String name) {
  32.         super(name);
  33.         this.miniMapNode = miniMapParent;
  34.         this.fallbackMaterial = fallbackMaterial;
  35.     }
  36.  
  37.     @Override
  38.     public int attachChild(Spatial child) {
  39.         int attachChild = super.attachChild(child);
  40.         Spatial checkAddOfChild = checkAddOfChild(child);
  41.         if (checkAddOfChild != null) {
  42.             Spatial resultSpat;
  43.             if (checkAddOfChild instanceof Node) {
  44.                 String name1 = checkAddOfChild.getName();
  45.                 resultSpat = GeometryBatchFactory.optimize((Node) checkAddOfChild);
  46.                 resultSpat.setName(name1);
  47.             } else {
  48.                 resultSpat = checkAddOfChild;
  49.             }
  50.             miniMapNode.attachChild(resultSpat);
  51.             mapSpatial.put(child, resultSpat);
  52.         }
  53.         return attachChild;
  54.     }
  55.  
  56.     private Spatial checkAddOfChild(Spatial child) {
  57.         String name1 = child.getName();
  58.         String nodeName = "MiniMap" + name1;
  59.         if (child instanceof Geometry) {
  60.             BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
  61.             if (worldBound1.getXExtent() > MINIMAP_EXTENT_LEN_RECOGNITION && worldBound1.getZExtent() > MINIMAP_EXTENT_LEN_RECOGNITION) {
  62.                 Geometry geom = (Geometry) child;
  63.                 final Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
  64.                 geometry.setLocalRotation(geometry.getWorldRotation());
  65.                 geometry.setLocalTranslation(geom.getWorldTranslation());
  66.                 TangentBinormalGenerator.generate(geometry);
  67.                 geometry.setMaterial(geom.getMaterial());
  68.                 return geometry;
  69.             }
  70.         } else {
  71.             boolean isBuilding = false;
  72.             for (String s : Daltura3DApplication.buildingNames) {
  73.                 if (name1.startsWith(s)) {
  74.                     isBuilding = true;
  75.                     break;
  76.                 }
  77.             }
  78.             if (isBuilding) {
  79.                 Node node = (Node) child;
  80.                 Material m = null;
  81.                 BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
  82.                 Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
  83.                 for (Spatial sp : node.getChildren()) {
  84.                     if (sp instanceof Geometry) {
  85.                         Geometry geom = (Geometry) sp;
  86.                         if (sp.getName().equalsIgnoreCase("roof")) {
  87.                             m = geom.getMaterial();
  88.                             break;
  89.                         }
  90.                     }
  91.                 }
  92.                 geometry.setLocalRotation(geometry.getWorldRotation());
  93.                 geometry.setLocalTranslation(child.getWorldTranslation());
  94.                 TangentBinormalGenerator.generate(geometry);
  95.                 if (m == null) {
  96.                     m = fallbackMaterial;
  97.                 }
  98.                 geometry.setMaterial(m);
  99.                 return geometry;
  100.             } else if (child instanceof Node) {
  101.                 Node n = (Node) child;
  102.                 Node no = null;
  103.                 for (Spatial s : n.getChildren()) {
  104.                     if (s.getName().equalsIgnoreCase("roof") && s instanceof Geometry) {
  105.                         Geometry ref = (Geometry) s;
  106.                         BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
  107.                         Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
  108.                         geometry.setLocalTranslation(ref.getWorldTranslation());
  109.                         geometry.setLocalRotation(ref.getWorldRotation());
  110.                         geometry.setMaterial(ref.getMaterial());
  111.                         return geometry;
  112.                     } else {
  113.                         Spatial checkAddOfChild = checkAddOfChild(s);
  114.                         if (checkAddOfChild != null) {
  115.                             if (no == null) {
  116.                                 no = new Node(nodeName);
  117.                             }
  118.                             no.attachChild(checkAddOfChild);
  119.                         }
  120.                     }
  121.                 }
  122.                 if (no != null) {
  123.                     return no;
  124.                 }
  125.             }
  126.         }
  127.         return null;
  128.     }
  129.  
  130.     @Override
  131.     public int detachChild(Spatial child) {
  132.         int detachChild = super.detachChild(child);
  133.         Spatial remove = mapSpatial.remove(child);
  134.         if (remove != null) {
  135.             miniMapNode.detachChild(mapSpatial.remove(child));
  136.         }
  137.         return detachChild;
  138.     }
  139.    
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement