-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingularity.js
526 lines (434 loc) · 12.1 KB
/
singularity.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
let line_weight = 2;
let increment = 0.001;
let radial_steps = 512;
let mass_lower = 600.0;
let mass_upper = 1200.0;
let aper_lower = 100.0;
let aper_upper = 400.0;
let forc_lower = 550.0;
let forc_upper = 2250.0;
let turb_lower = 0.001;
let turb_upper = 1.000;
let chao_lower = 0.001;
let chao_upper = 0.002;
let deta_lower = 4.0;
let deta_upper = 10.0;
let data = [];
let buffer_size = 2400;
let res_multi = 0.0;
let line_color = 0;
let canvas;
let osb;
let context;
let noise;
let features;
function remap(n, start1, stop1, start2, stop2)
{
return ((n-start1)/(stop1-start1))*(stop2-start2)+start2;
}
function evaluate(n, metadata)
{
let meta =
{
'description': "",
'prob': 0
}
let points =
{
'form': 0,
'rare': 0
}
if(n==1.0)
{
meta.desciption = "absolute";
points.rare = 2;
points.form = 7;
meta.prob = 0.001;
}
else if(n==0.0)
{
meta.desciption = "void";
points.rare = 2;
points.form = 7;
meta.prob = 0.001;
}
else if(n <= 0.01)
{
meta.desciption = "minimal";
points.rare = 1;
points.form = 5;
meta.prob = 0.01;
}
else if (n > 0.01 && n < 0.1)
{
meta.desciption = "marginal";
points.form = 3;
meta.prob = 0.09;
}
else if (n > 0.1 && n < 0.25)
{
meta.desciption = "low";
points.form = 1;
meta.prob = 0.15;
}
else if (n > 0.99)
{
meta.desciption = "extreme";
points.rare = 1;
points.form = 5;
meta.prob = 0.01;
}
else if (n < 0.99 && n > 0.9)
{
meta.desciption = "super";
points.form = 3;
meta.prob = 0.09;
}
else if (n < 0.90 && n > 0.75)
{
meta.desciption = "high";
points.form = 1;
meta.prob = 0.15;
}
else
{
meta.desciption = "average";
meta.prob = 0.5;
}
return metadata ? meta : points;
}
function generate_artblocks_metadata(formdata)
{
let meta_mass = evaluate(formdata.mass, true);
let meta_force = evaluate(formdata.force, true);
let meta_symmetry = evaluate(formdata.symmetry, true);
let meta_turbulence = evaluate(formdata.turbulence, true);
let meta_chaos = evaluate(formdata.chaos, true);
let prob = meta_mass.prob * meta_force.prob * meta_symmetry.prob * meta_turbulence.prob * meta_chaos.prob;
let massstr = ("Mass: " + (formdata.mass * 100).toFixed(1) + "%") + " [" + meta_mass.desciption.toUpperCase() + "]";
let forcestr = ("Force: " + (formdata.force * 100).toFixed(1) + "%") + " [" + meta_force.desciption.toUpperCase() + "]";
let symstr = ("Symmetry: " + (formdata.symmetry * 100).toFixed(1) + "%") + " [" + meta_symmetry.desciption.toUpperCase() + "]";
let turbstr = ("Turbulence: " + (formdata.turbulence * 100).toFixed(1) + "%") + " [" + meta_turbulence.desciption.toUpperCase() + "]";
let chaosstr = ("Chaos: " + (formdata.chaos * 100).toFixed(1) + "%") + " [" + meta_chaos.desciption.toUpperCase() + "]";
let prostr = "Chance: 1 in " + Math.trunc((1.0/(prob)));
return [massstr, forcestr, symstr, turbstr, chaosstr, prostr];
}
const lerp_colour = function(a, b, amount)
{
const ar = a >> 16,
ag = a >> 8 & 0xff,
ab = a & 0xff,
br = b >> 16,
bg = b >> 8 & 0xff,
bb = b & 0xff,
rr = ar + amount * (br - ar),
rg = ag + amount * (bg - ag),
rb = ab + amount * (bb - ab);
return (rr << 16) + (rg << 8) + (rb | 0);
};
function three_point_gradient(x, start, mid, end)
{
return (x < 0.5) ? lerp_colour(start, mid, remap(x, 0.0, 0.5, 0.0, 1.0)):
lerp_colour(mid, end, remap(x, 0.5, 1.0, 0.0, 1.0));
}
function sq(number)
{
return Math.pow(number, 2);
}
function lerp (start, end, amt)
{
return (1-amt)*start+amt*end;
}
function process_formdata(hashdata)
{
let idx_mass = 1;
let idx_aperture = 2;
let idx_force = 3;
let idx_symmetry = 4;
let idx_turbulence = 5;
let idx_chaos = 6;
let idx_saturation = 7;
let idx_detail = 8;
let formdata =
{
'mass': hashdata[idx_mass],
'aperture': hashdata[idx_aperture],
'force': hashdata[idx_force],
'symmetry': hashdata[idx_symmetry],
'turbulence': hashdata[idx_turbulence],
'chaos': hashdata[idx_chaos],
'saturation': hashdata[idx_saturation],
'detail': hashdata[idx_detail]
};
return formdata;
}
function evaluate_points(fd)
{
let points_mass = evaluate(fd.mass, false);
let points_force = evaluate(fd.force, false);
let points_symmetry = evaluate(fd.symmetry, false);
let points_turbulence = evaluate(fd.turbulence, false);
let points_chaos = evaluate(fd.chaos, false);
let points =
{
'form': points_mass.form +
points_force.form +
points_symmetry.form +
points_turbulence.form +
points_chaos.form,
'rare': points_mass.rare +
points_force.rare +
points_symmetry.rare +
points_turbulence.rare +
points_chaos.rare
};
return points;
}
function generate_renderdata(fd)
{
let points = evaluate_points(fd);
let renderdata =
{
'mass': lerp(mass_lower, mass_upper, fd.mass),
'aperture': lerp(aper_lower, aper_upper, fd.aperture),
'force': lerp(forc_lower, forc_upper, fd.force),
'symmetry': 1.0-fd.symmetry,
'turbulence': lerp(turb_lower, turb_upper, fd.turbulence),
'chaos': lerp(chao_lower, chao_upper, fd.chaos),
'saturation': fd.saturation,
'form_points': points.form,
'rare_points': points.rare,
'detail': lerp(deta_lower, deta_upper, fd.detail)
};
return renderdata;
}
function process_hash(txn)
{
let hash_index = 0;
for (let i = 2; i < 65; i += 2)
{
let from = i;
let to = i + 2;
let s = txn.substring(from, to);
data[hash_index] = parseInt(s, 16) / 255.0;
hash_index++;
}
return data;
}
function init(txn)
{
let dim = Math.min(window.innerWidth, window.innerHeight)
res_multi = dim / buffer_size;
canvas = document.querySelector("canvas");
can_context = canvas.getContext("2d");
can_context.imageSmoothingEnabled = true;
can_context.imageSmoothingQuality = "high";
canvas.width = dim;
canvas.height = dim;
can_context.fillStyle = '#000000';
can_context.fillRect(0, 0, dim, dim);
can_context.lineWidth = line_weight * res_multi;
line_color = 0xfffad7;
let hashdata = process_hash(txn);
let formdata = process_formdata(hashdata);
let renderdata = generate_renderdata(formdata);
render(renderdata);
let ab_metadata = generate_artblocks_metadata(formdata);
return ab_metadata;
}
function render(rd)
{
noise = new Noise().noiseDetail(rd.detail);
noise.noiseSeed(4);
for (let i = 0; i < rd.mass; i++)
{
let norm_inc = sq(i / rd.mass);
let ring_rad = rd.aperture + (i * increment);
let current_force = rd.force * norm_inc;
let alpha = parseInt((255.0 - ((norm_inc) * 255.0)));
let norm_turb = rd.turbulence * norm_inc;
let g, start, middle, end, sat;
switch(rd.rare_points)
{
case 0:
start = 0xffad77;
mid = 0xf91362;
end = 0x35126a;
break;
case 1:
start = 0xcffff0;
mid = 0x6096db;
end = 0x20fbbc;
break;
case 2:
start = 0x73d055;
mid = 0x1f968B;
end = 0x440154;
break;
default:
start = 0x12d6df;
mid = 0xb9ffad;
end = 0xf70fff;
break;
}
g = three_point_gradient(norm_inc, start, mid, end);
if (rd.form_points == 0)
{
sat = 0.0;
} else if (rd.form_points > 0 && rd.form_points < 7)
{
sat = lerp(0.0, 0.25, rd.saturation);
} else if (rd.form_points >= 7 && rd.form_points < 9)
{
sat = lerp(0.2, 0.75, rd.saturation);
} else if (rd.form_points >= 9 && rd.form_points < 10)
{
sat = lerp(0.75, 0.9, rd.saturation);
} else if (rd.form_points >= 10 && rd.form_points < 11)
{
sat = lerp(0.9, 1.0, rd.saturation);
} else
{
sat = 1.0;
}
let col = lerp_colour(line_color, g, sat);
let ang = (Math.PI * 2.0) / radial_steps;
can_context.beginPath();
for (let j = 0; j <= radial_steps; j++)
{
let theta = ang * j;
let ct = Math.cos(theta);
let st = Math.sin(theta);
let sample_x = ct + rd.symmetry;
let sample_y = st + rd.symmetry;
let ken = get_noise(norm_turb * sample_x, norm_turb * sample_y, (i * rd.chaos));
let current_aperture = ring_rad + ken * current_force;
let x = (canvas.width/2) + ((current_aperture * ct) * res_multi);
let y = (canvas.height/2) + ((current_aperture * st) * res_multi);
can_context.lineTo(x, y);
}
can_context.strokeStyle = '#' + col.toString(16) + alpha.toString(16);
can_context.stroke();
}
}
function get_noise(x, y, z)
{
var v = noise.get(x, y, z);
return v;
}
/*
Rune Madsen's Noise
https://github.com/runemadsen/rune.noise.js/blob/master/src/noise.js
*/
var PERLIN_YWRAPB = 4;
var PERLIN_YWRAP = 1<<PERLIN_YWRAPB;
var PERLIN_ZWRAPB = 8;
var PERLIN_ZWRAP = 1<<PERLIN_ZWRAPB;
var PERLIN_SIZE = 4095;
var SINCOS_PRECISION = 0.5;
var SINCOS_LENGTH = Math.floor(360 / SINCOS_PRECISION);
var sinLUT = new Array(SINCOS_LENGTH);
var cosLUT = new Array(SINCOS_LENGTH);
var DEG_TO_RAD = Math.PI/180.0;
for (var i = 0; i < SINCOS_LENGTH; i++)
{
sinLUT[i] = Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
cosLUT[i] = Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
}
var perlin_PI = SINCOS_LENGTH;
perlin_PI >>= 1;
var Noise = function()
{
this.perlin_octaves = 4;
this.perlin_amp_falloff = 0.5;
this.perlin = null;
}
Noise.prototype = {
noiseDetail: function(lod, falloff) {
if (lod>0) { this.perlin_octaves = lod; }
if (falloff>0) { this.perlin_amp_falloff = falloff; }
return this;
},
noiseSeed: function(seed)
{
var lcg = (function() {
var m = 4294967296,
a = 1664525,
c = 1013904223,
seed, z;
return {
setSeed : function(val) {
z = seed = (val == null ? Math.random() * m : val) >>> 0;
},
getSeed : function() {
return seed;
},
rand : function() {
z = (a * z + c) % m;
return z / m;
}
};
}());
lcg.setSeed(seed);
this.perlin = new Array(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
this.perlin[i] = lcg.rand();
}
return this;
},
get: function(x,y,z) {
y = y || 0;
z = z || 0;
if(this.perlin == null) {
this.perlin = new Array(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
this.perlin[i] = Math.random();
}
}
if (x<0) { x=-x; }
if (y<0) { y=-y; }
if (z<0) { z=-z; }
var xi=Math.floor(x), yi=Math.floor(y), zi=Math.floor(z);
var xf = x - xi;
var yf = y - yi;
var zf = z - zi;
var rxf, ryf;
var r=0;
var ampl=0.5;
var n1,n2,n3;
var noise_fsc = function(i)
{
return 0.5*(1.0-cosLUT[Math.floor(i*perlin_PI)%SINCOS_LENGTH]);
};
for (var o=0; o<this.perlin_octaves; o++) {
var of=xi+(yi<<PERLIN_YWRAPB)+(zi<<PERLIN_ZWRAPB);
rxf= noise_fsc(xf);
ryf= noise_fsc(yf);
n1 = this.perlin[of&PERLIN_SIZE];
n1 += rxf*(this.perlin[(of+1)&PERLIN_SIZE]-n1);
n2 = this.perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE];
n2 += rxf*(this.perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n2);
n1 += ryf*(n2-n1);
of += PERLIN_ZWRAP;
n2 = this.perlin[of&PERLIN_SIZE];
n2 += rxf*(this.perlin[(of+1)&PERLIN_SIZE]-n2);
n3 = this.perlin[(of+PERLIN_YWRAP)&PERLIN_SIZE];
n3 += rxf*(this.perlin[(of+PERLIN_YWRAP+1)&PERLIN_SIZE]-n3);
n2 += ryf*(n3-n2);
n1 += noise_fsc(zf)*(n2-n1);
r += n1*ampl;
ampl *= this.perlin_amp_falloff;
xi<<=1;
xf*=2;
yi<<=1;
yf*=2;
zi<<=1;
zf*=2;
if (xf>=1.0) { xi++; xf--; }
if (yf>=1.0) { yi++; yf--; }
if (zf>=1.0) { zi++; zf--; }
}
return r;
}
}
features = init(tokenData.hash);