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.math.FastMath;
35import com.jme3.scene.plugins.blender.BlenderContext;
36import com.jme3.scene.plugins.blender.file.Structure;
37import com.jme3.scene.plugins.blender.textures.NoiseGenerator.NoiseFunction;
38import com.jme3.texture.Image;
39import com.jme3.texture.Image.Format;
40import com.jme3.texture.Texture;
41import com.jme3.texture.Texture3D;
42import com.jme3.util.BufferUtils;
43import java.nio.ByteBuffer;
44import java.util.ArrayList;
45
46/**
47 * This class generates the 'distorted noise' texture.
48 * @author Marcin Roguski (Kaelthas)
49 */
50public class TextureGeneratorDistnoise extends TextureGenerator {
51
52	/**
53	 * Constructor stores the given noise generator.
54	 * @param noiseGenerator
55	 *        the noise generator
56	 */
57	public TextureGeneratorDistnoise(NoiseGenerator noiseGenerator) {
58		super(noiseGenerator);
59	}
60
61	@Override
62	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
63		float noisesize = ((Number) tex.getFieldValue("noisesize")).floatValue();
64		float distAmount = ((Number) tex.getFieldValue("dist_amount")).floatValue();
65		int noisebasis = ((Number) tex.getFieldValue("noisebasis")).intValue();
66		int noisebasis2 = ((Number) tex.getFieldValue("noisebasis2")).intValue();
67
68		TexturePixel texres = new TexturePixel();
69		float[] texvec = new float[] { 0, 0, 0 };
70		int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0;
71		float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD;
72		float[][] colorBand = this.computeColorband(tex, blenderContext);
73		Format format = colorBand != null ? Format.RGBA8 : Format.Luminance8;
74		int bytesPerPixel = colorBand != null ? 4 : 1;
75		BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex);
76
77		byte[] data = new byte[width * height * depth * bytesPerPixel];
78		for (int i = -halfW; i < halfW; ++i) {
79			texvec[0] = wDelta * i / noisesize;
80			for (int j = -halfH; j < halfH; ++j) {
81				texvec[1] = hDelta * j / noisesize;
82				for (int k = -halfD; k < halfD; ++k) {
83					texvec[2] = dDelta * k;
84					texres.intensity = this.musgraveVariableLunacrityNoise(texvec[0], texvec[1], texvec[2], distAmount, noisebasis, noisebasis2);
85					texres.intensity = FastMath.clamp(texres.intensity, 0.0f, 1.0f);
86					if (colorBand != null) {
87						int colorbandIndex = (int) (texres.intensity * 1000.0f);
88						texres.red = colorBand[colorbandIndex][0];
89						texres.green = colorBand[colorbandIndex][1];
90						texres.blue = colorBand[colorbandIndex][2];
91
92						this.applyBrightnessAndContrast(bacd, texres);
93						data[index++] = (byte) (texres.red * 255.0f);
94						data[index++] = (byte) (texres.green * 255.0f);
95						data[index++] = (byte) (texres.blue * 255.0f);
96						data[index++] = (byte) (colorBand[colorbandIndex][3] * 255.0f);
97					} else {
98						this.applyBrightnessAndContrast(texres, bacd.contrast, bacd.brightness);
99						data[index++] = (byte) (texres.intensity * 255.0f);
100					}
101				}
102			}
103		}
104		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
105		dataArray.add(BufferUtils.createByteBuffer(data));
106		return new Texture3D(new Image(format, width, height, depth, dataArray));
107	}
108
109	/**
110     * "Variable Lacunarity Noise" A distorted variety of Perlin noise. This method is used to calculate distorted noise
111     * texture.
112     * @param x
113     * @param y
114     * @param z
115     * @param distortion
116     * @param nbas1
117     * @param nbas2
118     * @return
119     */
120    private float musgraveVariableLunacrityNoise(float x, float y, float z, float distortion, int nbas1, int nbas2) {
121        NoiseFunction abstractNoiseFunc1 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(nbas1));
122        if (abstractNoiseFunc1 == null) {
123            abstractNoiseFunc1 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(0));
124        }
125        NoiseFunction abstractNoiseFunc2 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(nbas2));
126        if (abstractNoiseFunc2 == null) {
127            abstractNoiseFunc2 = NoiseGenerator.noiseFunctions.get(Integer.valueOf(0));
128        }
129        // get a random vector and scale the randomization
130        float rx = abstractNoiseFunc1.execute(x + 13.5f, y + 13.5f, z + 13.5f) * distortion;
131        float ry = abstractNoiseFunc1.execute(x, y, z) * distortion;
132        float rz = abstractNoiseFunc1.execute(x - 13.5f, y - 13.5f, z - 13.5f) * distortion;
133        return abstractNoiseFunc2.executeSigned(x + rx, y + ry, z + rz); //distorted-domain noise
134    }
135}
136