diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java index c3cd83fab..f27c27d2b 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java @@ -201,6 +201,10 @@ public class MaterialHelper extends AbstractBlenderHelper { MaterialContext materialContext = new MaterialContext(structure, dataRepository); LOGGER.log(Level.INFO, "Material's name: {0}", materialContext.name); + DiffuseShader diffuseShader = this.getDiffuseShader(structure); + ColorRGBA diffuseColor = this.getDiffuseColor(structure, diffuseShader); + float[] diffuseColorArray = new float[] {diffuseColor.r, diffuseColor.g, diffuseColor.b}; + // texture Map texturesMap = new HashMap(); Type firstTextureType = null; @@ -236,7 +240,7 @@ public class MaterialHelper extends AbstractBlenderHelper { int blendType = ((Number) mtex.getFieldValue("blendtype")).intValue(); float[] color = new float[] { ((Number) mtex.getFieldValue("r")).floatValue(), ((Number) mtex.getFieldValue("g")).floatValue(), ((Number) mtex.getFieldValue("b")).floatValue() }; float colfac = ((Number) mtex.getFieldValue("colfac")).floatValue(); - texture = textureHelper.blendTexture(new float[] {1, 1, 1}, texture, color, colfac, blendType, negateTexture, dataRepository); + texture = textureHelper.blendTexture(diffuseColorArray, texture, color, colfac, blendType, negateTexture, dataRepository); texture.setWrap(WrapMode.Repeat); //TODO: textures merging if (materialContext.shadeless) { @@ -289,7 +293,7 @@ public class MaterialHelper extends AbstractBlenderHelper { if (materialContext.vertexColor) { result.setBoolean(materialContext.shadeless ? "VertexColor" : "UseVertexColor", true); } - ColorRGBA diffuseColor = null; + if (materialContext.shadeless) { // color of shadeless? doesn't seem to work in blender .. diffuseColor = ColorRGBA.White.clone(); @@ -297,9 +301,7 @@ public class MaterialHelper extends AbstractBlenderHelper { result.setBoolean("UseMaterialColors", Boolean.TRUE); // setting the colors - DiffuseShader diffuseShader = this.getDiffuseShader(structure); result.setBoolean("Minnaert", diffuseShader == DiffuseShader.MINNAERT); - diffuseColor = this.getDiffuseColor(structure, diffuseShader); if (!materialContext.transparent) { diffuseColor.a = 1; } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/ImageLoader.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/ImageLoader.java index dd99a8167..37f76e9af 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/ImageLoader.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/ImageLoader.java @@ -1,10 +1,40 @@ +/* + * Copyright (c) 2009-2010 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package com.jme3.scene.plugins.blender.textures; import java.io.InputStream; import java.util.logging.Logger; import com.jme3.scene.plugins.blender.file.BlenderInputStream; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ImageType; import com.jme3.texture.Image; import com.jme3.texture.plugins.AWTLoader; import com.jme3.texture.plugins.DDSLoader; @@ -94,4 +124,13 @@ import com.jme3.texture.plugins.TGALoader; } return result; } + + /** + * Image types that can be loaded. AWT: png, jpg, jped or bmp TGA: tga DDS: DirectX image files + * + * @author Marcin Roguski (Kaelthas) + */ + private static enum ImageType { + AWT, TGA, DDS; + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/NoiseGenerator.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/NoiseGenerator.java index e441cd1f7..3b0b5f2bc 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/NoiseGenerator.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/NoiseGenerator.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -42,9 +43,7 @@ import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.AbstractBlenderHelper; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.CBData; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; +import com.jme3.scene.plugins.blender.textures.TextureGeneratorMusgrave.MusgraveData; /** * This generator is responsible for creating various noises used to create @@ -54,22 +53,8 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; * @author Marcin Roguski (Kaelthas) */ /*package*/ class NoiseGenerator extends AbstractBlenderHelper { - private static final Logger LOGGER = Logger.getLogger(NoiseGenerator.class.getName()); - - // return value - protected static final int TEX_INT = 0; - protected static final int TEX_RGB = 1; - protected static final int TEX_NOR = 2; - - // noisetype - protected static final int TEX_NOISESOFT = 0; - protected static final int TEX_NOISEPERL = 1; - - // tex->stype - protected static final int TEX_DEFAULT = 0; - protected static final int TEX_COLOR = 1; - + // flag protected static final int TEX_COLORBAND = 1; protected static final int TEX_FLIPBLEND = 2; @@ -82,31 +67,6 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; protected static final int TEX_REPEAT_YMIR = 256; protected static final int TEX_FLAG_MASK = TEX_COLORBAND | TEX_FLIPBLEND | TEX_NEGALPHA | TEX_CHECKER_ODD | TEX_CHECKER_EVEN | TEX_PRV_ALPHA | TEX_PRV_NOR | TEX_REPEAT_XMIR | TEX_REPEAT_YMIR; - // tex->noisebasis2 - protected static final int TEX_SIN = 0; - protected static final int TEX_SAW = 1; - protected static final int TEX_TRI = 2; - - // tex->stype - protected static final int TEX_SOFT = 0; - protected static final int TEX_SHARP = 1; - protected static final int TEX_SHARPER = 2; - - // tex->stype - protected static final int TEX_BAND = 0; - protected static final int TEX_RING = 1; - protected static final int TEX_BANDNOISE = 2; - protected static final int TEX_RINGNOISE = 3; - - // tex->stype - protected static final int TEX_LIN = 0; - protected static final int TEX_QUAD = 1; - protected static final int TEX_EASE = 2; - protected static final int TEX_DIAG = 3; - protected static final int TEX_SPHERE = 4; - protected static final int TEX_HALO = 5; - protected static final int TEX_RAD = 6; - // tex->stype protected static final int TEX_PLASTIC = 0; protected static final int TEX_WALLIN = 1; @@ -169,136 +129,127 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; } } } - protected static Map noiseFunctions = new HashMap(); - + + protected static Map noiseFunctions = new HashMap(); static { - // orgBlenderNoise (*Was BLI_hnoise(), removed noisesize, so other functions can call it without scaling.*) - noiseFunctions.put(Integer.valueOf(0), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(0), new NoiseFunction() { + // originalBlenderNoise @Override public float execute(float x, float y, float z) { - return this.orgBlenderNoise(x, y, z); + return NoiseFunctions.originalBlenderNoise(x, y, z); } @Override - public float executeS(float x, float y, float z) { - return 2.0f * this.orgBlenderNoise(x, y, z) - 1.0f; + public float executeSigned(float x, float y, float z) { + return 2.0f * NoiseFunctions.originalBlenderNoise(x, y, z) - 1.0f; } }); - // orgPerlinNoise (*For use with BLI_gNoise/gTurbulence, returns signed noise.*) - noiseFunctions.put(Integer.valueOf(1), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(1), new NoiseFunction() { + // orgPerlinNoise @Override public float execute(float x, float y, float z) { - return 0.5f + 0.5f * this.noise3Perlin(new float[]{x, y, z}); + return 0.5f + 0.5f * NoiseFunctions.noise3Perlin(x, y, z); } @Override - public float executeS(float x, float y, float z) { - return this.noise3Perlin(new float[]{x, y, z}); + public float executeSigned(float x, float y, float z) { + return NoiseFunctions.noise3Perlin(x, y, z); } }); - // newPerlin (* for use with BLI_gNoise()/BLI_gTurbulence(), returns unsigned improved perlin noise *) - noiseFunctions.put(Integer.valueOf(2), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(2), new NoiseFunction() { + // newPerlin @Override public float execute(float x, float y, float z) { - return 0.5f + 0.5f * this.newPerlin(x, y, z); + return 0.5f + 0.5f * NoiseFunctions.newPerlin(x, y, z); } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { return this.execute(x, y, z); } }); - // voronoi_F1 - noiseFunctions.put(Integer.valueOf(3), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(3), new NoiseFunction() { + // voronoi_F1 @Override public float execute(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return da[0]; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return 2.0f * da[0] - 1.0f; } }); - // voronoi_F2 - noiseFunctions.put(Integer.valueOf(4), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(4), new NoiseFunction() { + // voronoi_F2 @Override public float execute(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return da[1]; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return 2.0f * da[1] - 1.0f; } }); - // voronoi_F3 - noiseFunctions.put(Integer.valueOf(5), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(5), new NoiseFunction() { + // voronoi_F3 @Override public float execute(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return da[2]; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return 2.0f * da[2] - 1.0f; } }); - // voronoi_F4 - noiseFunctions.put(Integer.valueOf(6), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(6), new NoiseFunction() { + // voronoi_F4 @Override public float execute(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return da[3]; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return 2.0f * da[3] - 1.0f; } }); - // voronoi_F1F2 - noiseFunctions.put(Integer.valueOf(7), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(7), new NoiseFunction() { + // voronoi_F1F2 @Override public float execute(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return da[1] - da[0]; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float[] da = new float[4], pa = new float[12]; - AbstractNoiseFunc.voronoi(x, y, z, da, pa, 1, 0); + NoiseFunctions.voronoi(x, y, z, da, pa, 1, 0); return 2.0f * (da[1] - da[0]) - 1.0f; } }); - // voronoi_Cr - noiseFunctions.put(Integer.valueOf(8), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(8), new NoiseFunction() { + // voronoi_Cr @Override public float execute(float x, float y, float z) { float t = 10 * noiseFunctions.get(Integer.valueOf(7)).execute(x, y, z);// voronoi_F1F2 @@ -306,14 +257,13 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { float t = 10.0f * noiseFunctions.get(Integer.valueOf(7)).execute(x, y, z);// voronoi_F1F2 return t > 1.0f ? 1.0f : 2.0f * t - 1.0f; } }); - // cellNoise - noiseFunctions.put(Integer.valueOf(14), new AbstractNoiseFunc() { - + noiseFunctions.put(Integer.valueOf(14), new NoiseFunction() { + // cellNoise @Override public float execute(float x, float y, float z) { int xi = (int) Math.floor(x); @@ -325,42 +275,38 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; } @Override - public float executeS(float x, float y, float z) { + public float executeSigned(float x, float y, float z) { return 2.0f * this.execute(x, y, z) - 1.0f; } }); } /** Distance metrics for voronoi. e parameter only used in Minkovsky. */ - protected static Map distanceFunctions = new HashMap(); + protected static Map distanceFunctions = new HashMap(); static { - // real distance - distanceFunctions.put(Integer.valueOf(0), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(0), new DistanceFunction() { + // real distance @Override public float execute(float x, float y, float z, float e) { return (float) Math.sqrt(x * x + y * y + z * z); } }); - // distance squared - distanceFunctions.put(Integer.valueOf(1), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(1), new DistanceFunction() { + // distance squared @Override public float execute(float x, float y, float z, float e) { return x * x + y * y + z * z; } }); - // manhattan/taxicab/cityblock distance - distanceFunctions.put(Integer.valueOf(2), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(2), new DistanceFunction() { + // manhattan/taxicab/cityblock distance @Override public float execute(float x, float y, float z, float e) { return FastMath.abs(x) + FastMath.abs(y) + FastMath.abs(z); } }); - // Chebychev - distanceFunctions.put(Integer.valueOf(3), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(3), new DistanceFunction() { + // Chebychev @Override public float execute(float x, float y, float z, float e) { x = FastMath.abs(x); @@ -370,18 +316,16 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; return z > t ? z : t; } }); - // minkovsky preset exponent 0.5 (MinkovskyH) - distanceFunctions.put(Integer.valueOf(4), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(4), new DistanceFunction() { + // Minkovsky, preset exponent 0.5 (MinkovskyH) @Override public float execute(float x, float y, float z, float e) { float d = (float) (Math.sqrt(FastMath.abs(x)) + Math.sqrt(FastMath.abs(y)) + Math.sqrt(FastMath.abs(z))); return d * d; } }); - // minkovsky preset exponent 4 (Minkovsky4) - distanceFunctions.put(Integer.valueOf(5), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(5), new DistanceFunction() { + // Minkovsky, preset exponent 0.25 (Minkovsky4) @Override public float execute(float x, float y, float z, float e) { x *= x; @@ -390,43 +334,37 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; return (float) Math.sqrt(Math.sqrt(x * x + y * y + z * z)); } }); - // Minkovsky, general case, slow, maybe too slow to be useful - distanceFunctions.put(Integer.valueOf(6), new DistanceFunc() { - + distanceFunctions.put(Integer.valueOf(6), new DistanceFunction() { + // Minkovsky, general case @Override public float execute(float x, float y, float z, float e) { return (float) Math.pow(Math.pow(FastMath.abs(x), e) + Math.pow(FastMath.abs(y), e) + Math.pow(FastMath.abs(z), e), 1.0f / e); } }); } + protected static Map musgraveFunctions = new HashMap(); - static { musgraveFunctions.put(Integer.valueOf(TEX_MFRACTAL), new MusgraveFunction() { @Override - public float execute(Structure tex, float x, float y, float z) { - float mg_H = ((Number) tex.getFieldValue("mg_H")).floatValue(); - float mg_lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); - float mg_octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - - float rmd, value = 1.0f, pwr = 1.0f, pwHL = (float) Math.pow(mg_lacunarity, -mg_H); - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); + public float execute(MusgraveData musgraveData, float x, float y, float z) { + float rmd, value = 1.0f, pwr = 1.0f, pwHL = (float) Math.pow(musgraveData.lacunarity, -musgraveData.h); + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(musgraveData.noisebasis)); if (abstractNoiseFunc == null) { abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(0)); } - for (int i = 0; i < (int) mg_octaves; ++i) { - value *= pwr * abstractNoiseFunc.executeS(x, y, z) + 1.0f; + for (int i = 0; i < (int) musgraveData.octaves; ++i) { + value *= pwr * abstractNoiseFunc.executeSigned(x, y, z) + 1.0f; pwr *= pwHL; - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; } - rmd = (float) (mg_octaves - Math.floor(mg_octaves)); + rmd = (float) (musgraveData.octaves - Math.floor(musgraveData.octaves)); if (rmd != 0.0f) { - value *= rmd * abstractNoiseFunc.executeS(x, y, z) * pwr + 1.0f; + value *= rmd * abstractNoiseFunc.executeSigned(x, y, z) * pwr + 1.0f; } return value; } @@ -434,38 +372,32 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; musgraveFunctions.put(Integer.valueOf(TEX_RIDGEDMF), new MusgraveFunction() { @Override - public float execute(Structure tex, float x, float y, float z) { - float mg_H = ((Number) tex.getFieldValue("mg_H")).floatValue(); - float mg_lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); - float mg_octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); - float mg_offset = ((Number) tex.getFieldValue("mg_offset")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - float mg_gain = ((Number) tex.getFieldValue("mg_gain")).floatValue(); + public float execute(MusgraveData musgraveData, float x, float y, float z) { float result, signal, weight; - float pwHL = (float) Math.pow(mg_lacunarity, -mg_H); - float pwr = pwHL; /* starts with i=1 instead of 0 */ + float pwHL = (float) Math.pow(musgraveData.lacunarity, -musgraveData.h); + float pwr = pwHL; - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(musgraveData.noisebasis)); if (abstractNoiseFunc == null) { abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(0)); } - signal = mg_offset - FastMath.abs(abstractNoiseFunc.executeS(x, y, z)); + signal = musgraveData.offset - FastMath.abs(abstractNoiseFunc.executeSigned(x, y, z)); signal *= signal; result = signal; weight = 1.0f; - for (int i = 1; i < (int) mg_octaves; ++i) { - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; - weight = signal * mg_gain; + for (int i = 1; i < (int) musgraveData.octaves; ++i) { + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; + weight = signal * musgraveData.gain; if (weight > 1.0f) { weight = 1.0f; } else if (weight < 0.0) { weight = 0.0f; } - signal = mg_offset - FastMath.abs(abstractNoiseFunc.executeS(x, y, z)); + signal = musgraveData.offset - FastMath.abs(abstractNoiseFunc.executeSigned(x, y, z)); signal *= signal; signal *= weight; result += signal * pwr; @@ -477,43 +409,37 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; musgraveFunctions.put(Integer.valueOf(TEX_HYBRIDMF), new MusgraveFunction() { @Override - public float execute(Structure tex, float x, float y, float z) { - float mg_H = ((Number) tex.getFieldValue("mg_H")).floatValue(); - float mg_lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); - float mg_octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); - float mg_offset = ((Number) tex.getFieldValue("mg_offset")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - float mg_gain = ((Number) tex.getFieldValue("mg_gain")).floatValue(); + public float execute(MusgraveData musgraveData, float x, float y, float z) { float result, signal, weight, rmd; - float pwHL = (float) Math.pow(mg_lacunarity, -mg_H); - float pwr = pwHL; /* starts with i=1 instead of 0 */ - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); + float pwHL = (float) Math.pow(musgraveData.lacunarity, -musgraveData.h); + float pwr = pwHL; + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(musgraveData.noisebasis)); if (abstractNoiseFunc == null) { abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(0)); } - result = abstractNoiseFunc.executeS(x, y, z) + mg_offset; - weight = mg_gain * result; - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + result = abstractNoiseFunc.executeSigned(x, y, z) + musgraveData.offset; + weight = musgraveData.gain * result; + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; - for (int i = 1; weight > 0.001f && i < (int) mg_octaves; ++i) { + for (int i = 1; weight > 0.001f && i < (int) musgraveData.octaves; ++i) { if (weight > 1.0f) { weight = 1.0f; } - signal = (abstractNoiseFunc.executeS(x, y, z) + mg_offset) * pwr; + signal = (abstractNoiseFunc.executeSigned(x, y, z) + musgraveData.offset) * pwr; pwr *= pwHL; result += weight * signal; - weight *= mg_gain * signal; - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + weight *= musgraveData.gain * signal; + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; } - rmd = mg_octaves - (float) Math.floor(mg_octaves); + rmd = musgraveData.octaves - (float) Math.floor(musgraveData.octaves); if (rmd != 0.0f) { - result += rmd * (abstractNoiseFunc.executeS(x, y, z) + mg_offset) * pwr; + result += rmd * (abstractNoiseFunc.executeSigned(x, y, z) + musgraveData.offset) * pwr; } return result; } @@ -521,29 +447,25 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; musgraveFunctions.put(Integer.valueOf(TEX_FBM), new MusgraveFunction() { @Override - public float execute(Structure tex, float x, float y, float z) { - float mg_H = ((Number) tex.getFieldValue("mg_H")).floatValue(); - float mg_lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); - float mg_octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - float rmd, value = 0.0f, pwr = 1.0f, pwHL = (float) Math.pow(mg_lacunarity, -mg_H); + public float execute(MusgraveData musgraveData, float x, float y, float z) { + float rmd, value = 0.0f, pwr = 1.0f, pwHL = (float) Math.pow(musgraveData.lacunarity, -musgraveData.h); - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(musgraveData.noisebasis)); if (abstractNoiseFunc == null) { abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(0)); } - for (int i = 0; i < (int) mg_octaves; ++i) { - value += abstractNoiseFunc.executeS(x, y, z) * pwr; + for (int i = 0; i < (int) musgraveData.octaves; ++i) { + value += abstractNoiseFunc.executeSigned(x, y, z) * pwr; pwr *= pwHL; - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; } - rmd = (float) (mg_octaves - Math.floor(mg_octaves)); + rmd = (float) (musgraveData.octaves - Math.floor(musgraveData.octaves)); if (rmd != 0.f) { - value += rmd * abstractNoiseFunc.executeS(x, y, z) * pwr; + value += rmd * abstractNoiseFunc.executeSigned(x, y, z) * pwr; } return value; } @@ -551,677 +473,102 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; musgraveFunctions.put(Integer.valueOf(TEX_HTERRAIN), new MusgraveFunction() { @Override - public float execute(Structure tex, float x, float y, float z) { - float mg_H = ((Number) tex.getFieldValue("mg_H")).floatValue(); - float mg_lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); - float mg_octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - float mg_offset = ((Number) tex.getFieldValue("mg_offset")).floatValue(); + public float execute(MusgraveData musgraveData, float x, float y, float z) { float value, increment, rmd; - float pwHL = (float) Math.pow(mg_lacunarity, -mg_H); - float pwr = pwHL; /* starts with i=1 instead of 0 */ - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); + float pwHL = (float) Math.pow(musgraveData.lacunarity, -musgraveData.h); + float pwr = pwHL; + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(musgraveData.noisebasis)); if (abstractNoiseFunc == null) { abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(0)); } - /* first unscaled octave of function; later octaves are scaled */ - value = mg_offset + abstractNoiseFunc.executeS(x, y, z); - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + value = musgraveData.offset + abstractNoiseFunc.executeSigned(x, y, z); + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; - for (int i = 1; i < (int) mg_octaves; ++i) { - increment = (abstractNoiseFunc.executeS(x, y, z) + mg_offset) * pwr * value; + for (int i = 1; i < (int) musgraveData.octaves; ++i) { + increment = (abstractNoiseFunc.executeSigned(x, y, z) + musgraveData.offset) * pwr * value; value += increment; pwr *= pwHL; - x *= mg_lacunarity; - y *= mg_lacunarity; - z *= mg_lacunarity; + x *= musgraveData.lacunarity; + y *= musgraveData.lacunarity; + z *= musgraveData.lacunarity; } - rmd = mg_octaves - (float) Math.floor(mg_octaves); + rmd = musgraveData.octaves - (float) Math.floor(musgraveData.octaves); if (rmd != 0.0) { - increment = (abstractNoiseFunc.executeS(x, y, z) + mg_offset) * pwr * value; + increment = (abstractNoiseFunc.executeSigned(x, y, z) + musgraveData.offset) * pwr * value; value += rmd * increment; } return value; } }); } - - /** - * THE FOLLOWING METHODS HELP IN COMPUTATION OF THE TEXTURES. - */ - protected void brightnesAndContrast(TexResult texres, float contrast, float brightness) { - texres.tin = (texres.tin - 0.5f) * contrast + brightness - 0.5f; - if (texres.tin < 0.0f) { - texres.tin = 0.0f; - } else if (texres.tin > 1.0f) { - texres.tin = 1.0f; - } - } - - protected void brightnesAndContrastRGB(Structure tex, TexResult texres) { - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float bright = ((Number) tex.getFieldValue("bright")).floatValue(); - float rfac = ((Number) tex.getFieldValue("rfac")).floatValue(); - float gfac = ((Number) tex.getFieldValue("gfac")).floatValue(); - float bfac = ((Number) tex.getFieldValue("bfac")).floatValue(); - - texres.tr = rfac * ((texres.tr - 0.5f) * contrast + bright - 0.5f); - if (texres.tr < 0.0f) { - texres.tr = 0.0f; - } - texres.tg = gfac * ((texres.tg - 0.5f) * contrast + bright - 0.5f); - if (texres.tg < 0.0f) { - texres.tg = 0.0f; - } - texres.tb = bfac * ((texres.tb - 0.5f) * contrast + bright - 0.5f); - if (texres.tb < 0.0f) { - texres.tb = 0.0f; - } - } - - /* this allows colorbanded textures to control normals as well */ - public void texNormalDerivate(ColorBand colorBand, TexResult texres, DataRepository dataRepository) { - if (texres.nor != null) { - TexResult fakeTexresult; - try { - fakeTexresult = (TexResult) texres.clone(); - } catch (CloneNotSupportedException e) { - throw new IllegalStateException("Texture result class MUST support cloning!", e); - } - - float fac0 = fakeTexresult.tr + fakeTexresult.tg + fakeTexresult.tb; - fakeTexresult.tin = texres.nor[0]; - this.doColorband(colorBand, fakeTexresult, dataRepository); - - float fac1 = fakeTexresult.tr + fakeTexresult.tg + fakeTexresult.tb; - fakeTexresult.tin = texres.nor[1]; - this.doColorband(colorBand, fakeTexresult, dataRepository); - - float fac2 = fakeTexresult.tr + fakeTexresult.tg + fakeTexresult.tb; - fakeTexresult.tin = texres.nor[2]; - this.doColorband(colorBand, fakeTexresult, dataRepository); - - float fac3 = fakeTexresult.tr + fakeTexresult.tg + fakeTexresult.tb; - - texres.nor[0] = 0.3333f * (fac0 - fac1); - texres.nor[1] = 0.3333f * (fac0 - fac2); - texres.nor[2] = 0.3333f * (fac0 - fac3); - - texres.nor[0] = texres.tin - texres.nor[0]; - texres.nor[1] = texres.tin - texres.nor[1]; - texres.nor[2] = texres.tin - texres.nor[2]; - } - } - - /** - * This method calculates the colorband for the texture. - * @param colorBand - * the colorband data - * @param texres - * the texture pixel result - * @param dataRepository - * the data repository - * @return true if calculation suceedess and false otherwise - */ - public boolean doColorband(ColorBand colorBand, TexResult texres, DataRepository dataRepository) { - CBData cbd1, cbd2, cbd0, cbd3; - int i1 = 0, i2 = 0, a; - float fac, mfac; - float[] t = new float[4]; - - if (colorBand == null || colorBand.tot == 0) { - return true; - } - - cbd1 = colorBand.data[0]; - if (colorBand.tot == 1) { - texres.tr = cbd1.r; - texres.tg = cbd1.g; - texres.tb = cbd1.b; - texres.ta = cbd1.a; - } else { - if (texres.tin <= cbd1.pos && colorBand.ipotype < 2) { - texres.tr = cbd1.r; - texres.tg = cbd1.g; - texres.tb = cbd1.b; - texres.ta = cbd1.a; - } else { - /* we're looking for first pos > in */ - for (a = 0; a < colorBand.tot; ++a, ++i1) { - cbd1 = colorBand.data[i1]; - if (cbd1.pos > texres.tin) { - break; - } - } - - if (a == colorBand.tot) { - cbd2 = colorBand.data[i1 - 1]; - try { - cbd1 = (CBData) cbd2.clone(); - } catch (CloneNotSupportedException e) { - throw new IllegalStateException("Clone not supported for " + CBData.class.getName() + " class! Fix that!"); - } - cbd1.pos = 1.0f; - } else if (a == 0) { - try { - cbd2 = (CBData) cbd1.clone(); - } catch (CloneNotSupportedException e) { - throw new IllegalStateException("Clone not supported for " + CBData.class.getName() + " class! Fix that!"); - } - cbd2.pos = 0.0f; - } else { - cbd2 = colorBand.data[i1 - 1]; - } - - if (texres.tin >= cbd1.pos && colorBand.ipotype < 2) { - texres.tr = cbd1.r; - texres.tg = cbd1.g; - texres.tb = cbd1.b; - texres.ta = cbd1.a; - } else { - - if (cbd2.pos != cbd1.pos) { - fac = (texres.tin - cbd1.pos) / (cbd2.pos - cbd1.pos); - } else { - fac = 0.0f; - } - - if (colorBand.ipotype == 4) { - /* constant */ - texres.tr = cbd2.r; - texres.tg = cbd2.g; - texres.tb = cbd2.b; - texres.ta = cbd2.a; - return true; - } - - if (colorBand.ipotype >= 2) { - /* ipo from right to left: 3 2 1 0 */ - - if (a >= colorBand.tot - 1) { - cbd0 = cbd1; - } else { - cbd0 = colorBand.data[i1 + 1]; - } - if (a < 2) { - cbd3 = cbd2; - } else { - cbd3 = colorBand.data[i2 - 1]; - } - - fac = FastMath.clamp(fac, 0.0f, 1.0f); - - if (colorBand.ipotype == 3) { - this.setFourIpo(fac, t, KEY_CARDINAL); - } else { - this.setFourIpo(fac, t, KEY_BSPLINE); - } - - texres.tr = t[3] * cbd3.r + t[2] * cbd2.r + t[1] * cbd1.r + t[0] * cbd0.r; - texres.tg = t[3] * cbd3.g + t[2] * cbd2.g + t[1] * cbd1.g + t[0] * cbd0.g; - texres.tb = t[3] * cbd3.b + t[2] * cbd2.b + t[1] * cbd1.b + t[0] * cbd0.b; - texres.ta = t[3] * cbd3.a + t[2] * cbd2.a + t[1] * cbd1.a + t[0] * cbd0.a; - texres.tr = FastMath.clamp(texres.tr, 0.0f, 1.0f); - texres.tg = FastMath.clamp(texres.tg, 0.0f, 1.0f); - texres.tb = FastMath.clamp(texres.tb, 0.0f, 1.0f); - texres.ta = FastMath.clamp(texres.ta, 0.0f, 1.0f); - } else { - - if (colorBand.ipotype == 1) { /* EASE */ - mfac = fac * fac; - fac = 3.0f * mfac - 2.0f * mfac * fac; - } - mfac = 1.0f - fac; - - texres.tr = mfac * cbd1.r + fac * cbd2.r; - texres.tg = mfac * cbd1.g + fac * cbd2.g; - texres.tb = mfac * cbd1.b + fac * cbd2.b; - texres.ta = mfac * cbd1.a + fac * cbd2.a; - } - } - } - } - return true; - } - - protected void setFourIpo(float d, float[] data, int type) { - if (type == KEY_LINEAR) { - data[0] = 0.0f; - data[1] = 1.0f - d; - data[2] = d; - data[3] = 0.0f; - } else { - float d2 = d * d; - float d3 = d2 * d; - if (type == KEY_CARDINAL) { - float fc = 0.71f; - data[0] = -fc * d3 + 2.0f * fc * d2 - fc * d; - data[1] = (2.0f - fc) * d3 + (fc - 3.0f) * d2 + 1.0f; - data[2] = (fc - 2.0f) * d3 + (3.0f - 2.0f * fc) * d2 + fc * d; - data[3] = fc * d3 - fc * d2; - } else if (type == KEY_BSPLINE) { - data[0] = -0.16666666f * d3 + 0.5f * d2 - 0.5f * d + 0.16666666f; - data[1] = 0.5f * d3 - d2 + 0.6666666f; - data[2] = -0.5f * d3 + 0.5f * d2 + 0.5f * d + 0.16666666f; - data[3] = 0.16666666f * d3; - } - } - } - - interface IWaveForm { - - float execute(float x); - } - protected static IWaveForm[] waveformFunctions = new IWaveForm[3]; - - static { - waveformFunctions[0] = new IWaveForm() {// tex_sin - - @Override - public float execute(float x) { - return 0.5f + 0.5f * (float) Math.sin(x); - } - }; - waveformFunctions[1] = new IWaveForm() {// tex_saw - - @Override - public float execute(float x) { - int n = (int) (x / FastMath.TWO_PI); - x -= n * FastMath.TWO_PI; - if (x < 0.0f) { - x += FastMath.TWO_PI; - } - return x / FastMath.TWO_PI; - } - }; - waveformFunctions[2] = new IWaveForm() {// tex_tri - - @Override - public float execute(float x) { - return 1.0f - 2.0f * FastMath.abs((float) Math.floor(x * 1.0f / FastMath.TWO_PI + 0.5f) - x * 1.0f / FastMath.TWO_PI); - } - }; - } - - /* computes basic wood intensity value at x,y,z */ - public float woodInt(Structure tex, float x, float y, float z, DataRepository dataRepository) { - int noisebasis2 = ((Number) tex.getFieldValue("noisebasis2")).intValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - int stype = ((Number) tex.getFieldValue("stype")).intValue(); - float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); - int noiseType = ((Number) tex.getFieldValue("noisetype")).intValue(); - float wi = 0; - int waveform = noisebasis2; /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */ - int wt = stype; /* wood type: TEX_BAND=0, TEX_RING=1, TEX_BANDNOISE=2, TEX_RINGNOISE=3 */ - - if (waveform > TEX_TRI || waveform < TEX_SIN) { - waveform = 0; /* check to be sure noisebasis2 is initialized ahead of time */ - } - - if (wt == TEX_BAND) { - wi = waveformFunctions[waveform].execute((x + y + z) * 10.0f); - } else if (wt == TEX_RING) { - wi = waveformFunctions[waveform].execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f); - } else if (wt == TEX_BANDNOISE) { - wi = turbul * this.bliGNoise(noisesize, x, y, z, noiseType != TEX_NOISESOFT, noisebasis); - wi = waveformFunctions[waveform].execute((x + y + z) * 10.0f + wi); - } else if (wt == TEX_RINGNOISE) { - wi = turbul * this.bliGNoise(noisesize, x, y, z, noiseType != TEX_NOISESOFT, noisebasis); - wi = waveformFunctions[waveform].execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f + wi); - } - return wi; - } - - /* computes basic marble intensity at x,y,z */ - public float marbleInt(Structure tex, float x, float y, float z, DataRepository dataRepository) { - float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); - int noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); - int stype = ((Number) tex.getFieldValue("stype")).intValue();/* marble type: TEX_SOFT=0, TEX_SHARP=1,TEX_SHAPER=2 */ - float turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); - int noisetype = ((Number) tex.getFieldValue("noisetype")).intValue(); - int waveform = ((Number) tex.getFieldValue("noisebasis2")).intValue(); /* wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 */ - - if (waveform > TEX_TRI || waveform < TEX_SIN) { - waveform = 0; /* check to be sure noisebasis2 isn't initialized ahead of time */ - } - - float n = 5.0f * (x + y + z); - float mi = n + turbul * this.bliGTurbulence(noisesize, x, y, z, noisedepth, noisetype != TEX_NOISESOFT, noisebasis); - - if (stype >= NoiseGenerator.TEX_SOFT) { /* TEX_SOFT always true */ - mi = waveformFunctions[waveform].execute(mi); - if (stype == TEX_SHARP) { - mi = (float) Math.sqrt(mi); - } else if (stype == TEX_SHARPER) { - mi = (float) Math.sqrt(Math.sqrt(mi)); - } - } - return mi; - } - - public void voronoi(float x, float y, float z, float[] da, float[] pa, float me, int dtype) { - AbstractNoiseFunc.voronoi(x, y, z, da, pa, me, dtype); - } - - public void cellNoiseV(float x, float y, float z, float[] ca) { - AbstractNoiseFunc.cellNoiseV(x, y, z, ca); - } - - /** - * THE FOLLOWING METHODS HELP IN NOISE COMPUTATIONS - */ - /** - * Separated from orgBlenderNoise above, with scaling. - * @param noisesize - * @param x - * @param y - * @param z - * @return - */ - private float bliHnoise(float noisesize, float x, float y, float z) { - if (noisesize == 0.0) { - return 0.0f; - } - x = (1.0f + x) / noisesize; - y = (1.0f + y) / noisesize; - z = (1.0f + z) / noisesize; - return noiseFunctions.get(0).execute(x, y, z); - } - - /** - * @param noisesize - * @param x - * @param y - * @param z - * @param nr - * @return - */ - public float bliTurbulence(float noisesize, float x, float y, float z, int nr) { - float d = 0.5f, div = 1.0f; - - float s = this.bliHnoise(noisesize, x, y, z); - while (nr > 0) { - s += d * this.bliHnoise(noisesize * d, x, y, z); - div += d; - d *= 0.5; - --nr; - } - return s / div; - } - - /** - * @param noisesize - * @param x - * @param y - * @param z - * @param nr - * @return - */ - public float bliTurbulence1(float noisesize, float x, float y, float z, int nr) { - float s, d = 0.5f, div = 1.0f; - - s = FastMath.abs((-1.0f + 2.0f * this.bliHnoise(noisesize, x, y, z))); - while (nr > 0) { - s += Math.abs(d * (-1.0f + 2.0f * this.bliHnoise(noisesize * d, x, y, z))); - div += d; - d *= 0.5; - --nr; - } - return s / div; - } - - /** - * @param noisesize - * @param x - * @param y - * @param z - * @return - */ - public float bliHnoisep(float noisesize, float x, float y, float z) { - return noiseFunctions.get(Integer.valueOf(0)).noise3Perlin(new float[]{x / noisesize, y / noisesize, z / noisesize}); - } - - /** - * @param point - * @param lofreq - * @param hifreq - * @return - */ - public float turbulencePerlin(float[] point, float lofreq, float hifreq) { - float freq, t = 0, p[] = new float[]{point[0] + 123.456f, point[1], point[2]}; - for (freq = lofreq; freq < hifreq; freq *= 2.) { - t += Math.abs(noiseFunctions.get(Integer.valueOf(0)).noise3Perlin(p)) / freq; - p[0] *= 2.0f; - p[1] *= 2.0f; - p[2] *= 2.0f; - } - return t - 0.3f; /* readjust to make mean value = 0.0 */ - } - - /** - * @param noisesize - * @param x - * @param y - * @param z - * @param nr - * @return - */ - public float turbulencep(float noisesize, float x, float y, float z, int nr) { - float[] vec = new float[]{x / noisesize, y / noisesize, z / noisesize}; - ++nr; - return this.turbulencePerlin(vec, 1.0f, (1 << nr)); - } - - /** - * Newnoise: generic noise function for use with different noisebases - * @param x - * @param y - * @param z - * @param oct - * @param isHard - * @param noisebasis - * @return - */ - public float bliGNoise(float noisesize, float x, float y, float z, boolean isHard, int noisebasis) { - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); - if (abstractNoiseFunc == null) { - abstractNoiseFunc = noiseFunctions.get(0); - noisebasis = 0; - } - if (noisebasis == 0) {// add one to make return value same as BLI_hnoise - x += 1; - y += 1; - z += 1; - } - - if (noisesize != 0.0) { - noisesize = 1.0f / noisesize; - x *= noisesize; - y *= noisesize; - z *= noisesize; - } - if (isHard) { - return Math.abs(2.0f * abstractNoiseFunc.execute(x, y, z) - 1.0f); - } - return abstractNoiseFunc.execute(x, y, z); - } - - /** - * Newnoise: generic turbulence function for use with different noisebasis - * @param x - * @param y - * @param z - * @param oct - * @param isHard - * @param noisebasis - * @return - */ - public float bliGTurbulence(float noisesize, float x, float y, float z, int oct, boolean isHard, int noisebasis) { - AbstractNoiseFunc abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noisebasis)); - if (abstractNoiseFunc == null) { - abstractNoiseFunc = noiseFunctions.get(0); - noisebasis = 0; - } - if (noisebasis == 0) {// add one to make return value same as BLI_hnoise - x += 1; - y += 1; - z += 1; - } - float sum = 0, t, amp = 1, fscale = 1; - - if (noisesize != 0.0) { - noisesize = 1.0f / noisesize; - x *= noisesize; - y *= noisesize; - z *= noisesize; - } - for (int i = 0; i <= oct; ++i, amp *= 0.5, fscale *= 2) { - t = abstractNoiseFunc.execute(fscale * x, fscale * y, fscale * z); - if (isHard) { - t = FastMath.abs(2.0f * t - 1.0f); - } - sum += t * amp; - } - - sum *= (float) (1 << oct) / (float) ((1 << oct + 1) - 1); - return sum; - } - - /** - * "Variable Lacunarity Noise" A distorted variety of Perlin noise. This method is used to calculate distorted noise - * texture. - * @param x - * @param y - * @param z - * @param distortion - * @param nbas1 - * @param nbas2 - * @return - */ - public float mgVLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2) { - AbstractNoiseFunc abstractNoiseFunc1 = noiseFunctions.get(Integer.valueOf(nbas1)); - if (abstractNoiseFunc1 == null) { - abstractNoiseFunc1 = noiseFunctions.get(Integer.valueOf(0)); - } - AbstractNoiseFunc abstractNoiseFunc2 = noiseFunctions.get(Integer.valueOf(nbas2)); - if (abstractNoiseFunc2 == null) { - abstractNoiseFunc2 = noiseFunctions.get(Integer.valueOf(0)); - } - // get a random vector and scale the randomization - float rx = abstractNoiseFunc1.execute(x + 13.5f, y + 13.5f, z + 13.5f) * distortion; - float ry = abstractNoiseFunc1.execute(x, y, z) * distortion; - float rz = abstractNoiseFunc1.execute(x - 13.5f, y - 13.5f, z - 13.5f) * distortion; - return abstractNoiseFunc2.executeS(x + rx, y + ry, z + rz); //distorted-domain noise - } - - public void mgMFractalOrfBmTex(Structure tex, float[] texvec, ColorBand colorBand, TexResult texres, DataRepository dataRepository) { - int stype = ((Number) tex.getFieldValue("stype")).intValue(); - float nsOutscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - - MusgraveFunction mgravefunc = stype == TEX_MFRACTAL ? musgraveFunctions.get(Integer.valueOf(stype)) : musgraveFunctions.get(Integer.valueOf(TEX_FBM)); - - texres.tin = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1], texvec[2]); - if (texres.nor != null) { - float offs = nabla / noisesize; // also scaling of texvec - // calculate bumpnormal - texres.nor[0] = nsOutscale * mgravefunc.execute(tex, texvec[0] + offs, texvec[1], texvec[2]); - texres.nor[1] = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1] + offs, texvec[2]); - texres.nor[2] = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1], texvec[2] + offs); - this.texNormalDerivate(colorBand, texres, dataRepository); - } - this.brightnesAndContrast(texres, contrast, brightness); - } - - public void mgRidgedOrHybridMFTex(Structure tex, float[] texvec, ColorBand colorBand, TexResult texres, DataRepository dataRepository) { - int stype = ((Number) tex.getFieldValue("stype")).intValue(); - float nsOutscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - - MusgraveFunction mgravefunc = stype == TEX_RIDGEDMF ? musgraveFunctions.get(Integer.valueOf(stype)) : musgraveFunctions.get(Integer.valueOf(TEX_HYBRIDMF)); - - texres.tin = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1], texvec[2]); - if (texres.nor != null) { - float offs = nabla / noisesize; // also scaling of texvec - // calculate bumpnormal - texres.nor[0] = nsOutscale * mgravefunc.execute(tex, texvec[0] + offs, texvec[1], texvec[2]); - texres.nor[1] = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1] + offs, texvec[2]); - texres.nor[2] = nsOutscale * mgravefunc.execute(tex, texvec[0], texvec[1], texvec[2] + offs); - this.texNormalDerivate(colorBand, texres, dataRepository); - } - this.brightnesAndContrast(texres, contrast, brightness); - } - - public void mgHTerrainTex(Structure tex, float[] texvec, ColorBand colorBand, TexResult texres, DataRepository dataRepository) { - float nsOutscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - - MusgraveFunction musgraveFunction = musgraveFunctions.get(Integer.valueOf(TEX_HTERRAIN)); - texres.tin = nsOutscale * musgraveFunction.execute(tex, texvec[0], texvec[1], texvec[2]); - if (texres.nor != null) { - float offs = nabla / noisesize; // also scaling of texvec - // calculate bumpnormal - texres.nor[0] = nsOutscale * musgraveFunction.execute(tex, texvec[0] + offs, texvec[1], texvec[2]); - texres.nor[1] = nsOutscale * musgraveFunction.execute(tex, texvec[0], texvec[1] + offs, texvec[2]); - texres.nor[2] = nsOutscale * musgraveFunction.execute(tex, texvec[0], texvec[1], texvec[2] + offs); - this.texNormalDerivate(colorBand, texres, dataRepository); - } - this.brightnesAndContrast(texres, contrast, brightness); - } - - /** - * This class is abstract to the noise functions computations. It has two methods. One calculates the Signed (with - * 'S' at the end) and the other Unsigned value. - * @author Marcin Roguski (Kaelthas) - */ - protected static abstract class AbstractNoiseFunc { - - /** - * This method calculates the unsigned value of the noise. - * @param x - * the x texture coordinate - * @param y - * the y texture coordinate - * @param z - * the z texture coordinate - * @return value of the noise - */ - public abstract float execute(float x, float y, float z); - - /** - * This method calculates the signed value of the noise. - * @param x - * the x texture coordinate - * @param y - * the y texture coordinate - * @param z - * the z texture coordinate - * @return value of the noise - */ - public abstract float executeS(float x, float y, float z); - - /* + + public static class NoiseFunctions { + public static float noise(float x, float y, float z, float noiseSize, int noiseDepth, int noiseBasis, boolean isHard) { + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noiseBasis)); + if (abstractNoiseFunc == null) { + abstractNoiseFunc = noiseFunctions.get(0); + noiseBasis = 0; + } + + if (noiseBasis == 0) { + ++x; + ++y; + ++z; + } + + if (noiseSize != 0.0) { + noiseSize = 1.0f / noiseSize; + x *= noiseSize; + y *= noiseSize; + z *= noiseSize; + } + float result = abstractNoiseFunc.execute(x, y, z); + return isHard ? Math.abs(2.0f * result - 1.0f) : result; + } + + public static float turbulence(float x, float y, float z, float noiseSize, int noiseDepth, int noiseBasis, boolean isHard) { + NoiseFunction abstractNoiseFunc = noiseFunctions.get(Integer.valueOf(noiseBasis)); + if (abstractNoiseFunc == null) { + abstractNoiseFunc = noiseFunctions.get(0); + noiseBasis = 0; + } + + if (noiseBasis == 0) { + ++x; + ++y; + ++z; + } + if (noiseSize != 0.0) { + noiseSize = 1.0f / noiseSize; + x *= noiseSize; + y *= noiseSize; + z *= noiseSize; + } + + float sum = 0, t, amp = 1, fscale = 1; + for (int i = 0; i <= noiseDepth; ++i, amp *= 0.5, fscale *= 2) { + t = abstractNoiseFunc.execute(fscale * x, fscale * y, fscale * z); + if (isHard) { + t = FastMath.abs(2.0f * t - 1.0f); + } + sum += t * amp; + } + + sum *= (float) (1 << noiseDepth) / (float) ((1 << noiseDepth + 1) - 1); + return sum; + } + + /** * Not 'pure' Worley, but the results are virtually the same. Returns distances in da and point coords in pa */ - protected static void voronoi(float x, float y, float z, float[] da, float[] pa, float me, int dtype) { - float xd, yd, zd, d, p[]; + public static void voronoi(float x, float y, float z, float[] da, float[] pa, float distanceExponent, int distanceType) { + float xd, yd, zd, d, p[] = new float[3]; - DistanceFunc distanceFunc = distanceFunctions.get(Integer.valueOf(dtype)); + DistanceFunction distanceFunc = distanceFunctions.get(Integer.valueOf(distanceType)); if (distanceFunc == null) { distanceFunc = distanceFunctions.get(Integer.valueOf(0)); } @@ -1230,14 +577,14 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; int yi = (int) FastMath.floor(y); int zi = (int) FastMath.floor(z); da[0] = da[1] = da[2] = da[3] = 1e10f; - for (int xx = xi - 1; xx <= xi + 1; ++xx) { - for (int yy = yi - 1; yy <= yi + 1; ++yy) { - for (int zz = zi - 1; zz <= zi + 1; ++zz) { - p = AbstractNoiseFunc.hashPoint(xx, yy, zz); - xd = x - (p[0] + xx); - yd = y - (p[1] + yy); - zd = z - (p[2] + zz); - d = distanceFunc.execute(xd, yd, zd, me); + for (int i = xi - 1; i <= xi + 1; ++i) { + for (int j = yi - 1; j <= yi + 1; ++j) { + for (int k = zi - 1; k <= zi + 1; ++k) { + NoiseMath.hash(i, j, k, p); + xd = x - (p[0] + i); + yd = y - (p[1] + j); + zd = z - (p[2] + k); + d = distanceFunc.execute(xd, yd, zd, distanceExponent); if (d < da[0]) { da[3] = da[2]; da[2] = da[1]; @@ -1252,9 +599,9 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; pa[3] = pa[0]; pa[4] = pa[1]; pa[5] = pa[2]; - pa[0] = p[0] + xx; - pa[1] = p[1] + yy; - pa[2] = p[2] + zz; + pa[0] = p[0] + i; + pa[1] = p[1] + j; + pa[2] = p[2] + k; } else if (d < da[1]) { da[3] = da[2]; da[2] = da[1]; @@ -1265,108 +612,119 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; pa[6] = pa[3]; pa[7] = pa[4]; pa[8] = pa[5]; - pa[3] = p[0] + xx; - pa[4] = p[1] + yy; - pa[5] = p[2] + zz; + pa[3] = p[0] + i; + pa[4] = p[1] + j; + pa[5] = p[2] + k; } else if (d < da[2]) { da[3] = da[2]; da[2] = d; pa[9] = pa[6]; pa[10] = pa[7]; pa[11] = pa[8]; - pa[6] = p[0] + xx; - pa[7] = p[1] + yy; - pa[8] = p[2] + zz; + pa[6] = p[0] + i; + pa[7] = p[1] + j; + pa[8] = p[2] + k; } else if (d < da[3]) { da[3] = d; - pa[9] = p[0] + xx; - pa[10] = p[1] + yy; - pa[11] = p[2] + zz; + pa[9] = p[0] + i; + pa[10] = p[1] + j; + pa[11] = p[2] + k; } } } } } - - // needed for voronoi - protected static float[] hashPoint(int x, int y, int z) { - float[] result = new float[3]; - result[0] = hashpntf[3 * hash[hash[hash[z & 255] + y & 255] + x & 255]]; - result[1] = hashpntf[3 * hash[hash[hash[z & 255] + y & 255] + x & 255] + 1]; - result[2] = hashpntf[3 * hash[hash[hash[z & 255] + y & 255] + x & 255] + 2]; - return result; + + // instead of adding another permutation array, just use hash table defined above + public static float newPerlin(float x, float y, float z) { + int A, AA, AB, B, BA, BB; + float floorX = (float) Math.floor(x), floorY = (float) Math.floor(y), floorZ = (float) Math.floor(z); + int intX = (int) floorX & 0xFF, intY = (int) floorY & 0xFF, intZ = (int) floorZ & 0xFF; + x -= floorX; + y -= floorY; + z -= floorZ; + //computing fading curves + floorX = NoiseMath.npfade(x); + floorY = NoiseMath.npfade(y); + floorZ = NoiseMath.npfade(z); + A = hash[intX] + intY; + AA = hash[A] + intZ; + AB = hash[A + 1] + intZ; + B = hash[intX + 1] + intY; + BA = hash[B] + intZ; + BB = hash[B + 1] + intZ; + return NoiseMath.lerp(floorZ, NoiseMath.lerp(floorY, NoiseMath.lerp(floorX, NoiseMath.grad(hash[AA], x, y, z), + NoiseMath.grad(hash[BA], x - 1, y, z)), + NoiseMath.lerp(floorX, NoiseMath.grad(hash[AB], x, y - 1, z), + NoiseMath.grad(hash[BB], x - 1, y - 1, z))), + NoiseMath.lerp(floorY, NoiseMath.lerp(floorX, NoiseMath.grad(hash[AA + 1], x, y, z - 1), + NoiseMath.grad(hash[BA + 1], x - 1, y, z - 1)), + NoiseMath.lerp(floorX, NoiseMath.grad(hash[AB + 1], x, y - 1, z - 1), + NoiseMath.grad(hash[BB + 1], x - 1, y - 1, z - 1)))); } - public float noise3Perlin(float[] vec) { - int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; - float rx0, rx1, ry0, ry1, rz0, rz1, sx, sy, sz, a, b, c, d, t, u, v; - int i, j; - - t = vec[0] + 10000.0f; - bx0 = (int) t & 255; - bx1 = bx0 + 1 & 255; - rx0 = t - (int) t; - rx1 = rx0 - 1.0f; + public static float noise3Perlin(float x, float y, float z) { + float t = x + 10000.0f; + int bx0 = (int) t & 0xFF; + int bx1 = bx0 + 1 & 0xFF; + float rx0 = t - (int) t; + float rx1 = rx0 - 1.0f; - t = vec[0] + 10000.0f; - by0 = (int) t & 255; - by1 = by0 + 1 & 255; - ry0 = t - (int) t; - ry1 = ry0 - 1.0f; + t = y + 10000.0f; + int by0 = (int) t & 0xFF; + int by1 = by0 + 1 & 0xFF; + float ry0 = t - (int) t; + float ry1 = ry0 - 1.0f; - t = vec[0] + 10000.0f; - bz0 = (int) t & 255; - bz1 = bz0 + 1 & 255; - rz0 = t - (int) t; - rz1 = rz0 - 1.0f; + t = z + 10000.0f; + int bz0 = (int) t & 0xFF; + int bz1 = bz0 + 1 & 0xFF; + float rz0 = t - (int) t; + float rz1 = rz0 - 1.0f; - i = p[bx0]; - j = p[bx1]; + int i = p[bx0]; + int j = p[bx1]; - b00 = p[i + by0]; - b10 = p[j + by0]; - b01 = p[i + by1]; - b11 = p[j + by1]; + int b00 = p[i + by0]; + int b10 = p[j + by0]; + int b01 = p[i + by1]; + int b11 = p[j + by1]; - // lerp moved to improved perlin above + float sx = NoiseMath.surve(rx0); + float sy = NoiseMath.surve(ry0); + float sz = NoiseMath.surve(rz0); - sx = this.surve(rx0); - sy = this.surve(ry0); - sz = this.surve(rz0); - - float[] q = new float[3]; - q = g[b00 + bz0]; - u = this.at(rx0, ry0, rz0, q); + float[] q = g[b00 + bz0]; + float u = NoiseMath.at(rx0, ry0, rz0, q); q = g[b10 + bz0]; - v = this.at(rx1, ry0, rz0, q); - a = this.lerp(sx, u, v); + float v = NoiseMath.at(rx1, ry0, rz0, q); + float a = NoiseMath.lerp(sx, u, v); q = g[b01 + bz0]; - u = this.at(rx0, ry1, rz0, q); + u = NoiseMath.at(rx0, ry1, rz0, q); q = g[b11 + bz0]; - v = this.at(rx1, ry1, rz0, q); - b = this.lerp(sx, u, v); + v = NoiseMath.at(rx1, ry1, rz0, q); + float b = NoiseMath.lerp(sx, u, v); - c = this.lerp(sy, a, b); // interpolate in y at lo x + float c = NoiseMath.lerp(sy, a, b); q = g[b00 + bz1]; - u = this.at(rx0, ry0, rz1, q); + u = NoiseMath.at(rx0, ry0, rz1, q); q = g[b10 + bz1]; - v = this.at(rx1, ry0, rz1, q); - a = this.lerp(sx, u, v); + v = NoiseMath.at(rx1, ry0, rz1, q); + a = NoiseMath.lerp(sx, u, v); q = g[b01 + bz1]; - u = this.at(rx0, ry1, rz1, q); + u = NoiseMath.at(rx0, ry1, rz1, q); q = g[b11 + bz1]; - v = this.at(rx1, ry1, rz1, q); - b = this.lerp(sx, u, v); + v = NoiseMath.at(rx1, ry1, rz1, q); + b = NoiseMath.lerp(sx, u, v); - d = this.lerp(sy, a, b); // interpolate in y at hi x - - return 1.5f * this.lerp(sz, c, d); // interpolate in z + float d = NoiseMath.lerp(sy, a, b); + return 1.5f * NoiseMath.lerp(sz, c, d); } - public float orgBlenderNoise(float x, float y, float z) { + public static float originalBlenderNoise(float x, float y, float z) { float n = 0.5f; int ix = (int) Math.floor(x); @@ -1394,47 +752,25 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; cn4 = 1.0f - 3.0f * cn4 - 2.0f * cn4 * jx; cn5 = 1.0f - 3.0f * cn5 - 2.0f * cn5 * jy; cn6 = 1.0f - 3.0f * cn6 - 2.0f * cn6 * jz; - - int b00 = hash[hash[ix & 255] + (iy & 255)]; - int b10 = hash[hash[ix + 1 & 255] + (iy & 255)]; - int b01 = hash[hash[ix & 255] + (iy + 1 & 255)]; - int b11 = hash[hash[ix + 1 & 255] + (iy + 1 & 255)]; - - int b20 = iz & 255; - int b21 = iz + 1 & 255; - - // 0 - float i = cn1 * cn2 * cn3; - int hIndex = 3 * hash[b20 + b00]; - n += i * (hashvectf[hIndex] * ox + hashvectf[hIndex + 1] * oy + hashvectf[hIndex + 2] * oz); - // 1 - i = cn1 * cn2 * cn6; - hIndex = 3 * hash[b21 + b00]; - n += i * (hashvectf[hIndex] * ox + hashvectf[hIndex + 1] * oy + hashvectf[hIndex + 2] * jz); - // 2 - i = cn1 * cn5 * cn3; - hIndex = 3 * hash[b20 + b01]; - n += i * (hashvectf[hIndex] * ox + hashvectf[hIndex + 1] * jy + hashvectf[hIndex + 2] * oz); - // 3 - i = cn1 * cn5 * cn6; - hIndex = 3 * hash[b21 + b01]; - n += i * (hashvectf[hIndex] * ox + hashvectf[hIndex + 1] * jy + hashvectf[hIndex + 2] * jz); - // 4 - i = cn4 * cn2 * cn3; - hIndex = 3 * hash[b20 + b10]; - n += i * (hashvectf[hIndex] * jx + hashvectf[hIndex + 1] * oy + hashvectf[hIndex + 2] * oz); - // 5 - i = cn4 * cn2 * cn6; - hIndex = 3 * hash[b21 + b10]; - n += i * (hashvectf[hIndex] * jx + hashvectf[hIndex + 1] * oy + hashvectf[hIndex + 2] * jz); - // 6 - i = cn4 * cn5 * cn3; - hIndex = 3 * hash[b20 + b11]; - n += i * (hashvectf[hIndex] * jx + hashvectf[hIndex + 1] * jy + hashvectf[hIndex + 2] * oz); - // 7 - i = cn4 * cn5 * cn6; - hIndex = 3 * hash[b21 + b11]; - n += i * (hashvectf[hIndex] * jx + hashvectf[hIndex + 1] * jy + hashvectf[hIndex + 2] * jz); + float[] cn = new float[] {cn1 * cn2 * cn3, cn1 * cn2 * cn6, cn1 * cn5 * cn3, cn1 * cn5 * cn6, + cn4 * cn2 * cn3, cn4 * cn2 * cn6, cn4 * cn5 * cn3, cn4 * cn5 * cn6,}; + + int b00 = hash[hash[ix & 0xFF] + (iy & 0xFF)]; + int b01 = hash[hash[ix & 0xFF] + (iy + 1 & 0xFF)]; + int b10 = hash[hash[ix + 1 & 0xFF] + (iy & 0xFF)]; + int b11 = hash[hash[ix + 1 & 0xFF] + (iy + 1 & 0xFF)]; + int[] b1 = new int[] {b00, b00, b01, b01, b10, b10, b11, b11}; + + int[] b2 = new int[] {iz & 0xFF, iz + 1 & 0xFF}; + + float[] xFactor = new float[] {ox, ox, ox, ox, jx, jx, jx, jx}; + float[] yFactor = new float[] {oy, oy, jy, jy, oy, oy, jy, jy}; + float[] zFactor = new float[] {oz, jz, oz, jz, oz, jz, oz, jz}; + + for(int i=0;i<8;++i) { + int hIndex = 3 * hash[b1[i] + b2[i%2]]; + n += cn[i] * (hashvectf[hIndex] * xFactor[i] + hashvectf[hIndex + 1] * yFactor[i] + hashvectf[hIndex + 2] * zFactor[i]); + } if (n < 0.0f) { n = 0.0f; @@ -1443,79 +779,67 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; } return n; } + } - // instead of adding another permutation array, just use hash table defined above - public float newPerlin(float x, float y, float z) { - int A, AA, AB, B, BA, BB; - float u = (float) Math.floor(x), v = (float) Math.floor(y), w = (float) Math.floor(z); - int X = (int) u & 255, Y = (int) v & 255, Z = (int) w & 255; // FIND UNIT CUBE THAT CONTAINS POINT - x -= u; // FIND RELATIVE X,Y,Z - y -= v; // OF POINT IN CUBE. - z -= w; - u = this.npfade(x); // COMPUTE FADE CURVES - v = this.npfade(y); // FOR EACH OF X,Y,Z. - w = this.npfade(z); - A = hash[X] + Y; - AA = hash[A] + Z; - AB = hash[A + 1] + Z; // HASH COORDINATES OF - B = hash[X + 1] + Y; - BA = hash[B] + Z; - BB = hash[B + 1] + Z; // THE 8 CUBE CORNERS, - return this.lerp(w, this.lerp(v, this.lerp(u, this.grad(hash[AA], x, y, z), // AND ADD - this.grad(hash[BA], x - 1, y, z)), // BLENDED - this.lerp(u, this.grad(hash[AB], x, y - 1, z), // RESULTS - this.grad(hash[BB], x - 1, y - 1, z))),// FROM 8 - this.lerp(v, this.lerp(u, this.grad(hash[AA + 1], x, y, z - 1), // CORNERS - this.grad(hash[BA + 1], x - 1, y, z - 1)), // OF CUBE - this.lerp(u, this.grad(hash[AB + 1], x, y - 1, z - 1), this.grad(hash[BB + 1], x - 1, y - 1, z - 1)))); - } + /** + * This class is abstract to the noise functions computations. It has two methods. One calculates the Signed (with + * 'S' at the end) and the other Unsigned value. + * @author Marcin Roguski (Kaelthas) + */ + interface NoiseFunction { /** - * Returns a vector/point/color in ca, using point hasharray directly + * This method calculates the unsigned value of the noise. + * @param x + * the x texture coordinate + * @param y + * the y texture coordinate + * @param z + * the z texture coordinate + * @return value of the noise */ - protected static void cellNoiseV(float x, float y, float z, float[] ca) { - int xi = (int) Math.floor(x); - int yi = (int) Math.floor(y); - int zi = (int) Math.floor(z); - float[] p = AbstractNoiseFunc.hashPoint(xi, yi, zi); - ca[0] = p[0]; - ca[1] = p[1]; - ca[2] = p[2]; - } + float execute(float x, float y, float z); - protected float lerp(float t, float a, float b) { + /** + * This method calculates the signed value of the noise. + * @param x + * the x texture coordinate + * @param y + * the y texture coordinate + * @param z + * the z texture coordinate + * @return value of the noise + */ + float executeSigned(float x, float y, float z); + } + + public static class NoiseMath { + public static float lerp(float t, float a, float b) { return a + t * (b - a); } - protected float npfade(float t) { + public static float npfade(float t) { return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); } - protected float grad(int hash, float x, float y, float z) { - int h = hash & 0x0F; // CONVERT LO 4 BITS OF HASH CODE - float u = h < 8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. - v = h < 4 ? y : h == 12 || h == 14 ? x : z; + public static float grad(int hash, float x, float y, float z) { + int h = hash & 0x0F; + float u = h < 8 ? x : y, v = h < 4 ? y : h == 12 || h == 14 ? x : z; return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } - /** - * Dot product of two vectors. - * @param a - * the first vector - * @param b - * the second vector - * @return the dot product of two vectors - */ - protected float dot(float[] a, float[] b) { - return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; - } - - protected float surve(float t) { + public static float surve(float t) { return t * t * (3.0f - 2.0f * t); } - protected float at(float rx, float ry, float rz, float[] q) { - return rx * q[0] + ry * q[1] + rz * q[2]; + public static float at(float x, float y, float z, float[] q) { + return x * q[0] + y * q[1] + z * q[2]; + } + + public static void hash(int x, int y, int z, float[] result) { + result[0] = hashpntf[3 * hash[hash[hash[z & 0xFF] + y & 0xFF] + x & 0xFF]]; + result[1] = hashpntf[3 * hash[hash[hash[z & 0xFF] + y & 0xFF] + x & 0xFF] + 1]; + result[2] = hashpntf[3 * hash[hash[hash[z & 0xFF] + y & 0xFF] + x & 0xFF] + 2]; } } @@ -1528,7 +852,7 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; * This interface is used for distance calculation classes. Distance metrics for voronoi. e parameter only used in * Minkovsky. */ - interface DistanceFunc { + interface DistanceFunction { /** * This method calculates the distance for voronoi algorithms. @@ -1547,6 +871,6 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; interface MusgraveFunction { - float execute(Structure tex, float x, float y, float z); + float execute(MusgraveData musgraveData, float x, float y, float z); } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGenerator.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGenerator.java index f479b5626..ac23b375b 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGenerator.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGenerator.java @@ -1,12 +1,47 @@ +/* + * Copyright (c) 2009-2010 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package com.jme3.scene.plugins.blender.textures; +import java.util.Map; +import java.util.TreeMap; +import java.util.logging.Level; import java.util.logging.Logger; +import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; +import com.jme3.scene.plugins.blender.file.DynamicArray; import com.jme3.scene.plugins.blender.file.Pointer; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; import com.jme3.texture.Texture; /** @@ -17,7 +52,7 @@ import com.jme3.texture.Texture; private static final Logger LOGGER = Logger.getLogger(TextureGenerator.class.getName()); protected NoiseGenerator noiseGenerator; - + public TextureGenerator(NoiseGenerator noiseGenerator) { this.noiseGenerator = noiseGenerator; } @@ -47,7 +82,7 @@ import com.jme3.texture.Texture; * the data repository * @return read colorband or null if not present */ - protected ColorBand readColorband(Structure tex, DataRepository dataRepository) { + private ColorBand readColorband(Structure tex, DataRepository dataRepository) { ColorBand result = null; int flag = ((Number) tex.getFieldValue("flag")).intValue(); if ((flag & NoiseGenerator.TEX_COLORBAND) != 0) { @@ -57,10 +92,340 @@ import com.jme3.texture.Texture; colorbandStructure = pColorband.fetchData(dataRepository.getInputStream()).get(0); result = new ColorBand(colorbandStructure); } catch (BlenderFileException e) { - LOGGER.warning("Cannot fetch the colorband structure. The reason: " + e.getLocalizedMessage()); - // TODO: throw an exception here ??? + LOGGER.log(Level.WARNING, "Cannot fetch the colorband structure. The reason: {0}", e.getLocalizedMessage()); } } return result; } + + protected float[][] computeColorband(Structure tex, DataRepository dataRepository) { + ColorBand colorBand = this.readColorband(tex, dataRepository); + float[][] result = null; + if(colorBand!=null) { + result = new float[1001][4];//1001 - amount of possible cursor positions; 4 = [r, g, b, a] + ColorBandData[] dataArray = colorBand.data; + + if(dataArray.length==1) {//special case; use only one color for all types of colorband interpolation + for(int i=0;i cbDataMap = new TreeMap(); + for(int i=0;i 1.0f) { + texres.intensity = 1.0f; + } + } + + /** + * The result pixel of generated texture computations; + * + * @author Marcin Roguski (Kaelthas) + */ + protected static class TextureResult implements Cloneable { + public float intensity, red, green, blue, alpha; + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + } + + /** + * A class constaining the colorband data. + * + * @author Marcin Roguski (Kaelthas) + */ + protected static class ColorBand { + //interpolation types + public static final int IPO_LINEAR = 0; + public static final int IPO_EASE = 1; + public static final int IPO_BSPLINE = 2; + public static final int IPO_CARDINAL = 3; + public static final int IPO_CONSTANT = 4; + + public int cursorsAmount, ipoType; + public ColorBandData[] data; + + /** + * Constructor. Loads the data from the given structure. + * + * @param cbdataStructure + * the colorband structure + */ + @SuppressWarnings("unchecked") + public ColorBand(Structure colorbandStructure) { + this.cursorsAmount = ((Number) colorbandStructure.getFieldValue("tot")).intValue(); + this.ipoType = ((Number) colorbandStructure.getFieldValue("ipotype")).intValue(); + this.data = new ColorBandData[this.cursorsAmount]; + DynamicArray data = (DynamicArray) colorbandStructure.getFieldValue("data"); + for (int i = 0; i < this.cursorsAmount; ++i) { + this.data[i] = new ColorBandData(data.get(i)); + } + } + } + + /** + * Class to store the single colorband cursor data. + * + * @author Marcin Roguski (Kaelthas) + */ + protected static class ColorBandData implements Cloneable { + public final float r, g, b, a; + public int pos; + + /** + * Copy constructor. + */ + private ColorBandData(ColorBandData data) { + this.r = data.r; + this.g = data.g; + this.b = data.b; + this.a = data.a; + this.pos = data.pos; + } + + /** + * Constructor. Loads the data from the given structure. + * + * @param cbdataStructure + * the structure containing the CBData object + */ + public ColorBandData(Structure cbdataStructure) { + this.r = ((Number) cbdataStructure.getFieldValue("r")).floatValue(); + this.g = ((Number) cbdataStructure.getFieldValue("g")).floatValue(); + this.b = ((Number) cbdataStructure.getFieldValue("b")).floatValue(); + this.a = ((Number) cbdataStructure.getFieldValue("a")).floatValue(); + this.pos = (int) (((Number) cbdataStructure.getFieldValue("pos")).floatValue() * 1000.0f); + } + + @Override + public ColorBandData clone() { + try { + return (ColorBandData) super.clone(); + } catch (CloneNotSupportedException e) { + return new ColorBandData(this); + } + } + + @Override + public String toString() { + return "P: " + this.pos + " [" + this.r+", "+this.g+", "+this.b+", "+this.a+"]"; + } + } + + /** + * This class contains brightness and contrast data. + * @author Marcin Roguski (Kaelthas) + */ + protected static class BrightnessAndContrastData { + public final float contrast; + public final float brightness; + public final float rFactor; + public final float gFactor; + public final float bFactor; + + /** + * Constructor reads the required data from the given structure. + * @param tex texture structure + */ + public BrightnessAndContrastData(Structure tex) { + contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); + brightness = ((Number) tex.getFieldValue("bright")).floatValue() - 0.5f; + rFactor = ((Number) tex.getFieldValue("rfac")).floatValue(); + gFactor = ((Number) tex.getFieldValue("gfac")).floatValue(); + bFactor = ((Number) tex.getFieldValue("bfac")).floatValue(); + } + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorBlend.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorBlend.java index 7798dfb10..bb2bb83f1 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorBlend.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorBlend.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -36,8 +37,6 @@ import java.util.ArrayList; import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -49,7 +48,63 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public final class TextureGeneratorBlend extends TextureGenerator { - + + private static final IntensityFunction INTENSITY_FUNCTION[] = new IntensityFunction[7]; + static { + INTENSITY_FUNCTION[0] = new IntensityFunction() {//Linear: stype = 0 (TEX_LIN) + @Override + public float getIntensity(float x, float y, float z) { + return (1.0f + x) * 0.5f; + } + }; + INTENSITY_FUNCTION[1] = new IntensityFunction() {//Quad: stype = 1 (TEX_QUAD) + @Override + public float getIntensity(float x, float y, float z) { + float result = (1.0f + x) * 0.5f; + return result * result; + } + }; + INTENSITY_FUNCTION[2] = new IntensityFunction() {//Ease: stype = 2 (TEX_EASE) + @Override + public float getIntensity(float x, float y, float z) { + float result = (1.0f + x) * 0.5f; + if (result <= 0.0f) { + return 0.0f; + } else if (result >= 1.0f) { + return 1.0f; + } else { + return result * result *(3.0f - 2.0f * result); + } + } + }; + INTENSITY_FUNCTION[3] = new IntensityFunction() {//Diagonal: stype = 3 (TEX_DIAG) + @Override + public float getIntensity(float x, float y, float z) { + return (2.0f + x + y) * 0.25f; + } + }; + INTENSITY_FUNCTION[4] = new IntensityFunction() {//Sphere: stype = 4 (TEX_SPHERE) + @Override + public float getIntensity(float x, float y, float z) { + float result = 1.0f - (float) Math.sqrt(x * x + y * y + z * z); + return result < 0.0f ? 0.0f : result; + } + }; + INTENSITY_FUNCTION[5] = new IntensityFunction() {//Halo: stype = 5 (TEX_HALO) + @Override + public float getIntensity(float x, float y, float z) { + float result = 1.0f - (float) Math.sqrt(x * x + y * y + z * z); + return result <= 0.0f ? 0.0f : result * result; + } + }; + INTENSITY_FUNCTION[6] = new IntensityFunction() {//Radial: stype = 6 (TEX_RAD) + @Override + public float getIntensity(float x, float y, float z) { + return (float) Math.atan2(y, x) * FastMath.INV_TWO_PI + 0.5f; + } + }; + } + /** * Constructor stores the given noise generator. * @param noiseGenerator @@ -63,81 +118,51 @@ public final class TextureGeneratorBlend extends TextureGenerator { protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { int flag = ((Number) tex.getFieldValue("flag")).intValue(); int stype = ((Number) tex.getFieldValue("stype")).intValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth, x, y, t; - float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + TextureResult texres = new TextureResult(); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD, x, y; + float[][] colorBand = this.computeColorband(tex, dataRepository); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + boolean flipped = (flag & NoiseGenerator.TEX_FLIPBLEND) != 0; + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { - texvec[0] = wDelta * i; + x = wDelta * i; for (int j = -halfH; j < halfH; ++j) { - texvec[1] = hDelta * j; + if (flipped) { + y = x; + x = hDelta * j; + } else { + y = hDelta * j; + } for (int k = -halfD; k < halfD; ++k) { - texvec[2] = dDelta * k; - if ((flag & NoiseGenerator.TEX_FLIPBLEND) != 0) { - x = texvec[1]; - y = texvec[0]; - } else { - x = texvec[0]; - y = texvec[1]; - } - - if (stype == NoiseGenerator.TEX_LIN) { /* lin */ - texres.tin = (1.0f + x) / 2.0f; - } else if (stype == NoiseGenerator.TEX_QUAD) { /* quad */ - texres.tin = (1.0f + x) / 2.0f; - if (texres.tin < 0.0f) { - texres.tin = 0.0f; - } else { - texres.tin *= texres.tin; - } - } else if (stype == NoiseGenerator.TEX_EASE) { /* ease */ - texres.tin = (1.0f + x) / 2.0f; - if (texres.tin <= 0.0f) { - texres.tin = 0.0f; - } else if (texres.tin >= 1.0f) { - texres.tin = 1.0f; - } else { - t = texres.tin * texres.tin; - texres.tin = 3.0f * t - 2.0f * t * texres.tin; - } - } else if (stype == NoiseGenerator.TEX_DIAG) { /* diag */ - texres.tin = (2.0f + x + y) / 4.0f; - } else if (stype == NoiseGenerator.TEX_RAD) { /* radial */ - texres.tin = (float) Math.atan2(y, x) / FastMath.TWO_PI + 0.5f; - } else { /* sphere TEX_SPHERE */ - texres.tin = 1.0f - (float) Math.sqrt(x * x + y * y + texvec[2] * texvec[2]); - if (texres.tin < 0.0f) { - texres.tin = 0.0f; - } - if (stype == NoiseGenerator.TEX_HALO) { - texres.tin *= texres.tin; - } /* halo */ - } + texres.intensity = INTENSITY_FUNCTION[stype].getIntensity(x, y, dDelta * k); + if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, brightness); - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + private static interface IntensityFunction { + float getIntensity(float x, float y, float z); + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorClouds.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorClouds.java index 9b3cbd5bc..ffb6abdf9 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorClouds.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorClouds.java @@ -1,42 +1,42 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; import java.nio.ByteBuffer; import java.util.ArrayList; +import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -48,7 +48,14 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public class TextureGeneratorClouds extends TextureGenerator { - + // tex->noisetype + protected static final int TEX_NOISESOFT = 0; + protected static final int TEX_NOISEPERL = 1; + + // tex->stype + protected static final int TEX_DEFAULT = 0; + protected static final int TEX_COLOR = 1; + /** * Constructor stores the given noise generator. * @param noiseGenerator @@ -60,69 +67,64 @@ public class TextureGeneratorClouds extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - // preparing the proper data - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); + TextureResult texres = new TextureResult(); // reading the data from the texture structure float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); int noiseDepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); int noiseBasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); int noiseType = ((Number) tex.getFieldValue("noisetype")).intValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float bright = ((Number) tex.getFieldValue("bright")).floatValue(); - boolean isHard = noiseType != NoiseGenerator.TEX_NOISESOFT; + boolean isHard = noiseType != TEX_NOISESOFT; int sType = ((Number) tex.getFieldValue("stype")).intValue(); - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); - Format format = sType == NoiseGenerator.TEX_COLOR || colorBand != null ? Format.RGB8 : Format.Luminance8; - int bytesPerPixel = sType == NoiseGenerator.TEX_COLOR || colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + float[][] colorBand = this.computeColorband(tex, dataRepository); + Format format = sType == TEX_COLOR || colorBand != null ? Format.RGB8 : Format.Luminance8; + int bytesPerPixel = sType == TEX_COLOR || colorBand != null ? 3 : 1; + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j; for (int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k; - texres.tin = noiseGenerator.bliGTurbulence(noisesize, texvec[0], texvec[1], texvec[2], noiseDepth, isHard, noiseBasis); + texres.intensity = NoiseGenerator.NoiseFunctions.turbulence(texvec[0], texvec[1], texvec[2], noisesize, noiseDepth, noiseBasis, isHard); + texres.intensity = FastMath.clamp(texres.intensity, 0.0f, 1.0f); if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) { - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - // calculate bumpnormal - texres.nor[0] = noiseGenerator.bliGTurbulence(noisesize, texvec[0] + nabla, texvec[1], texvec[2], noiseDepth, isHard, noiseBasis); - texres.nor[1] = noiseGenerator.bliGTurbulence(noisesize, texvec[0], texvec[1] + nabla, texvec[2], noiseDepth, isHard, noiseBasis); - texres.nor[2] = noiseGenerator.bliGTurbulence(noisesize, texvec[0], texvec[1], texvec[2] + nabla, noiseDepth, isHard, noiseBasis); - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); - } - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); - } else if (sType == NoiseGenerator.TEX_COLOR) { - // in this case, int. value should really be computed from color, - // and bumpnormal from that, would be too slow, looks ok as is - texres.tr = texres.tin; - texres.tg = noiseGenerator.bliGTurbulence(noisesize, texvec[1], texvec[0], texvec[2], noiseDepth, isHard, noiseBasis); - texres.tb = noiseGenerator.bliGTurbulence(noisesize, texvec[1], texvec[2], texvec[0], noiseDepth, isHard, noiseBasis); - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); + } else if (sType == TEX_COLOR) { + texres.red = texres.intensity; + texres.green = NoiseGenerator.NoiseFunctions.turbulence(texvec[1], texvec[0], texvec[2], noisesize, noiseDepth, noiseBasis, isHard); + texres.blue = NoiseGenerator.NoiseFunctions.turbulence(texvec[1], texvec[2], texvec[0], noisesize, noiseDepth, noiseBasis, isHard); + + texres.green = FastMath.clamp(texres.green, 0.0f, 1.0f); + texres.blue = FastMath.clamp(texres.blue, 0.0f, 1.0f); + + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, bright); - data.put((byte) (texres.tin * 255)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } + ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorDistnoise.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorDistnoise.java index a7bd7fc03..a1ffab9a8 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorDistnoise.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorDistnoise.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -35,8 +36,7 @@ import java.util.ArrayList; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; +import com.jme3.scene.plugins.blender.textures.NoiseGenerator.NoiseFunction; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -61,56 +61,73 @@ public class TextureGeneratorDistnoise extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); float distAmount = ((Number) tex.getFieldValue("dist_amount")).floatValue(); int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); int noisebasis2 = ((Number) tex.getFieldValue("noisebasis2")).intValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - TexResult texres = new TexResult(); + TextureResult texres = new TextureResult(); float[] texvec = new float[] { 0, 0, 0 }; - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i / noisesize; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j / noisesize; for (int k = -halfD; k < halfD; ++k) { - texvec[2] = dDelta * k;// z - texres.tin = noiseGenerator.mgVLNoise(texvec[0], texvec[1], texvec[2], distAmount, noisebasis, noisebasis2); + texvec[2] = dDelta * k; + texres.intensity = this.musgraveVariableLunacrityNoise(texvec[0], texvec[1], texvec[2], distAmount, noisebasis, noisebasis2); if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) { - float offs = nabla / noisesize; // also scaling of texvec - /* calculate bumpnormal */ - texres.nor[0] = noiseGenerator.mgVLNoise(texvec[0] + offs, texvec[1], texvec[2], distAmount, noisebasis, noisebasis2); - texres.nor[1] = noiseGenerator.mgVLNoise(texvec[0], texvec[1] + offs, texvec[2], distAmount, noisebasis, noisebasis2); - texres.nor[2] = noiseGenerator.mgVLNoise(texvec[0], texvec[1], texvec[2] + offs, distAmount, noisebasis, noisebasis2); - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); - } + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, brightness); - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + /** + * "Variable Lacunarity Noise" A distorted variety of Perlin noise. This method is used to calculate distorted noise + * texture. + * @param x + * @param y + * @param z + * @param distortion + * @param nbas1 + * @param nbas2 + * @return + */ + private float musgraveVariableLunacrityNoise(float x, float y, float z, float distortion, int nbas1, int nbas2) { + NoiseFunction abstractNoiseFunc1 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(nbas1)); + if (abstractNoiseFunc1 == null) { + abstractNoiseFunc1 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(0)); + } + NoiseFunction abstractNoiseFunc2 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(nbas2)); + if (abstractNoiseFunc2 == null) { + abstractNoiseFunc2 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(0)); + } + // get a random vector and scale the randomization + float rx = abstractNoiseFunc1.execute(x + 13.5f, y + 13.5f, z + 13.5f) * distortion; + float ry = abstractNoiseFunc1.execute(x, y, z) * distortion; + float rz = abstractNoiseFunc1.execute(x - 13.5f, y - 13.5f, z - 13.5f) * distortion; + return abstractNoiseFunc2.executeSigned(x + rx, y + ry, z + rz); //distorted-domain noise + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMagic.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMagic.java index 6045365ea..9b2002f25 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMagic.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMagic.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -35,8 +36,6 @@ import java.util.ArrayList; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -48,7 +47,70 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public class TextureGeneratorMagic extends TextureGenerator { - + private static NoiseDepthFunction[] noiseDepthFunctions = new NoiseDepthFunction[10]; + static { + noiseDepthFunctions[0] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[1] = -(float) Math.cos(xyz[0] - xyz[1] + xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[1] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[0] = (float) Math.cos(xyz[0] - xyz[1] - xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[2] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[2] = (float) Math.sin(-xyz[0] - xyz[1] - xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[3] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[0] = -(float) Math.cos(-xyz[0] + xyz[1] - xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[4] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[1] = -(float) Math.sin(-xyz[0] + xyz[1] + xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[5] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[1] = -(float) Math.cos(-xyz[0] + xyz[1] + xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[6] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[0] = (float) Math.cos(xyz[0] + xyz[1] + xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[7] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[2] = (float) Math.sin(xyz[0] + xyz[1] - xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[8] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[0] = -(float) Math.cos(-xyz[0] - xyz[1] + xyz[2]) * turbulence; + } + }; + noiseDepthFunctions[9] = new NoiseDepthFunction() { + @Override + public void compute(float[] xyz, float turbulence) { + xyz[1] = -(float) Math.sin(xyz[0] - xyz[1] + xyz[2]) * turbulence; + } + }; + } + /** * Constructor stores the given noise generator. * @param noiseGenerator @@ -60,88 +122,67 @@ public class TextureGeneratorMagic extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - float x, y, z, turb; + float xyz[] = new float[3], turb; int noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); float turbul = ((Number) tex.getFieldValue("turbul")).floatValue() / 5.0f; float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * 4); + TextureResult texres = new TextureResult(); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + float[][] colorBand = this.computeColorband(tex, dataRepository); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + byte[] data = new byte[width * height * depth * 3]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j; for (int k = -halfD; k < halfD; ++k) { turb = turbul; - texvec[2] = dDelta * k;// z - x = (float) Math.sin((texvec[0] + texvec[1] + texvec[2]) * 5.0f); - y = (float) Math.cos((-texvec[0] + texvec[1] - texvec[2]) * 5.0f); - z = -(float) Math.cos((-texvec[0] - texvec[1] + texvec[2]) * 5.0f); + texvec[2] = dDelta * k; + xyz[0] = (float) Math.sin((texvec[0] + texvec[1] + texvec[2]) * 5.0f); + xyz[1] = (float) Math.cos((-texvec[0] + texvec[1] - texvec[2]) * 5.0f); + xyz[2] = -(float) Math.cos((-texvec[0] - texvec[1] + texvec[2]) * 5.0f); if (colorBand != null) { - texres.tin = 0.3333f * (x + y + z); - noiseGenerator.doColorband(colorBand, texres, dataRepository); + texres.intensity = 0.3333f * (xyz[0] + xyz[1] + xyz[2]); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; } else { if (noisedepth > 0) { - x *= turb; - y *= turb; - z *= turb; - y = -(float) Math.cos(x - y + z) * turb; - if (noisedepth > 1) { - x = (float) Math.cos(x - y - z) * turb; - if (noisedepth > 2) { - z = (float) Math.sin(-x - y - z) * turb; - if (noisedepth > 3) { - x = -(float) Math.cos(-x + y - z) * turb; - if (noisedepth > 4) { - y = -(float) Math.sin(-x + y + z) * turb; - if (noisedepth > 5) { - y = -(float) Math.cos(-x + y + z) * turb; - if (noisedepth > 6) { - x = (float) Math.cos(x + y + z) * turb; - if (noisedepth > 7) { - z = (float) Math.sin(x + y - z) * turb; - if (noisedepth > 8) { - x = -(float) Math.cos(-x - y + z) * turb; - if (noisedepth > 9) { - y = -(float) Math.sin(x - y + z) * turb; - } - } - } - } - } - } - } - } + xyz[0] *= turb; + xyz[1] *= turb; + xyz[2] *= turb; + for (int m=0;m dataArray = new ArrayList(1); - dataArray.add(data); - return new Texture3D(new Image(Format.ABGR8, width, height, depth, dataArray)); + dataArray.add(BufferUtils.createByteBuffer(data)); + return new Texture3D(new Image(Format.RGB8, width, height, depth, dataArray)); + } + + private static interface NoiseDepthFunction { + void compute(float[] xyz, float turbulence); } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMarble.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMarble.java index d84784e22..af8877160 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMarble.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMarble.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -35,8 +36,6 @@ import java.util.ArrayList; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -47,8 +46,12 @@ import com.jme3.util.BufferUtils; * This class generates the 'marble' texture. * @author Marcin Roguski (Kaelthas) */ -public class TextureGeneratorMarble extends TextureGenerator { - +public class TextureGeneratorMarble extends TextureGeneratorWood { + // tex->stype + protected static final int TEX_SOFT = 0; + protected static final int TEX_SHARP = 1; + protected static final int TEX_SHARPER = 2; + /** * Constructor stores the given noise generator. * @param noiseGenerator @@ -60,51 +63,86 @@ public class TextureGeneratorMarble extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - // preparing the proper data - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float bright = ((Number) tex.getFieldValue("bright")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + TextureResult texres = new TextureResult(); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + MarbleData marbleData = new MarbleData(tex); + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j; for (int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k; - texres.tin = noiseGenerator.marbleInt(tex, texvec[0], texvec[1], texvec[2], dataRepository); + texres.intensity = this.marbleInt(marbleData, texvec[0], texvec[1], texvec[2]); if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) {// calculate bumpnormal - texres.nor[0] = noiseGenerator.marbleInt(tex, texvec[0] + nabla, texvec[1], texvec[2], dataRepository); - texres.nor[1] = noiseGenerator.marbleInt(tex, texvec[0], texvec[1] + nabla, texvec[2], dataRepository); - texres.nor[2] = noiseGenerator.marbleInt(tex, texvec[0], texvec[1], texvec[2] + nabla, dataRepository); - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); - } - - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, bright); - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + public float marbleInt(MarbleData marbleData, float x, float y, float z) { + int waveform; + if (marbleData.waveform > TEX_TRI || marbleData.waveform < TEX_SIN) { + waveform = 0; + } else { + waveform = marbleData.waveform; + } + + float n = 5.0f * (x + y + z); + float mi = n + marbleData.turbul * NoiseGenerator.NoiseFunctions.turbulence(x, y, z, marbleData.noisesize, marbleData.noisedepth, marbleData.noisebasis, marbleData.isHard); + + if (marbleData.stype >= TEX_SOFT) { + mi = waveformFunctions[waveform].execute(mi); + if (marbleData.stype == TEX_SHARP) { + mi = (float) Math.sqrt(mi); + } else if (marbleData.stype == TEX_SHARPER) { + mi = (float) Math.sqrt(Math.sqrt(mi)); + } + } + return mi; + } + + private static class MarbleData { + public final float noisesize; + public final int noisebasis; + public final int noisedepth; + public final int stype; + public final float turbul; + public final int waveform; + public final boolean isHard; + + public MarbleData(Structure tex) { + noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); + noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); + noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); + stype = ((Number) tex.getFieldValue("stype")).intValue(); + turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); + int noisetype = ((Number) tex.getFieldValue("noisetype")).intValue(); + waveform = ((Number) tex.getFieldValue("noisebasis2")).intValue(); + isHard = noisetype != TEX_NOISESOFT; + } + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMusgrave.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMusgrave.java index 02c58b0cb..8877ef4f6 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMusgrave.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorMusgrave.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -35,8 +36,7 @@ import java.util.ArrayList; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; +import com.jme3.scene.plugins.blender.textures.NoiseGenerator.MusgraveFunction; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -62,52 +62,76 @@ public class TextureGeneratorMusgrave extends TextureGenerator { protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { int stype = ((Number) tex.getFieldValue("stype")).intValue(); float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - TexResult texres = new TexResult(); + TextureResult texres = new TextureResult(); float[] texvec = new float[] { 0, 0, 0 }; - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + MusgraveData musgraveData = new MusgraveData(tex); + MusgraveFunction musgraveFunction; + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i / noisesize; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j / noisesize; for (int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k / noisesize; - switch (stype) { - case NoiseGenerator.TEX_MFRACTAL: - case NoiseGenerator.TEX_FBM: - noiseGenerator.mgMFractalOrfBmTex(tex, texvec, colorBand, texres, dataRepository); - break; - case NoiseGenerator.TEX_RIDGEDMF: - case NoiseGenerator.TEX_HYBRIDMF: - noiseGenerator.mgRidgedOrHybridMFTex(tex, texvec, colorBand, texres, dataRepository); - break; - case NoiseGenerator.TEX_HTERRAIN: - noiseGenerator.mgHTerrainTex(tex, texvec, colorBand, texres, dataRepository); - break; - default: - throw new IllegalStateException("Unknown type of musgrave texture: " + stype); + musgraveFunction = NoiseGenerator.musgraveFunctions.get(Integer.valueOf(musgraveData.stype)); + if(musgraveFunction==null) { + throw new IllegalStateException("Unknown type of musgrave texture: " + stype); } + texres.intensity = musgraveData.outscale * musgraveFunction.execute(musgraveData, texvec[0], texvec[1], texvec[2]); + if(texres.intensity>1) { + texres.intensity = 1.0f; + } else if(texres.intensity < 0) { + texres.intensity = 0.0f; + } + if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + protected static class MusgraveData { + public final int stype; + public final float outscale; + public final float h; + public final float lacunarity; + public final float octaves; + public final int noisebasis; + public final float offset; + public final float gain; + + public MusgraveData(Structure tex) { + stype = ((Number) tex.getFieldValue("stype")).intValue(); + outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); + h = ((Number) tex.getFieldValue("mg_H")).floatValue(); + lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); + octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); + noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); + offset = ((Number) tex.getFieldValue("mg_offset")).floatValue(); + gain = ((Number) tex.getFieldValue("mg_gain")).floatValue(); + } + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorNoise.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorNoise.java index 2c9429ab1..9213906ee 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorNoise.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorNoise.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -36,8 +37,6 @@ import java.util.ArrayList; import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -61,49 +60,47 @@ public class TextureGeneratorNoise extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - float div = 3.0f; - int val, ran, loop; + int val, random, loop; int noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); - TexResult texres = new TexResult(); - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + TextureResult texres = new TextureResult(); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { for (int j = -halfH; j < halfH; ++j) { for (int k = -halfD; k < halfD; ++k) { - ran = FastMath.rand.nextInt();// BLI_rand(); - val = ran & 3; + random = FastMath.rand.nextInt(); + val = random & 3; loop = noisedepth; while (loop-- != 0) { - ran = ran >> 2; - val *= ran & 3; - div *= 3.0f; + random >>= 2; + val *= random & 3; } - texres.tin = val;// / div; + texres.intensity = val; if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, brightness); - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorStucci.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorStucci.java index 85f5cf821..4da0d0286 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorStucci.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorStucci.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -35,8 +36,6 @@ import java.util.ArrayList; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -48,7 +47,8 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public class TextureGeneratorStucci extends TextureGenerator { - + protected static final int TEX_NOISESOFT = 0; + /** * Constructor stores the given noise generator. * @param noiseGenerator @@ -64,70 +64,61 @@ public class TextureGeneratorStucci extends TextureGenerator { int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); int noisetype = ((Number) tex.getFieldValue("noisetype")).intValue(); float turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); - boolean isHard = noisetype != NoiseGenerator.TEX_NOISESOFT; + boolean isHard = noisetype != TEX_NOISESOFT; int stype = ((Number) tex.getFieldValue("stype")).intValue(); + if(noisesize<=0.001f) {//the texture goes black if this value is lower than 0.001f + noisesize = 0.001f; + } + float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth, b2, ofs; - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); + TextureResult texres = new TextureResult(); + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD, noiseValue, ofs;; + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j; for (int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k; - b2 = noiseGenerator.bliGNoise(noisesize, texvec[0], texvec[1], texvec[2], isHard, noisebasis); - + noiseValue = NoiseGenerator.NoiseFunctions.noise(texvec[0], texvec[1], texvec[2], noisesize, 0, noisebasis, isHard); ofs = turbul / 200.0f; - if (stype != 0) { - ofs *= b2 * b2; + ofs *= noiseValue * noiseValue; } - texres.tin = noiseGenerator.bliGNoise(noisesize, texvec[0], texvec[1], texvec[2] + ofs, isHard, noisebasis);// ==nor[2] + texres.intensity = NoiseGenerator.NoiseFunctions.noise(texvec[0], texvec[1], texvec[2] + ofs, noisesize, 0, noisebasis, isHard); if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) { - texres.nor[0] = noiseGenerator.bliGNoise(noisesize, texvec[0] + ofs, texvec[1], texvec[2], isHard, noisebasis); - texres.nor[1] = noiseGenerator.bliGNoise(noisesize, texvec[0], texvec[1] + ofs, texvec[2], isHard, noisebasis); - texres.nor[2] = texres.tin; - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); - - if (stype == NoiseGenerator.TEX_WALLOUT) { - texres.nor[0] = -texres.nor[0]; - texres.nor[1] = -texres.nor[1]; - texres.nor[2] = -texres.nor[2]; - } - } + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; } if (stype == NoiseGenerator.TEX_WALLOUT) { - texres.tin = 1.0f - texres.tin; + texres.intensity = 1.0f - texres.intensity; } - if (texres.tin < 0.0f) { - texres.tin = 0.0f; + if (texres.intensity < 0.0f) { + texres.intensity = 0.0f; } + //no brightness and contrast needed for stucci (it doesn't affect the texture) if (colorBand != null) { - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - data.put((byte) (texres.tin * 255.0f)); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorVoronoi.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorVoronoi.java index 890de2542..0448b29a3 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorVoronoi.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorVoronoi.java @@ -1,32 +1,33 @@ /* - * - * $Id: noise.c 14611 2008-04-29 08:24:33Z campbellbarton $ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * Copyright (c) 2009-2010 jMonkeyEngine * All rights reserved. * - * The Original Code is: all of this file. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: * - * Contributor(s): none yet. + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * ***** END GPL LICENSE BLOCK ***** + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.blender.textures; @@ -36,8 +37,7 @@ import java.util.ArrayList; import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; +import com.jme3.scene.plugins.blender.textures.NoiseGenerator.NoiseMath; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -61,115 +61,109 @@ public class TextureGeneratorVoronoi extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - float vn_w1 = ((Number) tex.getFieldValue("vn_w1")).floatValue(); - float vn_w2 = ((Number) tex.getFieldValue("vn_w2")).floatValue(); - float vn_w3 = ((Number) tex.getFieldValue("vn_w3")).floatValue(); - float vn_w4 = ((Number) tex.getFieldValue("vn_w4")).floatValue(); + float voronoiWeight1 = ((Number) tex.getFieldValue("vn_w1")).floatValue(); + float voronoiWeight2 = ((Number) tex.getFieldValue("vn_w2")).floatValue(); + float voronoiWeight3 = ((Number) tex.getFieldValue("vn_w3")).floatValue(); + float voronoiWeight4 = ((Number) tex.getFieldValue("vn_w4")).floatValue(); float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float ns_outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); - float vn_mexp = ((Number) tex.getFieldValue("vn_mexp")).floatValue(); - int vn_distm = ((Number) tex.getFieldValue("vn_distm")).intValue(); - int vn_coltype = ((Number) tex.getFieldValue("vn_coltype")).intValue(); - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float brightness = ((Number) tex.getFieldValue("bright")).floatValue(); + float outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); + float mexp = ((Number) tex.getFieldValue("vn_mexp")).floatValue(); + int distm = ((Number) tex.getFieldValue("vn_distm")).intValue(); + int voronoiColorType = ((Number) tex.getFieldValue("vn_coltype")).intValue(); - TexResult texres = new TexResult(); + TextureResult texres = new TextureResult(); float[] texvec = new float[] { 0, 0, 0 }; - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; - int halfW = width, halfH = height, halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; - ColorBand colorBand = this.readColorband(tex, dataRepository); - Format format = vn_coltype != 0 || colorBand != null ? Format.RGB8 : Format.Luminance8; - int bytesPerPixel = vn_coltype != 0 || colorBand != null ? 3 : 1; - - float[] da = new float[4], pa = new float[12]; /* distance and point coordinate arrays of 4 nearest neighbours */ - float[] ca = vn_coltype != 0 ? new float[3] : null; // cell color - float aw1 = FastMath.abs(vn_w1); - float aw2 = FastMath.abs(vn_w2); - float aw3 = FastMath.abs(vn_w3); - float aw4 = FastMath.abs(vn_w4); - float sc = aw1 + aw2 + aw3 + aw4; - if (sc != 0.f) { - sc = ns_outscale / sc; + int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; + + float[][] colorBand = this.computeColorband(tex, dataRepository); + Format format = voronoiColorType != 0 || colorBand != null ? Format.RGB8 : Format.Luminance8; + int bytesPerPixel = voronoiColorType != 0 || colorBand != null ? 3 : 1; + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + float[] da = new float[4], pa = new float[12]; + float[] hashPoint = voronoiColorType != 0 ? new float[3] : null; + float[] voronoiWeights = new float[] {FastMath.abs(voronoiWeight1), FastMath.abs(voronoiWeight2), + FastMath.abs(voronoiWeight3), FastMath.abs(voronoiWeight4)}; + float weight; + float sc = voronoiWeights[0] + voronoiWeights[1] + voronoiWeights[2] + voronoiWeights[3]; + if (sc != 0.0f) { + sc = outscale / sc; } - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i / noisesize; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j / noisesize; for (int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k; - noiseGenerator.voronoi(texvec[0], texvec[1], texvec[2], da, pa, vn_mexp, vn_distm); - texres.tin = sc * FastMath.abs(vn_w1 * da[0] + vn_w2 * da[1] + vn_w3 * da[2] + vn_w4 * da[3]); - if (vn_coltype != 0) { - noiseGenerator.cellNoiseV(pa[0], pa[1], pa[2], ca); - texres.tr = aw1 * ca[0]; - texres.tg = aw1 * ca[1]; - texres.tb = aw1 * ca[2]; - noiseGenerator.cellNoiseV(pa[3], pa[4], pa[5], ca); - texres.tr += aw2 * ca[0]; - texres.tg += aw2 * ca[1]; - texres.tb += aw2 * ca[2]; - noiseGenerator.cellNoiseV(pa[6], pa[7], pa[8], ca); - texres.tr += aw3 * ca[0]; - texres.tg += aw3 * ca[1]; - texres.tb += aw3 * ca[2]; - noiseGenerator.cellNoiseV(pa[9], pa[10], pa[11], ca); - texres.tr += aw4 * ca[0]; - texres.tg += aw4 * ca[1]; - texres.tb += aw4 * ca[2]; - if (vn_coltype >= 2) { + NoiseGenerator.NoiseFunctions.voronoi(texvec[0], texvec[1], texvec[2], da, pa, mexp, distm); + texres.intensity = sc * FastMath.abs(voronoiWeight1 * da[0] + voronoiWeight2 * da[1] + voronoiWeight3 * da[2] + voronoiWeight4 * da[3]); + if(texres.intensity>1.0f) { + texres.intensity = 1.0f; + } else if(texres.intensity<0.0f) { + texres.intensity = 0.0f; + } + + if (colorBand != null) {//colorband ALWAYS goes first and covers the color (if set) + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + } else if (voronoiColorType != 0) { + texres.red = texres.green = texres.blue = 0.0f; + for(int m=0; m<12; m+=3) { + weight = voronoiWeights[m/3]; + this.cellNoiseV(pa[m], pa[m + 1], pa[m + 2], hashPoint); + texres.red += weight * hashPoint[0]; + texres.green += weight * hashPoint[1]; + texres.blue += weight * hashPoint[2]; + } + if (voronoiColorType >= 2) { float t1 = (da[1] - da[0]) * 10.0f; - if (t1 > 1) { + if (t1 > 1.0f) { t1 = 1.0f; } - if (vn_coltype == 3) { - t1 *= texres.tin; + if (voronoiColorType == 3) { + t1 *= texres.intensity; } else { t1 *= sc; } - texres.tr *= t1; - texres.tg *= t1; - texres.tb *= t1; + texres.red *= t1; + texres.green *= t1; + texres.blue *= t1; } else { - texres.tr *= sc; - texres.tg *= sc; - texres.tb *= sc; - } - } - if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) { - float offs = nabla / noisesize; // also scaling of texvec - // calculate bumpnormal - noiseGenerator.voronoi(texvec[0] + offs, texvec[1], texvec[2], da, pa, vn_mexp, vn_distm); - texres.nor[0] = sc * FastMath.abs(vn_w1 * da[0] + vn_w2 * da[1] + vn_w3 * da[2] + vn_w4 * da[3]); - noiseGenerator.voronoi(texvec[0], texvec[1] + offs, texvec[2], da, pa, vn_mexp, vn_distm); - texres.nor[1] = sc * FastMath.abs(vn_w1 * da[0] + vn_w2 * da[1] + vn_w3 * da[2] + vn_w4 * da[3]); - noiseGenerator.voronoi(texvec[0], texvec[1], texvec[2] + offs, da, pa, vn_mexp, vn_distm); - texres.nor[2] = sc * FastMath.abs(vn_w1 * da[0] + vn_w2 * da[1] + vn_w3 * da[2] + vn_w4 * da[3]); - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); + texres.red *= sc; + texres.green *= sc; + texres.blue *= sc; } } - if (vn_coltype != 0 || colorBand != null) { - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f));// tin or tr?? - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + if (voronoiColorType != 0 || colorBand != null) { + this.applyBrightnessAndContrast(bacd, texres); + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, brightness); - data.put((byte) (texres.tin * 255.0f)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + /** + * Returns a vector/point/color in ca, using point hasharray directly + */ + private void cellNoiseV(float x, float y, float z, float[] hashPoint) { + int xi = (int) Math.floor(x); + int yi = (int) Math.floor(y); + int zi = (int) Math.floor(z); + NoiseMath.hash(xi, yi, zi, hashPoint); + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorWood.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorWood.java index 4466f5fa4..f9bd7f9d9 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorWood.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureGeneratorWood.java @@ -33,10 +33,9 @@ package com.jme3.scene.plugins.blender.textures; import java.nio.ByteBuffer; import java.util.ArrayList; +import com.jme3.math.FastMath; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.file.Structure; -import com.jme3.scene.plugins.blender.textures.TextureHelper.ColorBand; -import com.jme3.scene.plugins.blender.textures.TextureHelper.TexResult; import com.jme3.texture.Image; import com.jme3.texture.Image.Format; import com.jme3.texture.Texture; @@ -48,7 +47,21 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public class TextureGeneratorWood extends TextureGenerator { - + // tex->noisebasis2 + protected static final int TEX_SIN = 0; + protected static final int TEX_SAW = 1; + protected static final int TEX_TRI = 2; + + // tex->stype + protected static final int TEX_BAND = 0; + protected static final int TEX_RING = 1; + protected static final int TEX_BANDNOISE = 2; + protected static final int TEX_RINGNOISE = 3; + + // tex->noisetype + protected static final int TEX_NOISESOFT = 0; + protected static final int TEX_NOISEPERL = 1; + /** * Constructor stores the given noise generator. * @param noiseGenerator the noise generator @@ -59,53 +72,145 @@ public class TextureGeneratorWood extends TextureGenerator { @Override protected Texture generate(Structure tex, int width, int height, int depth, DataRepository dataRepository) { - // preparing the proper data - float contrast = ((Number) tex.getFieldValue("contrast")).floatValue(); - float bright = ((Number) tex.getFieldValue("bright")).floatValue(); - float nabla = ((Number) tex.getFieldValue("nabla")).floatValue(); - float wDelta = 1.0f / width, hDelta = 1.0f / height, dDelta = 1.0f / depth; float[] texvec = new float[] { 0, 0, 0 }; - TexResult texres = new TexResult(); - int halfW = width; - int halfH = height; - int halfD = depth; - width <<= 1; - height <<= 1; - depth <<= 1; + TextureResult texres = new TextureResult(); + int halfW = width >> 1; + int halfH = height >> 1; + int halfD = depth >> 1; + float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; - ColorBand colorBand = this.readColorband(tex, dataRepository); + float[][] colorBand = this.computeColorband(tex, dataRepository); Format format = colorBand != null ? Format.RGB8 : Format.Luminance8; int bytesPerPixel = colorBand != null ? 3 : 1; - - ByteBuffer data = BufferUtils.createByteBuffer(width * height * depth * bytesPerPixel); + WoodIntensityData woodIntensityData = new WoodIntensityData(tex); + BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); + + int index = 0; + byte[] data = new byte[width * height * depth * bytesPerPixel]; for (int i = -halfW; i < halfW; ++i) { texvec[0] = wDelta * i; for (int j = -halfH; j < halfH; ++j) { texvec[1] = hDelta * j; for(int k = -halfD; k < halfD; ++k) { texvec[2] = dDelta * k; - texres.tin = noiseGenerator.woodInt(tex, texvec[0], texvec[1], texvec[2], dataRepository); + texres.intensity = this.woodIntensity(woodIntensityData, texvec[0], texvec[1], texvec[2]); + if (colorBand != null) { - noiseGenerator.doColorband(colorBand, texres, dataRepository); - if (texres.nor != null) {// calculate bumpnormal - texres.nor[0] = noiseGenerator.woodInt(tex, texvec[0] + nabla, texvec[1], texvec[2], dataRepository); - texres.nor[1] = noiseGenerator.woodInt(tex, texvec[0], texvec[1] + nabla, texvec[2], dataRepository); - texres.nor[2] = noiseGenerator.woodInt(tex, texvec[0], texvec[1], texvec[2] + nabla, dataRepository); - noiseGenerator.texNormalDerivate(colorBand, texres, dataRepository); - } - noiseGenerator.brightnesAndContrastRGB(tex, texres); - data.put((byte) (texres.tr * 255.0f)); - data.put((byte) (texres.tg * 255.0f)); - data.put((byte) (texres.tb * 255.0f)); + int colorbandIndex = (int) (texres.intensity * 1000.0f); + texres.red = colorBand[colorbandIndex][0]; + texres.green = colorBand[colorbandIndex][1]; + texres.blue = colorBand[colorbandIndex][2]; + + this.applyBrightnessAndContrast(bacd, texres); + + data[index++] = (byte) (texres.red * 255.0f); + data[index++] = (byte) (texres.green * 255.0f); + data[index++] = (byte) (texres.blue * 255.0f); } else { - noiseGenerator.brightnesAndContrast(texres, contrast, bright); - data.put((byte) (texres.tin * 255)); + this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); + data[index++] = (byte) (texres.intensity * 255.0f); } } } } + ArrayList dataArray = new ArrayList(1); - dataArray.add(data); + dataArray.add(BufferUtils.createByteBuffer(data)); return new Texture3D(new Image(format, width, height, depth, dataArray)); } + + protected static WaveForm[] waveformFunctions = new WaveForm[3]; + static { + waveformFunctions[0] = new WaveForm() {// sinus (TEX_SIN) + + @Override + public float execute(float x) { + return 0.5f + 0.5f * (float) Math.sin(x); + } + }; + waveformFunctions[1] = new WaveForm() {// saw (TEX_SAW) + + @Override + public float execute(float x) { + int n = (int) (x * FastMath.INV_TWO_PI); + x -= n * FastMath.TWO_PI; + if (x < 0.0f) { + x += FastMath.TWO_PI; + } + return x * FastMath.INV_TWO_PI; + } + }; + waveformFunctions[2] = new WaveForm() {// triangle (TEX_TRI) + + @Override + public float execute(float x) { + return 1.0f - 2.0f * FastMath.abs((float) Math.floor(x * FastMath.INV_TWO_PI + 0.5f) - x * FastMath.INV_TWO_PI); + } + }; + } + + /** + * Computes basic wood intensity value at x,y,z. + * @param woodIntData + * @param x X coordinate of the texture pixel + * @param y Y coordinate of the texture pixel + * @param z Z coordinate of the texture pixel + * @return wood intensity at position [x, y, z] + */ + public float woodIntensity(WoodIntensityData woodIntData, float x, float y, float z) { + float result; + + switch(woodIntData.woodType) { + case TEX_BAND: + result = woodIntData.waveformFunction.execute((x + y + z) * 10.0f); + break; + case TEX_RING: + result = woodIntData.waveformFunction.execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f); + break; + case TEX_BANDNOISE: + result = woodIntData.turbul * NoiseGenerator.NoiseFunctions.noise(x, y, z, woodIntData.noisesize, 0, woodIntData.noisebasis, woodIntData.isHard); + result = woodIntData.waveformFunction.execute((x + y + z) * 10.0f + result); + break; + case TEX_RINGNOISE: + result = woodIntData.turbul * NoiseGenerator.NoiseFunctions.noise(x, y, z, woodIntData.noisesize, 0, woodIntData.noisebasis, woodIntData.isHard); + result = woodIntData.waveformFunction.execute((float) Math.sqrt(x * x + y * y + z * z) * 20.0f + result); + break; + default: + result = 0; + } + return result; + } + + /** + * A class that collects the data for wood intensity calculations. + * @author Marcin Roguski (Kaelthas) + */ + private static class WoodIntensityData { + public final WaveForm waveformFunction; + public final int noisebasis; + public final float noisesize; + public final float turbul; + public final int noiseType; + public final int woodType; + public final boolean isHard; + + public WoodIntensityData(Structure tex) { + int waveform = ((Number) tex.getFieldValue("noisebasis2")).intValue();//wave form: TEX_SIN=0, TEX_SAW=1, TEX_TRI=2 + if (waveform > TEX_TRI || waveform < TEX_SIN) { + waveform = 0; // check to be sure noisebasis2 is initialized ahead of time + } + waveformFunction = waveformFunctions[waveform]; + noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); + woodType = ((Number) tex.getFieldValue("stype")).intValue(); + noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); + turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); + noiseType = ((Number) tex.getFieldValue("noisetype")).intValue(); + isHard = noiseType != TEX_NOISESOFT; + } + } + + protected static interface WaveForm { + + float execute(float x); + } } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java index a5717d06b..18cbae303 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java @@ -34,8 +34,6 @@ package com.jme3.scene.plugins.blender.textures; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.awt.image.ColorConvertOp; -import java.io.IOException; -import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; @@ -58,7 +56,6 @@ import com.jme3.scene.plugins.blender.AbstractBlenderHelper; import com.jme3.scene.plugins.blender.DataRepository; import com.jme3.scene.plugins.blender.DataRepository.LoadedFeatureDataType; import com.jme3.scene.plugins.blender.exceptions.BlenderFileException; -import com.jme3.scene.plugins.blender.file.DynamicArray; import com.jme3.scene.plugins.blender.file.FileBlockHeader; import com.jme3.scene.plugins.blender.file.Pointer; import com.jme3.scene.plugins.blender.file.Structure; @@ -78,7 +75,7 @@ import com.jme3.util.BufferUtils; */ public class TextureHelper extends AbstractBlenderHelper { private static final Logger LOGGER = Logger.getLogger(TextureHelper.class.getName()); - + // texture types public static final int TEX_NONE = 0; public static final int TEX_CLOUDS = 1; @@ -95,8 +92,8 @@ public class TextureHelper extends AbstractBlenderHelper { public static final int TEX_VORONOI = 12; public static final int TEX_DISTNOISE = 13; public static final int TEX_POINTDENSITY = 14;//v. 25+ - public static final int TEX_VOXELDATA = 15;//v. 25+ - + public static final int TEX_VOXELDATA = 15;//v. 25+ + // mapto public static final int MAP_COL = 1; public static final int MAP_NORM = 2; @@ -132,27 +129,9 @@ public class TextureHelper extends AbstractBlenderHelper { public static final int MTEX_BLEND_COLOR = 13; public static final int MTEX_NUM_BLENDTYPES = 14; - // variables used in rampBlend method - public static final int MA_RAMP_BLEND = 0; - public static final int MA_RAMP_ADD = 1; - public static final int MA_RAMP_MULT = 2; - public static final int MA_RAMP_SUB = 3; - public static final int MA_RAMP_SCREEN = 4; - public static final int MA_RAMP_DIV = 5; - public static final int MA_RAMP_DIFF = 6; - public static final int MA_RAMP_DARK = 7; - public static final int MA_RAMP_LIGHT = 8; - public static final int MA_RAMP_OVERLAY = 9; - public static final int MA_RAMP_DODGE = 10; - public static final int MA_RAMP_BURN = 11; - public static final int MA_RAMP_HUE = 12; - public static final int MA_RAMP_SAT = 13; - public static final int MA_RAMP_VAL = 14; - public static final int MA_RAMP_COLOR = 15; - protected NoiseGenerator noiseGenerator; private Map textureGenerators = new HashMap(); - + /** * This constructor parses the given blender version and stores the result. * It creates noise generator and texture generators. @@ -196,42 +175,42 @@ public class TextureHelper extends AbstractBlenderHelper { int width = dataRepository.getBlenderKey().getGeneratedTextureWidth(); int height = dataRepository.getBlenderKey().getGeneratedTextureHeight(); int depth = dataRepository.getBlenderKey().getGeneratedTextureDepth(); - + switch (type) { - case TEX_IMAGE:// (it is first because probably this will be most commonly used) - Pointer pImage = (Pointer) tex.getFieldValue("ima"); - if (pImage.isNotNull()){ - Structure image = pImage.fetchData(dataRepository.getInputStream()).get(0); - result = this.getTextureFromImage(image, dataRepository); - } - break; - case TEX_CLOUDS: - case TEX_WOOD: - case TEX_MARBLE: - case TEX_MAGIC: - case TEX_BLEND: - case TEX_STUCCI: - case TEX_NOISE: - case TEX_MUSGRAVE: - case TEX_VORONOI: - case TEX_DISTNOISE: - TextureGenerator textureGenerator = textureGenerators.get(Integer.valueOf(type)); - result = textureGenerator.generate(tex, width, height, depth, dataRepository); - break; - case TEX_NONE:// No texture, do nothing - break; - case TEX_POINTDENSITY: - LOGGER.warning("Point density texture loading currently not supported!"); - break; - case TEX_VOXELDATA: - LOGGER.warning("Voxel data texture loading currently not supported!"); - break; - case TEX_PLUGIN: - case TEX_ENVMAP:// TODO: implement envmap texture - LOGGER.log(Level.WARNING, "Unsupported texture type: {0} for texture: {1}", new Object[]{type, tex.getName()}); - break; - default: - throw new BlenderFileException("Unknown texture type: " + type + " for texture: " + tex.getName()); + case TEX_IMAGE:// (it is first because probably this will be most commonly used) + Pointer pImage = (Pointer) tex.getFieldValue("ima"); + if (pImage.isNotNull()){ + Structure image = pImage.fetchData(dataRepository.getInputStream()).get(0); + result = this.getTextureFromImage(image, dataRepository); + } + break; + case TEX_CLOUDS: + case TEX_WOOD: + case TEX_MARBLE: + case TEX_MAGIC: + case TEX_BLEND: + case TEX_STUCCI: + case TEX_NOISE: + case TEX_MUSGRAVE: + case TEX_VORONOI: + case TEX_DISTNOISE: + TextureGenerator textureGenerator = textureGenerators.get(Integer.valueOf(type)); + result = textureGenerator.generate(tex, width, height, depth, dataRepository); + break; + case TEX_NONE:// No texture, do nothing + break; + case TEX_POINTDENSITY: + LOGGER.warning("Point density texture loading currently not supported!"); + break; + case TEX_VOXELDATA: + LOGGER.warning("Voxel data texture loading currently not supported!"); + break; + case TEX_PLUGIN: + case TEX_ENVMAP:// TODO: implement envmap texture + LOGGER.log(Level.WARNING, "Unsupported texture type: {0} for texture: {1}", new Object[]{type, tex.getName()}); + break; + default: + throw new BlenderFileException("Unknown texture type: " + type + " for texture: " + tex.getName()); } if (result != null) { result.setName(tex.getName()); @@ -282,7 +261,7 @@ public class TextureHelper extends AbstractBlenderHelper { newData.put(dataIndex++, (byte) (resultPixel[0] * 255.0f)); newData.put(dataIndex++, (byte) (resultPixel[1] * 255.0f)); newData.put(dataIndex++, (byte) (resultPixel[2] * 255.0f)); - newData.put(dataIndex++, (byte) 255.0f);//1.0f * 255.0f + newData.put(dataIndex++, (byte) 255.0f);//1.0f * 255.0f } if(texture.getType()==Texture.Type.TwoDimensional) { return new Texture2D(new Image(Format.RGBA8, width, height, newData)); @@ -312,88 +291,87 @@ public class TextureHelper extends AbstractBlenderHelper { * @return texture intensity for the current pixel */ protected float setupMaterialColor(ByteBuffer data, Format imageFormat, boolean neg, float[] materialColor) { - // at least one byte is always taken :) float tin = 0.0f; - byte pixelValue = data.get(); + byte pixelValue = data.get();// at least one byte is always taken :) float firstPixelValue = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; switch (imageFormat) { - case ABGR8: - pixelValue = data.get(); - materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - pixelValue = data.get(); - materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - pixelValue = data.get(); - materialColor[0] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - break; - case BGR8: - materialColor[2] = firstPixelValue; - pixelValue = data.get(); - materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - pixelValue = data.get(); - materialColor[0] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - break; - case RGB8: - materialColor[0] = firstPixelValue; - pixelValue = data.get(); - materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - pixelValue = data.get(); - materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - break; - case RGBA8: - materialColor[0] = firstPixelValue; - pixelValue = data.get(); - materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - pixelValue = data.get(); - materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; - data.get(); // ignore alpha - break; - case Luminance8: - tin = neg ? 1.0f - firstPixelValue : firstPixelValue; - neg = false;//do not negate the materialColor, it must be unchanged - break; - case Luminance8Alpha8: - tin = neg ? 1.0f - firstPixelValue : firstPixelValue; - neg = false;//do not negate the materialColor, it must be unchanged - data.get(); // ignore alpha - break; - case Luminance16: - case Luminance16Alpha16: - case Alpha16: - case Alpha8: - case ARGB4444: - case Depth: - case Depth16: - case Depth24: - case Depth32: - case Depth32F: - case DXT1: - case DXT1A: - case DXT3: - case DXT5: - case Intensity16: - case Intensity8: - case LATC: - case LTC: - case Luminance16F: - case Luminance16FAlpha16F: - case Luminance32F: - case RGB10: - case RGB111110F: - case RGB16: - case RGB16F: - case RGB16F_to_RGB111110F: - case RGB16F_to_RGB9E5: - case RGB32F: - case RGB565: - case RGB5A1: - case RGB9E5: - case RGBA16: - case RGBA16F: - case RGBA32F: - LOGGER.log(Level.WARNING, "Image type not yet supported for blending: {0}", imageFormat); - break; - default: - throw new IllegalStateException("Unknown image format type: " + imageFormat); + case ABGR8: + pixelValue = data.get(); + materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + pixelValue = data.get(); + materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + pixelValue = data.get(); + materialColor[0] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + break; + case BGR8: + materialColor[2] = firstPixelValue; + pixelValue = data.get(); + materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + pixelValue = data.get(); + materialColor[0] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + break; + case RGB8: + materialColor[0] = firstPixelValue; + pixelValue = data.get(); + materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + pixelValue = data.get(); + materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + break; + case RGBA8: + materialColor[0] = firstPixelValue; + pixelValue = data.get(); + materialColor[1] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + pixelValue = data.get(); + materialColor[2] = pixelValue >= 0 ? 1.0f - pixelValue / 255.0f : (~pixelValue + 1) / 255.0f; + data.get(); // ignore alpha + break; + case Luminance8: + tin = neg ? 1.0f - firstPixelValue : firstPixelValue; + neg = false;//do not negate the materialColor, it must be unchanged + break; + case Luminance8Alpha8: + tin = neg ? 1.0f - firstPixelValue : firstPixelValue; + neg = false;//do not negate the materialColor, it must be unchanged + data.get(); // ignore alpha + break; + case Luminance16: + case Luminance16Alpha16: + case Alpha16: + case Alpha8: + case ARGB4444: + case Depth: + case Depth16: + case Depth24: + case Depth32: + case Depth32F: + case DXT1: + case DXT1A: + case DXT3: + case DXT5: + case Intensity16: + case Intensity8: + case LATC: + case LTC: + case Luminance16F: + case Luminance16FAlpha16F: + case Luminance32F: + case RGB10: + case RGB111110F: + case RGB16: + case RGB16F: + case RGB16F_to_RGB111110F: + case RGB16F_to_RGB9E5: + case RGB32F: + case RGB565: + case RGB5A1: + case RGB9E5: + case RGBA16: + case RGBA16F: + case RGBA32F: + LOGGER.log(Level.WARNING, "Image type not yet supported for blending: {0}", imageFormat); + break; + default: + throw new IllegalStateException("Unknown image format type: " + imageFormat); } if (neg) { materialColor[0] = 1.0f - materialColor[0]; @@ -422,64 +400,59 @@ public class TextureHelper extends AbstractBlenderHelper { * the data repository */ protected void blendPixel(float[] result, float[] materialColor, float[] color, float textureIntensity, float textureFactor, int blendtype, DataRepository dataRepository) { - float facm, col; - + float oneMinusFactor, col; + textureIntensity *= textureFactor; + switch (blendtype) { case MTEX_BLEND: - textureIntensity *= textureFactor; - facm = 1.0f - textureIntensity; - result[0] = textureIntensity * color[0] + facm * materialColor[0]; - result[1] = textureIntensity * color[1] + facm * materialColor[1]; - result[2] = textureIntensity * color[2] + facm * materialColor[2]; + oneMinusFactor = 1.0f - textureIntensity; + result[0] = textureIntensity * color[0] + oneMinusFactor * materialColor[0]; + result[1] = textureIntensity * color[1] + oneMinusFactor * materialColor[1]; + result[2] = textureIntensity * color[2] + oneMinusFactor * materialColor[2]; break; case MTEX_MUL: - textureIntensity *= textureFactor; - facm = 1.0f - textureFactor; - result[0] = (facm + textureIntensity * materialColor[0]) * color[0]; - result[1] = (facm + textureIntensity * materialColor[1]) * color[1]; - result[2] = (facm + textureIntensity * materialColor[2]) * color[2]; + oneMinusFactor = 1.0f - textureFactor; + result[0] = (oneMinusFactor + textureIntensity * materialColor[0]) * color[0]; + result[1] = (oneMinusFactor + textureIntensity * materialColor[1]) * color[1]; + result[2] = (oneMinusFactor + textureIntensity * materialColor[2]) * color[2]; break; case MTEX_DIV: - textureIntensity *= textureFactor; - facm = 1.0f - textureIntensity; + oneMinusFactor = 1.0f - textureIntensity; if (color[0] != 0.0) { - result[0] = (facm * materialColor[0] + textureIntensity * materialColor[0] / color[0]) * 0.5f; + result[0] = (oneMinusFactor * materialColor[0] + textureIntensity * materialColor[0] / color[0]) * 0.5f; } if (color[1] != 0.0) { - result[1] = (facm * materialColor[1] + textureIntensity * materialColor[1] / color[1]) * 0.5f; + result[1] = (oneMinusFactor * materialColor[1] + textureIntensity * materialColor[1] / color[1]) * 0.5f; } if (color[2] != 0.0) { - result[2] = (facm * materialColor[2] + textureIntensity * materialColor[2] / color[2]) * 0.5f; + result[2] = (oneMinusFactor * materialColor[2] + textureIntensity * materialColor[2] / color[2]) * 0.5f; } break; case MTEX_SCREEN: - textureIntensity *= textureFactor; - facm = 1.0f - textureFactor; - result[0] = 1.0f - (facm + textureIntensity * (1.0f - materialColor[0])) * (1.0f - color[0]); - result[1] = 1.0f - (facm + textureIntensity * (1.0f - materialColor[1])) * (1.0f - color[1]); - result[2] = 1.0f - (facm + textureIntensity * (1.0f - materialColor[2])) * (1.0f - color[2]); + oneMinusFactor = 1.0f - textureFactor; + result[0] = 1.0f - (oneMinusFactor + textureIntensity * (1.0f - materialColor[0])) * (1.0f - color[0]); + result[1] = 1.0f - (oneMinusFactor + textureIntensity * (1.0f - materialColor[1])) * (1.0f - color[1]); + result[2] = 1.0f - (oneMinusFactor + textureIntensity * (1.0f - materialColor[2])) * (1.0f - color[2]); break; case MTEX_OVERLAY: - textureIntensity *= textureFactor; - facm = 1.0f - textureFactor; + oneMinusFactor = 1.0f - textureFactor; if (materialColor[0] < 0.5f) { - result[0] = color[0] * (facm + 2.0f * textureIntensity * materialColor[0]); + result[0] = color[0] * (oneMinusFactor + 2.0f * textureIntensity * materialColor[0]); } else { - result[0] = 1.0f - (facm + 2.0f * textureIntensity * (1.0f - materialColor[0])) * (1.0f - color[0]); + result[0] = 1.0f - (oneMinusFactor + 2.0f * textureIntensity * (1.0f - materialColor[0])) * (1.0f - color[0]); } if (materialColor[1] < 0.5f) { - result[1] = color[1] * (facm + 2.0f * textureIntensity * materialColor[1]); + result[1] = color[1] * (oneMinusFactor + 2.0f * textureIntensity * materialColor[1]); } else { - result[1] = 1.0f - (facm + 2.0f * textureIntensity * (1.0f - materialColor[1])) * (1.0f - color[1]); + result[1] = 1.0f - (oneMinusFactor + 2.0f * textureIntensity * (1.0f - materialColor[1])) * (1.0f - color[1]); } if (materialColor[2] < 0.5f) { - result[2] = color[2] * (facm + 2.0f * textureIntensity * materialColor[2]); + result[2] = color[2] * (oneMinusFactor + 2.0f * textureIntensity * materialColor[2]); } else { - result[2] = 1.0f - (facm + 2.0f * textureIntensity * (1.0f - materialColor[2])) * (1.0f - color[2]); + result[2] = 1.0f - (oneMinusFactor + 2.0f * textureIntensity * (1.0f - materialColor[2])) * (1.0f - color[2]); } break; case MTEX_SUB: - textureIntensity *= textureFactor; result[0] = materialColor[0] - textureIntensity * color[0]; result[1] = materialColor[1] - textureIntensity * color[1]; result[2] = materialColor[2] - textureIntensity * color[2]; @@ -488,20 +461,17 @@ public class TextureHelper extends AbstractBlenderHelper { result[2] = FastMath.clamp(result[2], 0.0f, 1.0f); break; case MTEX_ADD: - textureIntensity *= textureFactor; result[0] = (textureIntensity * color[0] + materialColor[0]) * 0.5f; result[1] = (textureIntensity * color[1] + materialColor[1]) * 0.5f; result[2] = (textureIntensity * color[2] + materialColor[2]) * 0.5f; break; case MTEX_DIFF: - textureIntensity *= textureFactor; - facm = 1.0f - textureIntensity; - result[0] = facm * color[0] + textureIntensity * Math.abs(materialColor[0] - color[0]); - result[1] = facm * color[1] + textureIntensity * Math.abs(materialColor[1] - color[1]); - result[2] = facm * color[2] + textureIntensity * Math.abs(materialColor[2] - color[2]); + oneMinusFactor = 1.0f - textureIntensity; + result[0] = oneMinusFactor * materialColor[0] + textureIntensity * Math.abs(materialColor[0] - color[0]); + result[1] = oneMinusFactor * materialColor[1] + textureIntensity * Math.abs(materialColor[1] - color[1]); + result[2] = oneMinusFactor * materialColor[2] + textureIntensity * Math.abs(materialColor[2] - color[2]); break; case MTEX_DARK: - textureIntensity *= textureFactor; col = textureIntensity * color[0]; result[0] = col < materialColor[0] ? col : materialColor[0]; col = textureIntensity * color[1]; @@ -510,7 +480,6 @@ public class TextureHelper extends AbstractBlenderHelper { result[2] = col < materialColor[2] ? col : materialColor[2]; break; case MTEX_LIGHT: - textureIntensity *= textureFactor; col = textureIntensity * color[0]; result[0] = col > materialColor[0] ? col : materialColor[0]; col = textureIntensity * color[1]; @@ -519,24 +488,11 @@ public class TextureHelper extends AbstractBlenderHelper { result[2] = col > materialColor[2] ? col : materialColor[2]; break; case MTEX_BLEND_HUE: - textureIntensity *= textureFactor; - System.arraycopy(materialColor, 0, result, 0, 3); - this.rampBlend(MA_RAMP_HUE, result, textureIntensity, color, dataRepository); - break; case MTEX_BLEND_SAT: - textureIntensity *= textureFactor; - System.arraycopy(materialColor, 0, result, 0, 3); - this.rampBlend(MA_RAMP_SAT, result, textureIntensity, color, dataRepository); - break; case MTEX_BLEND_VAL: - textureIntensity *= textureFactor; - System.arraycopy(materialColor, 0, result, 0, 3); - this.rampBlend(MA_RAMP_VAL, result, textureIntensity, color, dataRepository); - break; case MTEX_BLEND_COLOR: - textureIntensity *= textureFactor; System.arraycopy(materialColor, 0, result, 0, 3); - this.rampBlend(MA_RAMP_COLOR, result, textureIntensity, color, dataRepository); + this.rampBlend(blendtype, result, textureIntensity, color, dataRepository); break; default: throw new IllegalStateException("Unknown blend type: " + blendtype); @@ -544,10 +500,10 @@ public class TextureHelper extends AbstractBlenderHelper { } /** - * The method that performs the ramp blending (whatever it is :P - copied from blender sources). + * The method that performs the ramp blending. * * @param type - * the ramp type + * the blend type * @param rgb * the rgb value where the result is stored * @param fac @@ -558,239 +514,60 @@ public class TextureHelper extends AbstractBlenderHelper { * the data repository */ protected void rampBlend(int type, float[] rgb, float fac, float[] col, DataRepository dataRepository) { - float tmp, facm = 1.0f - fac; + float oneMinusFactor = 1.0f - fac; MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class); - switch (type) { - case MA_RAMP_HUE: - if (rgb.length == 3) { + if (rgb.length >= 3) { + switch (type) { + case MTEX_BLEND_HUE: { float[] colorTransformResult = new float[3]; materialHelper.rgbToHsv(col[0], col[1], col[2], colorTransformResult); if (colorTransformResult[1] != 0.0f) { float colH = colorTransformResult[0]; materialHelper.rgbToHsv(rgb[0], rgb[1], rgb[2], colorTransformResult); materialHelper.hsvToRgb(colH, colorTransformResult[1], colorTransformResult[2], colorTransformResult); - rgb[0] = facm * rgb[0] + fac * colorTransformResult[0]; - rgb[1] = facm * rgb[1] + fac * colorTransformResult[1]; - rgb[2] = facm * rgb[2] + fac * colorTransformResult[2]; + rgb[0] = oneMinusFactor * rgb[0] + fac * colorTransformResult[0]; + rgb[1] = oneMinusFactor * rgb[1] + fac * colorTransformResult[1]; + rgb[2] = oneMinusFactor * rgb[2] + fac * colorTransformResult[2]; } + break; } - break; - case MA_RAMP_SAT: - if (rgb.length == 3) { + case MTEX_BLEND_SAT: { float[] colorTransformResult = new float[3]; materialHelper.rgbToHsv(rgb[0], rgb[1], rgb[2], colorTransformResult); - float rH = colorTransformResult[0]; - float rS = colorTransformResult[1]; - float rV = colorTransformResult[2]; - if (rS != 0) { + float h = colorTransformResult[0]; + float s = colorTransformResult[1]; + float v = colorTransformResult[2]; + if (s != 0.0f) { materialHelper.rgbToHsv(col[0], col[1], col[2], colorTransformResult); - materialHelper.hsvToRgb(rH, (facm * rS + fac * colorTransformResult[1]), rV, rgb); + materialHelper.hsvToRgb(h, (oneMinusFactor * s + fac * colorTransformResult[1]), v, rgb); } + break; } - break; - case MA_RAMP_VAL: - if (rgb.length == 3) { + case MTEX_BLEND_VAL: { float[] rgbToHsv = new float[3]; float[] colToHsv = new float[3]; materialHelper.rgbToHsv(rgb[0], rgb[1], rgb[2], rgbToHsv); materialHelper.rgbToHsv(col[0], col[1], col[2], colToHsv); - materialHelper.hsvToRgb(rgbToHsv[0], rgbToHsv[1], (facm * rgbToHsv[2] + fac * colToHsv[2]), rgb); + materialHelper.hsvToRgb(rgbToHsv[0], rgbToHsv[1], (oneMinusFactor * rgbToHsv[2] + fac * colToHsv[2]), rgb); + break; } - break; - case MA_RAMP_COLOR: - if (rgb.length == 3) { + case MTEX_BLEND_COLOR: { float[] rgbToHsv = new float[3]; float[] colToHsv = new float[3]; materialHelper.rgbToHsv(col[0], col[1], col[2], colToHsv); if (colToHsv[2] != 0) { materialHelper.rgbToHsv(rgb[0], rgb[1], rgb[2], rgbToHsv); materialHelper.hsvToRgb(colToHsv[0], colToHsv[1], rgbToHsv[2], rgbToHsv); - rgb[0] = facm * rgb[0] + fac * rgbToHsv[0]; - rgb[1] = facm * rgb[1] + fac * rgbToHsv[1]; - rgb[2] = facm * rgb[2] + fac * rgbToHsv[2]; + rgb[0] = oneMinusFactor * rgb[0] + fac * rgbToHsv[0]; + rgb[1] = oneMinusFactor * rgb[1] + fac * rgbToHsv[1]; + rgb[2] = oneMinusFactor * rgb[2] + fac * rgbToHsv[2]; } + break; } - break; - case MA_RAMP_BLEND: - rgb[0] = facm * rgb[0] + fac * col[0]; - if (rgb.length == 3) { - rgb[1] = facm * rgb[1] + fac * col[1]; - rgb[2] = facm * rgb[2] + fac * col[2]; - } - break; - case MA_RAMP_ADD: - rgb[0] += fac * col[0]; - if (rgb.length == 3) { - rgb[1] += fac * col[1]; - rgb[2] += fac * col[2]; - } - break; - case MA_RAMP_MULT: - rgb[0] *= facm + fac * col[0]; - if (rgb.length == 3) { - rgb[1] *= facm + fac * col[1]; - rgb[2] *= facm + fac * col[2]; - } - break; - case MA_RAMP_SCREEN: - rgb[0] = 1.0f - (facm + fac * (1.0f - col[0])) * (1.0f - rgb[0]); - if (rgb.length == 3) { - rgb[1] = 1.0f - (facm + fac * (1.0f - col[1])) * (1.0f - rgb[1]); - rgb[2] = 1.0f - (facm + fac * (1.0f - col[2])) * (1.0f - rgb[2]); - } - break; - case MA_RAMP_OVERLAY: - if (rgb[0] < 0.5f) { - rgb[0] *= facm + 2.0f * fac * col[0]; - } else { - rgb[0] = 1.0f - (facm + 2.0f * fac * (1.0f - col[0])) * (1.0f - rgb[0]); - } - if (rgb.length == 3) { - if (rgb[1] < 0.5f) { - rgb[1] *= facm + 2.0f * fac * col[1]; - } else { - rgb[1] = 1.0f - (facm + 2.0f * fac * (1.0f - col[1])) * (1.0f - rgb[1]); - } - if (rgb[2] < 0.5f) { - rgb[2] *= facm + 2.0f * fac * col[2]; - } else { - rgb[2] = 1.0f - (facm + 2.0f * fac * (1.0f - col[2])) * (1.0f - rgb[2]); - } - } - break; - case MA_RAMP_SUB: - rgb[0] -= fac * col[0]; - if (rgb.length == 3) { - rgb[1] -= fac * col[1]; - rgb[2] -= fac * col[2]; - } - break; - case MA_RAMP_DIV: - if (col[0] != 0.0) { - rgb[0] = facm * rgb[0] + fac * rgb[0] / col[0]; - } - if (rgb.length == 3) { - if (col[1] != 0.0) { - rgb[1] = facm * rgb[1] + fac * rgb[1] / col[1]; - } - if (col[2] != 0.0) { - rgb[2] = facm * rgb[2] + fac * rgb[2] / col[2]; - } - } - break; - case MA_RAMP_DIFF: - rgb[0] = facm * rgb[0] + fac * Math.abs(rgb[0] - col[0]); - if (rgb.length == 3) { - rgb[1] = facm * rgb[1] + fac * Math.abs(rgb[1] - col[1]); - rgb[2] = facm * rgb[2] + fac * Math.abs(rgb[2] - col[2]); - } - break; - case MA_RAMP_DARK: - tmp = fac * col[0]; - if (tmp < rgb[0]) { - rgb[0] = tmp; - } - if (rgb.length == 3) { - tmp = fac * col[1]; - if (tmp < rgb[1]) { - rgb[1] = tmp; - } - tmp = fac * col[2]; - if (tmp < rgb[2]) { - rgb[2] = tmp; - } - } - break; - case MA_RAMP_LIGHT: - tmp = fac * col[0]; - if (tmp > rgb[0]) { - rgb[0] = tmp; - } - if (rgb.length == 3) { - tmp = fac * col[1]; - if (tmp > rgb[1]) { - rgb[1] = tmp; - } - tmp = fac * col[2]; - if (tmp > rgb[2]) { - rgb[2] = tmp; - } - } - break; - case MA_RAMP_DODGE: - if (rgb[0] != 0.0) { - tmp = 1.0f - fac * col[0]; - if (tmp <= 0.0) { - rgb[0] = 1.0f; - } else if ((tmp = rgb[0] / tmp) > 1.0) { - rgb[0] = 1.0f; - } else { - rgb[0] = tmp; - } - } - if (rgb.length == 3) { - if (rgb[1] != 0.0) { - tmp = 1.0f - fac * col[1]; - if (tmp <= 0.0) { - rgb[1] = 1.0f; - } else if ((tmp = rgb[1] / tmp) > 1.0) { - rgb[1] = 1.0f; - } else { - rgb[1] = tmp; - } - } - if (rgb[2] != 0.0) { - tmp = 1.0f - fac * col[2]; - if (tmp <= 0.0) { - rgb[2] = 1.0f; - } else if ((tmp = rgb[2] / tmp) > 1.0) { - rgb[2] = 1.0f; - } else { - rgb[2] = tmp; - } - } - - } - break; - case MA_RAMP_BURN: - tmp = facm + fac * col[0]; - if (tmp <= 0.0) { - rgb[0] = 0.0f; - } else if ((tmp = 1.0f - (1.0f - rgb[0]) / tmp) < 0.0) { - rgb[0] = 0.0f; - } else if (tmp > 1.0) { - rgb[0] = 1.0f; - } else { - rgb[0] = tmp; - } - - if (rgb.length == 3) { - tmp = facm + fac * col[1]; - if (tmp <= 0.0) { - rgb[1] = 0.0f; - } else if ((tmp = 1.0f - (1.0f - rgb[1]) / tmp) < 0.0) { - rgb[1] = 0.0f; - } else if (tmp > 1.0) { - rgb[1] = 1.0f; - } else { - rgb[1] = tmp; - } - - tmp = facm + fac * col[2]; - if (tmp <= 0.0) { - rgb[2] = 0.0f; - } else if ((tmp = 1.0f - (1.0f - rgb[2]) / tmp) < 0.0) { - rgb[2] = 0.0f; - } else if (tmp > 1.0) { - rgb[2] = 1.0f; - } else { - rgb[2] = tmp; - } - } - break; - default: - throw new IllegalStateException("Unknown ramp type: " + type); + default: + throw new IllegalStateException("Unknown ramp type: " + type); + } } } @@ -863,7 +640,7 @@ public class TextureHelper extends AbstractBlenderHelper { } return image.getRGB(x, y) & 0xff; } - + /** * This method transforms given vector's coordinates into ARGB color (A is always = 255). * @param x X factor of the vector @@ -871,13 +648,13 @@ public class TextureHelper extends AbstractBlenderHelper { * @param z Z factor of the vector * @return color representation of the given vector */ - protected int vectorToColor(float x, float y, float z) { - int r = Math.round(255 * (x + 1f) / 2f); - int g = Math.round(255 * (y + 1f) / 2f); - int b = Math.round(255 * (z + 1f) / 2f); - return (255 << 24) + (r << 16) + (g << 8) + b; - } - + protected int vectorToColor(float x, float y, float z) { + int r = Math.round(255 * (x + 1f) / 2f); + int g = Math.round(255 * (y + 1f) / 2f); + int b = Math.round(255 * (z + 1f) / 2f); + return (255 << 24) + (r << 16) + (g << 8) + b; + } + /** * This class returns a texture read from the file or from packed blender data. * @@ -933,7 +710,7 @@ public class TextureHelper extends AbstractBlenderHelper { AssetManager assetManager = dataRepository.getAssetManager(); name = name.replaceAll("\\\\", "\\/"); Texture result = null; - + List assetNames = new ArrayList(); if (name.startsWith("//")) { String relativePath = name.substring(1); @@ -946,14 +723,14 @@ public class TextureHelper extends AbstractBlenderHelper { String[] paths = name.split("\\/"); StringBuilder sb = new StringBuilder(paths[paths.length-1]);//the asset name assetNames.add(paths[paths.length-1]); - + for(int i=paths.length-2;i>=0;--i) { sb.insert(0, '/'); sb.insert(0, paths[i]); assetNames.add(sb.toString()); } } - + //now try to locate the asset for(String assetName : assetNames) { try { @@ -966,107 +743,8 @@ public class TextureHelper extends AbstractBlenderHelper { return result; } - /** - * This method closes the given stream. - * - * @param is - * the input stream that is to be closed - */ - protected void closeStream(InputStream is) { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); - } - } - } - @Override public boolean shouldBeLoaded(Structure structure, DataRepository dataRepository) { return (dataRepository.getBlenderKey().getFeaturesToLoad() & FeaturesToLoad.TEXTURES) != 0; } - - /** - * Image types that can be loaded. AWT: png, jpg, jped or bmp TGA: tga DDS: DirectX image files - * - * @author Marcin Roguski (Kaelthas) - */ - public static enum ImageType { - AWT, TGA, DDS; - } - - /** - * The result pixel of generated texture computations; - * - * @author Marcin Roguski (Kaelthas) - */ - protected static class TexResult implements Cloneable { - public float tin, tr, tg, tb, ta; - public int talpha; - public float[] nor; - - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } - } - - /** - * A class constaining the colorband data. - * - * @author Marcin Roguski (Kaelthas) - */ - protected static class ColorBand { - public int flag, tot, cur, ipotype; - public CBData[] data = new CBData[32]; - - /** - * Constructor. Loads the data from the given structure. - * - * @param cbdataStructure - * the colorband structure - */ - @SuppressWarnings("unchecked") - public ColorBand(Structure colorbandStructure) { - this.flag = ((Number) colorbandStructure.getFieldValue("flag")).intValue(); - this.tot = ((Number) colorbandStructure.getFieldValue("tot")).intValue(); - this.cur = ((Number) colorbandStructure.getFieldValue("cur")).intValue(); - this.ipotype = ((Number) colorbandStructure.getFieldValue("ipotype")).intValue(); - DynamicArray data = (DynamicArray) colorbandStructure.getFieldValue("data"); - for (int i = 0; i < data.getTotalSize(); ++i) { - this.data[i] = new CBData(data.get(i)); - } - } - } - - /** - * Class to store the single colorband unit data. - * - * @author Marcin Roguski (Kaelthas) - */ - protected static class CBData implements Cloneable { - public float r, g, b, a, pos; - public int cur; - - /** - * Constructor. Loads the data from the given structure. - * - * @param cbdataStructure - * the structure containing the CBData object - */ - public CBData(Structure cbdataStructure) { - this.r = ((Number) cbdataStructure.getFieldValue("r")).floatValue(); - this.g = ((Number) cbdataStructure.getFieldValue("g")).floatValue(); - this.b = ((Number) cbdataStructure.getFieldValue("b")).floatValue(); - this.a = ((Number) cbdataStructure.getFieldValue("a")).floatValue(); - this.pos = ((Number) cbdataStructure.getFieldValue("pos")).floatValue(); - this.cur = ((Number) cbdataStructure.getFieldValue("cur")).intValue(); - } - - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } - } } \ No newline at end of file diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.java index a5f001009..a286a8d33 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.java @@ -320,7 +320,54 @@ public class UVCoordinatesGenerator { } public void merge(BoundingTube boundingTube) { - // TODO: implement + //get tubes (tube1.radius >= tube2.radius) + BoundingTube tube1, tube2; + if(this.radius>=boundingTube.radius) { + tube1 = this; + tube2 = boundingTube; + } else { + tube1 = boundingTube; + tube2 = this; + } + float r1 = tube1.radius; + float r2 = tube2.radius; + + //get the distance between tubes projected on XY plane + Vector3f distance = boundingTube.center.subtract(this.center); + distance.z = 0; + float d = distance.length(); + + //calculate union depending on tubes location + if(d>=r1+r2) {//tube2 is outside or touches tube1 + + } else if(dr1-r2) {//tube2 crosses tube1 + + } else {//tube2 is inside tube1 + + } + + if(d >= this.radius + boundingTube.radius || + (d < this.radius + boundingTube.radius && d > this.radius - boundingTube.radius)) { + + } + + float centerZ = distance.z; + + float maxz = this.center.z + height*0.5f; + float minz = this.center.z - height*0.5f; + + distance.z = this.center.z = 0; + + Vector3f distanceNormal = distance.normalize(); + Vector3f start = this.center.subtract(distanceNormal.multLocal(this.radius)); + distanceNormal.normalizeLocal(); + Vector3f stop = start.add(distance).addLocal(distanceNormal.multLocal(this.radius+boundingTube.radius)); + this.center = start.add(stop.subtractLocal(start)).multLocal(0.5f); + this.center.z = centerZ; + this.radius = this.center.subtract(start).length(); + maxz = Math.max(maxz, boundingTube.center.z + boundingTube.height*0.5f); + minz = Math.min(minz, boundingTube.center.z - boundingTube.height*0.5f); + this.height = maxz - minz; } public float getRadius() {