Home | History | Annotate | Download | only in textures
      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.texture.Image;
     37 import com.jme3.texture.Image.Format;
     38 import com.jme3.texture.Texture;
     39 import com.jme3.texture.Texture3D;
     40 import com.jme3.util.BufferUtils;
     41 import java.nio.ByteBuffer;
     42 import java.util.ArrayList;
     43 
     44 /**
     45  * This class generates the 'stucci' texture.
     46  * @author Marcin Roguski (Kaelthas)
     47  */
     48 public class TextureGeneratorStucci extends TextureGenerator {
     49 	protected static final int TEX_NOISESOFT = 0;
     50 
     51 	/**
     52 	 * Constructor stores the given noise generator.
     53 	 * @param noiseGenerator
     54 	 *        the noise generator
     55 	 */
     56 	public TextureGeneratorStucci(NoiseGenerator noiseGenerator) {
     57 		super(noiseGenerator);
     58 	}
     59 
     60 	@Override
     61 	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
     62 		float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue();
     63 		int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue();
     64 		int noisetype = ((Number) tex.getFieldValue("noisetype")).intValue();
     65 		float turbul = ((Number) tex.getFieldValue("turbul")).floatValue();
     66 		boolean isHard = noisetype != TEX_NOISESOFT;
     67 		int stype = ((Number) tex.getFieldValue("stype")).intValue();
     68 
     69 		if(noisesize<=0.001f) {//the texture goes black if this value is lower than 0.001f
     70 			noisesize = 0.001f;
     71 		}
     72 
     73 		float[] texvec = new float[] { 0, 0, 0 };
     74 		TexturePixel texres = new TexturePixel();
     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, noiseValue, ofs;;
     77 		float[][] colorBand = this.computeColorband(tex, blenderContext);
     78 		Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8;
     79 		int bytesPerPixel = colorBand != null ? 4 : 1;
     80 
     81 		byte[] data = new byte[width * height * depth * bytesPerPixel];
     82 		for (int i = -halfW; i < halfW; ++i) {
     83 			texvec[0] = wDelta * i;
     84 			for (int j = -halfH; j < halfH; ++j) {
     85 				texvec[1] = hDelta * j;
     86 				for (int k = -halfD; k < halfD; ++k) {
     87 					texvec[2] = dDelta * k;
     88 					noiseValue = NoiseGenerator.NoiseFunctions.noise(texvec[0], texvec[1], texvec[2], noisesize, 0, noisebasis, isHard);
     89 					ofs = turbul / 200.0f;
     90 					if (stype != 0) {
     91 						ofs *= noiseValue * noiseValue;
     92 					}
     93 
     94 					texres.intensity = NoiseGenerator.NoiseFunctions.noise(texvec[0], texvec[1], texvec[2] + ofs, noisesize, 0, noisebasis, isHard);
     95 					if (colorBand != null) {
     96 						int colorbandIndex = (int) (texres.intensity * 1000.0f);
     97 						texres.red = colorBand[colorbandIndex][0];
     98 						texres.green = colorBand[colorbandIndex][1];
     99 						texres.blue = colorBand[colorbandIndex][2];
    100 						texres.alpha = colorBand[colorbandIndex][3];
    101 					}
    102 
    103 					if (stype == NoiseGenerator.TEX_WALLOUT) {
    104 						texres.intensity = 1.0f - texres.intensity;
    105 					}
    106 					if (texres.intensity < 0.0f) {
    107 						texres.intensity = 0.0f;
    108 					}
    109 					//no brightness and contrast needed for stucci (it doesn't affect the texture)
    110 					if (colorBand != null) {
    111 						data[index++] = (byte) (texres.red * 255.0f);
    112 						data[index++] = (byte) (texres.green * 255.0f);
    113 						data[index++] = (byte) (texres.blue * 255.0f);
    114 						data[index++] = (byte) (texres.alpha * 255.0f);
    115 					} else {
    116 						data[index++] = (byte) (texres.intensity * 255.0f);
    117 					}
    118 				}
    119 			}
    120 		}
    121 		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
    122 		dataArray.add(BufferUtils.createByteBuffer(data));
    123 		return new Texture3D(new Image(format, width, height, depth, dataArray));
    124 	}
    125 }
    126