Search in sources :

Example 1 with FbxTexture

use of com.jme3.scene.plugins.fbx.objects.FbxTexture in project jmonkeyengine by jMonkeyEngine.

the class FbxMaterial method toJmeObject.

@Override
protected Material toJmeObject() {
    ColorRGBA ambient = null;
    ColorRGBA diffuse = null;
    ColorRGBA specular = null;
    ColorRGBA transp = null;
    ColorRGBA emissive = null;
    float shininess = 1f;
    boolean separateTexCoord = false;
    Texture diffuseMap = null;
    Texture specularMap = null;
    Texture normalMap = null;
    Texture transpMap = null;
    Texture emitMap = null;
    Texture aoMap = null;
    FbxTexture fbxDiffuseMap = null;
    Object diffuseColor = properties.getProperty("DiffuseColor");
    if (diffuseColor != null) {
        if (diffuseColor instanceof ColorRGBA) {
            diffuse = ((ColorRGBA) diffuseColor).clone();
        } else if (diffuseColor instanceof FbxTexture) {
            FbxTexture tex = (FbxTexture) diffuseColor;
            fbxDiffuseMap = tex;
            diffuseMap = tex.getJmeObject();
            diffuseMap.getImage().setColorSpace(ColorSpace.sRGB);
        }
    }
    Object diffuseFactor = properties.getProperty("DiffuseFactor");
    if (diffuseFactor != null && diffuseFactor instanceof Float) {
        float factor = (Float) diffuseFactor;
        if (diffuse != null) {
            multRGB(diffuse, factor);
        } else {
            diffuse = new ColorRGBA(factor, factor, factor, 1f);
        }
    }
    Object specularColor = properties.getProperty("SpecularColor");
    if (specularColor != null) {
        if (specularColor instanceof ColorRGBA) {
            specular = ((ColorRGBA) specularColor).clone();
        } else if (specularColor instanceof FbxTexture) {
            FbxTexture tex = (FbxTexture) specularColor;
            specularMap = tex.getJmeObject();
            specularMap.getImage().setColorSpace(ColorSpace.sRGB);
        }
    }
    Object specularFactor = properties.getProperty("SpecularFactor");
    if (specularFactor != null && specularFactor instanceof Float) {
        float factor = (Float) specularFactor;
        if (specular != null) {
            multRGB(specular, factor);
        } else {
            specular = new ColorRGBA(factor, factor, factor, 1f);
        }
    }
    Object transparentColor = properties.getProperty("TransparentColor");
    if (transparentColor != null) {
        if (transparentColor instanceof ColorRGBA) {
            transp = ((ColorRGBA) transparentColor).clone();
        } else if (transparentColor instanceof FbxTexture) {
            FbxTexture tex = (FbxTexture) transparentColor;
            transpMap = tex.getJmeObject();
            transpMap.getImage().setColorSpace(ColorSpace.sRGB);
        }
    }
    Object transparencyFactor = properties.getProperty("TransparencyFactor");
    if (transparencyFactor != null && transparencyFactor instanceof Float) {
        float factor = (Float) transparencyFactor;
        if (transp != null) {
            transp.a *= factor;
        } else {
            transp = new ColorRGBA(1f, 1f, 1f, factor);
        }
    }
    Object emissiveColor = properties.getProperty("EmissiveColor");
    if (emissiveColor != null) {
        if (emissiveColor instanceof ColorRGBA) {
            emissive = ((ColorRGBA) emissiveColor).clone();
        } else if (emissiveColor instanceof FbxTexture) {
            FbxTexture tex = (FbxTexture) emissiveColor;
            emitMap = tex.getJmeObject();
            emitMap.getImage().setColorSpace(ColorSpace.sRGB);
        }
    }
    Object emissiveFactor = properties.getProperty("EmissiveFactor");
    if (emissiveFactor != null && emissiveFactor instanceof Float) {
        float factor = (Float) emissiveFactor;
        if (emissive != null) {
            multRGB(emissive, factor);
        } else {
            emissive = new ColorRGBA(factor, factor, factor, 1f);
        }
    }
    Object ambientColor = properties.getProperty("AmbientColor");
    if (ambientColor != null && ambientColor instanceof ColorRGBA) {
        ambient = ((ColorRGBA) ambientColor).clone();
    }
    Object ambientFactor = properties.getProperty("AmbientFactor");
    if (ambientFactor != null && ambientFactor instanceof Float) {
        float factor = (Float) ambientFactor;
        if (ambient != null) {
            multRGB(ambient, factor);
        } else {
            ambient = new ColorRGBA(factor, factor, factor, 1f);
        }
    }
    Object shininessFactor = properties.getProperty("Shininess");
    if (shininessFactor != null) {
        if (shininessFactor instanceof Float) {
            shininess = (Float) shininessFactor;
        } else if (shininessFactor instanceof FbxTexture) {
        // TODO: support shininess textures
        }
    }
    Object bumpNormal = properties.getProperty("NormalMap");
    if (bumpNormal != null) {
        if (bumpNormal instanceof FbxTexture) {
            // TODO: check all meshes that use this material have tangents
            //       otherwise shading errors occur
            FbxTexture tex = (FbxTexture) bumpNormal;
            normalMap = tex.getJmeObject();
            normalMap.getImage().setColorSpace(ColorSpace.Linear);
        }
    }
    Object aoColor = properties.getProperty("DiffuseColor2");
    if (aoColor != null) {
        if (aoColor instanceof FbxTexture) {
            FbxTexture tex = (FbxTexture) aoColor;
            if (tex.getUvSet() != null && fbxDiffuseMap != null) {
                if (!tex.getUvSet().equals(fbxDiffuseMap.getUvSet())) {
                    separateTexCoord = true;
                }
            }
            aoMap = tex.getJmeObject();
            aoMap.getImage().setColorSpace(ColorSpace.sRGB);
        }
    }
    assert ambient == null || ambient.a == 1f;
    assert diffuse == null || diffuse.a == 1f;
    assert specular == null || specular.a == 1f;
    assert emissive == null || emissive.a == 1f;
    assert transp == null || (transp.r == 1f && transp.g == 1f && transp.b == 1f);
    // to handle it. Gotta disable specularity then.
    if (shininess < 1f) {
        shininess = 1f;
        specular = ColorRGBA.Black;
    }
    // Try to guess if we need to enable alpha blending.
    // FBX does not specify this explicitly.
    boolean useAlphaBlend = false;
    if (diffuseMap != null && diffuseMap == transpMap) {
        // jME3 already uses alpha from diffuseMap
        // (if alpha blend is enabled)
        useAlphaBlend = true;
        transpMap = null;
    } else if (diffuseMap != null && transpMap != null && diffuseMap != transpMap) {
        // TODO: potential bug here. Alpha from diffuse may 
        // leak unintentionally. 
        useAlphaBlend = true;
    } else if (transpMap != null) {
        // We have alpha map but no diffuse map, OK.
        useAlphaBlend = true;
    }
    if (transp != null && transp.a != 1f) {
        // Consolidate transp into diffuse
        // (jME3 doesn't use a separate alpha color)
        // TODO: potential bug here. Alpha from diffuse may 
        // leak unintentionally. 
        useAlphaBlend = true;
        if (diffuse != null) {
            diffuse.a = transp.a;
        } else {
            diffuse = transp;
        }
    }
    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    mat.setName(name);
    // TODO: load this from FBX material.
    mat.setReceivesShadows(true);
    if (useAlphaBlend) {
        // No idea if this is a transparent or translucent model, gotta guess..
        mat.setTransparent(true);
        mat.setFloat("AlphaDiscardThreshold", 0.01f);
        mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
    }
    mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
    // Set colors.
    if (ambient != null || diffuse != null || specular != null) {
        // If either of those is set, we have to set them all.
        // NOTE: default specular is black, unless it is set explicitly.
        mat.setBoolean("UseMaterialColors", true);
        mat.setColor("Ambient", /*ambient  != null ? ambient  :*/
        ColorRGBA.White);
        mat.setColor("Diffuse", diffuse != null ? diffuse : ColorRGBA.White);
        mat.setColor("Specular", specular != null ? specular : ColorRGBA.Black);
    }
    if (emissive != null) {
        mat.setColor("GlowColor", emissive);
    }
    // Set shininess.
    if (shininess > 1f) {
        // Convert shininess from 
        // Phong (FBX shading model) to Blinn (jME3 shading model).
        float blinnShininess = (shininess * 5.1f) + 1f;
        mat.setFloat("Shininess", blinnShininess);
    }
    // Set textures.
    if (diffuseMap != null) {
        mat.setTexture("DiffuseMap", diffuseMap);
    }
    if (specularMap != null) {
        mat.setTexture("SpecularMap", specularMap);
    }
    if (normalMap != null) {
        mat.setTexture("NormalMap", normalMap);
    }
    if (transpMap != null) {
    //            mat.setTexture("AlphaMap", transpMap);
    }
    if (emitMap != null) {
        mat.setTexture("GlowMap", emitMap);
    }
    if (aoMap != null) {
        mat.setTexture("LightMap", aoMap);
        if (separateTexCoord) {
            mat.setBoolean("SeparateTexCoord", true);
        }
    }
    return mat;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) FbxObject(com.jme3.scene.plugins.fbx.obj.FbxObject) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture)

