Java Code Examples for com.jme3.asset.AssetManager#loadTexture()

The following examples show how to use com.jme3.asset.AssetManager#loadTexture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaFxImageManager.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@FxThread
private @NotNull Image readJMETexture(final int width, final int height, @NotNull final String externalForm,
                                      @NotNull final Path cacheFile) {

    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Texture texture = assetManager.loadTexture(externalForm);

    final BufferedImage textureImage;
    try {
        textureImage = ImageToAwt.convert(texture.getImage(), false, true, 0);
    } catch (final UnsupportedOperationException e) {
        EditorUtil.handleException(LOGGER, this, e);
        return Icons.IMAGE_512;
    }

    final int imageWidth = textureImage.getWidth();
    final int imageHeight = textureImage.getHeight();

    return scaleAndWrite(width, height, cacheFile, textureImage, imageWidth, imageHeight);
}
 
Example 2
Source File: TextureLayer.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Set a new diffuse texture.
 *
 * @param diffuseFile the file to diffuse texture or null.
 */
@FromAnyThread
public void setDiffuseFile(@Nullable final Path diffuseFile) {
    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Path assetFile = diffuseFile == null ? null : getAssetFile(diffuseFile);
    final String assetPath = assetFile == null ? null : toAssetPath(assetFile);
    final Texture texture = assetPath == null ? null : assetManager.loadTexture(assetPath);
    settings.setDiffuse(texture, getLayer());
}
 
Example 3
Source File: TextureLayer.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Set the new normal texture.
 *
 * @param normalFile the file to normal texture or null.
 */
@FromAnyThread
public void setNormalFile(@Nullable final Path normalFile) {
    final AssetManager assetManager = EditorUtil.getAssetManager();
    final Path assetFile = normalFile == null ? null : getAssetFile(normalFile);
    final String assetPath = assetFile == null ? null : toAssetPath(assetFile);
    final Texture texture = assetPath == null ? null : assetManager.loadTexture(assetPath);
    settings.setNormal(texture, getLayer());
}
 
Example 4
Source File: TestAbsoluteLocators.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args){
    AssetManager am = JmeSystem.newAssetManager();

    am.registerLoader(AWTLoader.class, "jpg");
    am.registerLoader(WAVLoader.class, "wav");

    // register absolute locator
    am.registerLocator("/",  ClasspathLocator.class);

    // find a sound
    AudioData audio = am.loadAudio("Sound/Effects/Gun.wav");

    // find a texture
    Texture tex = am.loadTexture("Textures/Terrain/Pond/Pond.jpg");

    if (audio == null)
        throw new RuntimeException("Cannot find audio!");
    else
        System.out.println("Audio loaded from Sounds/Effects/Gun.wav");

    if (tex == null)
        throw new RuntimeException("Cannot find texture!");
    else
        System.out.println("Texture loaded from Textures/Terrain/Pond/Pond.jpg");

    System.out.println("Success!");
}
 
Example 5
Source File: SkyFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Spatial createSky(AssetManager assetManager, String textureName, boolean sphereMap) {
    TextureKey key = new TextureKey(textureName, true);
    key.setGenerateMips(true);
    key.setAsCube(!sphereMap);
    Texture tex = assetManager.loadTexture(key);
    return createSky(assetManager, tex, sphereMap);
}
 
Example 6
Source File: ArmatureDebugger.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void initialize(AssetManager assetManager, Camera camera) {

        armatureNode.setCamera(camera);

        Material matJoints = new Material(assetManager, "Common/MatDefs/Misc/Billboard.j3md");
        Texture t = assetManager.loadTexture("Common/Textures/dot.png");
        matJoints.setTexture("Texture", t);
        matJoints.getAdditionalRenderState().setDepthTest(false);
        matJoints.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
        joints.setQueueBucket(RenderQueue.Bucket.Translucent);
        joints.setMaterial(matJoints);

        Material matWires = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        matWires.setBoolean("VertexColor", true);
        matWires.getAdditionalRenderState().setLineWidth(1f);
        wires.setMaterial(matWires);

        Material matOutline = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        matOutline.setBoolean("VertexColor", true);
        matOutline.getAdditionalRenderState().setLineWidth(1f);
        outlines.setMaterial(matOutline);

        Material matOutline2 = new Material(assetManager, "Common/MatDefs/Misc/DashedLine.j3md");
        matOutline2.getAdditionalRenderState().setLineWidth(1);
        outlines.getChild(1).setMaterial(matOutline2);

        Material matWires2 = new Material(assetManager, "Common/MatDefs/Misc/DashedLine.j3md");
        matWires2.getAdditionalRenderState().setLineWidth(1);
        wires.getChild(1).setMaterial(matWires2);

    }
 
