1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.jme3.scene.plugins.blender.textures; 33 34 import com.jme3.math.FastMath; 35 import com.jme3.scene.plugins.blender.BlenderContext; 36 import com.jme3.scene.plugins.blender.file.Structure; 37 import com.jme3.scene.plugins.blender.textures.NoiseGenerator.NoiseMath; 38 import com.jme3.texture.Image; 39 import com.jme3.texture.Image.Format; 40 import com.jme3.texture.Texture; 41 import com.jme3.texture.Texture3D; 42 import com.jme3.util.BufferUtils; 43 import java.nio.ByteBuffer; 44 import java.util.ArrayList; 45 46 /** 47 * This class generates the 'voronoi' texture. 48 * @author Marcin Roguski (Kaelthas) 49 */ 50 public class TextureGeneratorVoronoi extends TextureGenerator { 51 52 /** 53 * Constructor stores the given noise generator. 54 * @param noiseGenerator 55 * the noise generator 56 */ TextureGeneratorVoronoi(NoiseGenerator noiseGenerator)57 public TextureGeneratorVoronoi(NoiseGenerator noiseGenerator) { 58 super(noiseGenerator); 59 } 60 61 @Override generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext)62 protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) { 63 float voronoiWeight1 = ((Number) tex.getFieldValue("vn_w1")).floatValue(); 64 float voronoiWeight2 = ((Number) tex.getFieldValue("vn_w2")).floatValue(); 65 float voronoiWeight3 = ((Number) tex.getFieldValue("vn_w3")).floatValue(); 66 float voronoiWeight4 = ((Number) tex.getFieldValue("vn_w4")).floatValue(); 67 float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); 68 float outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); 69 float mexp = ((Number) tex.getFieldValue("vn_mexp")).floatValue(); 70 int distm = ((Number) tex.getFieldValue("vn_distm")).intValue(); 71 int voronoiColorType = ((Number) tex.getFieldValue("vn_coltype")).intValue(); 72 73 TexturePixel texres = new TexturePixel(); 74 float[] texvec = new float[] { 0, 0, 0 }; 75 int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; 76 float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; 77 78 float[][] colorBand = this.computeColorband(tex, blenderContext); 79 Format format = voronoiColorType != 0 || colorBand != null ? Format.RGBA8 : Format.Luminance8; 80 int bytesPerPixel = voronoiColorType != 0 || colorBand != null ? 4 : 1; 81 BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); 82 83 float[] da = new float[4], pa = new float[12]; 84 float[] hashPoint = voronoiColorType != 0 ? new float[3] : null; 85 float[] voronoiWeights = new float[] {FastMath.abs(voronoiWeight1), FastMath.abs(voronoiWeight2), 86 FastMath.abs(voronoiWeight3), FastMath.abs(voronoiWeight4)}; 87 float weight; 88 float sc = voronoiWeights[0] + voronoiWeights[1] + voronoiWeights[2] + voronoiWeights[3]; 89 if (sc != 0.0f) { 90 sc = outscale / sc; 91 } 92 93 byte[] data = new byte[width * height * depth * bytesPerPixel]; 94 for (int i = -halfW; i < halfW; ++i) { 95 texvec[0] = wDelta * i / noisesize; 96 for (int j = -halfH; j < halfH; ++j) { 97 texvec[1] = hDelta * j / noisesize; 98 for (int k = -halfD; k < halfD; ++k) { 99 texvec[2] = dDelta * k; 100 NoiseGenerator.NoiseFunctions.voronoi(texvec[0], texvec[1], texvec[2], da, pa, mexp, distm); 101 texres.intensity = sc * FastMath.abs(voronoiWeight1 * da[0] + voronoiWeight2 * da[1] + voronoiWeight3 * da[2] + voronoiWeight4 * da[3]); 102 if(texres.intensity>1.0f) { 103 texres.intensity = 1.0f; 104 } else if(texres.intensity<0.0f) { 105 texres.intensity = 0.0f; 106 } 107 108 if (colorBand != null) {//colorband ALWAYS goes first and covers the color (if set) 109 int colorbandIndex = (int) (texres.intensity * 1000.0f); 110 texres.red = colorBand[colorbandIndex][0]; 111 texres.green = colorBand[colorbandIndex][1]; 112 texres.blue = colorBand[colorbandIndex][2]; 113 texres.alpha = colorBand[colorbandIndex][3]; 114 } else if (voronoiColorType != 0) { 115 texres.red = texres.green = texres.blue = 0.0f; 116 texres.alpha = 1.0f; 117 for(int m=0; m<12; m+=3) { 118 weight = voronoiWeights[m/3]; 119 this.cellNoiseV(pa[m], pa[m + 1], pa[m + 2], hashPoint); 120 texres.red += weight * hashPoint[0]; 121 texres.green += weight * hashPoint[1]; 122 texres.blue += weight * hashPoint[2]; 123 } 124 if (voronoiColorType >= 2) { 125 float t1 = (da[1] - da[0]) * 10.0f; 126 if (t1 > 1.0f) { 127 t1 = 1.0f; 128 } 129 if (voronoiColorType == 3) { 130 t1 *= texres.intensity; 131 } else { 132 t1 *= sc; 133 } 134 texres.red *= t1; 135 texres.green *= t1; 136 texres.blue *= t1; 137 } else { 138 texres.red *= sc; 139 texres.green *= sc; 140 texres.blue *= sc; 141 } 142 } 143 144 if (voronoiColorType != 0 || colorBand != null) { 145 this.applyBrightnessAndContrast(bacd, texres); 146 data[index++] = (byte) (texres.red * 255.0f); 147 data[index++] = (byte) (texres.green * 255.0f); 148 data[index++] = (byte) (texres.blue * 255.0f); 149 data[index++] = (byte) (texres.alpha * 255.0f); 150 } else { 151 this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); 152 data[index++] = (byte) (texres.intensity * 255.0f); 153 } 154 } 155 } 156 } 157 ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1); 158 dataArray.add(BufferUtils.createByteBuffer(data)); 159 return new Texture3D(new Image(format, width, height, depth, dataArray)); 160 } 161 162 /** 163 * Returns a vector/point/color in ca, using point hasharray directly 164 */ cellNoiseV(float x, float y, float z, float[] hashPoint)165 private void cellNoiseV(float x, float y, float z, float[] hashPoint) { 166 int xi = (int) Math.floor(x); 167 int yi = (int) Math.floor(y); 168 int zi = (int) Math.floor(z); 169 NoiseMath.hash(xi, yi, zi, hashPoint); 170 } 171 } 172