Search in sources :

Example 6 with Modifier

use of com.jme3.scene.plugins.blender.modifiers.Modifier in project jmonkeyengine by jMonkeyEngine.

the class ArmatureModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Armature modifier is invalid! Cannot be applied to: {0}", node.getName());
    }
    if (modifying) {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            LOGGER.log(Level.FINE, "Applying armature modifier to: {0}", temporalMesh);
            LOGGER.fine("Creating map between bone name and its index.");
            for (int i = 0; i < skeleton.getBoneCount(); ++i) {
                Bone bone = skeleton.getBone(i);
                temporalMesh.addBoneIndex(bone.getName(), i);
            }
            temporalMesh.applyAfterMeshCreate(this);
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) Bone(com.jme3.animation.Bone)

Example 7 with Modifier

use of com.jme3.scene.plugins.blender.modifiers.Modifier in project jmonkeyengine by jMonkeyEngine.

the class ArrayModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Array modifier is invalid! Cannot be applied to: {0}", node.getName());
    } else {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            LOGGER.log(Level.FINE, "Applying array modifier to: {0}", temporalMesh);
            if (offset == null) {
                // the node will be repeated several times in the same place
                offset = new float[] { 0.0f, 0.0f, 0.0f };
            }
            if (scale == null) {
                // the node will be repeated several times in the same place
                scale = new float[] { 0.0f, 0.0f, 0.0f };
            } else {
                // getting bounding box
                temporalMesh.updateModelBound();
                BoundingVolume boundingVolume = temporalMesh.getWorldBound();
                if (boundingVolume instanceof BoundingBox) {
                    scale[0] *= ((BoundingBox) boundingVolume).getXExtent() * 2.0f;
                    scale[1] *= ((BoundingBox) boundingVolume).getYExtent() * 2.0f;
                    scale[2] *= ((BoundingBox) boundingVolume).getZExtent() * 2.0f;
                } else if (boundingVolume instanceof BoundingSphere) {
                    float radius = ((BoundingSphere) boundingVolume).getRadius();
                    scale[0] *= radius * 2.0f;
                    scale[1] *= radius * 2.0f;
                    scale[2] *= radius * 2.0f;
                } else {
                    throw new IllegalStateException("Unknown bounding volume type: " + boundingVolume.getClass().getName());
                }
            }
            // adding object's offset
            float[] objectOffset = new float[] { 0.0f, 0.0f, 0.0f };
            if (pOffsetObject != null && pOffsetObject.isNotNull()) {
                FileBlockHeader offsetObjectBlock = blenderContext.getFileBlock(pOffsetObject.getOldMemoryAddress());
                ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
                try {
                    // we take the structure in case the object was not yet loaded
                    Structure offsetStructure = offsetObjectBlock.getStructure(blenderContext);
                    Vector3f translation = objectHelper.getTransformation(offsetStructure, blenderContext).getTranslation();
                    objectOffset[0] = translation.x;
                    objectOffset[1] = translation.y;
                    objectOffset[2] = translation.z;
                } catch (BlenderFileException e) {
                    LOGGER.log(Level.WARNING, "Problems in blender file structure! Object offset cannot be applied! The problem: {0}", e.getMessage());
                }
            }
            // getting start and end caps
            MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
            TemporalMesh[] caps = new TemporalMesh[] { null, null };
            Pointer[] pCaps = new Pointer[] { pStartCap, pEndCap };
            for (int i = 0; i < pCaps.length; ++i) {
                if (pCaps[i].isNotNull()) {
                    FileBlockHeader capBlock = blenderContext.getFileBlock(pCaps[i].getOldMemoryAddress());
                    try {
                        // we take the structure in case the object was not yet loaded
                        Structure capStructure = capBlock.getStructure(blenderContext);
                        Pointer pMesh = (Pointer) capStructure.getFieldValue("data");
                        List<Structure> meshesArray = pMesh.fetchData();
                        caps[i] = meshHelper.toTemporalMesh(meshesArray.get(0), blenderContext);
                    } catch (BlenderFileException e) {
                        LOGGER.log(Level.WARNING, "Problems in blender file structure! Cap object cannot be applied! The problem: {0}", e.getMessage());
                    }
                }
            }
            Vector3f translationVector = new Vector3f(offset[0] + scale[0] + objectOffset[0], offset[1] + scale[1] + objectOffset[1], offset[2] + scale[2] + objectOffset[2]);
            if (blenderContext.getBlenderKey().isFixUpAxis()) {
                float y = translationVector.y;
                translationVector.y = translationVector.z;
                translationVector.z = y == 0 ? 0 : -y;
            }
            // getting/calculating repeats amount
            int count = 0;
            if (fittype == 0) {
                // Fixed count
                count = this.count - 1;
            } else if (fittype == 1) {
                // Fixed length
                float length = this.length;
                if (translationVector.length() > 0.0f) {
                    count = (int) (length / translationVector.length()) - 1;
                }
            } else if (fittype == 2) {
                // Fit curve
                throw new IllegalStateException("Fit curve should be transformed to Fixed Length array type!");
            } else {
                throw new IllegalStateException("Unknown fit type: " + fittype);
            }
            // adding translated nodes and caps
            Vector3f totalTranslation = new Vector3f(translationVector);
            if (count > 0) {
                TemporalMesh originalMesh = temporalMesh.clone();
                for (int i = 0; i < count; ++i) {
                    TemporalMesh clone = originalMesh.clone();
                    for (Vector3f v : clone.getVertices()) {
                        v.addLocal(totalTranslation);
                    }
                    temporalMesh.append(clone);
                    totalTranslation.addLocal(translationVector);
                }
            }
            if (caps[0] != null) {
                translationVector.multLocal(-1);
                TemporalMesh capsClone = caps[0].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(translationVector);
                }
                temporalMesh.append(capsClone);
            }
            if (caps[1] != null) {
                TemporalMesh capsClone = caps[1].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(totalTranslation);
                }
                temporalMesh.append(capsClone);
            }
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) BoundingSphere(com.jme3.bounding.BoundingSphere) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) BoundingVolume(com.jme3.bounding.BoundingVolume) Structure(com.jme3.scene.plugins.blender.file.Structure) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

