Search in sources :

Example 31 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class TemporalMesh method clone.

@Override
public TemporalMesh clone() {
    try {
        TemporalMesh result = new TemporalMesh(meshStructure, blenderContext, false);
        result.name = name;
        for (Vector3f v : vertices) {
            result.vertices.add(v.clone());
        }
        for (Vector3f n : normals) {
            result.normals.add(n.clone());
        }
        for (Map<String, Float> group : vertexGroups) {
            result.vertexGroups.add(new HashMap<String, Float>(group));
        }
        for (byte[] vertColor : verticesColors) {
            result.verticesColors.add(vertColor.clone());
        }
        result.materials = materials;
        result.properties = properties;
        result.boneIndexes.putAll(boneIndexes);
        result.postMeshCreationModifiers.addAll(postMeshCreationModifiers);
        for (Face face : faces) {
            result.faces.add(face.clone());
        }
        for (Edge edge : edges) {
            result.edges.add(edge.clone());
        }
        for (Point point : points) {
            result.points.add(point.clone());
        }
        result.rebuildIndexesMappings();
        return result;
    } catch (BlenderFileException e) {
        LOGGER.log(Level.SEVERE, "Error while cloning the temporal mesh: {0}. Returning null.", e.getLocalizedMessage());
    }
    return null;
}
Also used : Vector3f(com.jme3.math.Vector3f) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException)

Example 32 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class ArmatureModifier method buildBones.

private void buildBones(Long armatureObjectOMA, Structure boneStructure, Bone parent, List<Bone> result, Long spatialOMA, BlenderContext blenderContext) throws BlenderFileException {
    BoneContext bc = new BoneContext(armatureObjectOMA, boneStructure, blenderContext);
    bc.buildBone(result, spatialOMA, blenderContext);
}
Also used : BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext)

Example 33 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException 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)

Example 34 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class TextureHelper method readTextureData.

/**
     * Reads the texture data from the given material or sky structure.
     * @param structure
     *            the structure of material or sky
     * @param diffuseColorArray
     *            array of diffuse colors
     * @param skyTexture
     *            indicates it we're going to read sky texture or not
     * @return a list of combined textures
     * @throws BlenderFileException
     *             an exception is thrown when problems with reading the blend file occur
     */
@SuppressWarnings("unchecked")
public List<CombinedTexture> readTextureData(Structure structure, float[] diffuseColorArray, boolean skyTexture) throws BlenderFileException {
    DynamicArray<Pointer> mtexsArray = (DynamicArray<Pointer>) structure.getFieldValue("mtex");
    int separatedTextures = skyTexture ? 0 : ((Number) structure.getFieldValue("septex")).intValue();
    List<TextureData> texturesList = new ArrayList<TextureData>();
    for (int i = 0; i < mtexsArray.getTotalSize(); ++i) {
        Pointer p = mtexsArray.get(i);
        if (p.isNotNull() && (separatedTextures & 1 << i) == 0) {
            TextureData textureData = new TextureData();
            textureData.mtex = p.fetchData().get(0);
            textureData.uvCoordinatesType = skyTexture ? UVCoordinatesType.TEXCO_ORCO.blenderValue : ((Number) textureData.mtex.getFieldValue("texco")).intValue();
            textureData.projectionType = ((Number) textureData.mtex.getFieldValue("mapping")).intValue();
            textureData.uvCoordinatesName = textureData.mtex.getFieldValue("uvName").toString();
            if (textureData.uvCoordinatesName != null && textureData.uvCoordinatesName.trim().length() == 0) {
                textureData.uvCoordinatesName = null;
            }
            Pointer pTex = (Pointer) textureData.mtex.getFieldValue("tex");
            if (pTex.isNotNull()) {
                Structure tex = pTex.fetchData().get(0);
                textureData.textureStructure = tex;
                texturesList.add(textureData);
            }
        }
    }
    LOGGER.info("Loading model's textures.");
    List<CombinedTexture> loadedTextures = new ArrayList<CombinedTexture>();
    if (blenderContext.getBlenderKey().isOptimiseTextures()) {
        LOGGER.fine("Optimising the useage of model's textures.");
        Map<Number, List<TextureData>> textureDataMap = this.sortTextures(texturesList);
        for (Entry<Number, List<TextureData>> entry : textureDataMap.entrySet()) {
            if (entry.getValue().size() > 0) {
                CombinedTexture combinedTexture = new CombinedTexture(entry.getKey().intValue(), !skyTexture);
                for (TextureData textureData : entry.getValue()) {
                    int texflag = ((Number) textureData.mtex.getFieldValue("texflag")).intValue();
                    boolean negateTexture = (texflag & 0x04) != 0;
                    Texture texture = this.getTexture(textureData.textureStructure, textureData.mtex, blenderContext);
                    if (texture != null) {
                        int blendType = ((Number) textureData.mtex.getFieldValue("blendtype")).intValue();
                        float[] color = new float[] { ((Number) textureData.mtex.getFieldValue("r")).floatValue(), ((Number) textureData.mtex.getFieldValue("g")).floatValue(), ((Number) textureData.mtex.getFieldValue("b")).floatValue() };
                        float colfac = ((Number) textureData.mtex.getFieldValue("colfac")).floatValue();
                        TextureBlender textureBlender = TextureBlenderFactory.createTextureBlender(texture.getImage().getFormat(), texflag, negateTexture, blendType, diffuseColorArray, color, colfac);
                        combinedTexture.add(texture, textureBlender, textureData.uvCoordinatesType, textureData.projectionType, textureData.textureStructure, textureData.uvCoordinatesName, blenderContext);
                    }
                }
                if (combinedTexture.getTexturesCount() > 0) {
                    loadedTextures.add(combinedTexture);
                }
            }
        }
    } else {
        LOGGER.fine("No textures optimisation applied.");
        int[] mappings = new int[] { MaterialContext.MTEX_COL, MaterialContext.MTEX_NOR, MaterialContext.MTEX_EMIT, MaterialContext.MTEX_SPEC, MaterialContext.MTEX_ALPHA, MaterialContext.MTEX_AMB };
        for (TextureData textureData : texturesList) {
            Texture texture = this.getTexture(textureData.textureStructure, textureData.mtex, blenderContext);
            if (texture != null) {
                Number mapto = (Number) textureData.mtex.getFieldValue("mapto");
                int texflag = ((Number) textureData.mtex.getFieldValue("texflag")).intValue();
                boolean negateTexture = (texflag & 0x04) != 0;
                for (int i = 0; i < mappings.length; ++i) {
                    if ((mappings[i] & mapto.intValue()) != 0) {
                        CombinedTexture combinedTexture = new CombinedTexture(mappings[i], !skyTexture);
                        int blendType = ((Number) textureData.mtex.getFieldValue("blendtype")).intValue();
                        float[] color = new float[] { ((Number) textureData.mtex.getFieldValue("r")).floatValue(), ((Number) textureData.mtex.getFieldValue("g")).floatValue(), ((Number) textureData.mtex.getFieldValue("b")).floatValue() };
                        float colfac = ((Number) textureData.mtex.getFieldValue("colfac")).floatValue();
                        TextureBlender textureBlender = TextureBlenderFactory.createTextureBlender(texture.getImage().getFormat(), texflag, negateTexture, blendType, diffuseColorArray, color, colfac);
                        combinedTexture.add(texture, textureBlender, textureData.uvCoordinatesType, textureData.projectionType, textureData.textureStructure, textureData.uvCoordinatesName, blenderContext);
                        if (combinedTexture.getTexturesCount() > 0) {
                            // the added texture might not have been accepted (if for example loading generated textures is disabled)
                            loadedTextures.add(combinedTexture);
                        }
                    }
                }
            }
        }
    }
    return loadedTextures;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) TextureBlender(com.jme3.scene.plugins.blender.textures.blending.TextureBlender) Texture(com.jme3.texture.Texture) DynamicArray(com.jme3.scene.plugins.blender.file.DynamicArray) ArrayList(java.util.ArrayList) List(java.util.List) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 35 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class TextureHelper method loadImageAsTexture.

/**
     * This class returns a texture read from the file or from packed blender
     * data.
     * 
     * @param imageStructure
     *            image structure filled with data
     * @param imaflag
     *            the image flag
     * @param blenderContext
     *            the blender context
     * @return the texture that can be used by JME engine
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is
     *             somehow invalid or corrupted
     */
public Texture loadImageAsTexture(Structure imageStructure, int imaflag, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Fetching texture with OMA = {0}", imageStructure.getOldMemoryAddress());
    Texture result = null;
    Image im = (Image) blenderContext.getLoadedFeature(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    // if (im == null) {  HACK force reaload always, as constructor in else case is destroying the TextureKeys!
    if ("ID".equals(imageStructure.getType())) {
        LOGGER.fine("Loading texture from external blend file.");
        result = (Texture) this.loadLibrary(imageStructure);
    } else {
        String texturePath = imageStructure.getFieldValue("name").toString();
        Pointer pPackedFile = (Pointer) imageStructure.getFieldValue("packedfile");
        if (pPackedFile.isNull()) {
            LOGGER.log(Level.FINE, "Reading texture from file: {0}", texturePath);
            result = this.loadImageFromFile(texturePath, imaflag, blenderContext);
        } else {
            LOGGER.fine("Packed texture. Reading directly from the blend file!");
            Structure packedFile = pPackedFile.fetchData().get(0);
            Pointer pData = (Pointer) packedFile.getFieldValue("data");
            FileBlockHeader dataFileBlock = blenderContext.getFileBlock(pData.getOldMemoryAddress());
            blenderContext.getInputStream().setPosition(dataFileBlock.getBlockPosition());
            // Should the texture be flipped? It works for sinbad ..
            result = new Texture2D(new ImageLoader().loadImage(blenderContext.getInputStream(), dataFileBlock.getBlockPosition(), true));
        }
    }
    if (result != null) {
        // render result is not being loaded
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.STRUCTURE, imageStructure);
        blenderContext.addLoadedFeatures(imageStructure.getOldMemoryAddress(), LoadedDataType.FEATURE, result.getImage());
        result.setName(imageStructure.getName());
    }
    return result;
}
Also used : Texture2D(com.jme3.texture.Texture2D) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Image(com.jme3.texture.Image) Structure(com.jme3.scene.plugins.blender.file.Structure) Texture(com.jme3.texture.Texture)

Aggregations

Structure (com.jme3.scene.plugins.blender.file.Structure)31 Pointer (com.jme3.scene.plugins.blender.file.Pointer)26 ArrayList (java.util.ArrayList)19 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)10 Vector3f (com.jme3.math.Vector3f)7 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)7 List (java.util.List)7 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)6 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Map (java.util.Map)5 Node (com.jme3.scene.Node)4 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)4 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)4 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)4 Texture (com.jme3.texture.Texture)4 HashMap (java.util.HashMap)4 BlenderKey (com.jme3.asset.BlenderKey)3 Light (com.jme3.light.Light)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Spline (com.jme3.math.Spline)3