Search in sources :

Example 46 with Light

use of com.jme3.light.Light in project jmonkeyengine by jMonkeyEngine.

the class FbxMaterial method createMaterial.

private Material createMaterial() {
    Material m = new Material(scene.assetManager, "Common/MatDefs/Light/Lighting.j3md");
    m.setName(name);
    ambientColor.multLocal(ambientFactor);
    diffuseColor.multLocal(diffuseFactor);
    specularColor.multLocal(specularFactor);
    m.setColor("Ambient", new ColorRGBA(ambientColor.x, ambientColor.y, ambientColor.z, 1));
    m.setColor("Diffuse", new ColorRGBA(diffuseColor.x, diffuseColor.y, diffuseColor.z, 1));
    m.setColor("Specular", new ColorRGBA(specularColor.x, specularColor.y, specularColor.z, 1));
    m.setFloat("Shininess", shininessExponent);
    m.setBoolean("UseMaterialColors", true);
    // TODO replace with right way in JME to set "Aplha Test"
    m.setFloat("AlphaDiscardThreshold", 0.5f);
    m.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
    return m;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) Material(com.jme3.material.Material)

Example 47 with Light

use of com.jme3.light.Light 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 48 with Light

use of com.jme3.light.Light in project jmonkeyengine by jMonkeyEngine.

the class FbxNode method tryCreateGeometry.

private Spatial tryCreateGeometry(int materialIndex, Mesh jmeMesh, boolean single) {
    // Map meshes without material indices to material 0.
    if (materialIndex == -1) {
        materialIndex = 0;
    }
    Material jmeMat;
    if (materialIndex >= materials.size()) {
        // Material index does not exist. Create default material.
        jmeMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        jmeMat.setReceivesShadows(true);
    } else {
        FbxMaterial fbxMat = materials.get(materialIndex);
        jmeMat = fbxMat.getJmeObject();
    }
    String geomName = getName();
    if (single) {
        geomName += "-submesh";
    } else {
        geomName += "-mat-" + materialIndex + "-submesh";
    }
    Spatial spatial = new Geometry(geomName, jmeMesh);
    spatial.setMaterial(jmeMat);
    if (jmeMat.isTransparent()) {
        spatial.setQueueBucket(Bucket.Transparent);
    }
    if (jmeMat.isReceivesShadows()) {
        spatial.setShadowMode(ShadowMode.Receive);
    }
    spatial.updateModelBound();
    return spatial;
}
Also used : Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) Material(com.jme3.material.Material) FbxMaterial(com.jme3.scene.plugins.fbx.material.FbxMaterial) FbxMaterial(com.jme3.scene.plugins.fbx.material.FbxMaterial)

Example 49 with Light

use of com.jme3.light.Light in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method endElement.

@Override
public void endElement(String uri, String name, String qName) throws SAXException {
    if (qName.equals("node")) {
        node = node.getParent();
    } else if (qName.equals("nodes")) {
        node = null;
    } else if (qName.equals("entity")) {
        node = entityNode.getParent();
        entityNode = null;
    } else if (qName.equals("camera")) {
        node = cameraNode.getParent();
        cameraNode = null;
    } else if (qName.equals("light")) {
        // apply the node's world transform on the light..
        root.updateGeometricState();
        if (light != null) {
            if (light instanceof DirectionalLight) {
                DirectionalLight dl = (DirectionalLight) light;
                Quaternion q = node.getWorldRotation();
                Vector3f dir = dl.getDirection();
                q.multLocal(dir);
                dl.setDirection(dir);
            } else if (light instanceof PointLight) {
                PointLight pl = (PointLight) light;
                Vector3f pos = node.getWorldTranslation();
                pl.setPosition(pos);
            } else if (light instanceof SpotLight) {
                SpotLight sl = (SpotLight) light;
                Vector3f pos = node.getWorldTranslation();
                sl.setPosition(pos);
                Quaternion q = node.getWorldRotation();
                Vector3f dir = sl.getDirection();
                q.multLocal(dir);
                sl.setDirection(dir);
            }
        }
        light = null;
    }
    checkTopNode(qName);
    elementStack.pop();
}
Also used : Quaternion(com.jme3.math.Quaternion) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 50 with Light

