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 */ 32package com.jme3.scene.plugins.blender.textures; 33 34import com.jme3.math.FastMath; 35import com.jme3.scene.plugins.blender.BlenderContext; 36import com.jme3.scene.plugins.blender.file.Structure; 37import com.jme3.texture.Image; 38import com.jme3.texture.Image.Format; 39import com.jme3.texture.Texture; 40import com.jme3.texture.Texture3D; 41import com.jme3.util.BufferUtils; 42import java.nio.ByteBuffer; 43import java.util.ArrayList; 44 45/** 46 * This class generates the 'blend' texture. 47 * @author Marcin Roguski (Kaelthas) 48 */ 49public final class TextureGeneratorBlend extends TextureGenerator { 50 51 private static final IntensityFunction INTENSITY_FUNCTION[] = new IntensityFunction[7]; 52 static { 53 INTENSITY_FUNCTION[0] = new IntensityFunction() {//Linear: stype = 0 (TEX_LIN) 54 @Override 55 public float getIntensity(float x, float y, float z) { 56 return (1.0f + x) * 0.5f; 57 } 58 }; 59 INTENSITY_FUNCTION[1] = new IntensityFunction() {//Quad: stype = 1 (TEX_QUAD) 60 @Override 61 public float getIntensity(float x, float y, float z) { 62 float result = (1.0f + x) * 0.5f; 63 return result * result; 64 } 65 }; 66 INTENSITY_FUNCTION[2] = new IntensityFunction() {//Ease: stype = 2 (TEX_EASE) 67 @Override 68 public float getIntensity(float x, float y, float z) { 69 float result = (1.0f + x) * 0.5f; 70 if (result <= 0.0f) { 71 return 0.0f; 72 } else if (result >= 1.0f) { 73 return 1.0f; 74 } else { 75 return result * result *(3.0f - 2.0f * result); 76 } 77 } 78 }; 79 INTENSITY_FUNCTION[3] = new IntensityFunction() {//Diagonal: stype = 3 (TEX_DIAG) 80 @Override 81 public float getIntensity(float x, float y, float z) { 82 return (2.0f + x + y) * 0.25f; 83 } 84 }; 85 INTENSITY_FUNCTION[4] = new IntensityFunction() {//Sphere: stype = 4 (TEX_SPHERE) 86 @Override 87 public float getIntensity(float x, float y, float z) { 88 float result = 1.0f - (float) Math.sqrt(x * x + y * y + z * z); 89 return result < 0.0f ? 0.0f : result; 90 } 91 }; 92 INTENSITY_FUNCTION[5] = new IntensityFunction() {//Halo: stype = 5 (TEX_HALO) 93 @Override 94 public float getIntensity(float x, float y, float z) { 95 float result = 1.0f - (float) Math.sqrt(x * x + y * y + z * z); 96 return result <= 0.0f ? 0.0f : result * result; 97 } 98 }; 99 INTENSITY_FUNCTION[6] = new IntensityFunction() {//Radial: stype = 6 (TEX_RAD) 100 @Override 101 public float getIntensity(float x, float y, float z) { 102 return (float) Math.atan2(y, x) * FastMath.INV_TWO_PI + 0.5f; 103 } 104 }; 105 } 106 107 /** 108 * Constructor stores the given noise generator. 109 * @param noiseGenerator 110 * the noise generator 111 */ 112 public TextureGeneratorBlend(NoiseGenerator noiseGenerator) { 113 super(noiseGenerator); 114 } 115 116 @Override 117 protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) { 118 int flag = ((Number) tex.getFieldValue("flag")).intValue(); 119 int stype = ((Number) tex.getFieldValue("stype")).intValue(); 120 TexturePixel texres = new TexturePixel(); 121 int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; 122 float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD, x, y; 123 float[][] colorBand = this.computeColorband(tex, blenderContext); 124 BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); 125 Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8; 126 int bytesPerPixel = colorBand != null ? 4 : 1; 127 boolean flipped = (flag & NoiseGenerator.TEX_FLIPBLEND) != 0; 128 129 byte[] data = new byte[width * height * depth * bytesPerPixel]; 130 for (int i = -halfW; i < halfW; ++i) { 131 x = wDelta * i; 132 for (int j = -halfH; j < halfH; ++j) { 133 if (flipped) { 134 y = x; 135 x = hDelta * j; 136 } else { 137 y = hDelta * j; 138 } 139 for (int k = -halfD; k < halfD; ++k) { 140 texres.intensity = INTENSITY_FUNCTION[stype].getIntensity(x, y, dDelta * k); 141 142 if (colorBand != null) { 143 int colorbandIndex = (int) (texres.intensity * 1000.0f); 144 texres.red = colorBand[colorbandIndex][0]; 145 texres.green = colorBand[colorbandIndex][1]; 146 texres.blue = colorBand[colorbandIndex][2]; 147 148 this.applyBrightnessAndContrast(bacd, texres); 149 data[index++] = (byte) (texres.red * 255.0f); 150 data[index++] = (byte) (texres.green * 255.0f); 151 data[index++] = (byte) (texres.blue * 255.0f); 152 data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f); 153 } else { 154 this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); 155 data[index++] = (byte) (texres.intensity * 255.0f); 156 } 157 } 158 } 159 } 160 ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1); 161 dataArray.add(BufferUtils.createByteBuffer(data)); 162 return new Texture3D(new Image(format, width, height, depth, dataArray)); 163 } 164 165 private static interface IntensityFunction { 166 float getIntensity(float x, float y, float z); 167 } 168} 169