This is the code I’m using:
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package medievalsoul;
import com.jme3.asset.TextureKey;
import com.jme3.ext.projectivetexturemapping.SimpleTextureProjector;
import com.jme3.ext.projectivetexturemapping.TextureProjectorRenderer;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.SceneProcessor;
import com.jme3.renderer.Camera;
import com.jme3.renderer.queue.GeometryList;
import com.jme3.renderer.queue.OpaqueComparator;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.debug.WireFrustum;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.medievalsoul.texture.TextureGrid;
/**
*
public class ProjectiveTexturing
{
private ProjectorData pd1;
private TextureProjectorRenderer ptr;
private GameManager gm;
private boolean showWireframe;
public ProjectiveTexturing(GameManager gm, List<Geometry> targets, Vector3f position, String texturePath, float size, boolean showWireframe, int tileAmount)
{
this.gm = gm;
this.showWireframe = showWireframe;
//"/Textures/rune.png"
Texture2D texture1 = new Texture2D(TextureGrid.renderGrid(texturePath, tileAmount));
pd1 = new ProjectorData();
initProjectorData(pd1, new Vector3f(position), texture1);
if(targets != null)
{
GeometryList gl = new GeometryList(new OpaqueComparator());
for(Geometry geo : targets)
gl.add(geo);
pd1.projector.setTargetGeometryList(gl);
}
else
{
pd1.projector.setFallOffDistance(2.1f);
pd1.projector.setFallOffPower(4f);
}
pd1.projector.getProjectorCamera().setFrustumPerspective(90f, 1f, size, 10f);
pd1.projector.getProjectorCamera().setParallelProjection(true);
ptr = new TextureProjectorRenderer(gm.getAssetManager());
ptr.getTextureProjectors().add(pd1.projector);
Logger.getLogger("").log(
Level.SEVERE, "NUM_PROJECTORS: {0}, NUM_PASSES: {1}", new Object[]{ptr.getTextureProjectors().size(), ptr.getTextureProjectors().size()});
//remove processor to avoid conflicts ex:glow filter
gm.getViewPort().getProcessors().removeAll(targets);
gm.getViewPort().addProcessor(ptr);
}
private void initProjectorData(ProjectorData pd, Vector3f location, Texture2D texture)
{
texture.setMinFilter(Texture.MinFilter.Trilinear);
texture.setMagFilter(Texture.MagFilter.Bilinear);
texture.setAnisotropicFilter(16);
//texture.setWrap(Texture.WrapMode.EdgeClamp);
texture.setWrap(Texture.WrapMode.BorderClamp);
int textureWidth = texture.getImage().getWidth();
int textureHeight = texture.getImage().getHeight();
float textureAspectRatio = ((float) textureWidth) / ((float) textureHeight);
pd.projector = new SimpleTextureProjector(texture);
Camera projectorCamera = pd.projector.getProjectorCamera();
projectorCamera.setLocation(location);
projectorCamera.lookAt(Vector3f.ZERO.clone(), Vector3f.UNIT_X.clone());
projectorCamera.setFrustumPerspective(45, textureAspectRatio, 1f, 5f);
projectorCamera.setParallelProjection(false);
pd.frustumPoints = new Vector3f[8];
for (int i = 0; i < 8; i++)
{
pd.frustumPoints[i] = new Vector3f();
}
pd.frustum = new WireFrustum(pd.frustumPoints);
Geometry frustumMdl = new Geometry("f", pd.frustum);
if(this.showWireframe)
frustumMdl.setCullHint(Spatial.CullHint.Never);
else
frustumMdl.setCullHint(Spatial.CullHint.Always);
frustumMdl.setShadowMode(RenderQueue.ShadowMode.Off);
frustumMdl.setMaterial(new Material(gm.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"));
frustumMdl.getMaterial().setColor("Color", ColorRGBA.White);
gm.getRootNode().attachChild(frustumMdl);
}
public void update(Vector3f position)
{
pd1.projector.getProjectorCamera().setLocation(new Vector3f(position));
pd1.projector.getProjectorCamera().lookAtDirection(Vector3f.UNIT_Y.negate(), Vector3f.UNIT_X.clone());
pd1.projector.updateFrustumPoints(pd1.frustumPoints);
pd1.frustum.update(pd1.frustumPoints);
}
public void setLocalTranslation(Vector3f position)
{
pd1.projector.getProjectorCamera().setLocation(new Vector3f(position));
}
private class ProjectorData
{
SimpleTextureProjector projector;
WireFrustum frustum;
Vector3f[] frustumPoints;
}
}
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package net.medievalsoul.texture;
import com.jme3.texture.Image;
import com.jme3.texture.Texture2D;
import com.jme3.texture.plugins.AWTLoader;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import jme3tools.converters.ImageToAwt;
import medievalsoul.GameManager;
/**
*
-
@author Miguel
*/
public class TextureGrid
{
private static GameManager gm;
public TextureGrid(GameManager gm)
{
this.gm = gm;
}
private static BufferedImage createTransformed(BufferedImage image, AffineTransform at)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.transform(at);
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
public static Image split(BufferedImage img, int tileAmount)
{
AffineTransform at = AffineTransform.getRotateInstance(Math.PI, img.getWidth()/2, img.getHeight()/2.0);
img = createTransformed(img, at);
BufferedImage pic = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = pic.getGraphics();
int width = pic.getWidth() / tileAmount;
int height = pic.getHeight() / tileAmount;
// Tile the image to fill our area.
for (int x = 0; x < pic.getWidth(); x += width) {
for (int y = 0; y < pic.getHeight(); y += height) {
g.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), x, y, null);
}
}
g.dispose();
AWTLoader loader = new AWTLoader();
Image image = loader.load(pic, true);
return image;
}
public static Image renderGrid(String imagePath, int gridSize)
{
//“com/jme3/app/Monkey.png”
Texture2D texture = (Texture2D) gm.getAssetManager().loadTexture(imagePath);
BufferedImage bf = ImageToAwt.convert(texture.getImage(), false, true, 0);
return split(bf, gridSize);
}
}
This is the bit of code that is generating the texture to use in the projective texture mapping:
Texture2D texture1 = new Texture2D(TextureGrid.renderGrid(texturePath, tileAmount));
At this point I have 9x9 grid that im generating when i enter in a combat area and is taking almost 10 seconds to load in my asus transformer, I want to be able to generate a 20x20 or a 30x30 faster.
I want to know if there are other options available.
Sorry if im not being clear, English is not my main language.