import java.util.logging.Logger;\r
\r
import com.jme3.asset.BlenderKey.FeaturesToLoad;\r
-import com.jme3.asset.TextureKey;\r
import com.jme3.material.MatParam;\r
import com.jme3.material.Material;\r
import com.jme3.material.Material.MatParamTexture;\r
import com.jme3.util.BufferUtils;\r
\r
public class MaterialHelper extends AbstractBlenderHelper {\r
- private static final Logger LOGGER = Logger.getLogger(MaterialHelper.class.getName());\r
- protected static final float DEFAULT_SHININESS = 20.0f;\r
-\r
- public static final String TEXTURE_TYPE_COLOR = "ColorMap";\r
- public static final String TEXTURE_TYPE_DIFFUSE = "DiffuseMap";\r
- public static final String TEXTURE_TYPE_NORMAL = "NormalMap";\r
- public static final String TEXTURE_TYPE_SPECULAR = "SpecularMap";\r
- public static final String TEXTURE_TYPE_GLOW = "GlowMap";\r
- public static final String TEXTURE_TYPE_ALPHA = "AlphaMap";\r
-\r
- public static final Integer ALPHA_MASK_NONE = Integer.valueOf(0);\r
- public static final Integer ALPHA_MASK_CIRCLE = Integer.valueOf(1);\r
- public static final Integer ALPHA_MASK_CONE = Integer.valueOf(2);\r
- public static final Integer ALPHA_MASK_HYPERBOLE = Integer.valueOf(3);\r
- protected final Map<Integer, IAlphaMask> alphaMasks = new HashMap<Integer, IAlphaMask>();\r
- \r
+ private static final Logger LOGGER = Logger.getLogger(MaterialHelper.class.getName());\r
+ protected static final float DEFAULT_SHININESS = 20.0f;\r
+\r
+ public static final String TEXTURE_TYPE_COLOR = "ColorMap";\r
+ public static final String TEXTURE_TYPE_DIFFUSE = "DiffuseMap";\r
+ public static final String TEXTURE_TYPE_NORMAL = "NormalMap";\r
+ public static final String TEXTURE_TYPE_SPECULAR = "SpecularMap";\r
+ public static final String TEXTURE_TYPE_GLOW = "GlowMap";\r
+ public static final String TEXTURE_TYPE_ALPHA = "AlphaMap";\r
+\r
+ public static final Integer ALPHA_MASK_NONE = Integer.valueOf(0);\r
+ public static final Integer ALPHA_MASK_CIRCLE = Integer.valueOf(1);\r
+ public static final Integer ALPHA_MASK_CONE = Integer.valueOf(2);\r
+ public static final Integer ALPHA_MASK_HYPERBOLE = Integer.valueOf(3);\r
+ protected final Map<Integer, IAlphaMask> alphaMasks = new HashMap<Integer, IAlphaMask>();\r
+\r
/**\r
* The type of the material's diffuse shader.\r
*/\r
* versions.\r
* \r
* @param blenderVersion\r
- * the version read from the blend file\r
+ * the version read from the blend file\r
*/\r
public MaterialHelper(String blenderVersion) {\r
super(blenderVersion);\r
- //setting alpha masks\r
+ // setting alpha masks\r
alphaMasks.put(ALPHA_MASK_NONE, new IAlphaMask() {\r
@Override\r
public void setImageSize(int width, int height) {}\r
- \r
+\r
@Override\r
public byte getAlpha(float x, float y) {\r
- return (byte)255;\r
+ return (byte) 255;\r
}\r
});\r
alphaMasks.put(ALPHA_MASK_CIRCLE, new IAlphaMask() {\r
- private float r;\r
- private float[] center;\r
- \r
+ private float r;\r
+ private float[] center;\r
+\r
@Override\r
public void setImageSize(int width, int height) {\r
r = Math.min(width, height) * 0.5f;\r
- center = new float[] {width*0.5f, height * 0.5f};\r
+ center = new float[] { width * 0.5f, height * 0.5f };\r
}\r
- \r
+\r
@Override\r
public byte getAlpha(float x, float y) {\r
- float d = FastMath.abs(FastMath.sqrt((x-center[0])*(x-center[0]) + (y-center[1])*(y-center[1])));\r
- return (byte)(d>=r ? 0 : 255);\r
+ float d = FastMath.abs(FastMath.sqrt((x - center[0]) * (x - center[0]) + (y - center[1]) * (y - center[1])));\r
+ return (byte) (d >= r ? 0 : 255);\r
}\r
});\r
alphaMasks.put(ALPHA_MASK_CONE, new IAlphaMask() {\r
- private float r;\r
- private float[] center;\r
- \r
+ private float r;\r
+ private float[] center;\r
+\r
@Override\r
public void setImageSize(int width, int height) {\r
r = Math.min(width, height) * 0.5f;\r
- center = new float[] {width*0.5f, height * 0.5f};\r
+ center = new float[] { width * 0.5f, height * 0.5f };\r
}\r
- \r
+\r
@Override\r
public byte getAlpha(float x, float y) {\r
- float d = FastMath.abs(FastMath.sqrt((x-center[0])*(x-center[0]) + (y-center[1])*(y-center[1])));\r
- return (byte)(d>=r ? 0 : -255.0f*d/r+255.0f);\r
+ float d = FastMath.abs(FastMath.sqrt((x - center[0]) * (x - center[0]) + (y - center[1]) * (y - center[1])));\r
+ return (byte) (d >= r ? 0 : -255.0f * d / r + 255.0f);\r
}\r
});\r
alphaMasks.put(ALPHA_MASK_HYPERBOLE, new IAlphaMask() {\r
- private float r;\r
- private float[] center;\r
- \r
+ private float r;\r
+ private float[] center;\r
+\r
@Override\r
public void setImageSize(int width, int height) {\r
r = Math.min(width, height) * 0.5f;\r
- center = new float[] {width*0.5f, height * 0.5f};\r
+ center = new float[] { width * 0.5f, height * 0.5f };\r
}\r
- \r
+\r
@Override\r
public byte getAlpha(float x, float y) {\r
- float d = FastMath.abs(FastMath.sqrt((x-center[0])*(x-center[0]) + (y-center[1])*(y-center[1]))) / r;\r
- return d>=1.0f ? 0 : (byte)((-FastMath.sqrt((2.0f-d)*d)+1.0f)*255.0f);\r
+ float d = FastMath.abs(FastMath.sqrt((x - center[0]) * (x - center[0]) + (y - center[1]) * (y - center[1]))) / r;\r
+ return d >= 1.0f ? 0 : (byte) ((-FastMath.sqrt((2.0f - d) * d) + 1.0f) * 255.0f);\r
}\r
});\r
}\r
* This method sets the face cull mode to be used with every loaded material.\r
* \r
* @param faceCullMode\r
- * the face cull mode\r
+ * the face cull mode\r
*/\r
public void setFaceCullMode(FaceCullMode faceCullMode) {\r
this.faceCullMode = faceCullMode;\r
List<Structure> mtex = p.fetchData(dataRepository.getInputStream());\r
if (mtex.size() == 1) {\r
Structure textureLink = mtex.get(0);\r
- int texflag = ((Number)textureLink.getFieldValue("texflag")).intValue();\r
- //int texco = ((Number) textureLink.getFieldValue("texco")).intValue();\r
- boolean negateTexture = (texflag & 0x04)==0;\r
- \r
+ int texflag = ((Number) textureLink.getFieldValue("texflag")).intValue();\r
+ // int texco = ((Number) textureLink.getFieldValue("texco")).intValue();\r
+ boolean negateTexture = (texflag & 0x04) == 0;\r
+\r
// if(texco == 0x10) {//TEXCO_UV (this is only supported now)\r
int mapto = ((Number) textureLink.getFieldValue("mapto")).intValue();\r
if (mapto != 0) {\r
result.setBoolean("UseMaterialColors", Boolean.FALSE);\r
// blending the texture with material color and texture's defined color\r
int blendType = ((Number) textureLink.getFieldValue("blendtype")).intValue();\r
- float[] color = new float[] { ((Number) textureLink.getFieldValue("r")).floatValue(),\r
- ((Number) textureLink.getFieldValue("g")).floatValue(),\r
- ((Number) textureLink.getFieldValue("b")).floatValue() };\r
+ float[] color = new float[] { ((Number) textureLink.getFieldValue("r")).floatValue(), ((Number) textureLink.getFieldValue("g")).floatValue(), ((Number) textureLink.getFieldValue("b")).floatValue() };\r
float colfac = ((Number) textureLink.getFieldValue("colfac")).floatValue();\r
- texture = textureHelper.blendTexture(diffuseColor.getColorArray(), texture, color, colfac, blendType,\r
- negateTexture, dataRepository);\r
+ texture = textureHelper.blendTexture(diffuseColor.getColorArray(), texture, color, colfac, blendType, negateTexture, dataRepository);\r
texture.setWrap(WrapMode.Repeat);\r
if (shadeless) {\r
result.setTexture(TEXTURE_TYPE_COLOR, texture);\r
* returned itself.\r
* \r
* @param material\r
- * a material to be cloned without textures\r
+ * a material to be cloned without textures\r
* @param imageType\r
- * type of image defined by blender; the constants are defined in TextureHelper\r
+ * type of image defined by blender; the constants are defined in TextureHelper\r
* @return material without textures of a specified type\r
*/\r
public Material getNonTexturedMaterial(Material material, int imageType) {\r
- String[] textureParamNames = new String[] { TEXTURE_TYPE_DIFFUSE, TEXTURE_TYPE_NORMAL, TEXTURE_TYPE_GLOW, TEXTURE_TYPE_SPECULAR,\r
- TEXTURE_TYPE_ALPHA };\r
+ String[] textureParamNames = new String[] { TEXTURE_TYPE_DIFFUSE, TEXTURE_TYPE_NORMAL, TEXTURE_TYPE_GLOW, TEXTURE_TYPE_SPECULAR, TEXTURE_TYPE_ALPHA };\r
Map<String, Texture> textures = new HashMap<String, Texture>(textureParamNames.length);\r
for (String textureParamName : textureParamNames) {\r
MatParamTexture matParamTexture = material.getTextureParam(textureParamName);\r
material.clearParam(textureParamName.getKey());\r
}\r
} catch (NumberFormatException e) {\r
- LOGGER.log(Level.WARNING, "The name of the texture does not contain the texture type value! {0} will not be removed!",\r
- name);\r
+ LOGGER.log(Level.WARNING, "The name of the texture does not contain the texture type value! {0} will not be removed!", name);\r
}\r
}\r
Material result = material.clone();\r
return result;\r
}\r
}\r
- \r
+\r
/**\r
* This method converts the given material into particles-usable material.\r
* The texture and glow color are being copied.\r
* The method assumes it receives the Lighting type of material.\r
- * @param material the source material\r
- * @param dataRepository the data repository\r
+ * @param material\r
+ * the source material\r
+ * @param dataRepository\r
+ * the data repository\r
* @return material converted into particles-usable material\r
*/\r
public Material getParticlesMaterial(Material material, Integer alphaMaskIndex, DataRepository dataRepository) {\r
Material result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Misc/Particle.j3md");\r
- \r
- //copying texture\r
+\r
+ // copying texture\r
MatParam diffuseMap = material.getParam("DiffuseMap");\r
- if(diffuseMap!=null) {\r
+ if (diffuseMap != null) {\r
Texture texture = ((Texture) diffuseMap.getValue()).clone();\r
- \r
- //applying alpha mask to the texture\r
+\r
+ // applying alpha mask to the texture\r
Image image = texture.getImage();\r
ByteBuffer sourceBB = image.getData(0);\r
sourceBB.rewind();\r
ByteBuffer bb = BufferUtils.createByteBuffer(w * h * 4);\r
IAlphaMask iAlphaMask = alphaMasks.get(alphaMaskIndex);\r
iAlphaMask.setImageSize(w, h);\r
- \r
- for(int x=0;x<w;++x) {\r
- for(int y=0;y<h;++y) {\r
+\r
+ for (int x = 0; x < w; ++x) {\r
+ for (int y = 0; y < h; ++y) {\r
bb.put(sourceBB.get());\r
bb.put(sourceBB.get());\r
bb.put(sourceBB.get());\r
\r
image = new Image(Format.RGBA8, w, h, bb);\r
texture.setImage(image);\r
- \r
+\r
result.setTextureParam("Texture", VarType.Texture2D, texture);\r
}\r
- \r
- //copying glow color\r
+\r
+ // copying glow color\r
MatParam glowColor = material.getParam("GlowColor");\r
- if(glowColor!=null) {\r
+ if (glowColor != null) {\r
ColorRGBA color = (ColorRGBA) glowColor.getValue();\r
result.setParam("GlowColor", VarType.Vector3, color);\r
}\r
return result;\r
}\r
- \r
- protected byte calculateAlpha(float x, float y) {\r
- return (byte)255;\r
- }\r
- \r
- protected Texture loadParticleAlphaMapTexture(DataRepository dataRepository) {\r
- TextureKey textureKey = new TextureKey(this.getClass().getPackage().getName().replace('.', '/') + "/particle_alpha_map.png");\r
- return dataRepository.getAssetManager().loadTexture(textureKey);\r
- }\r
\r
/**\r
* This method indicates if the material has a texture of a specified type.\r
* \r
* @param material\r
- * the material\r
+ * the material\r
* @param textureType\r
- * the type of the texture\r
+ * the type of the texture\r
* @return <b>true</b> if the texture exists in the material and <B>false</b> otherwise\r
*/\r
public boolean hasTexture(Material material, String textureType) {\r
* This method returns an enum describing the type of a diffuse shader used by this material.\r
* \r
* @param materialStructure\r
- * the material structure filled with data\r
+ * the material structure filled with data\r
* @return an enum describing the type of a diffuse shader used by this material\r
*/\r
public DiffuseShader getDiffuseShader(Structure materialStructure) {\r
* This method returns an ambient color used by the material.\r
* \r
* @param materialStructure\r
- * the material structure filled with data\r
+ * the material structure filled with data\r
* @return an ambient color used by the material\r
*/\r
public ColorRGBA getAmbientColor(Structure materialStructure) {\r
* This method returns an enum describing the type of a specular shader used by this material.\r
* \r
* @param materialStructure\r
- * the material structure filled with data\r
+ * the material structure filled with data\r
* @return an enum describing the type of a specular shader used by this material\r
*/\r
public SpecularShader getSpecularShader(Structure materialStructure) {\r
* This method returns a specular color used by the material.\r
* \r
* @param materialStructure\r
- * the material structure filled with data\r
+ * the material structure filled with data\r
* @return a specular color used by the material\r
*/\r
public ColorRGBA getSpecularColor(Structure materialStructure, SpecularShader specularShader) {\r
* This method returns the sihiness of this material or DEFAULT_SHININESS value if not present.\r
* \r
* @param materialStructure\r
- * the material structure filled with data\r
+ * the material structure filled with data\r
* @return the sihiness of this material or DEFAULT_SHININESS value if not present\r
*/\r
public float getShininess(Structure materialStructure) {\r
* curve) but needs to have 'mat' field/\r
* \r
* @param structureWithMaterials\r
- * the structure containing the mesh data\r
+ * the structure containing the mesh data\r
* @param dataRepository\r
- * the data repository\r
+ * the data repository\r
* @return a list of vertices colors, each color belongs to a single vertex\r
* @throws BlenderFileException\r
- * this exception is thrown when the blend file structure is somehow invalid or corrupted\r
+ * this exception is thrown when the blend file structure is somehow invalid or corrupted\r
*/\r
public Material[] getMaterials(Structure structureWithMaterials, DataRepository dataRepository) throws BlenderFileException {\r
Pointer ppMaterials = (Pointer) structureWithMaterials.getFieldValue("mat");\r
materials = new Material[materialStructures.size()];\r
int i = 0;\r
for (Structure s : materialStructures) {\r
- Material material = (Material) dataRepository.getLoadedFeature(s.getOldMemoryAddress(),\r
- LoadedFeatureDataType.LOADED_FEATURE);\r
+ Material material = (Material) dataRepository.getLoadedFeature(s.getOldMemoryAddress(), LoadedFeatureDataType.LOADED_FEATURE);\r
if (material == null) {\r
material = materialHelper.toMaterial(s, dataRepository);\r
}\r
* This method converts rgb values to hsv values.\r
* \r
* @param rgb\r
- * rgb values of the color\r
+ * rgb values of the color\r
* @param hsv\r
- * hsv values of a color (this table contains the result of the transformation)\r
+ * hsv values of a color (this table contains the result of the transformation)\r
*/\r
public void rgbToHsv(float r, float g, float b, float[] hsv) {\r
float cmax = r;\r
* This method converts rgb values to hsv values.\r
* \r
* @param h\r
- * hue\r
+ * hue\r
* @param s\r
- * saturation\r
+ * saturation\r
* @param v\r
- * value\r
+ * value\r
* @param rgb\r
- * rgb result vector (should have 3 elements)\r
+ * rgb result vector (should have 3 elements)\r
*/\r
public void hsvToRgb(float h, float s, float v, float[] rgb) {\r
h *= 360.0f;\r
}\r
}\r
}\r
- \r
+\r
+ /**\r
+ * An interface used in calculating alpha mask during particles' texture calculations.\r
+ * @author Marcin Roguski (Kaelthas)\r
+ */\r
protected static interface IAlphaMask {\r
+ /**\r
+ * This method sets the size of the texture's image.\r
+ * @param width\r
+ * the width of the image\r
+ * @param height\r
+ * the height of the image\r
+ */\r
void setImageSize(int width, int height);\r
+\r
+ /**\r
+ * This method returns the alpha value for the specified texture position.\r
+ * @param x\r
+ * the X coordinate of the texture position\r
+ * @param y\r
+ * the Y coordinate of the texture position\r
+ * @return the alpha value for the specified texture position\r
+ */\r
byte getAlpha(float x, float y);\r
}\r
}\r