OSDN Git Service

Update terrain.
[mikumikustudio/MikuMikuStudio.git] / engine / src / terrain / com / jme3 / terrain / noise / ShaderUtils.java
diff --git a/engine/src/terrain/com/jme3/terrain/noise/ShaderUtils.java b/engine/src/terrain/com/jme3/terrain/noise/ShaderUtils.java
new file mode 100644 (file)
index 0000000..d1c2f6e
--- /dev/null
@@ -0,0 +1,288 @@
+/**\r
+ * Copyright (c) 2011, Novyon Events\r
+ * \r
+ * All rights reserved.\r
+ * \r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * \r
+ * - Redistributions of source code must retain the above copyright notice, this\r
+ * list of conditions and the following disclaimer.\r
+ * \r
+ * - Redistributions in binary form must reproduce the above copyright notice,\r
+ * this list of conditions and the following disclaimer in the documentation\r
+ * and/or other materials provided with the distribution.\r
+ * \r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\r
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * \r
+ * @author Anthyon\r
+ */\r
+package com.jme3.terrain.noise;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.image.BufferedImage;\r
+import java.awt.image.DataBuffer;\r
+import java.awt.image.DataBufferInt;\r
+import java.awt.image.WritableRaster;\r
+import java.nio.ByteBuffer;\r
+import java.nio.ByteOrder;\r
+\r
+/**\r
+ * Helper class containing useful functions explained in the book:\r
+ * Texturing & Modeling - A Procedural Approach\r
+ * \r
+ * @author Anthyon\r
+ * \r
+ */\r
+public class ShaderUtils {\r
+\r
+       public static final float[] i2c(final int color) {\r
+               return new float[] { (color & 0x00ff0000) / 256f, (color & 0x0000ff00) / 256f, (color & 0x000000ff) / 256f,\r
+                               (color & 0xff000000) / 256f };\r
+       }\r
+\r
+       public static final int c2i(final float[] color) {\r
+               return (color.length == 4 ? (int) (color[3] * 256) : 0xff000000) | ((int) (color[0] * 256) << 16) | ((int) (color[1] * 256) << 8)\r
+                               | (int) (color[2] * 256);\r
+       }\r
+\r
+       public static final float mix(final float a, final float b, final float f) {\r
+               return (1 - f) * a + f * b;\r
+       }\r
+\r
+       public static final Color mix(final Color a, final Color b, final float f) {\r
+               return new Color((int) ShaderUtils.clamp(ShaderUtils.mix(a.getRed(), b.getRed(), f), 0, 255), (int) ShaderUtils.clamp(\r
+                               ShaderUtils.mix(a.getGreen(), b.getGreen(), f), 0, 255), (int) ShaderUtils.clamp(\r
+                               ShaderUtils.mix(a.getBlue(), b.getBlue(), f), 0, 255));\r
+       }\r
+\r
+       public static final int mix(final int a, final int b, final float f) {\r
+               return (int) ((1 - f) * a + f * b);\r
+       }\r
+\r
+       public static final float[] mix(final float[] c1, final float[] c2, final float f) {\r
+               return new float[] { ShaderUtils.mix(c1[0], c2[0], f), ShaderUtils.mix(c1[1], c2[1], f), ShaderUtils.mix(c1[2], c2[2], f) };\r
+       }\r
+\r
+       public static final float step(final float a, final float x) {\r
+               return x < a ? 0 : 1;\r
+       }\r
+\r
+       public static final float boxstep(final float a, final float b, final float x) {\r
+               return ShaderUtils.clamp((x - a) / (b - a), 0, 1);\r
+       }\r
+\r
+       public static final float pulse(final float a, final float b, final float x) {\r
+               return ShaderUtils.step(a, x) - ShaderUtils.step(b, x);\r
+       }\r
+\r
+       public static final float clamp(final float x, final float a, final float b) {\r
+               return x < a ? a : x > b ? b : x;\r
+       }\r
+\r
+       public static final float min(final float a, final float b) {\r
+               return a < b ? a : b;\r
+       }\r
+\r
+       public static final float max(final float a, final float b) {\r
+               return a > b ? a : b;\r
+       }\r
+\r
+       public static final float abs(final float x) {\r
+               return x < 0 ? -x : x;\r
+       }\r
+\r
+       public static final float smoothstep(final float a, final float b, final float x) {\r
+               if (x < a) {\r
+                       return 0;\r
+               } else if (x > b) {\r
+                       return 1;\r
+               }\r
+               float xx = (x - a) / (b - a);\r
+               return xx * xx * (3 - 2 * xx);\r
+       }\r
+\r
+       public static final float mod(final float a, final float b) {\r
+               int n = (int) (a / b);\r
+               float aa = a - n * b;\r
+               if (aa < 0) {\r
+                       aa += b;\r
+               }\r
+               return aa;\r
+       }\r
+\r
+       public static final int floor(final float x) {\r
+               return x > 0 ? (int) x : (int) x - 1;\r
+       }\r
+\r
+       public static final float ceil(final float x) {\r
+               return (int) x + (x > 0 && x != (int) x ? 1 : 0);\r
+       }\r
+\r
+       public static final float spline(float x, final float[] knot) {\r
+               float CR00 = -0.5f;\r
+               float CR01 = 1.5f;\r
+               float CR02 = -1.5f;\r
+               float CR03 = 0.5f;\r
+               float CR10 = 1.0f;\r
+               float CR11 = -2.5f;\r
+               float CR12 = 2.0f;\r
+               float CR13 = -0.5f;\r
+               float CR20 = -0.5f;\r
+               float CR21 = 0.0f;\r
+               float CR22 = 0.5f;\r
+               float CR23 = 0.0f;\r
+               float CR30 = 0.0f;\r
+               float CR31 = 1.0f;\r
+               float CR32 = 0.0f;\r
+               float CR33 = 0.0f;\r
+\r
+               int span;\r
+               int nspans = knot.length - 3;\r
+               float c0, c1, c2, c3; /* coefficients of the cubic. */\r
+               if (nspans < 1) {/* illegal */\r
+                       throw new RuntimeException("Spline has too few knots.");\r
+               }\r
+               /* Find the appropriate 4-point span of the spline. */\r
+               x = ShaderUtils.clamp(x, 0, 1) * nspans;\r
+               span = (int) x;\r
+               if (span >= knot.length - 3) {\r
+                       span = knot.length - 3;\r
+               }\r
+               x -= span;\r
+               /* Evaluate the span cubic at x using Horner’s rule. */\r
+               c3 = CR00 * knot[span + 0] + CR01 * knot[span + 1] + CR02 * knot[span + 2] + CR03 * knot[span + 3];\r
+               c2 = CR10 * knot[span + 0] + CR11 * knot[span + 1] + CR12 * knot[span + 2] + CR13 * knot[span + 3];\r
+               c1 = CR20 * knot[span + 0] + CR21 * knot[span + 1] + CR22 * knot[span + 2] + CR23 * knot[span + 3];\r
+               c0 = CR30 * knot[span + 0] + CR31 * knot[span + 1] + CR32 * knot[span + 2] + CR33 * knot[span + 3];\r
+               return ((c3 * x + c2) * x + c1) * x + c0;\r
+       }\r
+\r
+       public static final float[] spline(final float x, final float[][] knots) {\r
+               float[] retval = new float[knots.length];\r
+               for (int i = 0; i < knots.length; i++) {\r
+                       retval[i] = ShaderUtils.spline(x, knots[i]);\r
+               }\r
+               return retval;\r
+       }\r
+\r
+       public static final float gammaCorrection(final float gamma, final float x) {\r
+               return (float) Math.pow(x, 1 / gamma);\r
+       }\r
+\r
+       public static final float bias(final float b, final float x) {\r
+               return (float) Math.pow(x, Math.log(b) / Math.log(0.5));\r
+       }\r
+\r
+       public static final float gain(final float g, final float x) {\r
+               return x < 0.5 ? ShaderUtils.bias(1 - g, 2 * x) / 2 : 1 - ShaderUtils.bias(1 - g, 2 - 2 * x) / 2;\r
+       }\r
+\r
+       public static final float sinValue(final float s, final float minFreq, final float maxFreq, final float swidth) {\r
+               float value = 0;\r
+               float cutoff = ShaderUtils.clamp(0.5f / swidth, 0, maxFreq);\r
+               float f;\r
+               for (f = minFreq; f < 0.5 * cutoff; f *= 2) {\r
+                       value += Math.sin(2 * Math.PI * f * s) / f;\r
+               }\r
+               float fade = ShaderUtils.clamp(2 * (cutoff - f) / cutoff, 0, 1);\r
+               value += fade * Math.sin(2 * Math.PI * f * s) / f;\r
+               return value;\r
+       }\r
+\r
+       public static final float length(final float x, final float y, final float z) {\r
+               return (float) Math.sqrt(x * x + y * y + z * z);\r
+       }\r
+\r
+       public static final float[] rotate(final float[] v, final float[][] m) {\r
+               float x = v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2];\r
+               float y = v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2];\r
+               float z = v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2];\r
+               return new float[] { x, y, z };\r
+       }\r
+\r
+       public static final float[][] calcRotationMatrix(final float ax, final float ay, final float az) {\r
+               float[][] retval = new float[3][3];\r
+               float cax = (float) Math.cos(ax);\r
+               float sax = (float) Math.sin(ax);\r
+               float cay = (float) Math.cos(ay);\r
+               float say = (float) Math.sin(ay);\r
+               float caz = (float) Math.cos(az);\r
+               float saz = (float) Math.sin(az);\r
+\r
+               retval[0][0] = cay * caz;\r
+               retval[0][1] = -cay * saz;\r
+               retval[0][2] = say;\r
+               retval[1][0] = sax * say * caz + cax * saz;\r
+               retval[1][1] = -sax * say * saz + cax * caz;\r
+               retval[1][2] = -sax * cay;\r
+               retval[2][0] = -cax * say * caz + sax * saz;\r
+               retval[2][1] = cax * say * saz + sax * caz;\r
+               retval[2][2] = cax * cay;\r
+\r
+               return retval;\r
+       }\r
+\r
+       public static final float[] normalize(final float[] v) {\r
+               float l = ShaderUtils.length(v);\r
+               float[] r = new float[v.length];\r
+               int i = 0;\r
+               for (float vv : v) {\r
+                       r[i++] = vv / l;\r
+               }\r
+               return r;\r
+       }\r
+\r
+       public static final float length(final float[] v) {\r
+               float s = 0;\r
+               for (float vv : v) {\r
+                       s += vv * vv;\r
+               }\r
+               return (float) Math.sqrt(s);\r
+       }\r
+\r
+       public static final ByteBuffer getImageDataFromImage(BufferedImage bufferedImage) {\r
+               WritableRaster wr;\r
+               DataBuffer db;\r
+\r
+               BufferedImage bi = new BufferedImage(128, 64, BufferedImage.TYPE_INT_ARGB);\r
+               Graphics2D g = bi.createGraphics();\r
+               g.drawImage(bufferedImage, null, null);\r
+               bufferedImage = bi;\r
+               wr = bi.getRaster();\r
+               db = wr.getDataBuffer();\r
+\r
+               DataBufferInt dbi = (DataBufferInt) db;\r
+               int[] data = dbi.getData();\r
+\r
+               ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length * 4);\r
+               byteBuffer.order(ByteOrder.LITTLE_ENDIAN);\r
+               byteBuffer.asIntBuffer().put(data);\r
+               byteBuffer.flip();\r
+\r
+               return byteBuffer;\r
+       }\r
+\r
+       public static float frac(float f) {\r
+               return f - ShaderUtils.floor(f);\r
+       }\r
+\r
+       public static float[] floor(float[] fs) {\r
+               float[] retval = new float[fs.length];\r
+               for (int i = 0; i < fs.length; i++) {\r
+                       retval[i] = ShaderUtils.floor(fs[i]);\r
+               }\r
+               return retval;\r
+       }\r
+}\r