Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.b00n.daltura.client.scene;
- import com.jme3.bounding.BoundingBox;
- import com.jme3.material.Material;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.shape.Box;
- import com.jme3.util.TangentBinormalGenerator;
- import de.b00n.daltura.client.scene.appstates.cam.MiniMapState;
- import java.util.HashMap;
- import jme3tools.optimize.GeometryBatchFactory;
- /**
- * The idea is to have only one representating child, to use with the minimap, for each child of this regular node.
- * So basically:
- * <ul>
- * <li>Check if node has a minimum extent length in x and z of {@link #MINIMAP_EXTENT_LEN_RECOGNITION}</li>
- * <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>
- * </ul>
- * The created node will be detached/attached at the same moment as the regular nodes added to this node.
- *
- * @author sascha
- */
- public class MiniMapSynchedNode extends Node {
- public static final float MINIMAP_EXTENT_LEN_RECOGNITION = MiniMapState.MINI_MAP_CAMERA_DISTANCE * .005f;
- private HashMap<Spatial, Spatial> mapSpatial = new HashMap<>();
- private Node miniMapNode;
- private final Material fallbackMaterial;
- MiniMapSynchedNode(Node miniMapParent, Material fallbackMaterial, String name) {
- super(name);
- this.miniMapNode = miniMapParent;
- this.fallbackMaterial = fallbackMaterial;
- }
- @Override
- public int attachChild(Spatial child) {
- int attachChild = super.attachChild(child);
- Spatial checkAddOfChild = checkAddOfChild(child);
- if (checkAddOfChild != null) {
- Spatial resultSpat;
- if (checkAddOfChild instanceof Node) {
- String name1 = checkAddOfChild.getName();
- resultSpat = GeometryBatchFactory.optimize((Node) checkAddOfChild);
- resultSpat.setName(name1);
- } else {
- resultSpat = checkAddOfChild;
- }
- miniMapNode.attachChild(resultSpat);
- mapSpatial.put(child, resultSpat);
- }
- return attachChild;
- }
- private Spatial checkAddOfChild(Spatial child) {
- String name1 = child.getName();
- String nodeName = "MiniMap" + name1;
- if (child instanceof Geometry) {
- BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
- if (worldBound1.getXExtent() > MINIMAP_EXTENT_LEN_RECOGNITION && worldBound1.getZExtent() > MINIMAP_EXTENT_LEN_RECOGNITION) {
- Geometry geom = (Geometry) child;
- final Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
- geometry.setLocalRotation(geometry.getWorldRotation());
- geometry.setLocalTranslation(geom.getWorldTranslation());
- TangentBinormalGenerator.generate(geometry);
- geometry.setMaterial(geom.getMaterial());
- return geometry;
- }
- } else {
- boolean isBuilding = false;
- for (String s : Daltura3DApplication.buildingNames) {
- if (name1.startsWith(s)) {
- isBuilding = true;
- break;
- }
- }
- if (isBuilding) {
- Node node = (Node) child;
- Material m = null;
- BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
- Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
- for (Spatial sp : node.getChildren()) {
- if (sp instanceof Geometry) {
- Geometry geom = (Geometry) sp;
- if (sp.getName().equalsIgnoreCase("roof")) {
- m = geom.getMaterial();
- break;
- }
- }
- }
- geometry.setLocalRotation(geometry.getWorldRotation());
- geometry.setLocalTranslation(child.getWorldTranslation());
- TangentBinormalGenerator.generate(geometry);
- if (m == null) {
- m = fallbackMaterial;
- }
- geometry.setMaterial(m);
- return geometry;
- } else if (child instanceof Node) {
- Node n = (Node) child;
- Node no = null;
- for (Spatial s : n.getChildren()) {
- if (s.getName().equalsIgnoreCase("roof") && s instanceof Geometry) {
- Geometry ref = (Geometry) s;
- BoundingBox worldBound1 = (BoundingBox) child.getWorldBound();
- Geometry geometry = new Geometry(nodeName, new Box(worldBound1.getXExtent(), worldBound1.getYExtent(), worldBound1.getZExtent()));
- geometry.setLocalTranslation(ref.getWorldTranslation());
- geometry.setLocalRotation(ref.getWorldRotation());
- geometry.setMaterial(ref.getMaterial());
- return geometry;
- } else {
- Spatial checkAddOfChild = checkAddOfChild(s);
- if (checkAddOfChild != null) {
- if (no == null) {
- no = new Node(nodeName);
- }
- no.attachChild(checkAddOfChild);
- }
- }
- }
- if (no != null) {
- return no;
- }
- }
- }
- return null;
- }
- @Override
- public int detachChild(Spatial child) {
- int detachChild = super.detachChild(child);
- Spatial remove = mapSpatial.remove(child);
- if (remove != null) {
- miniMapNode.detachChild(mapSpatial.remove(child));
- }
- return detachChild;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement