Search in sources :

Example 1 with MaterialHelper

use of com.jme3.scene.plugins.blender.materials.MaterialHelper in project jmonkeyengine by jMonkeyEngine.

the class ParticlesModifier method postMeshCreationApply.

@Override
public void postMeshCreationApply(Node node, BlenderContext blenderContext) {
    LOGGER.log(Level.FINE, "Applying particles modifier to: {0}", node);
    MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
    ParticleEmitter emitter = particleEmitter.clone();
    // veryfying the alpha function for particles' texture
    Integer alphaFunction = MaterialHelper.ALPHA_MASK_HYPERBOLE;
    char nameSuffix = emitter.getName().charAt(emitter.getName().length() - 1);
    if (nameSuffix == 'B' || nameSuffix == 'N') {
        alphaFunction = MaterialHelper.ALPHA_MASK_NONE;
    }
    // removing the type suffix from the name
    emitter.setName(emitter.getName().substring(0, emitter.getName().length() - 1));
    // applying emitter shape
    EmitterShape emitterShape = emitter.getShape();
    List<Mesh> meshes = new ArrayList<Mesh>();
    for (Spatial spatial : node.getChildren()) {
        if (spatial instanceof Geometry) {
            Mesh mesh = ((Geometry) spatial).getMesh();
            if (mesh != null) {
                meshes.add(mesh);
                Material material = materialHelper.getParticlesMaterial(((Geometry) spatial).getMaterial(), alphaFunction, blenderContext);
                // TODO: divide into several pieces
                emitter.setMaterial(material);
            }
        }
    }
    if (meshes.size() > 0 && emitterShape instanceof EmitterMeshVertexShape) {
        ((EmitterMeshVertexShape) emitterShape).setMeshes(meshes);
    }
    node.attachChild(emitter);
}
Also used : ParticleEmitter(com.jme3.effect.ParticleEmitter) Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) EmitterShape(com.jme3.effect.shapes.EmitterShape) ArrayList(java.util.ArrayList) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material) EmitterMeshVertexShape(com.jme3.effect.shapes.EmitterMeshVertexShape)

Example 2 with MaterialHelper

use of com.jme3.scene.plugins.blender.materials.MaterialHelper in project jmonkeyengine by jMonkeyEngine.

the class MeshHelper method toTemporalMesh.

/**
     * Converts the mesh structure into temporal mesh.
     * The temporal mesh is stored in blender context and here always a clone is being returned because the mesh might
     * be modified by modifiers.
     * 
     * @param meshStructure
     *            the mesh structure
     * @param blenderContext
     *            the blender context
     * @return temporal mesh read from the given structure
     * @throws BlenderFileException
     *             an exception is thrown when problems with reading blend file occur
     */
public TemporalMesh toTemporalMesh(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading temporal mesh named: {0}.", meshStructure.getName());
    TemporalMesh temporalMesh = (TemporalMesh) blenderContext.getLoadedFeature(meshStructure.getOldMemoryAddress(), LoadedDataType.TEMPORAL_MESH);
    if (temporalMesh != null) {
        LOGGER.fine("The mesh is already loaded. Returning its clone.");
        return temporalMesh.clone();
    }
    if ("ID".equals(meshStructure.getType())) {
        LOGGER.fine("Loading mesh from external blend file.");
        return (TemporalMesh) this.loadLibrary(meshStructure);
    }
    String name = meshStructure.getName();
    LOGGER.log(Level.FINE, "Reading mesh: {0}.", name);
    temporalMesh = new TemporalMesh(meshStructure, blenderContext);
    LOGGER.fine("Loading materials.");
    MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
    temporalMesh.setMaterials(materialHelper.getMaterials(meshStructure, blenderContext));
    LOGGER.fine("Reading custom properties.");
    Properties properties = this.loadProperties(meshStructure, blenderContext);
    temporalMesh.setProperties(properties);
    blenderContext.addLoadedFeatures(meshStructure.getOldMemoryAddress(), LoadedDataType.STRUCTURE, meshStructure);
    blenderContext.addLoadedFeatures(meshStructure.getOldMemoryAddress(), LoadedDataType.TEMPORAL_MESH, temporalMesh);
    return temporalMesh.clone();
}
Also used : MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) Properties(com.jme3.scene.plugins.blender.objects.Properties)

Example 3 with MaterialHelper

use of com.jme3.scene.plugins.blender.materials.MaterialHelper in project jmonkeyengine by jMonkeyEngine.

the class MaterialHelper method getMaterials.

