public void getChildMap(List<Spatial> res, Node parent, Node n, String name, boolean raypick) { for (Spatial child : n.getChildren()) { if (child instanceof Node) { getChildMap(res, parent, (Node) child, name, raypick); } if (child.getName() != null && child.getName().startsWith(name)) { if (raypick && child instanceof Geometry) { child.setUserData(ENTITY_GEOMETRY_REFERENCE, parent); } res.add(child); } } }
/** * Reload a prop and unpick it. * * @param originalProp which prop to reload (not null) */ public void reload(Spatial originalProp) { String name = originalProp.getName(); logger.log(Level.INFO, "reloading prop {0}", MyString.quote(name)); Node reloaded = duplicate(originalProp); scene.deleteSpatial(originalProp); reloaded.setName(name); }
/** * Create a prop without adding it to the scene. * * @param propType which kind of prop to add (not null) * @param relativeSize the prop's size relative to standard (>0) * @return a new node to represent the prop, or null if the model is invalid */ private Node create(String propType, float relativeSize) { assert propType != null; assert relativeSize > 0f : relativeSize; Node propNode = loadModel(propType); Node modelNode = (Node) propNode.getChild(0); List<Spatial> parts = modelNode.getChildren(); /* * Texture each part of the prop and specify its hardness. */ for (Spatial part : parts) { String partName = part.getName(); if (!(part instanceof Geometry)) { logger.log(Level.SEVERE, "Prop part {0} is not a geometry.", MyString.quote(partName)); return null; } String materialName = material(partName); SpatialProperties.setHardness(part, materialName); Material material = Assets.loadBlockMaterial(materialName); part.setMaterial(material); } /* * Set the scale factor. */ float scaleFactor = scene.getScaleFactor(); float modelScale = relativeSize / scaleFactor; propNode.setLocalScale(modelScale); assert getRelativeSize(propNode) == relativeSize : relativeSize; /* * Set other properties. */ propNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive); /* * Give the prop a physics control. */ CollisionShape shape = createShape(propType, relativeSize, parts); float mass = calculateMass(propType, relativeSize); RigidBodyControl rigidBodyControl = new RigidBodyControl(shape, mass); propNode.addControl(rigidBodyControl); /* * Configure the physics control. */ rigidBodyControl.setFriction(friction); scene.getWorld().getPhysics().addObject(rigidBodyControl); /* * Give the prop a PickedControl. */ SpatialProperties.setPickType(propNode, pickType); PickedControl pickedControl = new PickedControl(); propNode.addControl(pickedControl); return propNode; }
/** * Duplicate a prop. Duplicating differs from cloning because the duplicate gets a unique name and * any descendants of the duplicate get renamed to match. Besides, cloning doesn't work well on * physics controls. * * @param originalProp which prop to duplicate (not null) * @return the new prop */ public Node duplicate(Spatial originalProp) { String name = originalProp.getName(); logger.log(Level.INFO, "duplicating prop {0}", MyString.quote(name)); String propType = SpatialProperties.getPrefixType(originalProp); float relativeSize = originalProp.getWorldScale().x; Vector3f centerLocation = MySpatial.getWorldLocation(originalProp); Quaternion orientation = MySpatial.getWorldOrientation(originalProp); Node result = add(propType, relativeSize, centerLocation, orientation); return result; }
public static Geometry findGeoByName(String name, Node parent) { for (Spatial sp : parent.getChildren()) { if (sp instanceof Geometry) { if (sp.getName().equals(name)) { return (Geometry) sp; } } } for (Spatial sp : parent.getChildren()) { if (sp instanceof Node) { Geometry result = findGeoByName(name, (Node) sp); if (result != null) { return (Geometry) result; } } } return null; }
private void applyGravity(Spatial blackHole, Spatial target, float tpf) { Vector3f difference = blackHole.getLocalTranslation().subtract(target.getLocalTranslation()); Vector3f gravity = difference.normalize().multLocal(tpf); float distance = difference.length(); if (target.getName().equals("Player")) { gravity.multLocal(250f / distance); target.getControl(PlayerControl.class).applyGravity(gravity.mult(80f)); } else if (target.getName().equals("Bullet")) { gravity.multLocal(250f / distance); target.getControl(BulletControl.class).applyGravity(gravity.mult(-0.8f)); } else if (target.getName().equals("Seeker")) { target.getControl(SeekerControl.class).applyGravity(gravity.mult(150000)); } else if (target.getName().equals("Wanderer")) { target.getControl(WandererControl.class).applyGravity(gravity.mult(150000)); } else if (target.getName().equals("Laser") || target.getName().equals("Glow")) { target.getControl(ParticleControl.class).applyGravity(gravity.mult(15000), distance); } }
/** * The method loads library of a given ID from linked blender file. * * @param id the ID of the linked feature (it contains its name and blender path) * @return loaded feature or null if none was found * @throws BlenderFileException and exception is throw when problems with reading a blend file * occur */ @SuppressWarnings("unchecked") protected Object loadLibrary(Structure id) throws BlenderFileException { Pointer pLib = (Pointer) id.getFieldValue("lib"); if (pLib.isNotNull()) { String fullName = id.getFieldValue("name").toString(); // we need full name with the prefix String nameOfFeatureToLoad = id.getName(); Structure library = pLib.fetchData().get(0); String path = library.getFieldValue("filepath").toString(); if (!blenderContext.getLinkedFeatures().keySet().contains(path)) { File file = new File(path); List<String> pathsToCheck = new ArrayList<String>(); String currentPath = file.getName(); do { pathsToCheck.add(currentPath); file = file.getParentFile(); if (file != null) { currentPath = file.getName() + '/' + currentPath; } } while (file != null); Spatial loadedAsset = null; BlenderKey blenderKey = null; for (String p : pathsToCheck) { blenderKey = new BlenderKey(p); blenderKey.setLoadUnlinkedAssets(true); try { loadedAsset = blenderContext.getAssetManager().loadAsset(blenderKey); break; // break if no exception was thrown } catch (AssetNotFoundException e) { LOGGER.log(Level.FINEST, "Cannot locate linked resource at path: {0}.", p); } } if (loadedAsset != null) { Map<String, Map<String, Object>> linkedData = loadedAsset.getUserData("linkedData"); for (Entry<String, Map<String, Object>> entry : linkedData.entrySet()) { String linkedDataFilePath = "this".equals(entry.getKey()) ? path : entry.getKey(); List<Node> scenes = (List<Node>) entry.getValue().get("scenes"); for (Node scene : scenes) { blenderContext.addLinkedFeature(linkedDataFilePath, "SC" + scene.getName(), scene); } List<Node> objects = (List<Node>) entry.getValue().get("objects"); for (Node object : objects) { blenderContext.addLinkedFeature(linkedDataFilePath, "OB" + object.getName(), object); } List<TemporalMesh> meshes = (List<TemporalMesh>) entry.getValue().get("meshes"); for (TemporalMesh mesh : meshes) { blenderContext.addLinkedFeature(linkedDataFilePath, "ME" + mesh.getName(), mesh); } List<MaterialContext> materials = (List<MaterialContext>) entry.getValue().get("materials"); for (MaterialContext materialContext : materials) { blenderContext.addLinkedFeature( linkedDataFilePath, "MA" + materialContext.getName(), materialContext); } List<Texture> textures = (List<Texture>) entry.getValue().get("textures"); for (Texture texture : textures) { blenderContext.addLinkedFeature( linkedDataFilePath, "TE" + texture.getName(), texture); } List<Texture> images = (List<Texture>) entry.getValue().get("images"); for (Texture image : images) { blenderContext.addLinkedFeature(linkedDataFilePath, "IM" + image.getName(), image); } List<Animation> animations = (List<Animation>) entry.getValue().get("animations"); for (Animation animation : animations) { blenderContext.addLinkedFeature( linkedDataFilePath, "AC" + animation.getName(), animation); } List<Camera> cameras = (List<Camera>) entry.getValue().get("cameras"); for (Camera camera : cameras) { blenderContext.addLinkedFeature(linkedDataFilePath, "CA" + camera.getName(), camera); } List<Light> lights = (List<Light>) entry.getValue().get("lights"); for (Light light : lights) { blenderContext.addLinkedFeature(linkedDataFilePath, "LA" + light.getName(), light); } Spatial sky = (Spatial) entry.getValue().get("sky"); if (sky != null) { blenderContext.addLinkedFeature(linkedDataFilePath, sky.getName(), sky); } List<Filter> filters = (List<Filter>) entry.getValue().get("filters"); for (Filter filter : filters) { blenderContext.addLinkedFeature(linkedDataFilePath, filter.getName(), filter); } } } else { LOGGER.log(Level.WARNING, "No features loaded from path: {0}.", path); } } Object result = blenderContext.getLinkedFeature(path, fullName); if (result == null) { LOGGER.log( Level.WARNING, "Could NOT find asset named {0} in the library of path: {1}.", new Object[] {nameOfFeatureToLoad, path}); } else { blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.STRUCTURE, id); blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.FEATURE, result); } return result; } else { LOGGER.warning("Library link points to nothing!"); } return null; }