Repository navigation
Expand file tree
/
Copy pathLayerNoise.java
More file actions
387 lines (325 loc) · 11.8 KB
/
Copy pathLayerNoise.java
File metadata and controls
387 lines (325 loc) · 11.8 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.awt.Color;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Arrays;
import javax.media.opengl.GL2;
import controlP5.Group;
import controlP5.Tab;
import toxi.math.noise.PerlinNoise;
import toxi.math.noise.SimplexNoise;
public class LayerNoise extends LayerColor {
FloatBuffer vbuffer;
FloatBuffer cbuffer;
int maxNumPoint=100000;
public FloatParameter noiseH = new FloatParameter(this,0f,0f,1f,"noiseH");
public FloatParameter noiseS = new FloatParameter(this,0f,0f,1f,"noiseS");
public FloatParameter noiseB = new FloatParameter(this,0f,0f,1f,"noiseB");
public FloatParameter sortX = new FloatParameter(this,0f,0f,1f,"sortX");
public FloatParameter sortY = new FloatParameter(this,0f,0f,1f,"sortY");
public FloatParameter noiseX = new FloatParameter(this,1f,0f,1f,"noiseX");
public FloatParameter noiseY = new FloatParameter(this,1f,0f,1f,"noiseY");
public FloatParameter updateColor = new FloatParameter(this,1,0,1,"updateColor");
public FloatParameter updatePosition = new FloatParameter(this,1,0,1,"updatePosition");
public FloatParameter size = new FloatParameter(this,1f,0f,200f,FloatParameter.FREE,"size");
public FloatParameter number = new FloatParameter(this,0f,0f,maxNumPoint,"number");
public FloatParameter number2 = new FloatParameter(this,1f,1f,100,"number2");
public ListParameter shape = new ListParameter(this,1,new int[]{1,2,3,4},new String[]{"pt","line","tri","quad"},"shape");
public ListParameter incNoiseX = new ListParameter(this,0,ListParameter.onOff,ListParameter.onOffNames,"incNoiseX");
public ListParameter incNoiseY = new ListParameter(this,0,ListParameter.onOff,ListParameter.onOffNames,"incNoiseY");
public ListParameter incNoiseC = new ListParameter(this,0,ListParameter.onOff,ListParameter.onOffNames,"incNoiseC");
public ListParameter noiseMode = new ListParameter(this,0,new int[]{0,1,2},new String[]{"rand","perlin","simplex"},"noiseMode");
public ListParameter continous = new ListParameter(this,0,ListParameter.onOff,ListParameter.onOffNames,"continous");
long lastCol=0;
long lastPos=0;
float[]x=null;
float[]y=null;
public LayerNoise(Application parent) {
super(parent);
setParameterMap();
}
private static final PerlinNoise perlinNoise = new PerlinNoise();
/*private double noise(double min, double max, double x){
double value = 0;
if(this.noiseMode.v()==1){
value = perlinNoise.noise((float)x) * (max-min)-min;
}else if(this.noiseMode.v()==2){
value = SimplexNoise.noise(x, 0) * (max-min)-min;
}else{
value = parent.random((float)min,(float)max);
}
//System.out.println(value);
return(value);
}*/
private double noise(double min, double max, double x, double y){
double value = 0;
if(this.noiseMode.v()==1){
value = perlinNoise.noise((float)x,(float) y) * (max-min)+min;
}else if(this.noiseMode.v()==2){
value = SimplexNoise.noise(x, y)* (max-min)+min;
}else{
value = parent.random((float)min,(float)max);
}
//System.out.println(value);
return(value);
}
/*private double noise(double min, double max, double x, double y, double z){
if(this.noiseMode.v()==1){
return perlinNoise.noise((float)x,(float)y,(float)z) * (max-min)+min;
}else if(this.noiseMode.v()==2){
return SimplexNoise.noise(x, y, z) * (max-min)+min;
}else{
return parent.random((float)min,(float)max);
}
}*/
long startTime = System.currentTimeMillis();
@Override
public void draw(GL2 gl){
int numPoints = (int)number.v();
//allocate buffer
if(vbuffer==null){
int vSize = (maxNumPoint * 2);
int cSize = (maxNumPoint * 4);
vSize = vSize << 2;
cSize = cSize << 2;
vbuffer = ByteBuffer.allocateDirect(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
cbuffer = ByteBuffer.allocateDirect(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
x = new float[maxNumPoint];
y = new float[maxNumPoint];
}
//update post and colours
long now = System.currentTimeMillis();
boolean pos = false;
if(now-lastPos> (1-updatePosition.v())*1000 && updatePosition.v()>0f){
pos=true;
lastPos=now;
}
boolean col=false;
if(now-lastCol> (1-updateColor.v())*1000 && updateColor.v()>0f){
col=true;
lastCol=now;
}
//parameter values
float noiseX = this.noiseX.v();
float noiseY = this.noiseY.v();
boolean incNoiseX = this.incNoiseX.v()==1;
boolean incNoiseY = this.incNoiseY.v()==1;
float g = this.gradient.v();
float ag = 1-g;
boolean continous = this.continous.v()==1;
int adaptedNumber = (int)number.v();
if((int)adaptedNumber>number.max){
adaptedNumber=(int)number.max;
}
adaptedNumber /=this.number2.v();
float shape = this.shape.v();
adaptedNumber = (shape==2)?adaptedNumber/10: (shape==3)?adaptedNumber/500: (shape==4)?adaptedNumber/=2000: adaptedNumber-2;
long timeLength = 10000000;
long timeSpeed = 2000;
double speedRadio = 1d/adaptedNumber*100;
double time = (double)(System.currentTimeMillis()%timeLength)/(double)timeLength*timeSpeed;
for (int i = 0; i < adaptedNumber; i++) {
// random x,y
if(pos){
x[i]= (float)((incNoiseX&&i>0?x[i-1]:Config.WIDTH/2)
+ this.noise(-1f,1f,i*2*speedRadio,time) * Config.WIDTH *noiseX *
(incNoiseX&&i>0?1f/numPoints*1000:1f));
y[i]= (float)((incNoiseY&&i>0?y[i-1]:Config.HEIGHT/2) +
this.noise(-1f,1f,(i+adaptedNumber)*speedRadio,time) *Config.HEIGHT * noiseY *
(incNoiseY&&i>0?1f/numPoints*1000:1f));
}
// random r,g,b
if(col){
if(shape==2 && continous && i>0){
for(int j=0;j<2;j++){
if(i%2==0){
HSBtoRGB(noiseH.v(),noiseS.v(),noiseB.v(),fgH.v(),fgS.v(),fgB.v());
cbuffer.put(foregroundR/255f);
cbuffer.put(foregroundG/255f);
cbuffer.put(foregroundB/255f);
cbuffer.put(fgAlpha.v());
}else{
HSBtoRGB();
HSBtoRGB(noiseH.v(),noiseS.v(),noiseB.v(),fgH.v(),fgS.v(),fgB.v());
cbuffer.put(foregroundR/255f*ag+backgroundR*g);
cbuffer.put(foregroundG/255f*ag+backgroundG*g);
cbuffer.put(foregroundB/255f*ag+backgroundB*g);
cbuffer.put(fgAlpha.v());
}
}
}else{
if(i%2==0){
HSBtoRGB(noiseH.v(),noiseS.v(),noiseB.v(),fgH.v(),fgS.v(),fgB.v());
cbuffer.put(foregroundR/255f);
cbuffer.put(foregroundG/255f);
cbuffer.put(foregroundB/255f);
cbuffer.put(fgAlpha.v());
}else{
HSBtoRGB();
HSBtoRGB(noiseH.v(),noiseS.v(),noiseB.v(),fgH.v(),fgS.v(),fgB.v());
cbuffer.put(foregroundR/255f*ag+backgroundR*g);
cbuffer.put(foregroundG/255f*ag+backgroundG*g);
cbuffer.put(foregroundB/255f*ag+backgroundB*g);
cbuffer.put(fgAlpha.v());
}
}
}
}
for(int j=0;j<2;j++){
HSBtoRGB(noiseH.v(),noiseS.v(),noiseB.v(),fgH.v(),fgS.v(),fgB.v());
cbuffer.put(foregroundR/255f);
cbuffer.put(foregroundG/255f);
cbuffer.put(foregroundB/255f);
cbuffer.put(fgAlpha.v());
}
//sorting
if(pos){
int a=0;
int block=(int)(adaptedNumber*sortX.v()*sortX.v()*sortX.v()*sortX.v());
if(block>0){
while(a+block<adaptedNumber){
Arrays.sort(x,a,a+block);
a+=block;
}
Arrays.sort(x,a,adaptedNumber);
}
block=(int)(adaptedNumber*sortY.v()*sortY.v()*sortY.v()*sortY.v());
a=0;
if(block>0){
while(a+block<adaptedNumber){
Arrays.sort(y,a,a+block);
a+=block;
}
Arrays.sort(y,a,adaptedNumber);
}
}
//put sorted data in buffer
for (int i = 0; i < adaptedNumber; i++) {
if(pos){
if(shape==2 && continous && i>0){
vbuffer.put(x[i]);
vbuffer.put(y[i]);
}
vbuffer.put(x[i]);
vbuffer.put(y[i]);
}
}
//closing the loop for continous lines
if(pos){
if(shape==2 && continous && adaptedNumber>0){
vbuffer.put(x[0]);
vbuffer.put(y[0]);
}
}
vbuffer.rewind();
cbuffer.rewind();
//draw
float size = this.size.v();
if(size>=1f){
//use buffers
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL2.GL_FLOAT, 0, vbuffer);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL2.GL_FLOAT, 0, cbuffer);
//size
gl.glPointSize(size);
gl.glLineWidth(size);
//shapes
if(shape==1){
gl.glDrawArrays(GL2.GL_POINTS, 0, adaptedNumber);
}
if(shape==2){
gl.glDrawArrays(GL2.GL_LINES, 0, (continous && adaptedNumber>0)?adaptedNumber*2+1:adaptedNumber);
}
if(shape==3){
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, adaptedNumber);
}
if(shape==4){
gl.glDrawArrays(GL2.GL_QUADS, 0, adaptedNumber);
}
}
//gl.glPopMatrix();
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
}
public void HSBtoRGB(float nH,float nS, float nB,float h,float s, float b){
h = parent.random(nH)+h;
s = parent.random(nS)+s;
b = parent.random(nB)+b;
if(h>1)h=h-1;
if(h<1)h=h+1;
if(s>1)s=1;
if(b>1)b=1;
Color rgb = new Color(Color.HSBtoRGB(h,s,b));
foregroundR = rgb.getRed();
foregroundG = rgb.getGreen();
foregroundB = rgb.getBlue();
}
public void HSBtoRGB(){
Color rgb = new Color(Color.HSBtoRGB(fgH.v(),fgS.v(),fgB.v()));
foregroundR = rgb.getRed();
foregroundG = rgb.getGreen();
foregroundB = rgb.getBlue();
rgb = new Color(Color.HSBtoRGB(bgH.v(),bgS.v(),bgB.v()));
backgroundR = rgb.getRed();
backgroundG = rgb.getGreen();
backgroundB = rgb.getBlue();
}
@Override
public void drawBackground(GL2 gl) {
HSBtoRGB();
HSBtoRGB();
//GL2 gl = PJOGL.gl.getGL2().getGL2();
gl.glColor4f( backgroundR/255f, backgroundG/255f, backgroundB/255f,bgAlpha.v());
gl.getGL2().glBegin(GL2.GL_QUADS);
gl.getGL2().glVertex2f(0f,0f);
gl.getGL2().glVertex2f(Config.WIDTH,0f);
gl.getGL2().glVertex2f(Config.WIDTH,Config.HEIGHT);
gl.getGL2().glVertex2f(0f,Config.HEIGHT);
gl.getGL2().glEnd();
}
@Override
public int buildUI(GUIController controller, int x, int y, Tab tab, Group group) {
int h=controller.h;
int hMargin = controller.hMargin;
String layerType = this.getType();
int startY=y;
y=+super.buildUI(controller,x,y,tab,group);
y+=controller.addColorGroup(layerType,"Colour Noise","noise",x,y,tab,null);
y+=h;
controller.addControlFloatParameter(layerType,"size",0,40, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"number",0,10000, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"number2",1,100, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"updatePosition",0,1, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"updateColor",0,1, x, y, tab,null);
y+=h+hMargin+hMargin;
controller.addControlFloatParameter(layerType,"sortX",0,1, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"sortY",0,1, x, y, tab,null);
y+=h+hMargin+hMargin;
controller.addControlFloatParameter(layerType,"noiseX",0,1, x, y, tab,null);
y+=h+hMargin;
controller.addControlFloatParameter(layerType,"noiseY",0,1, x, y, tab,null);
x=670;
y=startY;
controller.addRadio(layerType, "shape.v",shape.names,x, y, tab, null);
y+=5*(h+hMargin);
controller.addToggle(layerType, "incNoiseX.v", false, x, y, tab, group).setCaptionLabel("inc noise x");
y+=h+hMargin;
controller.addToggle(layerType, "incNoiseY.v", false, x, y, tab, group).setCaptionLabel("inc noise y");
// y+=h+hMargin;
// controller.addToggle(layerType, "incNoiseC.v", false, x, y, tab, group);
y+=2*(h+hMargin);
controller.addToggle(layerType, "continous.v",false,x, y, tab, null).setCaptionLabel("continuous lines");
y+=2*(h+hMargin);
controller.addRadio(layerType, "noiseMode.v",noiseMode.names,x, y, tab, null);
return(y);
}
public void keyEvent(int key, int keyCode, char ch, boolean a, boolean c, boolean s,boolean win,boolean apl,long[] lastPressedTime) {
super.keyEvent(key, keyCode, ch, a, c, s, win, apl, lastPressedTime);
}
}