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.scene.plugins.blender.BlenderContext; 35import com.jme3.scene.plugins.blender.file.Structure; 36import com.jme3.texture.Image; 37import com.jme3.texture.Image.Format; 38import com.jme3.texture.Texture; 39import com.jme3.texture.Texture3D; 40import com.jme3.util.BufferUtils; 41import java.nio.ByteBuffer; 42import java.util.ArrayList; 43 44/** 45 * This class generates the 'marble' texture. 46 * @author Marcin Roguski (Kaelthas) 47 */ 48public class TextureGeneratorMarble extends TextureGeneratorWood { 49 // tex->stype 50 protected static final int TEX_SOFT = 0; 51 protected static final int TEX_SHARP = 1; 52 protected static final int TEX_SHARPER = 2; 53 54 /** 55 * Constructor stores the given noise generator. 56 * @param noiseGenerator 57 * the noise generator 58 */ 59 public TextureGeneratorMarble(NoiseGenerator noiseGenerator) { 60 super(noiseGenerator); 61 } 62 63 @Override 64 protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) { 65 float[] texvec = new float[] { 0, 0, 0 }; 66 TexturePixel texres = new TexturePixel(); 67 int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; 68 float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; 69 float[][] colorBand = this.computeColorband(tex, blenderContext); 70 Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8; 71 int bytesPerPixel = colorBand != null ? 4 : 1; 72 BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); 73 MarbleData marbleData = new MarbleData(tex); 74 75 byte[] data = new byte[width * height * depth * bytesPerPixel]; 76 for (int i = -halfW; i < halfW; ++i) { 77 texvec[0] = wDelta * i; 78 for (int j = -halfH; j < halfH; ++j) { 79 texvec[1] = hDelta * j; 80 for (int k = -halfD; k < halfD; ++k) { 81 texvec[2] = dDelta * k; 82 texres.intensity = this.marbleInt(marbleData, texvec[0], texvec[1], texvec[2]); 83 if (colorBand != null) { 84 int colorbandIndex = (int) (texres.intensity * 1000.0f); 85 texres.red = colorBand[colorbandIndex][0]; 86 texres.green = colorBand[colorbandIndex][1]; 87 texres.blue = colorBand[colorbandIndex][2]; 88 89 this.applyBrightnessAndContrast(bacd, texres); 90 data[index++] = (byte) (texres.red * 255.0f); 91 data[index++] = (byte) (texres.green * 255.0f); 92 data[index++] = (byte) (texres.blue * 255.0f); 93 data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f); 94 } else { 95 this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); 96 data[index++] = (byte) (texres.intensity * 255.0f); 97 } 98 } 99 } 100 } 101 ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1); 102 dataArray.add(BufferUtils.createByteBuffer(data)); 103 return new Texture3D(new Image(format, width, height, depth, dataArray)); 104 } 105 106 public float marbleInt(MarbleData marbleData, float x, float y, float z) { 107 int waveform; 108 if (marbleData.waveform > TEX_TRI || marbleData.waveform < TEX_SIN) { 109 waveform = 0; 110 } else { 111 waveform = marbleData.waveform; 112 } 113 114 float n = 5.0f * (x + y + z); 115 float mi = n + marbleData.turbul * NoiseGenerator.NoiseFunctions.turbulence(x, y, z, marbleData.noisesize, marbleData.noisedepth, marbleData.noisebasis, marbleData.isHard); 116 117 if (marbleData.stype >= TEX_SOFT) { 118 mi = waveformFunctions[waveform].execute(mi); 119 if (marbleData.stype == TEX_SHARP) { 120 mi = (float) Math.sqrt(mi); 121 } else if (marbleData.stype == TEX_SHARPER) { 122 mi = (float) Math.sqrt(Math.sqrt(mi)); 123 } 124 } 125 return mi; 126 } 127 128 private static class MarbleData { 129 public final float noisesize; 130 public final int noisebasis; 131 public final int noisedepth; 132 public final int stype; 133 public final float turbul; 134 public final int waveform; 135 public final boolean isHard; 136 137 public MarbleData(Structure tex) { 138 noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); 139 noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); 140 noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue(); 141 stype = ((Number) tex.getFieldValue("stype")).intValue(); 142 turbul = ((Number) tex.getFieldValue("turbul")).floatValue(); 143 int noisetype = ((Number) tex.getFieldValue("noisetype")).intValue(); 144 waveform = ((Number) tex.getFieldValue("noisebasis2")).intValue(); 145 isHard = noisetype != TEX_NOISESOFT; 146 } 147 } 148} 149