159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.scene.plugins.blender.textures; 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.FastMath; 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.BlenderContext; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.file.Structure; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.plugins.blender.textures.NoiseGenerator.NoiseMath; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Image; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Image.Format; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Texture; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Texture3D; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.BufferUtils; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.ArrayList; 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This class generates the 'voronoi' texture. 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Marcin Roguski (Kaelthas) 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TextureGeneratorVoronoi extends TextureGenerator { 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Constructor stores the given noise generator. 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param noiseGenerator 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the noise generator 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public TextureGeneratorVoronoi(NoiseGenerator noiseGenerator) { 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(noiseGenerator); 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) { 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float voronoiWeight1 = ((Number) tex.getFieldValue("vn_w1")).floatValue(); 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float voronoiWeight2 = ((Number) tex.getFieldValue("vn_w2")).floatValue(); 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float voronoiWeight3 = ((Number) tex.getFieldValue("vn_w3")).floatValue(); 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float voronoiWeight4 = ((Number) tex.getFieldValue("vn_w4")).floatValue(); 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float mexp = ((Number) tex.getFieldValue("vn_mexp")).floatValue(); 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int distm = ((Number) tex.getFieldValue("vn_distm")).intValue(); 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int voronoiColorType = ((Number) tex.getFieldValue("vn_coltype")).intValue(); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta TexturePixel texres = new TexturePixel(); 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] texvec = new float[] { 0, 0, 0 }; 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[][] colorBand = this.computeColorband(tex, blenderContext); 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Format format = voronoiColorType != 0 || colorBand != null ? Format.RGBA8 : Format.Luminance8; 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int bytesPerPixel = voronoiColorType != 0 || colorBand != null ? 4 : 1; 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] da = new float[4], pa = new float[12]; 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] hashPoint = voronoiColorType != 0 ? new float[3] : null; 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] voronoiWeights = new float[] {FastMath.abs(voronoiWeight1), FastMath.abs(voronoiWeight2), 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta FastMath.abs(voronoiWeight3), FastMath.abs(voronoiWeight4)}; 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float weight; 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float sc = voronoiWeights[0] + voronoiWeights[1] + voronoiWeights[2] + voronoiWeights[3]; 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (sc != 0.0f) { 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sc = outscale / sc; 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta byte[] data = new byte[width * height * depth * bytesPerPixel]; 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = -halfW; i < halfW; ++i) { 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texvec[0] = wDelta * i / noisesize; 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int j = -halfH; j < halfH; ++j) { 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texvec[1] = hDelta * j / noisesize; 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int k = -halfD; k < halfD; ++k) { 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texvec[2] = dDelta * k; 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta NoiseGenerator.NoiseFunctions.voronoi(texvec[0], texvec[1], texvec[2], da, pa, mexp, distm); 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.intensity = sc * FastMath.abs(voronoiWeight1 * da[0] + voronoiWeight2 * da[1] + voronoiWeight3 * da[2] + voronoiWeight4 * da[3]); 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if(texres.intensity>1.0f) { 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.intensity = 1.0f; 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if(texres.intensity<0.0f) { 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.intensity = 0.0f; 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (colorBand != null) {//colorband ALWAYS goes first and covers the color (if set) 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int colorbandIndex = (int) (texres.intensity * 1000.0f); 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.red = colorBand[colorbandIndex][0]; 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.green = colorBand[colorbandIndex][1]; 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.blue = colorBand[colorbandIndex][2]; 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.alpha = colorBand[colorbandIndex][3]; 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (voronoiColorType != 0) { 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.red = texres.green = texres.blue = 0.0f; 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.alpha = 1.0f; 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for(int m=0; m<12; m+=3) { 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta weight = voronoiWeights[m/3]; 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.cellNoiseV(pa[m], pa[m + 1], pa[m + 2], hashPoint); 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.red += weight * hashPoint[0]; 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.green += weight * hashPoint[1]; 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.blue += weight * hashPoint[2]; 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (voronoiColorType >= 2) { 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float t1 = (da[1] - da[0]) * 10.0f; 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (t1 > 1.0f) { 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta t1 = 1.0f; 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (voronoiColorType == 3) { 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta t1 *= texres.intensity; 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta t1 *= sc; 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.red *= t1; 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.green *= t1; 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.blue *= t1; 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.red *= sc; 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.green *= sc; 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta texres.blue *= sc; 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (voronoiColorType != 0 || colorBand != null) { 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.applyBrightnessAndContrast(bacd, texres); 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data[index++] = (byte) (texres.red * 255.0f); 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data[index++] = (byte) (texres.green * 255.0f); 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data[index++] = (byte) (texres.blue * 255.0f); 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data[index++] = (byte) (texres.alpha * 255.0f); 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else { 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data[index++] = (byte) (texres.intensity * 255.0f); 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1); 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta dataArray.add(BufferUtils.createByteBuffer(data)); 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return new Texture3D(new Image(format, width, height, depth, dataArray)); 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Returns a vector/point/color in ca, using point hasharray directly 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void cellNoiseV(float x, float y, float z, float[] hashPoint) { 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int xi = (int) Math.floor(x); 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int yi = (int) Math.floor(y); 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta int zi = (int) Math.floor(z); 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta NoiseMath.hash(xi, yi, zi, hashPoint); 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 172