/**
     * This method returns the table of materials connected to the specified structure. The given structure can be of any type (ie. mesh or
     * curve) but needs to have 'mat' field/
     * 
     * @param structureWithMaterials
     *            the structure containing the mesh data
     * @param blenderContext
     *            the blender context
     * @return a list of vertices colors, each color belongs to a single vertex
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is somehow invalid or corrupted
     */
public MaterialContext[] getMaterials(Structure structureWithMaterials, BlenderContext blenderContext) throws BlenderFileException {
    Pointer ppMaterials = (Pointer) structureWithMaterials.getFieldValue("mat");
    MaterialContext[] materials = null;
    if (ppMaterials.isNotNull()) {
        List<Structure> materialStructures = ppMaterials.fetchData();
        if (materialStructures != null && materialStructures.size() > 0) {
            MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
            materials = new MaterialContext[materialStructures.size()];
            int i = 0;
            for (Structure s : materialStructures) {
                materials[i++] = s == null ? null : materialHelper.toMaterialContext(s, blenderContext);
            }
        }
    }
    return materials;
}
Also used : Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 4 with MaterialHelper

use of com.jme3.scene.plugins.blender.materials.MaterialHelper in project jmonkeyengine by jMonkeyEngine.

the class AbstractTextureBlender method blendHSV.

/**
     * The method that performs the ramp blending.
     * 
     * @param type
     *            the blend type
     * @param materialRGB
     *            the rgb value of the material, here the result is stored too
     * @param fac
     *            color affection factor
     * @param pixelColor
     *            the texture color
     * @param blenderContext
     *            the blender context
     */
protected void blendHSV(int type, float[] materialRGB, float fac, float[] pixelColor, BlenderContext blenderContext) {
    float oneMinusFactor = 1.0f - fac;
    MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
    switch(type) {
        case MTEX_BLEND_HUE:
            {
                // FIXME: not working well for image textures (works fine for generated textures)
                float[] colorTransformResult = new float[3];
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colorTransformResult);
                if (colorTransformResult[0] != 0.0f) {
                    float colH = colorTransformResult[0];
                    materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], colorTransformResult);
                    materialHelper.hsvToRgb(colH, colorTransformResult[1], colorTransformResult[2], colorTransformResult);
                    materialRGB[0] = oneMinusFactor * materialRGB[0] + fac * colorTransformResult[0];
                    materialRGB[1] = oneMinusFactor * materialRGB[1] + fac * colorTransformResult[1];
                    materialRGB[2] = oneMinusFactor * materialRGB[2] + fac * colorTransformResult[2];
                }
                break;
            }
        case MTEX_BLEND_SAT:
            {
                float[] colorTransformResult = new float[3];
                materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], colorTransformResult);
                float h = colorTransformResult[0];
                float s = colorTransformResult[1];
                float v = colorTransformResult[2];
                if (s != 0.0f) {
                    materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colorTransformResult);
                    materialHelper.hsvToRgb(h, oneMinusFactor * s + fac * colorTransformResult[1], v, materialRGB);
                }
                break;
            }
        case MTEX_BLEND_VAL:
            {
                float[] rgbToHsv = new float[3];
                float[] colToHsv = new float[3];
                materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], rgbToHsv);
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colToHsv);
                materialHelper.hsvToRgb(rgbToHsv[0], rgbToHsv[1], oneMinusFactor * rgbToHsv[2] + fac * colToHsv[2], materialRGB);
                break;
            }
        case MTEX_BLEND_COLOR:
            {
                // FIXME: not working well for image textures (works fine for generated textures)
                float[] rgbToHsv = new float[3];
                float[] colToHsv = new float[3];
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colToHsv);
                if (colToHsv[2] != 0) {
                    materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], rgbToHsv);
                    materialHelper.hsvToRgb(colToHsv[0], colToHsv[1], rgbToHsv[2], rgbToHsv);
                    materialRGB[0] = oneMinusFactor * materialRGB[0] + fac * rgbToHsv[0];
                    materialRGB[1] = oneMinusFactor * materialRGB[1] + fac * rgbToHsv[1];
                    materialRGB[2] = oneMinusFactor * materialRGB[2] + fac * rgbToHsv[2];
                }
                break;
            }
        default:
            throw new IllegalStateException("Unknown ramp type: " + type);
    }
}
Also used : MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper)

Example 5 with MaterialHelper

use of com.jme3.scene.plugins.blender.materials.MaterialHelper in project jmonkeyengine by jMonkeyEngine.

the class BlenderLoader method load.

