Revision 3002bef3
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/type/Dynamic3D.java | ||
---|---|---|
45 | 45 |
float x,y,z; |
46 | 46 |
float vx,vy,vz; |
47 | 47 |
} |
48 |
|
|
49 |
private class VectorNoise |
|
50 |
{ |
|
51 |
float[] nx; |
|
52 |
float[] ny; |
|
53 |
float[] nz; |
|
54 |
|
|
55 |
VectorNoise() |
|
56 |
{ |
|
57 |
nx = new float[NUM_NOISE]; |
|
58 |
nx[0] = mRnd.nextFloat(); |
|
59 |
for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1]+mRnd.nextFloat(); |
|
60 |
float sum = nx[NUM_NOISE-1] + mRnd.nextFloat(); |
|
61 |
for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum; |
|
62 |
|
|
63 |
ny = new float[NUM_NOISE]; |
|
64 |
for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f; |
|
65 |
|
|
66 |
nz = new float[NUM_NOISE]; |
|
67 |
for(int i=0; i<NUM_NOISE; i++) nz[i] = mRnd.nextFloat()-0.5f; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
48 |
|
|
71 | 49 |
private Vector<VectorCache> vc; |
72 | 50 |
private VectorCache tmp1, tmp2; |
73 | 51 |
|
74 | 52 |
private Vector<Static3D> vv; |
75 | 53 |
private Static3D prev, curr, next; |
76 |
|
|
77 |
private Vector<VectorNoise> vn; |
|
78 |
private VectorNoise tmpN; |
|
79 |
|
|
80 |
private float mFactor1, mFactor2; // used in Noise only. Those are noise factors; 1=noise of the (vec1X,vec1Y,vec1Z) vector; 2=noise of (vec2X,vec2Y,vec2Z) |
|
54 |
|
|
81 | 55 |
private float vec1X,vec1Y,vec1Z; // vector perpendicular to v(t) and in the same plane as v(t) and a(t) (for >2 points only, in case of 2 points this is calculated differently) |
82 | 56 |
private float vec2X,vec2Y,vec2Z; // vector perpendicular to v(t0 and to vec1. |
83 |
|
|
84 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
85 | 57 |
|
86 |
synchronized void createNoise() |
|
87 |
{ |
|
88 |
if( vn==null ) |
|
89 |
{ |
|
90 |
vn = new Vector<>(); |
|
91 |
for(int i=0; i<numPoints; i++) vn.add(new VectorNoise()); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 | 58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
96 | 59 |
// no array bounds checking! |
97 | 60 |
|
... | ... | |
219 | 182 |
|
220 | 183 |
cacheDirty = false; |
221 | 184 |
} |
222 |
|
|
223 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
224 | 185 |
|
225 |
private float noise(float time,int vecNum) |
|
226 |
{ |
|
227 |
float lower, upper, len; |
|
228 |
float d = time*(NUM_NOISE+1); |
|
229 |
int index = (int)d; |
|
230 |
if( index>=NUM_NOISE+1 ) index=NUM_NOISE; |
|
231 |
tmpN = vn.elementAt(vecNum); |
|
232 |
|
|
233 |
float t = d-index; |
|
234 |
t = t*t*(3-2*t); |
|
235 |
|
|
236 |
switch(index) |
|
237 |
{ |
|
238 |
case 0 : mFactor1 = mNoise*tmpN.ny[0]*t; |
|
239 |
mFactor2 = mNoise*tmpN.nz[0]*t; |
|
240 |
return time + mNoise*(d*tmpN.nx[0]-time); |
|
241 |
case NUM_NOISE: mFactor1= mNoise*tmpN.ny[NUM_NOISE-1]*(1-t); |
|
242 |
mFactor2= mNoise*tmpN.nz[NUM_NOISE-1]*(1-t); |
|
243 |
len = ((float)NUM_NOISE)/(NUM_NOISE+1); |
|
244 |
lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len); |
|
245 |
return (1.0f-lower)*(d-NUM_NOISE) + lower; |
|
246 |
default : float ya,yb; |
|
247 |
yb = tmpN.ny[index ]; |
|
248 |
ya = tmpN.ny[index-1]; |
|
249 |
mFactor1 = mNoise*((yb-ya)*t+ya); |
|
250 |
yb = tmpN.nz[index ]; |
|
251 |
ya = tmpN.nz[index-1]; |
|
252 |
mFactor2 = mNoise*((yb-ya)*t+ya); |
|
253 |
|
|
254 |
len = ((float)index)/(NUM_NOISE+1); |
|
255 |
lower = len + mNoise*(tmpN.nx[index-1]-len); |
|
256 |
len = ((float)index+1)/(NUM_NOISE+1); |
|
257 |
upper = len + mNoise*(tmpN.nx[index ]-len); |
|
258 |
|
|
259 |
return (upper-lower)*(d-index) + lower; |
|
260 |
} |
|
261 |
} |
|
262 |
|
|
263 | 186 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
264 | 187 |
// v is the speed vector (i.e. position p(t) differentiated by time) |
265 | 188 |
// a is the acceleration vector (differentiate once more) |
... | ... | |
350 | 273 |
*/ |
351 | 274 |
public Dynamic3D() |
352 | 275 |
{ |
353 |
vv = new Vector<>(); |
|
354 |
vc = new Vector<>(); |
|
355 |
vn = null; |
|
356 |
numPoints = 0; |
|
357 |
cacheDirty = false; |
|
358 |
mMode = MODE_LOOP; |
|
359 |
mDuration = 0; |
|
360 |
mCount = 0.5f; |
|
361 |
mNoise = 0.0f; |
|
276 |
this(0,0.5f); |
|
362 | 277 |
} |
363 | 278 |
|
364 | 279 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
381 | 296 |
mMode = MODE_LOOP; |
382 | 297 |
mDuration = duration; |
383 | 298 |
mCount = count; |
299 |
mDimension = 3; |
|
384 | 300 |
} |
385 | 301 |
|
386 | 302 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
434 | 350 |
{ |
435 | 351 |
vv.add(v); |
436 | 352 |
|
437 |
if( vn!=null ) vn.add(new VectorNoise()); |
|
353 |
if( vn!=null ) vn.add(new VectorNoise(3));
|
|
438 | 354 |
|
439 | 355 |
switch(numPoints) |
440 | 356 |
{ |
... | ... | |
466 | 382 |
{ |
467 | 383 |
vv.add(location, v); |
468 | 384 |
|
469 |
if( vn!=null ) vn.add(new VectorNoise()); |
|
385 |
if( vn!=null ) vn.add(new VectorNoise(3));
|
|
470 | 386 |
|
471 | 387 |
switch(numPoints) |
472 | 388 |
{ |
... | ... | |
609 | 525 |
{ |
610 | 526 |
time = noise(time,0); |
611 | 527 |
|
612 |
buffer[offset ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor1 + vec2X*mFactor2);
|
|
613 |
buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor1 + vec2Y*mFactor2);
|
|
614 |
buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor1 + vec2Z*mFactor2);
|
|
528 |
buffer[offset ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor[0] + vec2X*mFactor[1]);
|
|
529 |
buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor[0] + vec2Y*mFactor[1]);
|
|
530 |
buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor[0] + vec2Z*mFactor[1]);
|
|
615 | 531 |
} |
616 | 532 |
else |
617 | 533 |
{ |
... | ... | |
670 | 586 |
|
671 | 587 |
setUpVectors(time,tmp1); |
672 | 588 |
|
673 |
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor1 + vec2X*mFactor2);
|
|
674 |
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor1 + vec2Y*mFactor2);
|
|
675 |
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor1 + vec2Z*mFactor2);
|
|
589 |
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor[0] + vec2X*mFactor[1]);
|
|
590 |
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor[0] + vec2Y*mFactor[1]);
|
|
591 |
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor[0] + vec2Z*mFactor[1]);
|
|
676 | 592 |
} |
677 | 593 |
else |
678 | 594 |
{ |
Also available in: Unified diff
Move most of the NOISE complications from DynamicND classes to the parent Dynamic class.