* Nifty GUI now uses Unshaded.j3md for rendering which is cleaner

* Nifty GUI performance much faster, but global font text isn't taken into account in OGL1 mode (fix TBD)

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9624 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Sha..rd 13 years ago
parent 38ff6f7560
commit 2ab5e4abbe
  1. 6
      engine/src/core/com/jme3/font/BitmapText.java
  2. 170
      engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java
  3. 10
      engine/src/niftygui/com/jme3/niftygui/RenderFontJme.java
  4. 3
      engine/src/niftygui/com/jme3/niftygui/RenderImageJme.java

@ -361,17 +361,17 @@ public class BitmapText extends Node {
private void assemble() { private void assemble() {
// first generate quadlist // first generate quadlist
letters.update(); letters.update();
for (int i = 0; i < textPages.length; i++) { for (int i = 0; i < textPages.length; i++) {
textPages[i].assemble(letters); textPages[i].assemble(letters);
} }
needRefresh = false; needRefresh = false;
} }
public void render(RenderManager rm) { public void render(RenderManager rm, ColorRGBA color) {
for (BitmapTextPage page : textPages) { for (BitmapTextPage page : textPages) {
Material mat = page.getMaterial(); Material mat = page.getMaterial();
mat.setTexture("Texture", page.getTexture()); mat.setTexture("ColorMap", page.getTexture());
mat.setColor("Color", color);
mat.render(page, rm); mat.render(page, rm);
} }
} }

@ -31,6 +31,7 @@
*/ */
package com.jme3.niftygui; package com.jme3.niftygui;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText; import com.jme3.font.BitmapText;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.material.RenderState; import com.jme3.material.RenderState;
@ -63,13 +64,11 @@ public class RenderDeviceJme implements RenderDevice {
private NiftyJmeDisplay display; private NiftyJmeDisplay display;
private RenderManager rm; private RenderManager rm;
private Renderer r; private Renderer r;
private HashMap<String, BitmapText> textCacheLastFrame = new HashMap<String, BitmapText>(); private HashMap<CachedTextKey, BitmapText> textCacheLastFrame = new HashMap<CachedTextKey, BitmapText>();
private HashMap<String, BitmapText> textCacheCurrentFrame = new HashMap<String, BitmapText>(); private HashMap<CachedTextKey, BitmapText> textCacheCurrentFrame = new HashMap<CachedTextKey, BitmapText>();
private final Quad quad = new Quad(1, -1, true); private final Quad quad = new Quad(1, -1, true);
private final Geometry quadGeom = new Geometry("nifty-quad", quad); private final Geometry quadGeom = new Geometry("nifty-quad", quad);
private final Material niftyMat; private final Material unshadedMat;
private final Material niftyQuadMat;
private final Material niftyQuadGradMat;
private boolean clipWasSet = false; private boolean clipWasSet = false;
private BlendMode blendMode = null; private BlendMode blendMode = null;
private VertexBuffer quadDefaultTC = quad.getBuffer(Type.TexCoord); private VertexBuffer quadDefaultTC = quad.getBuffer(Type.TexCoord);
@ -78,6 +77,36 @@ public class RenderDeviceJme implements RenderDevice {
private Matrix4f tempMat = new Matrix4f(); private Matrix4f tempMat = new Matrix4f();
private ColorRGBA tempColor = new ColorRGBA(); private ColorRGBA tempColor = new ColorRGBA();
private static class CachedTextKey {
BitmapFont font;
String text;
// ColorRGBA color;
public CachedTextKey(BitmapFont font, String text/*, ColorRGBA color*/) {
this.font = font;
this.text = text;
// this.color = color;
}
@Override
public boolean equals(Object other) {
CachedTextKey otherKey = (CachedTextKey) other;
return font.equals(otherKey.font) &&
text.equals(otherKey.text)/* &&
color.equals(otherKey.color)*/;
}
@Override
public int hashCode() {
int hash = 5;
hash = 53 * hash + font.hashCode();
hash = 53 * hash + text.hashCode();
// hash = 53 * hash + color.hashCode();
return hash;
}
}
public RenderDeviceJme(NiftyJmeDisplay display) { public RenderDeviceJme(NiftyJmeDisplay display) {
this.display = display; this.display = display;
@ -89,17 +118,10 @@ public class RenderDeviceJme implements RenderDevice {
quadModTC.setUsage(Usage.Stream); quadModTC.setUsage(Usage.Stream);
//Color + texture color material for text and images // GUI material
niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyTex.j3md"); unshadedMat = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
niftyMat.getAdditionalRenderState().setDepthTest(false); unshadedMat.getAdditionalRenderState().setDepthTest(false);
//Color material for uniform colored quads unshadedMat.getAdditionalRenderState().setDepthWrite(false);
niftyQuadMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuad.j3md");
niftyQuadMat.getAdditionalRenderState().setDepthTest(false);
//vertex color only for gradient quads (although i didn't find a way in nifty to make a gradient using vertex color)
niftyQuadGradMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuadGrad.j3md");
niftyQuadGradMat.getAdditionalRenderState().setDepthTest(false);
} }
public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) { public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) {
@ -113,7 +135,6 @@ public class RenderDeviceJme implements RenderDevice {
// TODO: Cursor support // TODO: Cursor support
public MouseCursor createMouseCursor(String str, int x, int y) { public MouseCursor createMouseCursor(String str, int x, int y) {
return new MouseCursor() { return new MouseCursor() {
public void dispose() { public void dispose() {
} }
}; };
@ -126,6 +147,7 @@ public class RenderDeviceJme implements RenderDevice {
} }
public RenderImage createImage(String filename, boolean linear) { public RenderImage createImage(String filename, boolean linear) {
//System.out.println("createImage(" + filename + ", " + linear + ")");
return new RenderImageJme(filename, linear, display); return new RenderImageJme(filename, linear, display);
} }
@ -137,12 +159,10 @@ public class RenderDeviceJme implements RenderDevice {
} }
public void endFrame() { public void endFrame() {
HashMap<String, BitmapText> temp = textCacheLastFrame; HashMap<CachedTextKey, BitmapText> temp = textCacheLastFrame;
textCacheLastFrame = textCacheCurrentFrame; textCacheLastFrame = textCacheCurrentFrame;
textCacheCurrentFrame = temp; textCacheCurrentFrame = temp;
textCacheCurrentFrame.clear(); textCacheCurrentFrame.clear();
// System.exit(1);
} }
public int getWidth() { public int getWidth() {
@ -187,35 +207,6 @@ public class RenderDeviceJme implements RenderDevice {
return outColor.set(inColor.getRed(), inColor.getGreen(), inColor.getBlue(), inColor.getAlpha()); return outColor.set(inColor.getRed(), inColor.getGreen(), inColor.getBlue(), inColor.getAlpha());
} }
// private void setColor(Color color) {
// ByteBuffer buf = (ByteBuffer) quadColor.getData();
// buf.rewind();
//
// int color2 = convertColor(color);
// buf.putInt(color2);
// buf.putInt(color2);
// buf.putInt(color2);
// buf.putInt(color2);
//
// buf.flip();
// quadColor.updateData(buf);
// }
/**
*
* @param font
* @param str
* @param x
* @param y
* @param color
* @param size
* @deprecated use renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) instead
*/
@Deprecated
public void renderFont(RenderFont font, String str, int x, int y, Color color, float size) {
renderFont(font, str, x, y, color, size, size);
}
@Override @Override
public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) { public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY) {
if (str.length() == 0) { if (str.length() == 0) {
@ -228,7 +219,8 @@ public class RenderDeviceJme implements RenderDevice {
RenderFontJme jmeFont = (RenderFontJme) font; RenderFontJme jmeFont = (RenderFontJme) font;
String key = font + str + color.getColorString(); ColorRGBA colorRgba = convertColor(color, tempColor);
CachedTextKey key = new CachedTextKey(jmeFont.getFont(), str/*, colorRgba*/);
BitmapText text = textCacheLastFrame.get(key); BitmapText text = textCacheLastFrame.get(key);
if (text == null) { if (text == null) {
text = jmeFont.createText(); text = jmeFont.createText();
@ -237,19 +229,24 @@ public class RenderDeviceJme implements RenderDevice {
} }
textCacheCurrentFrame.put(key, text); textCacheCurrentFrame.put(key, text);
niftyMat.setColor("Color", convertColor(color, tempColor)); // unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); // unshadedMat.setBoolean("VertexColor", true);
// niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); // unshadedMat.setColor("Color", convertColor(color, tempColor));
text.setMaterial(niftyMat); // text.setMaterial(unshadedMat);
float width = text.getLineWidth();
// float height = text.getLineHeight();
float x0 = x + 0.5f * width * (1f - sizeX);
float y0 = y; // + 0.5f * height * (1f - sizeY);
tempMat.loadIdentity(); tempMat.loadIdentity();
tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setTranslation(x0, getHeight() - y0, 0);
tempMat.setScale(sizeX, sizeY, 0); tempMat.setScale(sizeX, sizeY, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
text.render(rm); text.render(rm, colorRgba);
// System.out.println("renderFont"); // System.out.format("renderFont(%s, %s, %d, %d, %s, %f, %f)\n", jmeFont.getFont(), str, x, y, color.toString(), sizeX, sizeY);
} }
public void renderImage(RenderImage image, int x, int y, int w, int h, public void renderImage(RenderImage image, int x, int y, int w, int h,
@ -259,10 +256,10 @@ public class RenderDeviceJme implements RenderDevice {
RenderImageJme jmeImage = (RenderImageJme) image; RenderImageJme jmeImage = (RenderImageJme) image;
Texture2D texture = jmeImage.getTexture(); Texture2D texture = jmeImage.getTexture();
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", convertColor(color, tempColor)); unshadedMat.setColor("Color", convertColor(color, tempColor));
niftyMat.setTexture("Texture", texture); unshadedMat.setTexture("ColorMap", texture);
//setColor(color); unshadedMat.setBoolean("VertexColor", false);
float imageWidth = jmeImage.getWidth(); float imageWidth = jmeImage.getWidth();
float imageHeight = jmeImage.getHeight(); float imageHeight = jmeImage.getHeight();
@ -295,9 +292,11 @@ public class RenderDeviceJme implements RenderDevice {
tempMat.setScale(w * scale, h * scale, 0); tempMat.setScale(w * scale, h * scale, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyMat.render(quadGeom, rm); unshadedMat.render(quadGeom, rm);
//
// System.out.println("renderImage (Sub)"); //System.out.format("renderImage2(%s, %d, %d, %d, %d, %d, %d, %d, %d, %s, %f, %d, %d)\n", texture.getKey().toString(),
// x, y, w, h, srcX, srcY, srcW, srcH,
// color.toString(), scale, centerX, centerY);
} }
public void renderImage(RenderImage image, int x, int y, int width, int height, public void renderImage(RenderImage image, int x, int y, int width, int height,
@ -305,10 +304,10 @@ public class RenderDeviceJme implements RenderDevice {
RenderImageJme jmeImage = (RenderImageJme) image; RenderImageJme jmeImage = (RenderImageJme) image;
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", convertColor(color, tempColor)); unshadedMat.setColor("Color", convertColor(color, tempColor));
niftyMat.setTexture("Texture", jmeImage.getTexture()); unshadedMat.setTexture("ColorMap", jmeImage.getTexture());
//setColor(color); unshadedMat.setBoolean("VertexColor", false);
quad.clearBuffer(Type.TexCoord); quad.clearBuffer(Type.TexCoord);
quad.setBuffer(quadDefaultTC); quad.setBuffer(quadDefaultTC);
@ -321,24 +320,25 @@ public class RenderDeviceJme implements RenderDevice {
tempMat.setScale(width * imageScale, height * imageScale, 0); tempMat.setScale(width * imageScale, height * imageScale, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyMat.render(quadGeom, rm); unshadedMat.render(quadGeom, rm);
//
// System.out.println("renderImage"); //System.out.format("renderImage1(%s, %d, %d, %d, %d, %s, %f)\n", jmeImage.getTexture().getKey().toString(), x, y, width, height, color.toString(), imageScale);
} }
public void renderQuad(int x, int y, int width, int height, Color color) { public void renderQuad(int x, int y, int width, int height, Color color) {
if (color.getAlpha() > 0) { unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); unshadedMat.setColor("Color", convertColor(color, tempColor));
niftyQuadMat.setColor("Color", convertColor(color, tempColor)); unshadedMat.setTexture("ColorMap", null);
unshadedMat.setBoolean("VertexColor", false);
tempMat.loadIdentity(); tempMat.loadIdentity();
tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setTranslation(x, getHeight() - y, 0);
tempMat.setScale(width, height, 0); tempMat.setScale(width, height, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyQuadMat.render(quadGeom, rm); unshadedMat.render(quadGeom, rm);
}
// System.out.println("renderQuad (Solid)"); //System.out.format("renderQuad1(%d, %d, %d, %d, %s)\n", x, y, width, height, color.toString());
} }
public void renderQuad(int x, int y, int width, int height, public void renderQuad(int x, int y, int width, int height,
@ -356,26 +356,30 @@ public class RenderDeviceJme implements RenderDevice {
buf.flip(); buf.flip();
quadColor.updateData(buf); quadColor.updateData(buf);
niftyQuadGradMat.getAdditionalRenderState().setBlendMode(convertBlend()); unshadedMat.getAdditionalRenderState().setBlendMode(convertBlend());
unshadedMat.setColor("Color", ColorRGBA.White);
unshadedMat.setTexture("ColorMap", null);
unshadedMat.setBoolean("VertexColor", true);
tempMat.loadIdentity(); tempMat.loadIdentity();
tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setTranslation(x, getHeight() - y, 0);
tempMat.setScale(width, height, 0); tempMat.setScale(width, height, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyQuadGradMat.render(quadGeom, rm); unshadedMat.render(quadGeom, rm);
//
// System.out.println("renderQuad (Grad)"); //System.out.format("renderQuad2(%d, %d, %d, %d, %s, %s, %s, %s)\n", x, y, width, height, topLeft.toString(),
// topRight.toString(),
// bottomRight.toString(),
// bottomLeft.toString());
} }
public void enableClip(int x0, int y0, int x1, int y1) { public void enableClip(int x0, int y0, int x1, int y1) {
// System.out.println("enableClip");
clipWasSet = true; clipWasSet = true;
r.setClipRect(x0, getHeight() - y1, x1 - x0, y1 - y0); r.setClipRect(x0, getHeight() - y1, x1 - x0, y1 - y0);
} }
public void disableClip() { public void disableClip() {
// System.out.println("disableClip");
if (clipWasSet) { if (clipWasSet) {
r.clearClipRect(); r.clearClipRect();
clipWasSet = false; clipWasSet = false;

@ -50,9 +50,6 @@ public class RenderFontJme implements RenderFont {
public RenderFontJme(String name, NiftyJmeDisplay display) { public RenderFontJme(String name, NiftyJmeDisplay display) {
this.display = display; this.display = display;
font = display.getAssetManager().loadFont(name); font = display.getAssetManager().loadFont(name);
if (font == null) {
throw new RuntimeException( "Font not loaded:" + name );
}
text = new BitmapText(font); text = new BitmapText(font);
actualSize = font.getPreferredSize(); actualSize = font.getPreferredSize();
text.setSize(actualSize); text.setSize(actualSize);
@ -62,6 +59,10 @@ public class RenderFontJme implements RenderFont {
return new BitmapText(font); return new BitmapText(font);
} }
public BitmapFont getFont() {
return font;
}
public BitmapText getText(){ public BitmapText getText(){
return text; return text;
} }
@ -80,8 +81,9 @@ public class RenderFontJme implements RenderFont {
* @return width of the given text for the current font * @return width of the given text for the current font
*/ */
public int getWidth(final String str) { public int getWidth(final String str) {
if (str.length() == 0) if (str.length() == 0) {
return 0; return 0;
}
// Note: BitmapFont is now fixed to return the proper line width // Note: BitmapFont is now fixed to return the proper line width
// at least for now. The older commented out (by someone else, not me) // at least for now. The older commented out (by someone else, not me)

@ -63,8 +63,9 @@ public class RenderImageJme implements RenderImage {
} }
public RenderImageJme(Texture2D texture){ public RenderImageJme(Texture2D texture){
if (texture.getImage() == null) if (texture.getImage() == null) {
throw new IllegalArgumentException("texture.getImage() cannot be null"); throw new IllegalArgumentException("texture.getImage() cannot be null");
}
this.texture = texture; this.texture = texture;
this.image = texture.getImage(); this.image = texture.getImage();

Loading…
Cancel
Save