Java Code Examples for com.jme3.texture.Texture

The following examples show how to use com.jme3.texture.Texture. These examples are extracted from open source projects. 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 Project: jmonkeybuilder   Source File: PaintTerrainToolControl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@JmeThread
public void startPainting(@NotNull final PaintingInput input, @NotNull final Quaternion brushRotation,
                          @NotNull final Vector3f contactPoint) {

    final Texture alphaTexture = getAlphaTexture();
    if (alphaTexture == null) {
        return;
    }

    super.startPainting(input, brushRotation, contactPoint);

    switch (input) {
        case MOUSE_PRIMARY:
        case MOUSE_SECONDARY: {
            startChange();
            paintTexture(input, contactPoint);
            break;
        }
    }
}
 
Example 2
/**
 * Get the normal map texture at the specified layer.
 * Run this on the GL thread!
 */
private Texture doGetNormalMap(int layer) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (layer == 0)
        matParam = terrain.getMaterial().getParam("NormalMap");
    else
        matParam = terrain.getMaterial().getParam("NormalMap_"+layer);

    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example 3
public void makeToonish(Spatial spatial){
        if (spatial instanceof Node){
            Node n = (Node) spatial;
            for (Spatial child : n.getChildren())
                makeToonish(child);
        }else if (spatial instanceof Geometry){
            Geometry g = (Geometry) spatial;
            Material m = g.getMaterial();
            if (m.getMaterialDef().getName().equals("Phong Lighting")){
                Texture t = assetManager.loadTexture("Textures/ColorRamp/toon.png");
//                t.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
//                t.setMagFilter(Texture.MagFilter.Nearest);
                m.setTexture("ColorRamp", t);
                m.setBoolean("UseMaterialColors", true);
                m.setColor("Specular", ColorRGBA.Black);
                m.setColor("Diffuse", ColorRGBA.White);
                m.setBoolean("VertexLighting", true);
            }
        }
    }
 
Example 4
private int convertWrapMode(Texture.WrapMode mode) {
    switch (mode) {
        case BorderClamp:
            return GL2GL3.GL_CLAMP_TO_BORDER;
        case Clamp:
            return GL2.GL_CLAMP;
        case EdgeClamp:
            return GL.GL_CLAMP_TO_EDGE;
        case Repeat:
            return GL.GL_REPEAT;
        case MirroredRepeat:
            return GL.GL_MIRRORED_REPEAT;
        default:
            throw new UnsupportedOperationException("Unknown wrap mode: " + mode);
    }
}
 
Example 5
private void doSetNormalMap(int layer, String texturePath) {
    Terrain terrain = (Terrain) getTerrain(null);
    if (terrain == null)
        return;

    if (texturePath == null) {
        // remove the texture if it is null
        if (layer == 0)
            terrain.getMaterial().clearParam("NormalMap");
        else
            terrain.getMaterial().clearParam("NormalMap_"+layer);
    } else {
        Texture tex = SceneApplication.getApplication().getAssetManager().loadTexture(texturePath);
        tex.setWrap(WrapMode.Repeat);

        if (layer == 0)
            terrain.getMaterial().setTexture("NormalMap", tex);
        else
            terrain.getMaterial().setTexture("NormalMap_"+layer, tex);
    }
    enableTextureButtons();
    setNeedsSave(true);
}
 
Example 6
/*********************************************************************\
|* Textures                                                          *|
\*********************************************************************/
private int convertTextureType(Texture.Type type, int samples) {
    switch (type) {
        case TwoDimensional:
            if (samples > 1) {
                return ARBTextureMultisample.GL_TEXTURE_2D_MULTISAMPLE;
            } else {
                return GL_TEXTURE_2D;
            }
        case TwoDimensionalArray:
            if (samples > 1) {
                return ARBTextureMultisample.GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
            } else {
                return EXTTextureArray.GL_TEXTURE_2D_ARRAY_EXT;
            }
        case ThreeDimensional:
            return GL_TEXTURE_3D;
        case CubeMap:
            return GL_TEXTURE_CUBE_MAP;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + type);
    }
}
 
Example 7
/**
 * Sets the texture to the given material.
 * 
 * @param material
 *            the material that we add texture to
 * @param mapTo
 *            the texture mapping type
 * @param texture
 *            the added texture
 */