Example 2 with FbxTexture

use of com.jme3.scene.plugins.fbx.objects.FbxTexture in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method loadObjects.

private void loadObjects(FbxElement element) throws IOException {
    FbxObject obj = null;
    for (FbxElement e : element.children) {
        switch(e.id) {
            case "Geometry":
                FbxMesh mesh = new FbxMesh(this, e);
                obj = mesh;
                if (mesh.geometries != null)
                    geomMap.put(mesh.id, mesh);
                break;
            case "Material":
                obj = new FbxMaterial(this, e);
                break;
            case "Model":
                FbxNode node = new FbxNode(this, e);
                obj = node;
                modelMap.put(node.id, node);
                if (node.isLimb())
                    limbMap.put(node.id, node);
                break;
            case "Pose":
                FbxBindPose pose = new FbxBindPose(this, e);
                obj = pose;
                bindMap.put(pose.id, pose);
                break;
            case "Texture":
                obj = new FbxTexture(this, e);
                break;
            case "Video":
                obj = new FbxImage(this, e);
                break;
            case "Deformer":
                obj = loadDeformer(e);
                break;
            case "AnimationLayer":
                FbxObject layer = new FbxObject(this, e);
                obj = layer;
                alayerMap.put(layer.id, layer);
                break;
            case "AnimationCurve":
                obj = new FbxAnimCurve(this, e);
                break;
            case "AnimationCurveNode":
                obj = new FbxAnimNode(this, e);
                break;
            default:
                obj = null;
        }
        if (obj != null)
            allObjects.put(obj.id, obj);
    }
}
Also used : FbxElement(com.jme3.scene.plugins.fbx.file.FbxElement) FbxObject(com.jme3.scene.plugins.fbx.objects.FbxObject) FbxNode(com.jme3.scene.plugins.fbx.objects.FbxNode) FbxAnimCurve(com.jme3.scene.plugins.fbx.objects.FbxAnimCurve) FbxTexture(com.jme3.scene.plugins.fbx.objects.FbxTexture) FbxMesh(com.jme3.scene.plugins.fbx.objects.FbxMesh) FbxBindPose(com.jme3.scene.plugins.fbx.objects.FbxBindPose) FbxAnimNode(com.jme3.scene.plugins.fbx.objects.FbxAnimNode) FbxMaterial(com.jme3.scene.plugins.fbx.objects.FbxMaterial) FbxImage(com.jme3.scene.plugins.fbx.objects.FbxImage)

Aggregations

Material (com.jme3.material.Material)1 ColorRGBA (com.jme3.math.ColorRGBA)1 FbxElement (com.jme3.scene.plugins.fbx.file.FbxElement)1 FbxObject (com.jme3.scene.plugins.fbx.obj.FbxObject)1 FbxAnimCurve (com.jme3.scene.plugins.fbx.objects.FbxAnimCurve)1 FbxAnimNode (com.jme3.scene.plugins.fbx.objects.FbxAnimNode)1 FbxBindPose (com.jme3.scene.plugins.fbx.objects.FbxBindPose)1 FbxImage (com.jme3.scene.plugins.fbx.objects.FbxImage)1 FbxMaterial (com.jme3.scene.plugins.fbx.objects.FbxMaterial)1 FbxMesh (com.jme3.scene.plugins.fbx.objects.FbxMesh)1 FbxNode (com.jme3.scene.plugins.fbx.objects.FbxNode)1 FbxObject (com.jme3.scene.plugins.fbx.objects.FbxObject)1 FbxTexture (com.jme3.scene.plugins.fbx.objects.FbxTexture)1 Texture (com.jme3.texture.Texture)1