Example 7
Source File: TestAbsoluteLocators.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args){
    AssetManager am = new DesktopAssetManager();

    am.registerLoader(AWTLoader.class.getName(), "jpg");
    am.registerLoader(WAVLoader.class.getName(), "wav");

    // register absolute locator
    am.registerLocator("/",  ClasspathLocator.class.getName());

    // find a sound
    AudioData audio = am.loadAudio("Sound/Effects/Gun.wav");

    // find a texture
    Texture tex = am.loadTexture("Textures/Terrain/Pond/Pond.jpg");

    if (audio == null)
        throw new RuntimeException("Cannot find audio!");
    else
        System.out.println("Audio loaded from Sounds/Effects/Gun.wav");

    if (tex == null)
        throw new RuntimeException("Cannot find texture!");
    else
        System.out.println("Texture loaded from Textures/Terrain/Pond/Pond.jpg");

    System.out.println("Success!");
}
 
Example 8
Source File: SkyFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
* Create a sky using the given cubemap or spheremap texture.
*
* @param assetManager from which to load materials
* @param textureName the path to the texture asset to use    
* @param envMapType see {@link EnvMapType}
* @return a new spatial representing the sky, ready to be attached to the
* scene graph    
*/  
public static Spatial createSky(AssetManager assetManager, String textureName, EnvMapType envMapType) {
    TextureKey key = new TextureKey(textureName, true);
    key.setGenerateMips(false);
    if (envMapType == EnvMapType.CubeMap) {
        key.setTextureTypeHint(Texture.Type.CubeMap);
    }
    Texture tex = assetManager.loadTexture(key);
    return createSky(assetManager, tex, envMapType);
}
 
Example 9
Source File: PlaceholderAssets.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Material getPlaceholderMaterial(AssetManager assetManager){
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture tex = assetManager.loadTexture("Common/Textures/MissingMaterial.png");
    tex.setWrap(Texture.WrapMode.Repeat);
    mat.setTexture("ColorMap", tex);
    return mat;
}
 
Example 10
Source File: PlaceholderAssets.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Spatial getPlaceholderModel(AssetManager assetManager){
    // What should be the size? Nobody knows
    // the user's expected scale...
    Box box = new Box(1, 1, 1);
    Geometry geom = new Geometry("placeholder", box);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture tex = assetManager.loadTexture("Common/Textures/MissingModel.png");
    tex.setWrap(Texture.WrapMode.Repeat);
    mat.setTexture("ColorMap", tex);
    geom.setMaterial(mat);
    return geom;
}
 
Example 11
Source File: SimpleWaterProcessor.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected void loadTextures(AssetManager manager) {
    normalTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/water_normalmap.png");
    dudvTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/dudv_map.jpg");
    normalTexture.setWrap(WrapMode.Repeat);
    dudvTexture.setWrap(WrapMode.Repeat);
}
 