private void setTexture(Material material, int mapTo, Texture texture) {
    switch (mapTo) {
        case MTEX_COL:
            material.setTexture(shadeless ? MaterialHelper.TEXTURE_TYPE_COLOR : MaterialHelper.TEXTURE_TYPE_DIFFUSE, texture);
            break;
        case MTEX_NOR:
            material.setTexture(MaterialHelper.TEXTURE_TYPE_NORMAL, texture);
            break;
        case MTEX_SPEC:
            material.setTexture(MaterialHelper.TEXTURE_TYPE_SPECULAR, texture);
            break;
        case MTEX_EMIT:
            material.setTexture(MaterialHelper.TEXTURE_TYPE_GLOW, texture);
            break;
        case MTEX_ALPHA:
            if (!shadeless) {
                material.setTexture(MaterialHelper.TEXTURE_TYPE_ALPHA, texture);
            } else {
                LOGGER.warning("JME does not support alpha map on unshaded material. Material name is " + name);
            }
            break;
        default:
            LOGGER.severe("Unknown mapping type: " + mapTo);
    }
}
 
Example 8
private int applyOverrides(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> overrides, int unit) {
    for (MatParamOverride override : overrides.getArray()) {
        VarType type = override.getVarType();

        MatParam paramDef = def.getMaterialParam(override.getName());

        if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) {
            continue;
        }

        Uniform uniform = shader.getUniform(override.getPrefixedName());

        if (override.getValue() != null) {
            if (type.isTextureType()) {
                renderer.setTexture(unit, (Texture) override.getValue());
                uniform.setValue(VarType.Int, unit);
                unit++;
            } else {
                uniform.setValue(type, override.getValue());
            }
        } else {
            uniform.clearValue();
        }
    }
    return unit;
}
 
Example 9
private int convertMinFilter(Texture.MinFilter filter) {
    switch (filter) {
        case Trilinear:
            return GLES20.GL_LINEAR_MIPMAP_LINEAR;
        case BilinearNearestMipMap:
            return GLES20.GL_LINEAR_MIPMAP_NEAREST;
        case NearestLinearMipMap:
            return GLES20.GL_NEAREST_MIPMAP_LINEAR;
        case NearestNearestMipMap:
            return GLES20.GL_NEAREST_MIPMAP_NEAREST;
        case BilinearNoMipMaps:
            return GLES20.GL_LINEAR;
        case NearestNoMipMaps:
            return GLES20.GL_NEAREST;
        default:
            throw new UnsupportedOperationException("Unknown min filter: " + filter);
    }
}
 
Example 10
/**
 * Set a texture parameter.
 *
 * @param name The name of the parameter
 * @param type The variable type {@link VarType}
 * @param value The texture value of the parameter.
 *
 * @throws IllegalArgumentException is value is null
 */
public void setTextureParam(String name, VarType type, Texture value) {
    if (value == null) {
        throw new IllegalArgumentException();
    }

    checkSetParam(type, name);
    MatParamTexture val = getTextureParam(name);
    if (val == null) {
        checkTextureParamColorSpace(name, value);
        paramValues.put(name, new MatParamTexture(type, name, value, value.getImage() != null ? value.getImage().getColorSpace() : null));
    } else {
        checkTextureParamColorSpace(name, value);
        val.setTextureValue(value);
        val.setColorSpace(value.getImage() != null ? value.getImage().getColorSpace() : null);
    }

    if (technique != null) {
        technique.notifyParamChanged(name, type, value);
    }

    // need to recompute sort ID
    sortingId = -1;
}
 
Example 11
Source Project: MikuMikuStudio   Source File: GdxRenderer.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
private int convertWrapMode(Texture.WrapMode mode) {
        switch (mode) {
//            case BorderClamp:
//                return GLES20.GL_CLAMP_TO_BORDER;
//            case Clamp:
//                return GLES20.GL_CLAMP;
            case EdgeClamp:
                return GL20.GL_CLAMP_TO_EDGE;
            case Repeat:
                return GL20.GL_REPEAT;
            case MirroredRepeat:
                return GL20.GL_MIRRORED_REPEAT;
            default:
                throw new UnsupportedOperationException("Unknown wrap mode: " + mode);
        }
    }
 
Example 12
@Test
public void oldStyleTextureParameters_shouldBeSupported() throws Exception {
    when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/texture-parameters-oldstyle.j3m"));

    final Texture textureOldStyle = Mockito.mock(Texture.class);
    final Texture textureOldStyleUsingQuotes = Mockito.mock(Texture.class);

    final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, textureOldStyleUsingQuotes);
    final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, textureOldStyle);

    j3MLoader.load(assetInfo);

    verify(assetManager).loadTexture(textureKeyUsingQuotes);
    verify(assetManager).loadTexture(textureKeyOldStyle);
    verify(textureOldStyle).setWrap(Texture.WrapMode.Repeat);
    verify(textureOldStyleUsingQuotes).setWrap(Texture.WrapMode.Repeat);
}
 
