OSDN Git Service

Javadocs added and removed unused methods.
authorKaelthas_Spellsinger@o2.pl <Kaelthas_Spellsinger@o2.pl@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 10 Jun 2011 16:25:04 +0000 (16:25 +0000)
committerKaelthas_Spellsinger@o2.pl <Kaelthas_Spellsinger@o2.pl@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 10 Jun 2011 16:25:04 +0000 (16:25 +0000)
git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@7566 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

engine/src/blender/com/jme3/scene/plugins/blender/helpers/v249/MaterialHelper.java

index 1aacb82..8197a62 100644 (file)
@@ -40,7 +40,6 @@ import java.util.logging.Level;
 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
@@ -63,22 +62,22 @@ import com.jme3.texture.Texture.WrapMode;
 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
@@ -101,66 +100,66 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -169,7 +168,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -244,10 +243,10 @@ public class MaterialHelper extends AbstractBlenderHelper {
                                        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
@@ -259,12 +258,9 @@ public class MaterialHelper extends AbstractBlenderHelper {
                                                                        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
@@ -309,14 +305,13 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -336,8 +331,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
                                                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
@@ -348,24 +342,26 @@ public class MaterialHelper extends AbstractBlenderHelper {
                        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
@@ -374,9 +370,9 @@ public class MaterialHelper extends AbstractBlenderHelper {
                        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
@@ -386,35 +382,26 @@ public class MaterialHelper extends AbstractBlenderHelper {
 \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
@@ -466,7 +453,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -478,7 +465,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -493,7 +480,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -505,7 +492,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -535,7 +522,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -548,12 +535,12 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -565,8 +552,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
                                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
@@ -581,9 +567,9 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -630,13 +616,13 @@ public class MaterialHelper extends AbstractBlenderHelper {
         * 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
@@ -687,9 +673,29 @@ public class MaterialHelper extends AbstractBlenderHelper {
                        }\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