Example 12
Source File: SSAOFilter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
    this.renderManager = renderManager;
    this.viewPort = vp;
    int screenWidth = w;
    int screenHeight = h;
    postRenderPasses = new ArrayList<Pass>();

    normalPass = new Pass();
    normalPass.init(renderManager.getRenderer(), (int) (screenWidth / downSampleFactor), (int) (screenHeight / downSampleFactor), Format.RGBA8, Format.Depth);


    frustumNearFar = new Vector2f();

    float farY = (vp.getCamera().getFrustumTop() / vp.getCamera().getFrustumNear()) * vp.getCamera().getFrustumFar();
    float farX = farY * ((float) screenWidth / (float) screenHeight);
    frustumCorner = new Vector3f(farX, farY, vp.getCamera().getFrustumFar());
    frustumNearFar.x = vp.getCamera().getFrustumNear();
    frustumNearFar.y = vp.getCamera().getFrustumFar();





    //ssao Pass
    ssaoMat = new Material(manager, "Common/MatDefs/SSAO/ssao.j3md");
    ssaoMat.setTexture("Normals", normalPass.getRenderedTexture());
    Texture random = manager.loadTexture("Common/MatDefs/SSAO/Textures/random.png");
    random.setWrap(Texture.WrapMode.Repeat);
    ssaoMat.setTexture("RandomMap", random);

    ssaoPass = new Pass() {

        @Override
        public boolean requiresDepthAsTexture() {
            return true;
        }
    };

    ssaoPass.init(renderManager.getRenderer(), (int) (screenWidth / downSampleFactor), (int) (screenHeight / downSampleFactor), Format.RGBA8, Format.Depth, 1, ssaoMat);
    ssaoPass.getRenderedTexture().setMinFilter(Texture.MinFilter.Trilinear);
    ssaoPass.getRenderedTexture().setMagFilter(Texture.MagFilter.Bilinear);
    postRenderPasses.add(ssaoPass);
    material = new Material(manager, "Common/MatDefs/SSAO/ssaoBlur.j3md");
    material.setTexture("SSAOMap", ssaoPass.getRenderedTexture());

    ssaoMat.setVector3("FrustumCorner", frustumCorner);
    ssaoMat.setFloat("SampleRadius", sampleRadius);
    ssaoMat.setFloat("Intensity", intensity);
    ssaoMat.setFloat("Scale", scale);
    ssaoMat.setFloat("Bias", bias);
    material.setBoolean("UseAo", useAo);
    material.setBoolean("UseOnlyAo", useOnlyAo);
    ssaoMat.setVector2("FrustumNearFar", frustumNearFar);
    material.setVector2("FrustumNearFar", frustumNearFar);
    ssaoMat.setParam("Samples", VarType.Vector2Array, samples);

    float xScale = 1.0f / w;
    float yScale = 1.0f / h;

    float blurScale = 2f;
    material.setFloat("XScale", blurScale * xScale);
    material.setFloat("YScale", blurScale * yScale);

}
 
Example 13
Source File: SimpleWaterProcessor.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void loadTextures(AssetManager manager) {
    normalTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/water_normalmap.png");
    dudvTexture = (Texture2D) manager.loadTexture("Common/MatDefs/Water/Textures/dudv_map.jpg");
    normalTexture.setWrap(WrapMode.Repeat);
    dudvTexture.setWrap(WrapMode.Repeat);
}
 
Example 14
Source File: SSAOFilter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
    protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
        this.renderManager = renderManager;
        this.viewPort = vp;
        int screenWidth = w;
        int screenHeight = h;
        postRenderPasses = new ArrayList<Pass>();

        normalPass = new Pass();
        normalPass.init(renderManager.getRenderer(), (int) (screenWidth / downSampleFactor), (int) (screenHeight / downSampleFactor), Format.RGBA8, Format.Depth);


        frustumNearFar = new Vector2f();

        float farY = (vp.getCamera().getFrustumTop() / vp.getCamera().getFrustumNear()) * vp.getCamera().getFrustumFar();
        float farX = farY * (screenWidth / (float) screenHeight);
        frustumCorner = new Vector3f(farX, farY, vp.getCamera().getFrustumFar());
        frustumNearFar.x = vp.getCamera().getFrustumNear();
        frustumNearFar.y = vp.getCamera().getFrustumFar();





        //ssao Pass
        ssaoMat = new Material(manager, "Common/MatDefs/SSAO/ssao.j3md");
        ssaoMat.setTexture("Normals", normalPass.getRenderedTexture());
        Texture random = manager.loadTexture("Common/MatDefs/SSAO/Textures/random.png");
        random.setWrap(Texture.WrapMode.Repeat);
        ssaoMat.setTexture("RandomMap", random);

        ssaoPass = new Pass("SSAO pass") {

            @Override
            public boolean requiresDepthAsTexture() {
                return true;
            }
        };

        ssaoPass.init(renderManager.getRenderer(), (int) (screenWidth / downSampleFactor), (int) (screenHeight / downSampleFactor), Format.RGBA8, Format.Depth, 1, ssaoMat);
