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.scene.plugins.blender.BlenderContext; 35 import com.jme3.scene.plugins.blender.file.Structure; 36 import com.jme3.scene.plugins.blender.textures.NoiseGenerator.MusgraveFunction; 37 import com.jme3.texture.Image; 38 import com.jme3.texture.Image.Format; 39 import com.jme3.texture.Texture; 40 import com.jme3.texture.Texture3D; 41 import com.jme3.util.BufferUtils; 42 import java.nio.ByteBuffer; 43 import java.util.ArrayList; 44 45 /** 46 * This class generates the 'musgrave' texture. 47 * @author Marcin Roguski (Kaelthas) 48 */ 49 public class TextureGeneratorMusgrave extends TextureGenerator { 50 51 /** 52 * Constructor stores the given noise generator. 53 * @param noiseGenerator 54 * the noise generator 55 */ 56 public TextureGeneratorMusgrave(NoiseGenerator noiseGenerator) { 57 super(noiseGenerator); 58 } 59 60 @Override 61 protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) { 62 int stype = ((Number) tex.getFieldValue("stype")).intValue(); 63 float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue(); 64 TexturePixel texres = new TexturePixel(); 65 float[] texvec = new float[] { 0, 0, 0 }; 66 int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0; 67 float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD; 68 float[][] colorBand = this.computeColorband(tex, blenderContext); 69 Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8; 70 int bytesPerPixel = colorBand != null ? 4 : 1; 71 MusgraveData musgraveData = new MusgraveData(tex); 72 MusgraveFunction musgraveFunction; 73 BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex); 74 75 byte[] data = new byte[width * height * depth * bytesPerPixel]; 76 for (int i = -halfW; i < halfW; ++i) { 77 texvec[0] = wDelta * i / noisesize; 78 for (int j = -halfH; j < halfH; ++j) { 79 texvec[1] = hDelta * j / noisesize; 80 for (int k = -halfD; k < halfD; ++k) { 81 texvec[2] = dDelta * k / noisesize; 82 musgraveFunction = NoiseGenerator.musgraveFunctions.get(Integer.valueOf(musgraveData.stype)); 83 if(musgraveFunction==null) { 84 throw new IllegalStateException("Unknown type of musgrave texture: " + stype); 85 } 86 texres.intensity = musgraveData.outscale * musgraveFunction.execute(musgraveData, texvec[0], texvec[1], texvec[2]); 87 if(texres.intensity>1) { 88 texres.intensity = 1.0f; 89 } else if(texres.intensity < 0) { 90 texres.intensity = 0.0f; 91 } 92 93 if (colorBand != null) { 94 int colorbandIndex = (int) (texres.intensity * 1000.0f); 95 texres.red = colorBand[colorbandIndex][0]; 96 texres.green = colorBand[colorbandIndex][1]; 97 texres.blue = colorBand[colorbandIndex][2]; 98 99 this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness); 100 data[index++] = (byte) (texres.red * 255.0f); 101 data[index++] = (byte) (texres.green * 255.0f); 102 data[index++] = (byte) (texres.blue * 255.0f); 103 data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f); 104 } else { 105 this.applyBrightnessAndContrast(bacd, texres); 106 data[index++] = (byte) (texres.intensity * 255.0f); 107 } 108 } 109 } 110 } 111 ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1); 112 dataArray.add(BufferUtils.createByteBuffer(data)); 113 return new Texture3D(new Image(format, width, height, depth, dataArray)); 114 } 115 116 protected static class MusgraveData { 117 public final int stype; 118 public final float outscale; 119 public final float h; 120 public final float lacunarity; 121 public final float octaves; 122 public final int noisebasis; 123 public final float offset; 124 public final float gain; 125 126 public MusgraveData(Structure tex) { 127 stype = ((Number) tex.getFieldValue("stype")).intValue(); 128 outscale = ((Number) tex.getFieldValue("ns_outscale")).floatValue(); 129 h = ((Number) tex.getFieldValue("mg_H")).floatValue(); 130 lacunarity = ((Number) tex.getFieldValue("mg_lacunarity")).floatValue(); 131 octaves = ((Number) tex.getFieldValue("mg_octaves")).floatValue(); 132 noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue(); 133 offset = ((Number) tex.getFieldValue("mg_offset")).floatValue(); 134 gain = ((Number) tex.getFieldValue("mg_gain")).floatValue(); 135 } 136 } 137 } 138