@Override
public Spatial load(AssetInfo assetInfo) throws IOException {
    try {
        BlenderContext blenderContext = this.setup(assetInfo);
        AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
        animationHelper.loadAnimations();
        BlenderKey blenderKey = blenderContext.getBlenderKey();
        LoadedFeatures loadedFeatures = new LoadedFeatures();
        for (FileBlockHeader block : blenderContext.getBlocks()) {
            switch(block.getCode()) {
                case BLOCK_OB00:
                    ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
                    Node object = (Node) objectHelper.toObject(block.getStructure(blenderContext), blenderContext);
                    if (LOGGER.isLoggable(Level.FINE)) {
                        LOGGER.log(Level.FINE, "{0}: {1}--> {2}", new Object[] { object.getName(), object.getLocalTranslation().toString(), object.getParent() == null ? "null" : object.getParent().getName() });
                    }
                    if (object.getParent() == null) {
                        loadedFeatures.objects.add(object);
                    }
                    if (object instanceof LightNode && ((LightNode) object).getLight() != null) {
                        loadedFeatures.lights.add(((LightNode) object).getLight());
                    } else if (object instanceof CameraNode && ((CameraNode) object).getCamera() != null) {
                        loadedFeatures.cameras.add(((CameraNode) object).getCamera());
                    }
                    break;
                case // Scene
                BLOCK_SC00:
                    loadedFeatures.sceneBlocks.add(block);
                    break;
                case // Material
                BLOCK_MA00:
                    MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
                    MaterialContext materialContext = materialHelper.toMaterialContext(block.getStructure(blenderContext), blenderContext);
                    loadedFeatures.materials.add(materialContext);
                    break;
                case // Mesh
                BLOCK_ME00:
                    MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
                    TemporalMesh temporalMesh = meshHelper.toTemporalMesh(block.getStructure(blenderContext), blenderContext);
                    loadedFeatures.meshes.add(temporalMesh);
                    break;
                case // Image
                BLOCK_IM00:
                    TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class);
                    Texture image = textureHelper.loadImageAsTexture(block.getStructure(blenderContext), 0, blenderContext);
                    if (image != null && image.getImage() != null) {
                        // render results are stored as images but are not being loaded
                        loadedFeatures.images.add(image);
                    }
                    break;
                case BLOCK_TE00:
                    Structure textureStructure = block.getStructure(blenderContext);
                    int type = ((Number) textureStructure.getFieldValue("type")).intValue();
                    if (type == TextureHelper.TEX_IMAGE) {
                        TextureHelper texHelper = blenderContext.getHelper(TextureHelper.class);
                        Texture texture = texHelper.getTexture(textureStructure, null, blenderContext);
                        if (texture != null) {
                            // null is returned when texture has no image
                            loadedFeatures.textures.add(texture);
                        }
                    } else {
                        LOGGER.fine("Only image textures can be loaded as unlinked assets. Generated textures will be applied to an existing object.");
                    }
                    break;
                case // World
                BLOCK_WO00:
                    LandscapeHelper landscapeHelper = blenderContext.getHelper(LandscapeHelper.class);
                    Structure worldStructure = block.getStructure(blenderContext);
                    String worldName = worldStructure.getName();
                    if (blenderKey.getUsedWorld() == null || blenderKey.getUsedWorld().equals(worldName)) {
                        Light ambientLight = landscapeHelper.toAmbientLight(worldStructure);
                        if (ambientLight != null) {
                            loadedFeatures.objects.add(new LightNode(null, ambientLight));
                            loadedFeatures.lights.add(ambientLight);
                        }
                        loadedFeatures.sky = landscapeHelper.toSky(worldStructure);
                        loadedFeatures.backgroundColor = landscapeHelper.toBackgroundColor(worldStructure);
                        Filter fogFilter = landscapeHelper.toFog(worldStructure);
                        if (fogFilter != null) {
                            loadedFeatures.filters.add(landscapeHelper.toFog(worldStructure));
                        }
                    }
                    break;
                case BLOCK_AC00:
                    LOGGER.fine("Loading unlinked animations is not yet supported!");
                    break;
                default:
                    LOGGER.log(Level.FINEST, "Ommiting the block: {0}.", block.getCode());
            }
        }
        LOGGER.fine("Baking constraints after every feature is loaded.");
        ConstraintHelper constraintHelper = blenderContext.getHelper(ConstraintHelper.class);
        constraintHelper.bakeConstraints(blenderContext);
        LOGGER.fine("Loading scenes and attaching them to the root object.");
        for (FileBlockHeader sceneBlock : loadedFeatures.sceneBlocks) {
            loadedFeatures.scenes.add(this.toScene(sceneBlock.getStructure(blenderContext), blenderContext));
        }
        LOGGER.fine("Creating the root node of the model and applying loaded nodes of the scene and loaded features to it.");
        Node modelRoot = new Node(blenderKey.getName());
        for (Node scene : loadedFeatures.scenes) {
            modelRoot.attachChild(scene);
        }
        if (blenderKey.isLoadUnlinkedAssets()) {
            LOGGER.fine("Setting loaded content as user data in resulting sptaial.");
            Map<String, Map<String, Object>> linkedData = new HashMap<String, Map<String, Object>>();
            Map<String, Object> thisFileData = new HashMap<String, Object>();
            thisFileData.put("scenes", loadedFeatures.scenes == null ? new ArrayList<Object>() : loadedFeatures.scenes);
            thisFileData.put("objects", loadedFeatures.objects == null ? new ArrayList<Object>() : loadedFeatures.objects);
            thisFileData.put("meshes", loadedFeatures.meshes == null ? new ArrayList<Object>() : loadedFeatures.meshes);
            thisFileData.put("materials", loadedFeatures.materials == null ? new ArrayList<Object>() : loadedFeatures.materials);
            thisFileData.put("textures", loadedFeatures.textures == null ? new ArrayList<Object>() : loadedFeatures.textures);
            thisFileData.put("images", loadedFeatures.images == null ? new ArrayList<Object>() : loadedFeatures.images);
            thisFileData.put("animations", loadedFeatures.animations == null ? new ArrayList<Object>() : loadedFeatures.animations);
            thisFileData.put("cameras", loadedFeatures.cameras == null ? new ArrayList<Object>() : loadedFeatures.cameras);
            thisFileData.put("lights", loadedFeatures.lights == null ? new ArrayList<Object>() : loadedFeatures.lights);
            thisFileData.put("filters", loadedFeatures.filters == null ? new ArrayList<Object>() : loadedFeatures.filters);
            thisFileData.put("backgroundColor", loadedFeatures.backgroundColor);
            thisFileData.put("sky", loadedFeatures.sky);
            linkedData.put("this", thisFileData);
            linkedData.putAll(blenderContext.getLinkedFeatures());
            modelRoot.setUserData("linkedData", linkedData);
        }
        return modelRoot;
    } catch (BlenderFileException e) {
        throw new IOException(e.getLocalizedMessage(), e);
    } catch (Exception e) {
        throw new IOException("Unexpected importer exception occured: " + e.getLocalizedMessage(), e);
    } finally {
        this.clear(assetInfo);
    }
}
Also used : HashMap(java.util.HashMap) LightNode(com.jme3.scene.LightNode) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) ArrayList(java.util.ArrayList) Texture(com.jme3.texture.Texture) LandscapeHelper(com.jme3.scene.plugins.blender.landscape.LandscapeHelper) LightNode(com.jme3.scene.LightNode) Light(com.jme3.light.Light) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) BlenderKey(com.jme3.asset.BlenderKey) Structure(com.jme3.scene.plugins.blender.file.Structure) ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) IOException(java.io.IOException) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) Filter(com.jme3.post.Filter) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) MaterialContext(com.jme3.scene.plugins.blender.materials.MaterialContext) HashMap(java.util.HashMap) Map(java.util.Map) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

Aggregations

MaterialHelper (com.jme3.scene.plugins.blender.materials.MaterialHelper)5 ArrayList (java.util.ArrayList)3 BlenderKey (com.jme3.asset.BlenderKey)2 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)2 ConstraintHelper (com.jme3.scene.plugins.blender.constraints.ConstraintHelper)2 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)2 Structure (com.jme3.scene.plugins.blender.file.Structure)2 LandscapeHelper (com.jme3.scene.plugins.blender.landscape.LandscapeHelper)2 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)2 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)2 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)2 TextureHelper (com.jme3.scene.plugins.blender.textures.TextureHelper)2 ModelKey (com.jme3.asset.ModelKey)1 ParticleEmitter (com.jme3.effect.ParticleEmitter)1 EmitterMeshVertexShape (com.jme3.effect.shapes.EmitterMeshVertexShape)1 EmitterShape (com.jme3.effect.shapes.EmitterShape)1 Light (com.jme3.light.Light)1 Material (com.jme3.material.Material)1 Filter (com.jme3.post.Filter)1 CameraNode (com.jme3.scene.CameraNode)1