• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 'noise' texture.
47  * @author Marcin Roguski (Kaelthas)
48  */
49 public class TextureGeneratorNoise extends TextureGenerator {
50 
51 	/**
52 	 * Constructor stores the given noise generator.
53 	 * @param noiseGenerator
54 	 *        the noise generator
55 	 */
TextureGeneratorNoise(NoiseGenerator noiseGenerator)56 	public TextureGeneratorNoise(NoiseGenerator noiseGenerator) {
57 		super(noiseGenerator);
58 	}
59 
60 	@Override
generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext)61 	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
62 		int val, random, loop;
63 		int noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue();
64 		TexturePixel texres = new TexturePixel();
65 		int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0;
66 		float[][] colorBand = this.computeColorband(tex, blenderContext);
67 		Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8;
68 		int bytesPerPixel = colorBand != null ? 4 : 1;
69 		BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex);
70 
71 		byte[] data = new byte[width * height * depth * bytesPerPixel];
72 		for (int i = -halfW; i < halfW; ++i) {
73 			for (int j = -halfH; j < halfH; ++j) {
74 				for (int k = -halfD; k < halfD; ++k) {
75 					random = FastMath.rand.nextInt();
76 					val = random & 3;
77 
78 					loop = noisedepth;
79 					while (loop-- != 0) {
80 						random >>= 2;
81 						val *= random & 3;
82 					}
83 					texres.intensity = FastMath.clamp(val, 0.0f, 1.0f);
84 					if (colorBand != null) {
85 						int colorbandIndex = (int) (texres.intensity * 1000.0f);
86 						texres.red = colorBand[colorbandIndex][0];
87 						texres.green = colorBand[colorbandIndex][1];
88 						texres.blue = colorBand[colorbandIndex][2];
89 
90 						this.applyBrightnessAndContrast(bacd, texres);
91 						data[index++] = (byte) (texres.red * 255.0f);
92 						data[index++] = (byte) (texres.green * 255.0f);
93 						data[index++] = (byte) (texres.blue * 255.0f);
94 						data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f);
95 					} else {
96 						this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness);
97 						data[index++] = (byte) (texres.intensity * 255.0f);
98 					}
99 				}
100 			}
101 		}
102 		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
103 		dataArray.add(BufferUtils.createByteBuffer(data));
104 		return new Texture3D(new Image(format, width, height, depth, dataArray));
105 	}
106 }
107