Example 13
Source Project: Lemur   Source File: GuiGlobals.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Texture loadTexture( String path, boolean repeat, boolean generateMips ) {
    TextureKey key = new TextureKey(path);
    key.setGenerateMips(generateMips);

    Texture t = assets.loadTexture(key);
    if( t == null ) {
        throw new RuntimeException("Error loading texture:" + path);
    }

    if( repeat ) {
        t.setWrap(Texture.WrapMode.Repeat);
    } else {
        // JME has deprecated Clamp and defaults to EdgeClamp.
        // I think the WrapMode.EdgeClamp javadoc is totally bonkers, though.
        t.setWrap(Texture.WrapMode.EdgeClamp);
    }

    return t;
}
 
Example 14
private int convertWrapMode(Texture.WrapMode mode) {
    switch (mode) {
        case BorderClamp:
            return GL_CLAMP_TO_BORDER;
        case Clamp:
            return GL_CLAMP;
        case EdgeClamp:
            return GL_CLAMP_TO_EDGE;
        case Repeat:
            return GL_REPEAT;
        case MirroredRepeat:
            return GL_MIRRORED_REPEAT;
        default:
            throw new UnsupportedOperationException("Unknown wrap mode: " + mode);
    }
}
 
Example 15
private Texture getAlphaTexture(Terrain terrain, int alphaLayer) {
    if (terrain == null)
        return null;
    MatParam matParam = null;
    if (alphaLayer == 0)
        matParam = terrain.getMaterial().getParam("AlphaMap");
    else if(alphaLayer == 1)
        matParam = terrain.getMaterial().getParam("AlphaMap_1");
    else if(alphaLayer == 2)
        matParam = terrain.getMaterial().getParam("AlphaMap_2");
    
    if (matParam == null || matParam.getValue() == null) {
        return null;
    }
    Texture tex = (Texture) matParam.getValue();
    return tex;
}
 
Example 16
@Override
public void simpleInitApp() {
    // create a simple plane/quad
    Quad quadMesh = new Quad(1, 1);
    quadMesh.updateGeometry(1, 1, true);

    Geometry quad = new Geometry("Textured Quad", quadMesh);

    assetManager.registerLocator("https://raw.githubusercontent.com/jMonkeyEngine/BookSamples/master/assets/Textures/",
                            UrlLocator.class);
    TextureKey key = new TextureKey("mucha-window.png", false);
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setTexture("ColorMap", tex);
    quad.setMaterial(mat);

    float aspect = tex.getImage().getWidth() / (float) tex.getImage().getHeight();
    quad.setLocalScale(new Vector3f(aspect * 1.5f, 1.5f, 1));
    quad.center();

    rootNode.attachChild(quad);
}
 
Example 17
public void initMaterial() {
    mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
    key.setGenerateMips(true);
    Texture tex = assetManager.loadTexture(key);
    mat.setTexture("ColorMap", tex);

    mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
    key2.setGenerateMips(true);
    Texture tex2 = assetManager.loadTexture(key2);
    mat2.setTexture("ColorMap", tex2);

    mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
    key3.setGenerateMips(true);
    Texture tex3 = assetManager.loadTexture(key3);
    tex3.setWrap(WrapMode.Repeat);
    mat3.setTexture("ColorMap", tex3);
}
 
Example 18
public void paintTexture(Terrain terrain, Vector3f markerLocation, float toolRadius, float toolWeight, int selectedTextureIndex) {
    if (selectedTextureIndex < 0 || markerLocation == null)
        return;
    
    int alphaIdx = selectedTextureIndex/4; // 4 = rgba = 4 textures
    Texture tex = getAlphaTexture(terrain, alphaIdx);
    Image image = tex.getImage();

    Vector2f UV = getPointPercentagePosition(terrain, markerLocation);

    // get the radius of the brush in pixel-percent
    float brushSize = toolRadius/(terrain.getTerrainSize()*((Node)terrain).getLocalScale().x);
    int texIndex = selectedTextureIndex - ((selectedTextureIndex/4)*4); // selectedTextureIndex/4 is an int floor, do not simplify the equation
    boolean erase = toolWeight<0;
    if (erase)
        toolWeight *= -1;

    doPaintAction(texIndex, image, UV, true, brushSize, erase, toolWeight);

    tex.getImage().setUpdateNeeded();
}
 