Example 8 with Modifier

use of com.jme3.scene.plugins.blender.modifiers.Modifier in project jmonkeyengine by jMonkeyEngine.

the class ObjectHelper method toObject.

/**
     * This method reads the given structure and createn an object that
     * represents the data.
     * 
     * @param objectStructure
     *            the object's structure
     * @param blenderContext
     *            the blender context
     * @return blener's object representation or null if its type is excluded from loading
     * @throws BlenderFileException
     *             an exception is thrown when the given data is inapropriate
     */
public Object toObject(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
    Object loadedResult = blenderContext.getLoadedFeature(objectStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (loadedResult != null) {
        return loadedResult;
    }
    LOGGER.fine("Loading blender object.");
    if ("ID".equals(objectStructure.getType())) {
        Node object = (Node) this.loadLibrary(objectStructure);
        if (object.getParent() != null) {
            LOGGER.log(Level.FINEST, "Detaching object {0}, loaded from external file, from its parent.", object);
            object.getParent().detachChild(object);
        }
        return object;
    }
    int type = ((Number) objectStructure.getFieldValue("type")).intValue();
    ObjectType objectType = ObjectType.valueOf(type);
    LOGGER.log(Level.FINE, "Type of the object: {0}.", objectType);
    int lay = ((Number) objectStructure.getFieldValue("lay")).intValue();
    if ((lay & blenderContext.getBlenderKey().getLayersToLoad()) == 0) {
        LOGGER.fine("The layer this object is located in is not included in loading.");
        return null;
    }
    blenderContext.pushParent(objectStructure);
    String name = objectStructure.getName();
    LOGGER.log(Level.FINE, "Loading obejct: {0}", name);
    int restrictflag = ((Number) objectStructure.getFieldValue("restrictflag")).intValue();
    boolean visible = (restrictflag & 0x01) != 0;
    Pointer pParent = (Pointer) objectStructure.getFieldValue("parent");
    Object parent = blenderContext.getLoadedFeature(pParent.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (parent == null && pParent.isNotNull()) {
        Structure parentStructure = pParent.fetchData().get(0);
        parent = this.toObject(parentStructure, blenderContext);
    }
    Transform t = this.getTransformation(objectStructure, blenderContext);
    LOGGER.log(Level.FINE, "Importing object of type: {0}", objectType);
    Node result = null;
    try {
        switch(objectType) {
            case LATTICE:
            case METABALL:
            case TEXT:
            case WAVE:
                LOGGER.log(Level.WARNING, "{0} type is not supported but the node will be returned in order to keep parent - child relationship.", objectType);
            case EMPTY:
            case ARMATURE:
                // need to use an empty node to properly create
                // parent-children relationships between nodes
                result = new Node(name);
                break;
            case MESH:
                result = new Node(name);
                MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
                Pointer pMesh = (Pointer) objectStructure.getFieldValue("data");
                List<Structure> meshesArray = pMesh.fetchData();
                TemporalMesh temporalMesh = meshHelper.toTemporalMesh(meshesArray.get(0), blenderContext);
                if (temporalMesh != null) {
                    result.attachChild(temporalMesh);
                }
                break;
            case SURF:
            case CURVE:
                result = new Node(name);
                Pointer pCurve = (Pointer) objectStructure.getFieldValue("data");
                if (pCurve.isNotNull()) {
                    CurvesHelper curvesHelper = blenderContext.getHelper(CurvesHelper.class);
                    Structure curveData = pCurve.fetchData().get(0);
                    TemporalMesh curvesTemporalMesh = curvesHelper.toCurve(curveData, blenderContext);
                    if (curvesTemporalMesh != null) {
                        result.attachChild(curvesTemporalMesh);
                    }
                }
                break;
            case LAMP:
                Pointer pLamp = (Pointer) objectStructure.getFieldValue("data");
                if (pLamp.isNotNull()) {
                    LightHelper lightHelper = blenderContext.getHelper(LightHelper.class);
                    List<Structure> lampsArray = pLamp.fetchData();
                    Light light = lightHelper.toLight(lampsArray.get(0), blenderContext);
                    if (light == null) {
                        // probably some light type is not supported, just create a node so that we can maintain child-parent relationship for nodes
                        result = new Node(name);
                    } else {
                        result = new LightNode(name, light);
                    }
                }
                break;
            case CAMERA:
                Pointer pCamera = (Pointer) objectStructure.getFieldValue("data");
                if (pCamera.isNotNull()) {
                    CameraHelper cameraHelper = blenderContext.getHelper(CameraHelper.class);
                    List<Structure> camerasArray = pCamera.fetchData();
                    Camera camera = cameraHelper.toCamera(camerasArray.get(0), blenderContext);
                    if (camera == null) {
                        // just create a node so that we can maintain child-parent relationship for nodes
                        result = new Node(name);
                    } else {
                        result = new CameraNode(name, camera);
                    }
                }
                break;
            default:
                LOGGER.log(Level.WARNING, "Unsupported object type: {0}", type);
        }
        if (result != null) {
            LOGGER.fine("Storing loaded feature in blender context and applying markers (those will be removed before the final result is released).");
            Long oma = objectStructure.getOldMemoryAddress();
            blenderContext.addLoadedFeatures(oma, LoadedDataType.STRUCTURE, objectStructure);
            blenderContext.addLoadedFeatures(oma, LoadedDataType.FEATURE, result);
            blenderContext.addMarker(OMA_MARKER, result, objectStructure.getOldMemoryAddress());
            if (objectType == ObjectType.ARMATURE) {
                blenderContext.addMarker(ARMATURE_NODE_MARKER, result, Boolean.TRUE);
            }
            result.setLocalTransform(t);
            result.setCullHint(visible ? CullHint.Always : CullHint.Inherit);
            if (parent instanceof Node) {
                ((Node) parent).attachChild(result);
            }
            LOGGER.fine("Reading and applying object's modifiers.");
            ModifierHelper modifierHelper = blenderContext.getHelper(ModifierHelper.class);
            Collection<Modifier> modifiers = modifierHelper.readModifiers(objectStructure, blenderContext);
            for (Modifier modifier : modifiers) {
                modifier.apply(result, blenderContext);
            }
            if (result.getChildren() != null && result.getChildren().size() > 0) {
                if (result.getChildren().size() == 1 && result.getChild(0) instanceof TemporalMesh) {
                    LOGGER.fine("Converting temporal mesh into jme geometries.");
                    ((TemporalMesh) result.getChild(0)).toGeometries();
                }
                LOGGER.fine("Applying proper scale to the geometries.");
                for (Spatial child : result.getChildren()) {
                    if (child instanceof Geometry) {
                        this.flipMeshIfRequired((Geometry) child, child.getWorldScale());
                    }
                }
            }
            // I prefer do compute bounding box here than read it from the file
            result.updateModelBound();
            LOGGER.fine("Applying animations to the object if such are defined.");
            AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
            animationHelper.applyAnimations(result, blenderContext.getBlenderKey().getAnimationMatchMethod());
            LOGGER.fine("Loading constraints connected with this object.");
            ConstraintHelper constraintHelper = blenderContext.getHelper(ConstraintHelper.class);
            constraintHelper.loadConstraints(objectStructure, blenderContext);
            LOGGER.fine("Loading custom properties.");
            if (blenderContext.getBlenderKey().isLoadObjectProperties()) {
                Properties properties = this.loadProperties(objectStructure, blenderContext);
                // each value and set it to Spatial
                if (properties != null && properties.getValue() != null) {
                    this.applyProperties(result, properties);
                }
            }
        }
    } finally {
        blenderContext.popParent();
    }
    return result;
}
Also used : LightNode(com.jme3.scene.LightNode) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) Pointer(com.jme3.scene.plugins.blender.file.Pointer) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LightNode(com.jme3.scene.LightNode) Light(com.jme3.light.Light) Camera(com.jme3.renderer.Camera) Structure(com.jme3.scene.plugins.blender.file.Structure) Modifier(com.jme3.scene.plugins.blender.modifiers.Modifier) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) CullHint(com.jme3.scene.Spatial.CullHint) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) Transform(com.jme3.math.Transform) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