//        ssaoPass.getRenderedTexture().setMinFilter(Texture.MinFilter.Trilinear);
//        ssaoPass.getRenderedTexture().setMagFilter(Texture.MagFilter.Bilinear);
        postRenderPasses.add(ssaoPass);
        material = new Material(manager, "Common/MatDefs/SSAO/ssaoBlur.j3md");
        material.setTexture("SSAOMap", ssaoPass.getRenderedTexture());

        ssaoMat.setVector3("FrustumCorner", frustumCorner);
        ssaoMat.setFloat("SampleRadius", sampleRadius);
        ssaoMat.setFloat("Intensity", intensity);
        ssaoMat.setFloat("Scale", scale);
        ssaoMat.setFloat("Bias", bias);
        material.setBoolean("UseAo", useAo);
        material.setBoolean("UseOnlyAo", useOnlyAo);
        ssaoMat.setVector2("FrustumNearFar", frustumNearFar);
        material.setVector2("FrustumNearFar", frustumNearFar);
        ssaoMat.setParam("Samples", VarType.Vector2Array, samples);
        ssaoMat.setBoolean("ApproximateNormals", approximateNormals);

        float xScale = 1.0f / w;
        float yScale = 1.0f / h;

        float blurScale = 2f;
        material.setFloat("XScale", blurScale * xScale);
        material.setFloat("YScale", blurScale * yScale);

    }
 
Example 15
Source File: TestMaterialCompare.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) {
    AssetManager assetManager = JmeSystem.newAssetManager(
            TestMaterialCompare.class.getResource("/com/jme3/asset/Desktop.cfg"));
    
    // Cloned materials
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setName("mat1");
    mat1.setColor("Color", ColorRGBA.Blue);

    Material mat2 = mat1.clone();
    mat2.setName("mat2");
    testEquality(mat1, mat2, true);

    // Cloned material with different render states
    Material mat3 = mat1.clone();
    mat3.setName("mat3");
    mat3.getAdditionalRenderState().setBlendMode(BlendMode.ModulateX2);
    testEquality(mat1, mat3, false);

    // Two separately loaded materials
    Material mat4 = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    mat4.setName("mat4");
    Material mat5 = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    mat5.setName("mat5");
    testEquality(mat4, mat5, true);
    
    // Comparing same textures
    TextureKey originalKey = (TextureKey) mat4.getTextureParam("DiffuseMap").getTextureValue().getKey();
    TextureKey tex1key = new TextureKey("Models/Sign Post/Sign Post.jpg", false);
    tex1key.setGenerateMips(true);
    
    // Texture keys from the original and the loaded texture
    // must be identical, otherwise the resultant textures not identical
    // and thus materials are not identical!
    if (!originalKey.equals(tex1key)){
        System.out.println("TEXTURE KEYS ARE NOT EQUAL");
    }
    
    Texture tex1 = assetManager.loadTexture(tex1key);
    mat4.setTexture("DiffuseMap", tex1);
    testEquality(mat4, mat5, true);
    
    // Change some stuff on the texture and compare, materials no longer equal
    tex1.setWrap(Texture.WrapMode.MirroredRepeat);
    testEquality(mat4, mat5, false);
    
    // Comparing different textures
    Texture tex2 = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    mat4.setTexture("DiffuseMap", tex2);
    testEquality(mat4, mat5, false);

    // Two materials created the same way
    Material mat6 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat6.setName("mat6");
    mat6.setColor("Color", ColorRGBA.Blue);
    testEquality(mat1, mat6, true);

    // Changing a material param
    mat6.setColor("Color", ColorRGBA.Green);
    testEquality(mat1, mat6, false);
}
 
Example 16
Source File: Picture.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Set the image to put on the picture.
 * 
 * @param assetManager The {@link AssetManager} to use to load the image.
 * @param imgName The image name.
 * @param useAlpha If true, the picture will appear transparent and allow
 * objects behind it to appear through. If false, the transparent
 * portions will be the image's color at that pixel.
 */
public void setImage(AssetManager assetManager, String imgName, boolean useAlpha){
    TextureKey key = new TextureKey(imgName, true);
    Texture2D tex = (Texture2D) assetManager.loadTexture(key);
    setTexture(assetManager, tex, useAlpha);
}
 
Example 17
Source File: Picture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Set the image to put on the picture.
 * 
 * @param assetManager The {@link AssetManager} to use to load the image.
 * @param imgName The image name.
 * @param useAlpha If true, the picture will appear transparent and allow
 * objects behind it to appear through. If false, the transparent
 * portions will be the image's color at that pixel.
 */
public void setImage(AssetManager assetManager, String imgName, boolean useAlpha){
    TextureKey key = new TextureKey(imgName, true);
    Texture2D tex = (Texture2D) assetManager.loadTexture(key);
    setTexture(assetManager, tex, useAlpha);
}