-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathShaderUtils.java
More file actions
272 lines (236 loc) · 9.61 KB
/
ShaderUtils.java
File metadata and controls
272 lines (236 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2011, Novyon Events
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Anthyon
*/
package com.jme3.terrain.noise;
import com.jme3.math.ColorRGBA;
import com.jme3.texture.Texture2D;
import java.nio.ByteBuffer;
/**
* Helper class containing useful functions explained in the book:
* Texturing & Modeling - A Procedural Approach
*
* @author Anthyon
*
*/
public class ShaderUtils {
/**
* A private constructor to inhibit instantiation of this class.
*/
private ShaderUtils() {
}
public static final float[] i2c(final int color) {
return new float[] { (color & 0x00ff0000) / 256f, (color & 0x0000ff00) / 256f, (color & 0x000000ff) / 256f,
(color & 0xff000000) / 256f };
}
public static final int c2i(final float[] color) {
return (color.length == 4 ? (int) (color[3] * 256) : 0xff000000) | ((int) (color[0] * 256) << 16) | ((int) (color[1] * 256) << 8)
| (int) (color[2] * 256);
}
public static final float mix(final float a, final float b, final float f) {
return (1 - f) * a + f * b;
}
public static final int mix(final int a, final int b, final float f) {
return (int) ((1 - f) * a + f * b);
}
public static final ColorRGBA mix(final ColorRGBA a, final ColorRGBA b, final float f) {
return new ColorRGBA(
ShaderUtils.mix(a.r, b.r, f),
ShaderUtils.mix(a.g, b.g, f),
ShaderUtils.mix(a.b, b.b, f),
ShaderUtils.mix(a.a, b.a, f));
}
public static final float[] mix(final float[] c1, final float[] c2, final float f) {
return new float[] { ShaderUtils.mix(c1[0], c2[0], f), ShaderUtils.mix(c1[1], c2[1], f), ShaderUtils.mix(c1[2], c2[2], f) };
}
public static final float step(final float a, final float x) {
return x < a ? 0 : 1;
}
public static final float boxstep(final float a, final float b, final float x) {
return ShaderUtils.clamp((x - a) / (b - a), 0, 1);
}
public static final float pulse(final float a, final float b, final float x) {
return ShaderUtils.step(a, x) - ShaderUtils.step(b, x);
}
public static final float clamp(final float x, final float a, final float b) {
return x < a ? a : x > b ? b : x;
}
public static final float min(final float a, final float b) {
return a < b ? a : b;
}
public static final float max(final float a, final float b) {
return a > b ? a : b;
}
public static final float abs(final float x) {
return x < 0 ? -x : x;
}
public static final float smoothstep(final float a, final float b, final float x) {
if (x < a) {
return 0;
} else if (x > b) {
return 1;
}
float xx = (x - a) / (b - a);
return xx * xx * (3 - 2 * xx);
}
public static final float mod(final float a, final float b) {
int n = (int) (a / b);
float aa = a - n * b;
if (aa < 0) {
aa += b;
}
return aa;
}
public static final int floor(final float x) {
return x > 0 ? (int) x : (int) x - 1;
}
public static final float ceil(final float x) {
return (int) x + (x > 0 && x != (int) x ? 1 : 0);
}
public static final float spline(float x, final float[] knot) {
float CR00 = -0.5f;
float CR01 = 1.5f;
float CR02 = -1.5f;
float CR03 = 0.5f;
float CR10 = 1.0f;
float CR11 = -2.5f;
float CR12 = 2.0f;
float CR13 = -0.5f;
float CR20 = -0.5f;
float CR21 = 0.0f;
float CR22 = 0.5f;
float CR23 = 0.0f;
float CR30 = 0.0f;
float CR31 = 1.0f;
float CR32 = 0.0f;
float CR33 = 0.0f;
int span;
int nspans = knot.length - 3;
float c0, c1, c2, c3; /* coefficients of the cubic. */
if (nspans < 1) {/* illegal */
throw new RuntimeException("Spline has too few knots.");
}
/* Find the appropriate 4-point span of the spline. */
x = ShaderUtils.clamp(x, 0, 1) * nspans;
span = (int) x;
if (span >= knot.length - 3) {
span = knot.length - 3;
}
x -= span;
/* Evaluate the span cubic at x using Horner’s rule. */
c3 = CR00 * knot[span + 0] + CR01 * knot[span + 1] + CR02 * knot[span + 2] + CR03 * knot[span + 3];
c2 = CR10 * knot[span + 0] + CR11 * knot[span + 1] + CR12 * knot[span + 2] + CR13 * knot[span + 3];
c1 = CR20 * knot[span + 0] + CR21 * knot[span + 1] + CR22 * knot[span + 2] + CR23 * knot[span + 3];
c0 = CR30 * knot[span + 0] + CR31 * knot[span + 1] + CR32 * knot[span + 2] + CR33 * knot[span + 3];
return ((c3 * x + c2) * x + c1) * x + c0;
}
public static final float[] spline(final float x, final float[][] knots) {
float[] retval = new float[knots.length];
for (int i = 0; i < knots.length; i++) {
retval[i] = ShaderUtils.spline(x, knots[i]);
}
return retval;
}
public static final float gammaCorrection(final float gamma, final float x) {
return (float) Math.pow(x, 1 / gamma);
}
public static final float bias(final float b, final float x) {
return (float) Math.pow(x, Math.log(b) / Math.log(0.5));
}
public static final float gain(final float g, final float x) {
return x < 0.5 ? ShaderUtils.bias(1 - g, 2 * x) / 2 : 1 - ShaderUtils.bias(1 - g, 2 - 2 * x) / 2;
}
public static final float sinValue(final float s, final float minFreq, final float maxFreq, final float sWidth) {
float value = 0;
float cutoff = ShaderUtils.clamp(0.5f / sWidth, 0, maxFreq);
float f;
for (f = minFreq; f < 0.5 * cutoff; f *= 2) {
value += Math.sin(2 * Math.PI * f * s) / f;
}
float fade = ShaderUtils.clamp(2 * (cutoff - f) / cutoff, 0, 1);
value += fade * Math.sin(2 * Math.PI * f * s) / f;
return value;
}
public static final float length(final float x, final float y, final float z) {
return (float) Math.sqrt(x * x + y * y + z * z);
}
public static final float[] rotate(final float[] v, final float[][] m) {
float x = v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2];
float y = v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2];
float z = v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2];
return new float[] { x, y, z };
}
public static final float[][] calcRotationMatrix(final float ax, final float ay, final float az) {
float[][] retval = new float[3][3];
float cax = (float) Math.cos(ax);
float sax = (float) Math.sin(ax);
float cay = (float) Math.cos(ay);
float say = (float) Math.sin(ay);
float caz = (float) Math.cos(az);
float saz = (float) Math.sin(az);
retval[0][0] = cay * caz;
retval[0][1] = -cay * saz;
retval[0][2] = say;
retval[1][0] = sax * say * caz + cax * saz;
retval[1][1] = -sax * say * saz + cax * caz;
retval[1][2] = -sax * cay;
retval[2][0] = -cax * say * caz + sax * saz;
retval[2][1] = cax * say * saz + sax * caz;
retval[2][2] = cax * cay;
return retval;
}
public static final float[] normalize(final float[] v) {
float l = ShaderUtils.length(v);
float[] r = new float[v.length];
int i = 0;
for (float vv : v) {
r[i++] = vv / l;
}
return r;
}
public static final float length(final float[] v) {
float s = 0;
for (float vv : v) {
s += vv * vv;
}
return (float) Math.sqrt(s);
}
public static final ByteBuffer getImageDataFromTexture(final Texture2D texture) {
return texture.getImage().getData(0);
}
public static float frac(float f) {
return f - ShaderUtils.floor(f);
}
public static float[] floor(float[] fs) {
float[] retval = new float[fs.length];
for (int i = 0; i < fs.length; i++) {
retval[i] = ShaderUtils.floor(fs[i]);
}
return retval;
}
}