Example 9 with Modifier

use of com.jme3.scene.plugins.blender.modifiers.Modifier in project jmonkeyengine by jMonkeyEngine.

the class TemporalMesh method toGeometries.

/**
     * The mesh builds geometries from the mesh. The result is stored in the blender context
     * under the mesh's OMA.
     */
public void toGeometries() {
    LOGGER.log(Level.FINE, "Converting temporal mesh {0} to jme geometries.", name);
    List<Geometry> result = new ArrayList<Geometry>();
    MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
    Node parent = this.getParent();
    parent.detachChild(this);
    this.prepareFacesGeometry(result, meshHelper);
    this.prepareLinesGeometry(result, meshHelper);
    this.preparePointsGeometry(result, meshHelper);
    blenderContext.addLoadedFeatures(meshStructure.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
    for (Geometry geometry : result) {
        parent.attachChild(geometry);
    }
    for (Modifier modifier : postMeshCreationModifiers) {
        modifier.postMeshCreationApply(parent, blenderContext);
    }
}
Also used : Geometry(com.jme3.scene.Geometry) Node(com.jme3.scene.Node) ArrayList(java.util.ArrayList) Modifier(com.jme3.scene.plugins.blender.modifiers.Modifier)

Example 10 with Modifier

use of com.jme3.scene.plugins.blender.modifiers.Modifier in project jmonkeyengine by jMonkeyEngine.

the class MaskModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Mirror modifier is invalid! Cannot be applied to: {0}", node.getName());
    } else {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            List<String> vertexGroupsToRemove = new ArrayList<String>();
            if (vertexGroupName != null) {
                vertexGroupsToRemove.add(vertexGroupName);
            } else if (pArmatureObject != null && pArmatureObject.isNotNull()) {
                try {
                    Structure armatureObject = pArmatureObject.fetchData().get(0);
                    Structure armatureStructure = ((Pointer) armatureObject.getFieldValue("data")).fetchData().get(0);
                    List<Structure> bonebase = ((Structure) armatureStructure.getFieldValue("bonebase")).evaluateListBase();
                    vertexGroupsToRemove.addAll(this.readBoneNames(bonebase));
                } catch (BlenderFileException e) {
                    LOGGER.log(Level.SEVERE, "Cannot load armature object for the mask modifier. Cause: {0}", e.getLocalizedMessage());
                    LOGGER.log(Level.SEVERE, "Mask modifier will NOT be applied to node named: {0}", node.getName());
                }
            } else {
                // if the mesh has no vertex groups then remove all verts
                // if the mesh has at least one vertex group - then do nothing
                // I have no idea why we should do that, but blender works this way
                Set<String> vertexGroupNames = new HashSet<String>();
                for (Map<String, Float> groups : temporalMesh.getVertexGroups()) {
                    vertexGroupNames.addAll(groups.keySet());
                }
                if (vertexGroupNames.size() == 0 && !invertMask || vertexGroupNames.size() > 0 && invertMask) {
                    temporalMesh.clear();
                }
            }
            if (vertexGroupsToRemove.size() > 0) {
                List<Integer> vertsToBeRemoved = new ArrayList<Integer>();
                for (int i = 0; i < temporalMesh.getVertexCount(); ++i) {
                    Map<String, Float> vertexGroups = temporalMesh.getVertexGroups().get(i);
                    boolean hasVertexGroup = false;
                    if (vertexGroups != null) {
                        for (String groupName : vertexGroupsToRemove) {
                            Float weight = vertexGroups.get(groupName);
                            if (weight != null && weight > 0) {
                                hasVertexGroup = true;
                                break;
                            }
                        }
                    }
                    if (!hasVertexGroup && !invertMask || hasVertexGroup && invertMask) {
                        vertsToBeRemoved.add(i);
                    }
                }
                Collections.reverse(vertsToBeRemoved);
                for (Integer vertexIndex : vertsToBeRemoved) {
                    this.removeVertexAt(vertexIndex, temporalMesh);
                }
            }
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Point(com.jme3.scene.plugins.blender.meshes.Point) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) ArrayList(java.util.ArrayList) List(java.util.List) Structure(com.jme3.scene.plugins.blender.file.Structure) Map(java.util.Map)

Aggregations

Structure (com.jme3.scene.plugins.blender.file.Structure)7 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)7 Pointer (com.jme3.scene.plugins.blender.file.Pointer)5 ArrayList (java.util.ArrayList)4 Geometry (com.jme3.scene.Geometry)3 Node (com.jme3.scene.Node)3 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)3 ParticleEmitter (com.jme3.effect.ParticleEmitter)2 EmitterMeshVertexShape (com.jme3.effect.shapes.EmitterMeshVertexShape)2 Vector3f (com.jme3.math.Vector3f)2 Spatial (com.jme3.scene.Spatial)2 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)2 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)2 Modifier (com.jme3.scene.plugins.blender.modifiers.Modifier)2 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)2 HashSet (java.util.HashSet)2 Bone (com.jme3.animation.Bone)1 BoundingBox (com.jme3.bounding.BoundingBox)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1 BoundingVolume (com.jme3.bounding.BoundingVolume)1