• 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 'magic' texture.
47  * @author Marcin Roguski (Kaelthas)
48  */
49 public class TextureGeneratorMagic extends TextureGenerator {
50 	private static NoiseDepthFunction[] noiseDepthFunctions = new NoiseDepthFunction[10];
51 	static {
52 		noiseDepthFunctions[0] = new NoiseDepthFunction() {
53 			@Override
54 			public void compute(float[] xyz, float turbulence) {
55 				xyz[1] = -(float) Math.cos(xyz[0] - xyz[1] + xyz[2]) * turbulence;
56 			}
57 		};
58 		noiseDepthFunctions[1] = new NoiseDepthFunction() {
59 			@Override
60 			public void compute(float[] xyz, float turbulence) {
61 				xyz[0] = (float) Math.cos(xyz[0] - xyz[1] - xyz[2]) * turbulence;
62 			}
63 		};
64 		noiseDepthFunctions[2] = new NoiseDepthFunction() {
65 			@Override
66 			public void compute(float[] xyz, float turbulence) {
67 				xyz[2] = (float) Math.sin(-xyz[0] - xyz[1] - xyz[2]) * turbulence;
68 			}
69 		};
70 		noiseDepthFunctions[3] = new NoiseDepthFunction() {
71 			@Override
72 			public void compute(float[] xyz, float turbulence) {
73 				xyz[0] = -(float) Math.cos(-xyz[0] + xyz[1] - xyz[2]) * turbulence;
74 			}
75 		};
76 		noiseDepthFunctions[4] = new NoiseDepthFunction() {
77 			@Override
78 			public void compute(float[] xyz, float turbulence) {
79 				xyz[1] = -(float) Math.sin(-xyz[0] + xyz[1] + xyz[2]) * turbulence;
80 			}
81 		};
82 		noiseDepthFunctions[5] = new NoiseDepthFunction() {
83 			@Override
84 			public void compute(float[] xyz, float turbulence) {
85 				xyz[1] = -(float) Math.cos(-xyz[0] + xyz[1] + xyz[2]) * turbulence;
86 			}
87 		};
88 		noiseDepthFunctions[6] = new NoiseDepthFunction() {
89 			@Override
90 			public void compute(float[] xyz, float turbulence) {
91 				xyz[0] = (float) Math.cos(xyz[0] + xyz[1] + xyz[2]) * turbulence;
92 			}
93 		};
94 		noiseDepthFunctions[7] = new NoiseDepthFunction() {
95 			@Override
96 			public void compute(float[] xyz, float turbulence) {
97 				xyz[2] = (float) Math.sin(xyz[0] + xyz[1] - xyz[2]) * turbulence;
98 			}
99 		};
100 		noiseDepthFunctions[8] = new NoiseDepthFunction() {
101 			@Override
102 			public void compute(float[] xyz, float turbulence) {
103 				xyz[0] = -(float) Math.cos(-xyz[0] - xyz[1] + xyz[2]) * turbulence;
104 			}
105 		};
106 		noiseDepthFunctions[9] = new NoiseDepthFunction() {
107 			@Override
108 			public void compute(float[] xyz, float turbulence) {
109 				xyz[1] = -(float) Math.sin(xyz[0] - xyz[1] + xyz[2]) * turbulence;
110 			}
111 		};
112 	}
113 
114 	/**
115 	 * Constructor stores the given noise generator.
116 	 * @param noiseGenerator
117 	 *        the noise generator
118 	 */
TextureGeneratorMagic(NoiseGenerator noiseGenerator)119 	public TextureGeneratorMagic(NoiseGenerator noiseGenerator) {
120 		super(noiseGenerator);
121 	}
122 
123 	@Override
generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext)124 	protected Texture generate(Structure tex, int width, int height, int depth, BlenderContext blenderContext) {
125 		float xyz[] = new float[3], turb;
126 		int noisedepth = ((Number) tex.getFieldValue("noisedepth")).intValue();
127 		float turbul = ((Number) tex.getFieldValue("turbul")).floatValue() / 5.0f;
128 		float[] texvec = new float[] { 0, 0, 0 };
129 		TexturePixel texres = new TexturePixel();
130 		int halfW = width >> 1, halfH = height >> 1, halfD = depth >> 1, index = 0;
131 		float wDelta = 1.0f / halfW, hDelta = 1.0f / halfH, dDelta = 1.0f / halfD;
132 		float[][] colorBand = this.computeColorband(tex, blenderContext);
133 		BrightnessAndContrastData bacd = new BrightnessAndContrastData(tex);
134 
135 		byte[] data = new byte[width * height * depth * 4];
136 		for (int i = -halfW; i < halfW; ++i) {
137 			texvec[0] = wDelta * i;
138 			for (int j = -halfH; j < halfH; ++j) {
139 				texvec[1] = hDelta * j;
140 				for (int k = -halfD; k < halfD; ++k) {
141 					turb = turbul;
142 					texvec[2] = dDelta * k;
143 					xyz[0] = (float) Math.sin((texvec[0] + texvec[1] + texvec[2]) * 5.0f);
144 					xyz[1] = (float) Math.cos((-texvec[0] + texvec[1] - texvec[2]) * 5.0f);
145 					xyz[2] = -(float) Math.cos((-texvec[0] - texvec[1] + texvec[2]) * 5.0f);
146 
147 					if (colorBand != null) {
148 						texres.intensity = FastMath.clamp(0.3333f * (xyz[0] + xyz[1] + xyz[2]), 0.0f, 1.0f);
149 						int colorbandIndex = (int) (texres.intensity * 1000.0f);
150 						texres.red = colorBand[colorbandIndex][0];
151 						texres.green = colorBand[colorbandIndex][1];
152 						texres.blue = colorBand[colorbandIndex][2];
153 						texres.alpha = colorBand[colorbandIndex][3];
154 					} else {
155 						if (noisedepth > 0) {
156 							xyz[0] *= turb;
157 							xyz[1] *= turb;
158 							xyz[2] *= turb;
159 							for (int m=0;m<noisedepth;++m) {
160 								noiseDepthFunctions[m].compute(xyz, turb);
161 							}
162 						}
163 
164 						if (turb != 0.0f) {
165 							turb *= 2.0f;
166 							xyz[0] /= turb;
167 							xyz[1] /= turb;
168 							xyz[2] /= turb;
169 						}
170 						texres.red = 0.5f - xyz[0];
171 						texres.green = 0.5f - xyz[1];
172 						texres.blue = 0.5f - xyz[2];
173 						texres.alpha = 1.0f;
174 					}
175 					this.applyBrightnessAndContrast(bacd, texres);
176 					data[index++] = (byte) (texres.red * 255.0f);
177 					data[index++] = (byte) (texres.green * 255.0f);
178 					data[index++] = (byte) (texres.blue * 255.0f);
179 					data[index++] = (byte) (texres.alpha * 255.0f);
180 				}
181 			}
182 		}
183 		ArrayList<ByteBuffer> dataArray = new ArrayList<ByteBuffer>(1);
184 		dataArray.add(BufferUtils.createByteBuffer(data));
185 		return new Texture3D(new Image(Format.RGBA8, width, height, depth, dataArray));
186 	}
187 
188 	private static interface NoiseDepthFunction {
compute(float[] xyz, float turbulence)189 		void compute(float[] xyz, float turbulence);
190 	}
191 }
192