* New block language parser

* Rewrote J3M loader
 * AssetManager.unregisterLocator() must be implemented
 * Added support for material default vars
 * Apply NVIDIA spot light fix for TerrainLighting

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8024 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..rd 13 years ago
parent 0316bec713
commit db0a4f23a8
  1. 2
      engine/src/core-data/Common/MatDefs/Gui/Gui.j3md
  2. 4
      engine/src/core-data/Common/MatDefs/Light/Lighting.frag
  3. 8
      engine/src/core-data/Common/MatDefs/Light/Lighting.j3md
  4. 2
      engine/src/core-data/Common/MatDefs/Misc/Unshaded.j3md
  5. 582
      engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java
  6. 13
      engine/src/core/com/jme3/asset/AssetManager.java
  7. 3
      engine/src/core/com/jme3/material/MatParam.java
  8. 9
      engine/src/core/com/jme3/material/Material.java
  9. 14
      engine/src/core/com/jme3/material/MaterialDef.java
  10. 92
      engine/src/core/com/jme3/util/blockparser/BlockLanguageParser.java
  11. 61
      engine/src/core/com/jme3/util/blockparser/Statement.java
  12. 2
      engine/src/desktop/com/jme3/asset/DesktopAssetManager.java
  13. 2
      engine/src/niftygui/Common/MatDefs/Nifty/Nifty.j3md
  14. 8
      engine/src/terrain/Common/MatDefs/Terrain/TerrainLighting.frag
  15. 2
      engine/src/test/jme3test/asset/TestAbsoluteLocators.java
  16. 2
      engine/src/test/jme3test/light/TestSpotLightTerrain.java