use of com.jme3.light.Light in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
    if (qName.equals("scene")) {
        if (elementStack.size() != 0) {
            throw new SAXException("dotScene parse error: 'scene' element must be the root XML element");
        }
        String version = attribs.getValue("formatVersion");
        if (version == null || (!version.equals("1.0.0") && !version.equals("1.0.1"))) {
            logger.log(Level.WARNING, "Unrecognized version number" + " in dotScene file: {0}", version);
        }
    } else if (qName.equals("nodes")) {
        if (root != null) {
            throw new SAXException("dotScene parse error: nodes element was specified twice");
        }
        if (sceneName == null) {
            root = new com.jme3.scene.Node("OgreDotScene" + (++sceneIdx));
        } else {
            root = new com.jme3.scene.Node(sceneName + "-scene_node");
        }
        node = root;
    } else if (qName.equals("externals")) {
        checkTopNode("scene");
    } else if (qName.equals("item")) {
        checkTopNode("externals");
    } else if (qName.equals("file")) {
        checkTopNode("item");
    // NOTE: This part of the file is ignored, it is parsed
    // by SceneMaterialLoader in the first pass.
    } else if (qName.equals("node")) {
        String curElement = elementStack.peek();
        if (!curElement.equals("node") && !curElement.equals("nodes")) {
            throw new SAXException("dotScene parse error: " + "node element can only appear under 'node' or 'nodes'");
        }
        parseNode(attribs);
    } else if (qName.equals("property")) {
        if (node != null) {
            String type = attribs.getValue("type");
            String name = attribs.getValue("name");
            String data = attribs.getValue("data");
            if (type.equals("BOOL")) {
                node.setUserData(name, Boolean.parseBoolean(data) || data.equals("1"));
            } else if (type.equals("FLOAT")) {
                node.setUserData(name, Float.parseFloat(data));
            } else if (type.equals("STRING")) {
                node.setUserData(name, data);
            } else if (type.equals("INT")) {
                node.setUserData(name, Integer.parseInt(data));
            }
        }
    } else if (qName.equals("entity")) {
        checkTopNode("node");
        parseEntity(attribs);
    } else if (qName.equals("camera")) {
        checkTopNode("node");
        parseCamera(attribs);
    } else if (qName.equals("clipping")) {
        checkTopNode("camera");
        parseCameraClipping(attribs);
    } else if (qName.equals("position")) {
        if (elementStack.peek().equals("node")) {
            node.setLocalTranslation(SAXUtil.parseVector3(attribs));
        } else if (elementStack.peek().equals("camera")) {
            cameraNode.setLocalTranslation(SAXUtil.parseVector3(attribs));
        }
    } else if (qName.equals("quaternion") || qName.equals("rotation")) {
        node.setLocalRotation(parseQuat(attribs));
    } else if (qName.equals("scale")) {
        node.setLocalScale(SAXUtil.parseVector3(attribs));
    } else if (qName.equals("light")) {
        parseLight(attribs);
    } else if (qName.equals("colourDiffuse") || qName.equals("colorDiffuse")) {
        if (elementStack.peek().equals("light")) {
            if (light != null) {
                light.setColor(parseColor(attribs));
            }
        } else {
            checkTopNode("environment");
        }
    } else if (qName.equals("colourAmbient") || qName.equals("colorAmbient")) {
        if (elementStack.peek().equals("environment")) {
            ColorRGBA color = parseColor(attribs);
            if (!color.equals(ColorRGBA.Black) && !color.equals(ColorRGBA.BlackNoAlpha)) {
                // Lets add an ambient light to the scene.
                AmbientLight al = new AmbientLight();
                al.setColor(color);
                root.addLight(al);
            }
        }
    } else if (qName.equals("normal") || qName.equals("direction")) {
        checkTopNode("light");
        parseLightNormal(attribs);
    } else if (qName.equals("lightAttenuation")) {
        parseLightAttenuation(attribs);
    } else if (qName.equals("spotLightRange") || qName.equals("lightRange")) {
        parseLightSpotLightRange(attribs);
    }
    elementStack.push(qName);
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) LightNode(com.jme3.scene.LightNode) CameraNode(com.jme3.scene.CameraNode) AmbientLight(com.jme3.light.AmbientLight) SAXException(org.xml.sax.SAXException)

Aggregations

Vector3f (com.jme3.math.Vector3f)64 Material (com.jme3.material.Material)61 DirectionalLight (com.jme3.light.DirectionalLight)55 Geometry (com.jme3.scene.Geometry)52 PointLight (com.jme3.light.PointLight)27 Spatial (com.jme3.scene.Spatial)27 Box (com.jme3.scene.shape.Box)26 Sphere (com.jme3.scene.shape.Sphere)26 ColorRGBA (com.jme3.math.ColorRGBA)24 Quaternion (com.jme3.math.Quaternion)21 Node (com.jme3.scene.Node)21 AmbientLight (com.jme3.light.AmbientLight)20 Texture (com.jme3.texture.Texture)18 SpotLight (com.jme3.light.SpotLight)16 FilterPostProcessor (com.jme3.post.FilterPostProcessor)15 KeyTrigger (com.jme3.input.controls.KeyTrigger)11 Test (org.junit.Test)11 TempVars (com.jme3.util.TempVars)10 Light (com.jme3.light.Light)9 Camera (com.jme3.renderer.Camera)9