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.math.FastMath;
     35 import com.jme3.scene.plugins.blender.BlenderContext;
     36 import com.jme3.scene.plugins.blender.file.Structure;
     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 'clouds' texture.
     47  * @author Marcin Roguski (Kaelthas)
     48  */
     49 public class TextureGeneratorClouds extends TextureGenerator {
     50 	// tex->noisetype
     51     protected static final int TEX_NOISESOFT = 0;
     52     protected static final int TEX_NOISEPERL = 1;
     53 
     54     // tex->stype
     55     protected static final int TEX_DEFAULT = 0;
     56     protected static final int TEX_COLOR = 1;
     57 
     58 	/**
     59 	 * Constructor stores the given noise generator.
     60 	 * @param noiseGenerator
     61 	 *        the noise generator
     62 	 */
     63 	public TextureGeneratorClouds(NoiseGenerator noiseGenerator) {
     64 		super(noiseGenerator);
     65 	}
     66 
     67 	@Override
     68 	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
     69 		float[] texvec = new float[] { 0, 0, 0 };
     70 		TexturePixel texres = new TexturePixel();
     71 
     72 		// reading the data from the texture structure
     73 		float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue();
     74 		int noiseDepth = ((Number) tex.getFieldValue("noisedepth")).intValue();
     75 		int noiseBasis = ((Number) tex.getFieldValue("noisebasis")).intValue();
     76 		int noiseType = ((Number) tex.getFieldValue("noisetype")).intValue();
     77 		boolean isHard = noiseType != TEX_NOISESOFT;
     78 		int sType = ((Number) tex.getFieldValue("stype")).intValue();
     79 		int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0;
     80 		float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD;
     81 		float[][] colorBand = this.computeColorband(tex, blenderContext);
     82 		Format format = sType == TEX_COLOR || colorBand != null ? Format.RGBA8 : Format.Luminance8;
     83 		int bytesPerPixel = sType == TEX_COLOR || colorBand != null ? 4 : 1;
     84 		BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex);
     85 
     86 		byte[] data = new byte[width * height * depth * bytesPerPixel];
     87 		for (int i = -halfW; i < halfW; ++i) {
     88 			texvec[0] = wDelta * i;
     89 			for (int j = -halfH; j < halfH; ++j) {
     90 				texvec[1] = hDelta * j;
     91 				for (int k = -halfD; k < halfD; ++k) {
     92 					texvec[2] = dDelta * k;
     93 					texres.intensity = NoiseGenerator.NoiseFunctions.turbulence(texvec[0], texvec[1], texvec[2], noisesize, noiseDepth, noiseBasis, isHard);
     94 					texres.intensity = FastMath.clamp(texres.intensity, 0.0f, 1.0f);
     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 
    101 						this.applyBrightnessAndContrast(bacd, texres);
    102 						data[index++] = (byte) (texres.red * 255.0f);
    103 						data[index++] = (byte) (texres.green * 255.0f);
    104 						data[index++] = (byte) (texres.blue * 255.0f);
    105 						data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f);
    106 					} else if (sType == TEX_COLOR) {
    107 						texres.red = texres.intensity;
    108 						texres.green = NoiseGenerator.NoiseFunctions.turbulence(texvec[1], texvec[0], texvec[2], noisesize, noiseDepth, noiseBasis, isHard);
    109 						texres.blue = NoiseGenerator.NoiseFunctions.turbulence(texvec[1], texvec[2], texvec[0], noisesize, noiseDepth, noiseBasis, isHard);
    110 
    111 						texres.green = FastMath.clamp(texres.green, 0.0f, 1.0f);
    112 						texres.blue = FastMath.clamp(texres.blue, 0.0f, 1.0f);
    113 
    114 						this.applyBrightnessAndContrast(bacd, texres);
    115 						data[index++] = (byte) (texres.red * 255.0f);
    116 						data[index++] = (byte) (texres.green * 255.0f);
    117 						data[index++] = (byte) (texres.blue * 255.0f);
    118 						data[index++] = (byte) (255);//1.0f * 255.0f
    119 					} else {
    120 						this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness);
    121 						data[index++] = (byte) (texres.intensity * 255.0f);
    122 					}
    123 				}
    124 			}
    125 		}
    126 
    127 		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
    128 		dataArray.add(BufferUtils.createByteBuffer(data));
    129 		return new Texture3D(new Image(format, width, height, depth, dataArray));
    130 	}
    131 }
    132