@ -2,7 +2,7 @@ MaterialDef Default GUI {
MaterialParameters { MaterialParameters {
Texture2D Texture Texture2D Texture
Color Color : Color Color Color ( Color )
Boolean VertexColor Boolean VertexColor
} }

@ -2,7 +2,6 @@
#define ATTENUATION #define ATTENUATION
//#define HQ_ATTENUATION //#define HQ_ATTENUATION
varying vec2 texCoord; varying vec2 texCoord;
#ifdef SEPARATE_TEXCOORD #ifdef SEPARATE_TEXCOORD
varying vec2 texCoord2; varying vec2 texCoord2;
@ -51,9 +50,8 @@ varying vec3 SpecularSum;
#ifdef COLORRAMP #ifdef COLORRAMP
uniform sampler2D m_ColorRamp; uniform sampler2D m_ColorRamp;
#endif #endif
uniform float m_AlphaDiscardThreshold;
uniform float m_AlphaDiscardThreshold;
#ifndef VERTEX_LIGHTING #ifndef VERTEX_LIGHTING
uniform float m_Shininess; uniform float m_Shininess;

@ -38,16 +38,16 @@ MaterialDef Phong Lighting {
Boolean UseVertexColor Boolean UseVertexColor
// Ambient color // Ambient color
Color Ambient : MaterialAmbient Color Ambient (MaterialAmbient)
// Diffuse color // Diffuse color
Color Diffuse : MaterialDiffuse Color Diffuse (MaterialDiffuse)
// Specular color // Specular color
Color Specular : MaterialSpecular Color Specular (MaterialSpecular)
// Specular power/shininess // Specular power/shininess
Float Shininess : MaterialShininess Float Shininess (MaterialShininess) : 1
// Diffuse map // Diffuse map
Texture2D DiffuseMap Texture2D DiffuseMap

@ -3,7 +3,7 @@ MaterialDef Unshaded {
MaterialParameters { MaterialParameters {
Texture2D ColorMap Texture2D ColorMap
Texture2D LightMap Texture2D LightMap
Color Color : Color Color Color ( Color )
Boolean VertexColor Boolean VertexColor
Boolean SeparateTexCoord Boolean SeparateTexCoord

@ -47,7 +47,6 @@ import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode; import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.material.TechniqueDef.LightMode; import com.jme3.material.TechniqueDef.LightMode;
import com.jme3.material.TechniqueDef.ShadowMode; import com.jme3.material.TechniqueDef.ShadowMode;
import com.jme3.shader.Shader.ShaderType;
import com.jme3.shader.VarType; import com.jme3.shader.VarType;
import com.jme3.texture.Image; import com.jme3.texture.Image;
import com.jme3.texture.Image.Format; import com.jme3.texture.Image.Format;
@ -57,13 +56,13 @@ import com.jme3.util.BufferUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Locale; import java.util.List;
import java.util.Scanner; import com.jme3.util.blockparser.BlockLanguageParser;
import com.jme3.util.blockparser.Statement;
public class J3MLoader implements AssetLoader { public class J3MLoader implements AssetLoader {
private AssetManager owner; private AssetManager owner;
private Scanner scan;
private AssetKey key; private AssetKey key;
private MaterialDef materialDef; private MaterialDef materialDef;
@ -75,6 +74,8 @@ public class J3MLoader implements AssetLoader {
private String vertName; private String vertName;
private String fragName; private String fragName;
private static final String whitespacePattern = "\\p{javaWhitespace}+";
public J3MLoader(){ public J3MLoader(){
} }
@ -86,29 +87,6 @@ public class J3MLoader implements AssetLoader {
throw new IOException("Expected '"+expected+"', got '"+got+"'!"); throw new IOException("Expected '"+expected+"', got '"+got+"'!");
} }
private void nextStatement(){
while (true){
if (scan.hasNext("\\}")){
break;
}else if (scan.hasNext("[\n;]")){
scan.next();
}else if (scan.hasNext("//")){
scan.useDelimiter("\n");
scan.next();
scan.useDelimiter("\\p{javaWhitespace}+");
}else{
break;
}
}
}
private String readString(String end){
scan.useDelimiter(end);
String str = scan.next();
scan.useDelimiter("\\p{javaWhitespace}+");
return str.trim();
}
private Image createColorTexture(ColorRGBA color){ private Image createColorTexture(ColorRGBA color){
if (color.getAlpha() == 1.0f){ if (color.getAlpha() == 1.0f){
// create RGB texture // create RGB texture
@ -128,75 +106,49 @@ public class J3MLoader implements AssetLoader {
} }
} }
private void readShaderStatement(ShaderType type) throws IOException { // <TYPE> <LANG> : <SOURCE>
String lang = readString(":"); private void readShaderStatement(String statement) throws IOException {
String[] split = statement.split(":");
String word = scan.next(); if (split.length != 2){
throwIfNequal(":", word); throw new IOException("Shader statement syntax incorrect" + statement);
word = readString("[\n;(\\})]"); // new line, semicolon, comment or brace will end a statement
// locate source code
if (type == ShaderType.Vertex)
vertName = word;
else if (type == ShaderType.Fragment)
fragName = word;
shaderLang = lang;
} }
String[] typeAndLang = split[0].split(whitespacePattern);
private void readLightMode(){ if (typeAndLang.length != 2){
String mode = readString("[\n;(\\})]"); throw new IOException("Shader statement syntax incorrect: " + statement);
LightMode lm = LightMode.valueOf(mode);
technique.setLightMode(lm);
} }
shaderLang = typeAndLang[1];
private void readShadowMode(){ if (typeAndLang[0].equals("VertexShader")){
String mode = readString("[\n;(\\})]"); vertName = split[1].trim();
ShadowMode sm = ShadowMode.valueOf(mode); }else if (typeAndLang[0].equals("FragmentShader")){
technique.setShadowMode(sm); fragName = split[1].trim();
} }
private void readParam() throws IOException{
String word = scan.next();
VarType type;
if (word.equals("Color")){
type = VarType.Vector4;
}else{
type = VarType.valueOf(word);
} }
word = readString("[\n;(//)(\\})]"); // LightMode <MODE>
FixedFuncBinding ffBinding = null; private void readLightMode(String statement) throws IOException{
if (word.contains(":")){ String[] split = statement.split(whitespacePattern);
// contains fixed func binding if (split.length != 2){
String[] split = word.split(":"); throw new IOException("LightMode statement syntax incorrect");
word = split[0].trim();
try {
ffBinding = FixedFuncBinding.valueOf(split[1].trim());
} catch (IllegalArgumentException ex){
throw new IOException("FixedFuncBinding '" +
split[1] + "' does not exist!");
}
} }
// TODO: add support for default vals LightMode lm = LightMode.valueOf(split[1]);
materialDef.addMaterialParam(type, word, null, ffBinding); technique.setLightMode(lm);
} }
private void readValueParam() throws IOException{ // ShadowMode <MODE>
String name = readString(":"); private void readShadowMode(String statement) throws IOException{
throwIfNequal(":", scan.next()); String[] split = statement.split(whitespacePattern);
if (split.length != 2){
// parse value throw new IOException("ShadowMode statement syntax incorrect");
MatParam p = material.getMaterialDef().getMaterialParam(name); }
if (p == null) ShadowMode sm = ShadowMode.valueOf(split[1]);
throw new IOException("The material parameter: "+name+" is undefined."); technique.setShadowMode(sm);
}
VarType type = p.getVarType(); private Object readValue(VarType type, String value) throws IOException{
if (type.isTextureType()){ if (type.isTextureType()){
// String texturePath = readString("[\n;(//)(\\})]"); // String texturePath = readString("[\n;(//)(\\})]");
String texturePath = readString("[\n;(\\})]"); String texturePath = value.trim();
boolean flipY = false; boolean flipY = false;
boolean repeat = false; boolean repeat = false;
if (texturePath.startsWith("Flip Repeat ")){ if (texturePath.startsWith("Flip Repeat ")){
@ -219,98 +171,147 @@ public class J3MLoader implements AssetLoader {
if (tex != null){ if (tex != null){
if (repeat) if (repeat)
tex.setWrap(WrapMode.Repeat); tex.setWrap(WrapMode.Repeat);
material.setTextureParam(name, type, tex);
} }
return tex;
}else{ }else{
String[] split = value.trim().split(whitespacePattern);
switch (type){ switch (type){
case Float: case Float:
material.setParam(name, type, scan.nextFloat()); if (split.length != 1){
break; throw new IOException("Float value parameter must have 1 entry: " + value);
}
return Float.parseFloat(split[0]);
case Vector2: case Vector2:
material.setParam(name, type, new Vector2f(scan.nextFloat(), if (split.length != 2){
scan.nextFloat())); throw new IOException("Vector2 value parameter must have 2 entries: " + value);
break; }
return new Vector2f(Float.parseFloat(split[0]),
Float.parseFloat(split[1]));
case Vector3: case Vector3:
material.setParam(name, type, new Vector3f(scan.nextFloat(), if (split.length != 3){
scan.nextFloat(), throw new IOException("Vector3 value parameter must have 3 entries: " + value);
scan.nextFloat())); }
break; return new Vector3f(Float.parseFloat(split[0]),
Float.parseFloat(split[1]),
Float.parseFloat(split[2]));
case Vector4: case Vector4:
material.setParam(name, type, new ColorRGBA(scan.nextFloat(), if (split.length != 4){
scan.nextFloat(), throw new IOException("Vector4 value parameter must have 4 entries: " + value);
scan.nextFloat(), }
scan.nextFloat())); return new ColorRGBA(Float.parseFloat(split[0]),
break; Float.parseFloat(split[1]),
Float.parseFloat(split[2]),
Float.parseFloat(split[3]));
case Int: case Int:
material.setParam(name, type, scan.nextInt()); if (split.length != 1){
break; throw new IOException("Int value parameter must have 1 entry: " + value);
}
return Integer.parseInt(split[0]);
case Boolean: case Boolean:
material.setParam(name, type, scan.nextBoolean()); if (split.length != 1){
break; throw new IOException("Boolean value parameter must have 1 entry: " + value);
}
return Boolean.parseBoolean(split[0]);
default: default:
throw new UnsupportedOperationException("Unknown type: "+type); throw new UnsupportedOperationException("Unknown type: "+type);
} }
} }
} }
private void readMaterialParams() throws IOException{ // <TYPE> <NAME> [ "(" <FFBINDING> ")" ] [ ":" <DEFAULTVAL> ]
nextStatement(); private void readParam(String statement) throws IOException{
String name;
String word = scan.next(); String defaultVal = null;
throwIfNequal("{", word); FixedFuncBinding ffBinding = null;
nextStatement(); String[] split = statement.split(":");
while (true){ // Parse default val
if (scan.hasNext("\\}")){ if (split.length == 1){
scan.next(); // Doesn't contain default value
break; }else{
if (split.length != 2){
throw new IOException("Parameter statement syntax incorrect");
}
statement = split[0].trim();
defaultVal = split[1].trim();
} }
readParam(); // Parse ffbinding
nextStatement(); int startParen = statement.indexOf("(");
if (startParen != -1){
// get content inside parentheses
int endParen = statement.indexOf(")", startParen);
String bindingStr = statement.substring(startParen+1, endParen).trim();
try {
ffBinding = FixedFuncBinding.valueOf(bindingStr);
} catch (IllegalArgumentException ex){
throw new IOException("FixedFuncBinding '" +
split[1] + "' does not exist!");
} }
statement = statement.substring(0, startParen);
} }
private void readExtendingMaterialParams() throws IOException{ // Parse type + name
nextStatement(); split = statement.split(whitespacePattern);
if (split.length != 2){
throw new IOException("Parameter statement syntax incorrect");
}
String word = scan.next(); VarType type;
throwIfNequal("{", word); if (split[0].equals("Color")){
type = VarType.Vector4;
}else{
type = VarType.valueOf(split[0]);
}
nextStatement(); name = split[1];
while (true){ Object defaultValObj = null;
if (scan.hasNext("\\}")){ if (defaultVal != null){
scan.next(); readValue(type, defaultVal);
break;
} }
readValueParam(); materialDef.addMaterialParam(type, name, defaultValObj, ffBinding);
nextStatement();
}
} }
private void readWorldParams() throws IOException{ private void readValueParam(String statement) throws IOException{
nextStatement(); // Use limit=1 incase filename contains colons
String[] split = statement.split(":", 2);
if (split.length != 2){
throw new IOException("Value parameter statement syntax incorrect");
}
String name = split[0].trim();
String word = scan.next(); // parse value
throwIfNequal("{", word); MatParam p = material.getMaterialDef().getMaterialParam(name);
if (p == null){
throw new IOException("The material parameter: "+name+" is undefined.");
}
nextStatement(); Object valueObj = readValue(p.getVarType(), split[1]);
if (p.getVarType().isTextureType()){
material.setTextureParam(name, p.getVarType(), (Texture) valueObj);
}else{
material.setParam(name, p.getVarType(), valueObj);
}
}
while (true){ private void readMaterialParams(List<Statement> paramsList) throws IOException{
if (scan.hasNext("\\}")){ for (Statement statement : paramsList){
scan.next(); readParam(statement.getLine());
break; }
} }
word = readString("[\n;(//)(\\})]"); private void readExtendingMaterialParams(List<Statement> paramsList) throws IOException{
if (word != null && !word.equals("")){ for (Statement statement : paramsList){
technique.addWorldParam(word); readValueParam(statement.getLine());
}
} }
nextStatement();
private void readWorldParams(List<Statement> worldParams) throws IOException{
for (Statement statement : worldParams){
technique.addWorldParam(statement.getLine());
} }
} }
@ -318,174 +319,111 @@ public class J3MLoader implements AssetLoader {
return word != null && word.equals("On"); return word != null && word.equals("On");
} }
private void readRenderStateStatement() throws IOException{ private void readRenderStateStatement(String statement) throws IOException{
String word = scan.next(); String[] split = statement.split(whitespacePattern);
if (word.equals("Wireframe")){ if (split[0].equals("Wireframe")){
renderState.setWireframe(parseBoolean(scan.next())); renderState.setWireframe(parseBoolean(split[1]));
}else if (word.equals("FaceCull")){ }else if (split[0].equals("FaceCull")){
renderState.setFaceCullMode(FaceCullMode.valueOf(scan.next())); renderState.setFaceCullMode(FaceCullMode.valueOf(split[1]));
}else if (word.equals("DepthWrite")){ }else if (split[0].equals("DepthWrite")){
renderState.setDepthWrite(parseBoolean(scan.next())); renderState.setDepthWrite(parseBoolean(split[1]));
}else if (word.equals("DepthTest")){ }else if (split[0].equals("DepthTest")){
renderState.setDepthTest(parseBoolean(scan.next())); renderState.setDepthTest(parseBoolean(split[1]));
}else if (word.equals("Blend")){ }else if (split[0].equals("Blend")){
renderState.setBlendMode(BlendMode.valueOf(scan.next())); renderState.setBlendMode(BlendMode.valueOf(split[1]));
}else if (word.equals("AlphaTestFalloff")){ }else if (split[0].equals("AlphaTestFalloff")){
renderState.setAlphaTest(true); renderState.setAlphaTest(true);
renderState.setAlphaFallOff(scan.nextFloat()); renderState.setAlphaFallOff(Float.parseFloat(split[1]));
}else if (word.equals("PolyOffset")){ }else if (split[0].equals("PolyOffset")){
float factor = scan.nextFloat(); float factor = Float.parseFloat(split[1]);
float units = scan.nextFloat(); float units = Float.parseFloat(split[2]);
renderState.setPolyOffset(factor, units); renderState.setPolyOffset(factor, units);
}else if (word.equals("ColorWrite")){ }else if (split[0].equals("ColorWrite")){
renderState.setColorWrite(parseBoolean(scan.next())); renderState.setColorWrite(parseBoolean(split[1]));
}else if (word.equals("PointSprite")){ }else if (split[0].equals("PointSprite")){
renderState.setPointSprite(parseBoolean(scan.next())); renderState.setPointSprite(parseBoolean(split[1]));
}else{ }else{
throwIfNequal(null, word); throwIfNequal(null, split[0]);
} }
} }
private void readAdditionalRenderState() throws IOException{ private void readAdditionalRenderState(List<Statement> renderStates) throws IOException{
nextStatement();
String word = scan.next();
throwIfNequal("{", word);
nextStatement();
renderState = material.getAdditionalRenderState(); renderState = material.getAdditionalRenderState();
for (Statement statement : renderStates){
while (true){ readRenderStateStatement(statement.getLine());
if (scan.hasNext("\\}")){
scan.next();
break;
} }
readRenderStateStatement();
nextStatement();
}
renderState = null; renderState = null;
} }
private void readRenderState() throws IOException{ private void readRenderState(List<Statement> renderStates) throws IOException{
nextStatement();
String word = scan.next();
throwIfNequal("{", word);
nextStatement();
renderState = new RenderState(); renderState = new RenderState();
for (Statement statement : renderStates){
while (true){ readRenderStateStatement(statement.getLine());
if (scan.hasNext("\\}")){
scan.next();
break;
}
readRenderStateStatement();
nextStatement();
} }
technique.setRenderState(renderState); technique.setRenderState(renderState);
renderState = null; renderState = null;
} }
private void readDefine(){ // <DEFINENAME> [ ":" <PARAMNAME> ]
// stops at either next statement or colon private void readDefine(String statement) throws IOException{
// ways to end a statement: String[] split = statement.split(":");
/* if (split.length == 1){
Block {
Statement<new line>
Statement;
Statement //comment
Statement }
*/
String defineName = readString("[\n;:(//)(\\})]");
if (defineName.equals(""))
return;
String matParamName = null;
if (scan.hasNext(":")){
scan.next();
// this time without colon
matParamName = readString("[\n;(//)(\\})]");
// add define <-> param mapping
technique.addShaderParamDefine(matParamName, defineName);
}else{
// add preset define // add preset define
technique.addShaderPresetDefine(defineName, VarType.Boolean, true); technique.addShaderPresetDefine(split[0].trim(), VarType.Boolean, true);
}else if (split.length == 2){
technique.addShaderParamDefine(split[1].trim(), split[0].trim());
}else{
throw new IOException("Define syntax incorrect");
} }
} }
private void readDefines() throws IOException{ private void readDefines(List<Statement> defineList) throws IOException{
nextStatement(); for (Statement statement : defineList){
readDefine(statement.getLine());
String word = scan.next();
throwIfNequal("{", word);
nextStatement();
while (true){
if (scan.hasNext("\\}")){
scan.next();
break;
}
readDefine();
nextStatement();
} }
} }
private void readTechniqueStatement() throws IOException{ private void readTechniqueStatement(Statement statement) throws IOException{
String word = scan.next(); String[] split = statement.getLine().split("[ \\{]");
if (word.equals("VertexShader")){ if (split[0].equals("VertexShader") ||
readShaderStatement(ShaderType.Vertex); split[0].equals("FragmentShader")){
}else if (word.equals("FragmentShader")){ readShaderStatement(statement.getLine());
readShaderStatement(ShaderType.Fragment); }else if (split[0].equals("LightMode")){
}else if (word.equals("LightMode")){ readLightMode(statement.getLine());
readLightMode(); }else if (split[0].equals("ShadowMode")){
}else if (word.equals("ShadowMode")){ readShadowMode(statement.getLine());
readShadowMode(); }else if (split[0].equals("WorldParameters")){
}else if (word.equals("WorldParameters")){ readWorldParams(statement.getContents());
readWorldParams(); }else if (split[0].equals("RenderState")){
}else if (word.equals("RenderState")){ readRenderState(statement.getContents());
readRenderState(); }else if (split[0].equals("Defines")){
}else if (word.equals("Defines")){ readDefines(statement.getContents());
readDefines();
}else{ }else{
throwIfNequal(null, word); throwIfNequal(null, split[0]);
} }
nextStatement();
} }
private void readTransparentStatement() throws IOException{ private void readTransparentStatement(String statement) throws IOException{
String on = readString("[\n;(\\})]"); String[] split = statement.split(whitespacePattern);
material.setTransparent(parseBoolean(on)); if (split.length != 2){
throw new IOException("Transparent statement syntax incorrect");
} }
material.setTransparent(parseBoolean(split[1]));
private void readTechnique() throws IOException{
String name = null;
if (!scan.hasNext("\\{")){
name = scan.next();
} }
technique = new TechniqueDef(name);
String word = scan.next();
throwIfNequal("{", word);
nextStatement();
while (true){ private void readTechnique(Statement techStat) throws IOException{
if (scan.hasNext("\\}")){ String[] split = techStat.getLine().split(whitespacePattern);
scan.next(); if (split.length == 1){
break; technique = new TechniqueDef(null);
}else if (split.length == 2){
technique = new TechniqueDef(split[1]);
}else{
throw new IOException("Technique statement syntax incorrect");
} }
readTechniqueStatement(); for (Statement statement : techStat.getContents()){
readTechniqueStatement(statement);
} }
if (vertName != null && fragName != null){ if (vertName != null && fragName != null){
@ -499,40 +437,44 @@ public class J3MLoader implements AssetLoader {
shaderLang = null; shaderLang = null;
} }
private void loadFromScanner() throws IOException{ private void loadFromRoot(List<Statement> roots) throws IOException{
nextStatement(); if (roots.size() == 2){
Statement exception = roots.get(0);
String line = exception.getLine();
if (line.startsWith("Exception")){
throw new AssetLoadException(line.substring("Exception ".length()));
}else{
throw new IOException("In multiroot material, expected first statement to be 'Exception'");
}
}else if (roots.size() != 1){
throw new IOException("Too many roots in J3M/J3MD file");
}
boolean extending = false; boolean extending = false;
String name = null; Statement materialStat = roots.get(0);
String word = scan.next(); String materialName = materialStat.getLine();
if (word.equals("Material")){ if (materialName.startsWith("MaterialDef")){
extending = true; materialName = materialName.substring("MaterialDef ".length()).trim();
}else if (word.equals("MaterialDef")){
extending = false; extending = false;
}else if (word.equals("Exception")){ }else if (materialName.startsWith("Material")){
String exception = scan.nextLine(); materialName = materialName.substring("Material ".length()).trim();
throw new AssetLoadException(exception); extending = true;
}else{ }else{
throw new IOException("Specified file is not a Material file"); throw new IOException("Specified file is not a Material file");
} }
nextStatement(); String[] split = materialName.split(":", 2);
word = readString("[(\\{)(//)\n:]"); if (materialName.equals("")){
if (word == null || word.equals(""))
throw new IOException("Material name cannot be empty"); throw new IOException("Material name cannot be empty");
}
name = word; if (split.length == 2){
nextStatement();
if (scan.hasNext(":")){
if (!extending){ if (!extending){
throw new IOException("Must use 'Material' when extending."); throw new IOException("Must use 'Material' when extending.");
} }
scan.next(); // skip colon String extendedMat = split[1].trim();
String extendedMat = readString("\\{");
MaterialDef def = (MaterialDef) owner.loadAsset(new AssetKey(extendedMat)); MaterialDef def = (MaterialDef) owner.loadAsset(new AssetKey(extendedMat));
if (def == null) if (def == null)
@ -541,45 +483,35 @@ public class J3MLoader implements AssetLoader {
material = new Material(def); material = new Material(def);
material.setKey(key); material.setKey(key);
// material.setAssetName(fileName); // material.setAssetName(fileName);
}else if (scan.hasNext("\\{")){ }else if (split.length == 1){
if (extending){ if (extending){
throw new IOException("Expected ':', got '{'"); throw new IOException("Expected ':', got '{'");
} }
materialDef = new MaterialDef(owner, name); materialDef = new MaterialDef(owner, materialName);
// NOTE: pass file name for defs so they can be loaded later // NOTE: pass file name for defs so they can be loaded later
materialDef.setAssetName(key.getName()); materialDef.setAssetName(key.getName());
} }else{
scan.next(); // skip { throw new IOException("Cannot use colon in material name/path");
nextStatement();
while (true){
if (scan.hasNext("\\}")){
scan.next();
break;
} }
word = scan.next(); for (Statement statement : materialStat.getContents()){
split = statement.getLine().split("[ \\{]");
String statType = split[0];
if (extending){ if (extending){
if (word.equals("MaterialParameters")){ if (statType.equals("MaterialParameters")){
readExtendingMaterialParams(); readExtendingMaterialParams(statement.getContents());
nextStatement(); }else if (statType.equals("AdditionalRenderState")){
}else if (word.equals("AdditionalRenderState")){ readAdditionalRenderState(statement.getContents());
readAdditionalRenderState(); }else if (statType.equals("Transparent")){
nextStatement(); readTransparentStatement(statement.getLine());
}else if (word.equals("Transparent")){
readTransparentStatement();
nextStatement();
} }
}else{ }else{
if (word.equals("Technique")){ if (statType.equals("Technique")){
readTechnique(); readTechnique(statement);
nextStatement(); }else if (statType.equals("MaterialParameters")){
}else if (word.equals("MaterialParameters")){ readMaterialParams(statement.getContents());
readMaterialParams();
nextStatement();
}else{ }else{
throw new IOException("Expected material statement, got '"+scan.next()+"'"); throw new IOException("Expected material statement, got '"+statType+"'");
} }
} }
} }
@ -590,10 +522,8 @@ public class J3MLoader implements AssetLoader {
InputStream in = info.openStream(); InputStream in = info.openStream();
try { try {
scan = new Scanner(in);
scan.useLocale(Locale.US);
key = info.getKey(); key = info.getKey();
loadFromScanner(); loadFromRoot(BlockLanguageParser.parse(in));
} finally { } finally {
if (in != null) if (in != null)
in.close(); in.close();

@ -79,6 +79,8 @@ public interface AssetManager {
* they were registered, to locate the asset by the {@link AssetKey}. * they were registered, to locate the asset by the {@link AssetKey}.
* Once an {@link AssetLocator} returns a non-null AssetInfo, it is sent * Once an {@link AssetLocator} returns a non-null AssetInfo, it is sent
* to the {@link AssetLoader} to load the asset. * to the {@link AssetLoader} to load the asset.
* Once a locator is registered, it can be removed via
* {@link #unregisterLocator(java.lang.String, java.lang.Class) }.
* *
* @param rootPath Specifies the root path from which to locate assets * @param rootPath Specifies the root path from which to locate assets
* for the given {@link AssetLocator}. The purpose of this parameter * for the given {@link AssetLocator}. The purpose of this parameter
@ -87,9 +89,20 @@ public interface AssetManager {
* *
* @see AssetLocator#setRootPath(java.lang.String) * @see AssetLocator#setRootPath(java.lang.String)
* @see AssetLocator#locate(com.jme3.asset.AssetManager, com.jme3.asset.AssetKey) * @see AssetLocator#locate(com.jme3.asset.AssetManager, com.jme3.asset.AssetKey)
* @see #unregisterLocator(java.lang.String, java.lang.Class)
*/ */
public void registerLocator(String rootPath, Class<? extends AssetLocator> locatorClass); public void registerLocator(String rootPath, Class<? extends AssetLocator> locatorClass);
/**
* Unregisters the given locator class. This essentially undoes the operation
* done by {@link #registerLocator(java.lang.String, java.lang.Class) }.
*
* @param rootPath Should be the same as the root path specified in {@link
* #registerLocator(java.lang.String, java.lang.Class) }.
* @param locatorClass The locator class to unregister
*/
public void unregisterLocator(String rootPath, Class<? extends AssetLocator> locatorClass);
/** /**
* Set an {@link AssetEventListener} to receive events from this * Set an {@link AssetEventListener} to receive events from this
* <code>AssetManager</code>. There can only be one {@link AssetEventListener} * <code>AssetManager</code>. There can only be one {@link AssetEventListener}

@ -128,7 +128,8 @@ public class MatParam implements Savable, Cloneable {
* Returns the value of this material parameter. * Returns the value of this material parameter.
* <p> * <p>
* Material parameters that are used for material definitions * Material parameters that are used for material definitions
* will not have a value. * will not have a value, unless there's a default value declared
* in the definition.
* *
* @return the value of this material parameter. * @return the value of this material parameter.
*/ */

@ -53,8 +53,6 @@ import com.jme3.math.Vector4f;
import com.jme3.renderer.Caps; import com.jme3.renderer.Caps;
import com.jme3.renderer.RenderManager; import com.jme3.renderer.RenderManager;
import com.jme3.renderer.Renderer; import com.jme3.renderer.Renderer;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.shader.Shader; import com.jme3.shader.Shader;
import com.jme3.shader.Uniform; import com.jme3.shader.Uniform;
@ -115,6 +113,13 @@ public class Material implements Asset, Cloneable, Savable, Comparable<Material>
throw new NullPointerException("Material definition cannot be null"); throw new NullPointerException("Material definition cannot be null");
} }
this.def = def; this.def = def;
// Load default values from definition (if any)
for (MatParam param : def.getMaterialParams()){
if (param.getValue() != null){
setParam(param.getName(), param.getVarType(), param.getValue());
}
}
} }
public Material(AssetManager contentMan, String defName) { public Material(AssetManager contentMan, String defName) {

@ -35,6 +35,7 @@ package com.jme3.material;
import com.jme3.asset.AssetManager; import com.jme3.asset.AssetManager;
import com.jme3.shader.VarType; import com.jme3.shader.VarType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -139,6 +140,19 @@ public class MaterialDef {
return matParams.get(name); return matParams.get(name);
} }
/**
* Returns a collection of all material parameters declared in this
* material definition.
* <p>
* Modifying the material parameters or the collection will lead
* to undefined results.
*
* @return All material parameters declared in this definition.
*/
public Collection<MatParam> getMaterialParams(){
return matParams.values();
}
/** /**
* Adds a new technique definition to this material definition. * Adds a new technique definition to this material definition.
* <p> * <p>

@ -0,0 +1,92 @@
package com.jme3.util.blockparser;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
public class BlockLanguageParser {
private Reader reader;
private ArrayList<Statement> statementStack = new ArrayList<Statement>();
private Statement lastStatement;
private int lineNumber = 1;
private BlockLanguageParser(){
}
private void reset(){
statementStack.clear();
statementStack.add(new Statement(0, "<root>"));
lastStatement = null;
lineNumber = 1;
}
private void pushStatement(StringBuilder buffer){
String content = buffer.toString().trim();
if (content.length() > 0){
// push last statement onto the list
lastStatement = new Statement(lineNumber, content);
Statement parent = statementStack.get(statementStack.size()-1);
parent.addStatement(lastStatement);
buffer.setLength(0);
}
}
private void load(InputStream in) throws IOException{
reset();
reader = new InputStreamReader(in);
StringBuilder buffer = new StringBuilder();
boolean insideComment = false;
char lastChar = '\0';
while (true){
int ci = reader.read();
char c = (char) ci;
if (c == '\r'){
continue;
}
if (insideComment && c == '\n'){
insideComment = false;
}else if (c == '/' && lastChar == '/'){
buffer.deleteCharAt(buffer.length()-1);
insideComment = true;
pushStatement(buffer);
lastChar = '\0';
}else if (!insideComment){
if (ci == -1 || c == '{' || c == '}' || c == '\n' || c == ';'){
pushStatement(buffer);
lastChar = '\0';
if (c == '{'){
// push last statement onto the stack
statementStack.add(lastStatement);
continue;
}else if (c == '}'){
// pop statement from stack
statementStack.remove(statementStack.size()-1);
continue;
}else if (c == '\n'){
lineNumber++;
}else if (ci == -1){
break;
}
}else{
buffer.append(c);
lastChar = c;
}
}
}
}
public static List<Statement> parse(InputStream in) throws IOException {
BlockLanguageParser parser = new BlockLanguageParser();
parser.load(in);
return parser.statementStack.get(0).getContents();
}
}

@ -0,0 +1,61 @@
package com.jme3.util.blockparser;
import java.util.ArrayList;
import java.util.List;
public class Statement {
private int lineNumber;
private String line;
private List<Statement> contents = new ArrayList<Statement>();
Statement(int lineNumber, String line) {
this.lineNumber = lineNumber;
this.line = line;
}
void addStatement(Statement statement){
// if (contents == null){
// contents = new ArrayList<Statement>();
// }
contents.add(statement);
}
public int getLineNumber(){
return lineNumber;
}
public String getLine() {
return line;
}
public List<Statement> getContents() {
return contents;
}
private String getIndent(int indent){
return " ".substring(0, indent);
}
private String toString(int indent){
StringBuilder sb = new StringBuilder();
sb.append(getIndent(indent));
sb.append(line);
if (contents != null){
sb.append(" {\n");
for (Statement statement : contents){
sb.append(statement.toString(indent+4));
sb.append("\n");
}
sb.append(getIndent(indent));
sb.append("}");
}
return sb.toString();
}
@Override
public String toString(){
return toString(0);
}
}

@ -143,7 +143,7 @@ public class DesktopAssetManager implements AssetManager {
} }
} }
public void unregisterLocator(String rootPath, Class<?> clazz){ public void unregisterLocator(String rootPath, Class<? extends AssetLocator> clazz){
handler.removeLocator(clazz, rootPath); handler.removeLocator(clazz, rootPath);
if (logger.isLoggable(Level.FINER)){ if (logger.isLoggable(Level.FINER)){
logger.log(Level.FINER, "Unregistered locator: {0}", logger.log(Level.FINER, "Unregistered locator: {0}",

@ -3,7 +3,7 @@ MaterialDef Default GUI {
MaterialParameters { MaterialParameters {
Texture2D Texture Texture2D Texture
Boolean UseTex Boolean UseTex
Vector4 Color : Color Vector4 Color (Color)
} }
Technique { Technique {

@ -654,10 +654,14 @@ void main(){
float innerAngleCos = floor(g_LightDirection.w) * 0.001; float innerAngleCos = floor(g_LightDirection.w) * 0.001;
float outerAngleCos = fract(g_LightDirection.w); float outerAngleCos = fract(g_LightDirection.w);
float innerMinusOuter = innerAngleCos - outerAngleCos; float innerMinusOuter = innerAngleCos - outerAngleCos;
spotFallOff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0);
spotFallOff = (curAngleCos - outerAngleCos) / innerMinusOuter;
if(spotFallOff <= 0.0){ if(spotFallOff <= 0.0){
gl_FragColor = AmbientSum * diffuseColor; gl_FragColor.rgb = AmbientSum * diffuseColor;
return; return;
}else{
spotFallOff = clamp(spotFallOff, 0.0, 1.0);
} }
} }

@ -44,7 +44,7 @@ public class TestAbsoluteLocators {
public static void main(String[] args){ public static void main(String[] args){
AssetManager am = new DesktopAssetManager(); AssetManager am = new DesktopAssetManager();
am.registerLoader(AWTLoader.class.getName(), "png"); am.registerLoader(AWTLoader.class.getName(), "jpg");
am.registerLoader(WAVLoader.class.getName(), "wav"); am.registerLoader(WAVLoader.class.getName(), "wav");
// register absolute locator // register absolute locator

@ -156,7 +156,7 @@ public class TestSpotLightTerrain extends SimpleApplication {
matTerrain.setFloat("DiffuseMap_3_scale", rockScale); matTerrain.setFloat("DiffuseMap_3_scale", rockScale);
// RIVER ROCK texture // RIVER ROCK texture
Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.png"); Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
riverRock.setWrap(WrapMode.Repeat); riverRock.setWrap(WrapMode.Repeat);
matTerrain.setTexture("DiffuseMap_4", riverRock); matTerrain.setTexture("DiffuseMap_4", riverRock);
matTerrain.setFloat("DiffuseMap_4_scale", rockScale); matTerrain.setFloat("DiffuseMap_4_scale", rockScale);

Loading…
Cancel
Save