Search in sources :

Example 61 with Texture

use of com.jme3.texture.Texture 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 62 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class FbxTexture method toJmeObject.

@Override
protected Texture toJmeObject() {
    Image image = null;
    TextureKey key = null;
    if (media != null) {
        image = (Image) media.getJmeObject();
        key = media.getTextureKey();
    }
    if (image == null) {
        image = PlaceholderAssets.getPlaceholderImage(assetManager);
    }
    Texture2D tex = new Texture2D(image);
    if (key != null) {
        tex.setKey(key);
        tex.setName(key.getName());
        tex.setAnisotropicFilter(key.getAnisotropy());
    }
    tex.setMinFilter(MinFilter.Trilinear);
    tex.setMagFilter(MagFilter.Bilinear);
    if (wrapModeU == 0) {
        tex.setWrap(WrapAxis.S, WrapMode.Repeat);
    }
    if (wrapModeV == 0) {
        tex.setWrap(WrapAxis.T, WrapMode.Repeat);
    }
    return tex;
}
Also used : TextureKey(com.jme3.asset.TextureKey) Texture2D(com.jme3.texture.Texture2D) Image(com.jme3.texture.Image)

Example 63 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class MaterialExtensionLoader method readExtendingMaterialStatement.

private void readExtendingMaterialStatement(Statement statement) throws IOException {
    if (statement.getLine().startsWith("set_texture_alias")) {
        String[] split = statement.getLine().split(" ", 3);
        String aliasName = split[1];
        String texturePath = split[2];
        String jmeParamName = matExt.getTextureMapping(aliasName);
        TextureKey texKey = new TextureKey(texturePath, false);
        texKey.setGenerateMips(true);
        Texture tex;
        try {
            tex = assetManager.loadTexture(texKey);
            tex.setWrap(WrapMode.Repeat);
        } catch (AssetNotFoundException ex) {
            logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[] { texKey, key });
            tex = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager));
            tex.setWrap(WrapMode.Repeat);
            tex.setKey(texKey);
        }
        material.setTexture(jmeParamName, tex);
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) Texture2D(com.jme3.texture.Texture2D) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) Texture(com.jme3.texture.Texture)

Example 64 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class TestMaterialWrite method testWriteMat.

@Test
public void testWriteMat() throws Exception {
    Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    mat.setBoolean("UseMaterialColors", true);
    mat.setColor("Diffuse", ColorRGBA.White);
    mat.setColor("Ambient", ColorRGBA.DarkGray);
    mat.setFloat("AlphaDiscardThreshold", 0.5f);
    mat.setFloat("Shininess", 2.5f);
    Texture tex = assetManager.loadTexture("Common/Textures/MissingTexture.png");
    tex.setMagFilter(Texture.MagFilter.Nearest);
    tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
    tex.setWrap(Texture.WrapAxis.S, Texture.WrapMode.Repeat);
    tex.setWrap(Texture.WrapAxis.T, Texture.WrapMode.MirroredRepeat);
    mat.setTexture("DiffuseMap", tex);
    mat.getAdditionalRenderState().setDepthWrite(false);
    mat.getAdditionalRenderState().setDepthTest(false);
    mat.getAdditionalRenderState().setLineWidth(5);
    mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    J3MExporter exporter = new J3MExporter();
    try {
        exporter.save(mat, stream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.err.println(stream.toString());
    J3MLoader loader = new J3MLoader();
    AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {

        @Override
        public InputStream openStream() {
            return new ByteArrayInputStream(stream.toByteArray());
        }
    };
    Material mat2 = (Material) loader.load(info);
    assertTrue(mat.contentEquals(mat2));
}
Also used : AssetKey(com.jme3.asset.AssetKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Material(com.jme3.material.Material) J3MExporter(com.jme3.material.plugin.export.material.J3MExporter) J3MLoader(com.jme3.material.plugins.J3MLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AssetInfo(com.jme3.asset.AssetInfo) Texture(com.jme3.texture.Texture) Test(org.junit.Test)

Example 65 with Texture

use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.

the class J3MLoaderTest method setupMockForTexture.

private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
    when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, null));
    final TextureKey textureKey = new TextureKey(path, flipY);
    textureKey.setGenerateMips(true);
    when(assetManager.loadTexture(textureKey)).thenReturn(texture);
    return textureKey;
}
Also used : TextureKey(com.jme3.asset.TextureKey) MatParamTexture(com.jme3.material.MatParamTexture)

Aggregations

Material (com.jme3.material.Material)97 Texture (com.jme3.texture.Texture)94 Vector3f (com.jme3.math.Vector3f)66 Geometry (com.jme3.scene.Geometry)40 Image (com.jme3.texture.Image)39 TextureKey (com.jme3.asset.TextureKey)31 ArrayList (java.util.ArrayList)27 Texture2D (com.jme3.texture.Texture2D)25 ColorRGBA (com.jme3.math.ColorRGBA)23 Box (com.jme3.scene.shape.Box)19 Spatial (com.jme3.scene.Spatial)18 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)18 ParticleEmitter (com.jme3.effect.ParticleEmitter)17 DirectionalLight (com.jme3.light.DirectionalLight)17 Vector2f (com.jme3.math.Vector2f)17 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)16 ByteBuffer (java.nio.ByteBuffer)16 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)15 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)15 Camera (com.jme3.renderer.Camera)14