Example 19
Source Project: MikuMikuStudio   Source File: GdxRenderer.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
/*********************************************************************\
     |* Textures                                                          *|
     \*********************************************************************/
    private int convertTextureType(Texture.Type type) {
        switch (type) {
            case TwoDimensional:
                return GL20.GL_TEXTURE_2D;
            //        case TwoDimensionalArray:
            //            return EXTTextureArray.GL_TEXTURE_2D_ARRAY_EXT;
//            case ThreeDimensional:
            //               return GLES20.GL_TEXTURE_3D;
            case CubeMap:
                return GL20.GL_TEXTURE_CUBE_MAP;
            default:
                throw new UnsupportedOperationException("Unknown texture type: " + type);
        }
    }
 
Example 20
/**
 * Save the terrain's alpha maps to disk, in the Textures/terrain-alpha/ directory
 * @throws IOException
 */
private synchronized void doSaveAlphaImages(Terrain terrain) {

    if (terrain == null) {
        getTerrain(rootNode);
        return;
    }
    
    AssetManager manager = SceneApplication.getApplication().getAssetManager();
    String assetFolder = null;
    if (manager != null && manager instanceof ProjectAssetManager)
        assetFolder = ((ProjectAssetManager)manager).getAssetFolderName();
    if (assetFolder == null)
        throw new IllegalStateException("AssetManager was not a ProjectAssetManager. Could not locate image save directories.");

    Texture alpha1 = doGetAlphaTexture(terrain, 0);
    BufferedImage bi1 = ImageToAwt.convert(alpha1.getImage(), false, true, 0);
    File imageFile1 = new File(assetFolder+alpha1.getKey().getName());
    Texture alpha2 = doGetAlphaTexture(terrain, 1);
    BufferedImage bi2 = ImageToAwt.convert(alpha2.getImage(), false, true, 0);
    File imageFile2 = new File(assetFolder+alpha2.getKey().getName());
    Texture alpha3 = doGetAlphaTexture(terrain, 2);
    BufferedImage bi3 = ImageToAwt.convert(alpha3.getImage(), false, true, 0);
    File imageFile3 = new File(assetFolder+alpha3.getKey().getName());
    try {
        ImageIO.write(bi1, "png", imageFile1);
        ImageIO.write(bi2, "png", imageFile2);
        ImageIO.write(bi3, "png", imageFile3);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    
}
 
Example 21
Source Project: jmonkeybuilder   Source File: TextureLayerSettings.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Update selected layer.
 *
 * @param newValue the selected layer.
 */
@FxThread
private void updateSelectedLayer(@Nullable final TextureLayer newValue) {

    final int layer = newValue == null ? -1 : newValue.getLayer();
    final Texture alphaTexture = layer == -1 ? null : getAlpha(layer);

    final PaintTerrainToolControl paintToolControl = paintingComponent.getPaintToolControl();
    paintToolControl.setAlphaTexture(alphaTexture);
    paintToolControl.setLayer(layer);
}
 
Example 22
private Spatial doCreateSky(Node parent,
                            Texture west,
                            Texture east,
                            Texture north,
                            Texture south,
                            Texture top,
                            Texture bottom,
                            Vector3f normalScale)
{
    AssetManager manager = SceneApplication.getApplication().getAssetManager();
    Spatial sky = SkyFactory.createSky(manager, west, east, north, south, top, bottom, normalScale);
    parent.attachChild(sky);
    return sky;
}
 
Example 23
Source Project: jmonkeybuilder   Source File: TextureLayerSettings.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set a new normal texture to a level.
 *
 * @param texture the normal texture.
 * @param layer   the layer.
 */
@FromAnyThread
public void setNormal(@Nullable final Texture texture, final int layer) {

    final Function<Integer, String> layerToNormalName = getLayerToNormalName();
    if (layerToNormalName == null) {
        return;
    }

    final Terrain terrain = getTerrain();
    final Material material = terrain.getMaterial();
    final String paramName = layerToNormalName.apply(layer);
    final MatParam matParam = material.getParam(paramName);
    final Texture current = matParam == null ? null : (Texture) matParam.getValue();

    if (texture != null) {
        texture.setWrap(Texture.WrapMode.Repeat);
    }

    final PropertyOperation<ChangeConsumer, Node, Texture> operation =
            new PropertyOperation<>(getTerrainNode(), TERRAIN_PARAM, texture, current);

    operation.setApplyHandler((node, newTexture) ->
            NodeUtils.visitGeometry(node, geometry -> updateTexture(newTexture, paramName, geometry)));

    final ModelChangeConsumer changeConsumer = paintingComponent.getChangeConsumer();
    changeConsumer.execute(operation);
}
 
Example 24
Source Project: jmonkeybuilder   Source File: TextureLayer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get diffuse file.
 *
 * @return the diffuse file.
 */
@FromAnyThread
public @Nullable Path getDiffuseFile() {

    final Texture diffuse = settings.getDiffuse(getLayer());
    final AssetKey assetKey = diffuse == null ? null : diffuse.getKey();
    if (diffuse == null || assetKey == null) {
        return null;
    }

    return getRealFile(assetKey.getName());
}
 
Example 25
@Override
    public void simpleInitApp() {
        flyCam.setEnabled(false);
//        inputManager.setCursorVisible(false);

        Texture tex = assetManager.loadTexture("Interface/Logo/Cursor.png");
        
        cursor = new Picture("cursor");
        cursor.setTexture(assetManager, (Texture2D) tex, true);
        cursor.setWidth(64);
        cursor.setHeight(64);
        guiNode.attachChild(cursor);

        inputManager.addRawInputListener(inputListener);

//        Image img = tex.getImage();
//        ByteBuffer data = img.getData(0);
//        IntBuffer image = BufferUtils.createIntBuffer(64 * 64);
//        for (int y = 0; y < 64; y++){
//            for (int x = 0; x < 64; x++){
//                int rgba = data.getInt();
//                image.put(rgba);
//            }
//        }
//        image.clear();
//
//        try {
//            Cursor cur = new Cursor(64, 64, 2, 62, 1, image, null);
//            Mouse.setNativeCursor(cur);
//        } catch (LWJGLException ex) {
//            Logger.getLogger(TestSoftwareMouse.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
 
Example 26
private ViewPort setupMirrorBuffers(Camera cam, Texture tex, boolean expand) {   
	
	if (environment != null){
		if (environment.getApplication() != null){
	        Camera clonecam = cam.clone();
	        ViewPort viewPort = environment.getApplication().getRenderManager().createPostView("MirrorView", clonecam);
	        clonecam.setParallelProjection(true);
	        viewPort.setClearFlags(true, true, true);
	        viewPort.setBackgroundColor(ColorRGBA.Black);
	        Picture pic = new Picture("fullscene");
	        pic.setLocalTranslation(-0.75f, -0.5f, 0f);
	        if( expand ) {
	            pic.setLocalScale(3f, 1f, 1f);
	        } else {
	            pic.setLocalScale(1.5f, 1f, 1f);            
	        }
	        pic.setQueueBucket(Bucket.Opaque);
	        pic.setTexture(environment.getApplication().getAssetManager(), (Texture2D)tex, false);
	        viewPort.attachScene(pic);
	        viewPort.setOutputFrameBuffer(null);
	        
	        pic.updateGeometricState();
	        
	        return viewPort;
		} else {
			throw new IllegalStateException("This VR environment is not attached to any application.");
		}
	} else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
  	} 
}
 
Example 27
Texture loadTexture(String name) {
    try {
    Texture tex = textureMap.get(name);
    if (tex == null) {
        tex = assetManager.loadTexture(name);
        textureMap.put(name, tex);
    }
    return tex;
    } catch(Exception ex) {
        logger.log(Level.FINE,"Txture "+name+" not found.");
        return null;
    }
}
 
Example 28
public static Texture.WrapMode getWrapMode(Integer value) {
    if (value == null) {
        return Texture.WrapMode.Repeat;
    }
    switch (value) {
        case 33071:
            return Texture.WrapMode.EdgeClamp;
        case 33648:
            return Texture.WrapMode.MirroredRepeat;
        default:
            return Texture.WrapMode.Repeat;
    }
}
 
Example 29
@Override
protected int convertTextureType(Texture.Type type) {
    switch (type) {
        case TwoDimensional:
            return GL.GL_TEXTURE_2D;
        case TwoDimensionalArray:
            return GL.GL_TEXTURE_2D_ARRAY;
        case ThreeDimensional:
            return GL2GL3.GL_TEXTURE_3D;
        case CubeMap:
            return GL.GL_TEXTURE_CUBE_MAP;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + type);
    }
}
 
Example 30
public void setTexture( Texture t ) {
    if( this.texture == t )
        return;
    this.texture = t;
    if( material != null ) {
        material.setTexture(texture);
    }
}