-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap.js
728 lines (620 loc) · 20.4 KB
/
nmap.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
class vec2 {
x;
y;
constructor(x,y){
this.x = x;
this.y = y;
}
sw(s = "xy"){
let arr = []
arr.length = 2
for(let i=0; i<2; i++){
if (s[i] == "x"){ arr[i] = this.x }
else { arr[i] = this.y }
}
return new vec2(arr[0],arr[1])
}
sw3(s = "xyy"){
let arr = []
arr.length = 3
for(let i=0; i<3; i++){
if (s[i] == "x"){ arr[i] = this.x }
else { arr[i] = this.y }
}
return new vec3(arr[0],arr[1],arr[2])
}
add(b){
return new vec2(
this.x + b.x,
this.y + b.y
)
}
sub(b){
return new vec2(
this.x - b.x,
this.y - b.y
)
}
mul(b){
return new vec2(
this.x * b.x,
this.y * b.y
)
}
dot(b){ return this.x * b.x + this.y * b.y; }
// Modulo operator
mod(b){
// console.log("a = ", this, "b =", b)
return new vec2(
( (this.x % b.x) + b.x ) % b.x,
( (this.y % b.y) + b.y ) % b.y
);
}
// Remainder operator
rem(b) { return new vec2( this.x % b.x, this.y % b.y ); }
fract() { return this.mod( new vec2(1.0,1.0) ); }
floor() { return new vec2( Math.floor(this.x), Math.floor(this.y) ); }
ceil() { return new vec2( Math.ceil(this.x), Math.ceil(this.y) ); }
sin() { return new vec2( Math.sin(this.x), Math.sin(this.y) ); }
hash2(){
let p3 = this.sw3("xyx").mul( new vec3(.1031, .1030, .0973) ).fract();
let a = p3.dot( p3.sw3("yzx").add( new vec3( 33.33 ) ) );
p3 = p3.add( new vec3(a) );
return p3.sw2("xx").add( p3.sw2("yz") ).mul( p3.sw2("zy") ).fract();
}
hash3(){
let p3 = this.sw3("xyx").mul( new vec3(.1031, .1030, .0973) ).fract();
let a = p3.dot( p3.sw3("yxz").add( new vec3( 33.33 ) ) );
p3 = p3.add( new vec3(a) );
return p3.sw3("xxy").add( p3.sw3("yzz") ).mul( p3.sw3("zyx") ).fract();
}
}
class vec3 {
x;
y;
z;
constructor(){
switch (arguments.length){
case 1:
this.x = arguments[0];
this.y = arguments[0];
this.z = arguments[0];
break;
case 3:
this.x = arguments[0];
this.y = arguments[1];
this.z = arguments[2];
break;
}
}
sw3(s = "xyy"){
let arr = []
arr.length = 3
for(let i=0; i<3; i++){
if (s[i] == "x") { arr[i] = this.x }
else if (s[i] == "y") { arr[i] = this.y }
else { arr[i] = this.z }
}
return new vec3(arr[0],arr[1],arr[2])
}
sw2(s = "xy"){
let arr = []
arr.length = 2
for(let i=0; i<2; i++){
if (s[i] == "x") { arr[i] = this.x }
else if (s[i] == "y") { arr[i] = this.y }
else { arr[i] = this.z }
}
return new vec2(arr[0],arr[1])
}
add(b){
return new vec3(
this.x + b.x,
this.y + b.y,
this.z + b.z
)
}
sub(b){
return new vec3(
this.x - b.x,
this.y - b.y,
this.z - b.z
)
}
mul(b){
return new vec3(
this.x * b.x,
this.y * b.y,
this.z * b.z
)
}
dot(b){ return this.x * b.x + this.y * b.y + this.z * b.z; }
// Modulo operator
mod(b){
return new vec3(
( (this.x % b.x) + b.x ) % b.x,
( (this.y % b.y) + b.y ) % b.y,
( (this.z % b.z) + b.z ) % b.z
);
}
// Remainder operator
rem(b) {
return new vec3(
this.x % b.x,
this.y % b.y,
this.z % b.z
);
}
fract() { return this.mod( new vec3(1.0) ); }
floor() { return new vec3( Math.floor(this.x), Math.floor(this.y), Math.floor(this.z) ); }
ceil() { return new vec3( Math.ceil(this.x), Math.ceil(this.y), Math.ceil(this.z) ); }
sin() { return new vec3( Math.sin(this.x), Math.sin(this.y), Math.sin(this.z) ); }
sqrt() { return new vec3( Math.sqrt(this.x), Math.sqrt(this.y), Math.sqrt(this.z) ); }
}
var didYouKnowEl = document.getElementById("didukno")
var facts = [
"Mali GPUs are made from gluing Pentiums with the FDIV bug together",
"GPUs do not have infinite precision. <i>CPUs do</i>",
"Gaming laptops have integrated handwarmers on the keyboard",
"This would be going faster if I built it in WASM",
"Ken Perlin probably had no clue what he was doing, given how complicated his noise is",
"You should hydrate",
"They call it the oven because you of in the cold food of out hot eat the food",
"Anisotropic mipmaps weren't officially in OpenGL until 4.0 because it was too powerful",
"Unity has more features than an aircraft",
"Inigo Quilez created GLSL. To fuel his god complex",
"Using trigonometry functions in code is a sign you've lost someone close to you and are trying to cope",
"WebGL renders scenes in sRGB. Or linear color. Nope, probably sRGB. Eh, maybe lin...",
"Violence isn't the answer to this one.",
"Gaming",
"Live-update rendering was added on 2022-03-11.<br>Before that, you had to wait in silence while your browser froze.",
"This tool was created because I wanted a texture generator, but was too lazy to learn desktop OpenGL",
"All output images from this tool are 8-bit color.",
"Programming socks: they make a difference.",
"Shadertoy is good competition.",
]
function showDidYouKnow(){
didYouKnowEl.style.display = "block"
let picker = Math.floor(Math.random()*facts.length)
didYouKnowEl.innerHTML = "Did you know? " + facts[picker]
}
function hideDidYouKnow(){
didYouKnowEl.style.display = "none"
}
var default_vert = `
attribute vec4 position;
// attribute vec2 texcoord0;
varying vec2 uv;
void main(){
gl_Position = position;
uv = (position.xy+1.0)*0.5;
// uv = texcoord0;
}
`
var default_frag = `
precision mediump float;
varying vec2 uv;
void main(){
gl_FragColor.rgba = vec4(uv,0.0,1.0);
}
`
var canvasGL = document.getElementById("glview")
var canvasJSK = document.getElementById("jskview")
var gl = canvasGL.getContext("webgl",{preserveDrawingBuffer:true})
var ctx = canvasJSK.getContext('2d')
var vertexShader = createShader(gl,gl.VERTEX_SHADER,default_vert)
var fragmentShader = createShader(gl,gl.FRAGMENT_SHADER,default_frag)
var program = createProgram(gl, vertexShader, fragmentShader);
gl.useProgram( program );
var posAttribLocation = gl.getAttribLocation(program, "position");
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
var positions = [
-1, -3,
-1, 1,
3, 1,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.enableVertexAttribArray(posAttribLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.vertexAttribPointer(posAttribLocation, 2, gl.FLOAT, false, 0, 0);
function createShader(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success) {
return shader;
}
console.log(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
}
function createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success) {
return program;
}
console.log(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
}
let resMul = 1
function showUniformElementIf(uniforms, this_uniform, this_div) {
let showdef = this_uniform["showIf"]
let uniToCheck = uniforms[ showdef["uniform"] ]
let valueToCompare = showdef["value"]
let showOrHide = function(){
let res = "none"
if (arguments[0] == true)
{ res = "block" }
this_div.style.display = res
}
let showIfFunc
switch ( showdef["op"] )
{
case ">":
showIfFunc = function(){
return showOrHide(uniToCheck.value > valueToCompare)
}
break;
default: // Assume you want Equals operator
showIfFunc = function(){
return showOrHide(uniToCheck.value == valueToCompare)
}
break;
}
showIfFunc()
uniToCheck.element.addEventListener("change",showIfFunc)
}
function setupUniforms(uniforms) {
// Clear uniform elements already present
var uniDiv = document.getElementById("uni")
uniDiv.innerHTML = ""
for (const key in uniforms){
let this_uniform = uniforms[key]
// New element for uniform
let newInput
let myDiv = document.createElement("div")
let mySibling
let changeFunc
let inputFunc
// Special styling uniforms with reserved keywords
let isStylingChange = false
switch(key) {
case "_newline":
uniDiv.appendChild( document.createElement("br") )
continue
case "_label":
myDiv.innerHTML = this_uniform.text
myDiv.style.opacity = 0.5
showUniformElementIf(uniforms,this_uniform,myDiv)
uniDiv.appendChild( myDiv )
continue
}
switch(this_uniform["type"]){
// Boolean checkbox
case "checkbox":
newInput = document.createElement("input")
newInput.type = "checkbox"
changeFunc = function() {
this_uniform.value = this.checked
}
break;
// Optionbox (represented internally as an integer starting at 0)
case "intOptions":
newInput = document.createElement("select")
for (const opt in this_uniform.options){
let name = this_uniform.options[opt]
let optEl = document.createElement("option")
optEl.innerHTML = name
newInput.appendChild(optEl)
}
changeFunc = function(){
this_uniform.value = this.selectedIndex
}
break;
// Float spinbox
case "floatEntry":
case "intEntry":
newInput = document.createElement("input")
newInput.type = "number"
if (this_uniform.type == "intEntry") {
newInput.step = 1;
} else if (this_uniform.step) {
newInput.step = this_uniform.step;
} else {
newInput.step = 0.1
}
newInput.min = this_uniform.min
newInput.max = this_uniform.max
changeFunc = function(){
this_uniform.value = this.value
}
break;
// Number slider
case "floatSlider":
case "intSlider":
newInput = document.createElement("input")
newInput.type = "range"
let numberDisplay = document.createElement("span")
mySibling = numberDisplay
if (this_uniform.type == "intSlider") {
newInput.step = 1;
} else if (this_uniform.step) {
newInput.step = this_uniform.step;
} else {
newInput.step = 0.1
}
newInput.min = this_uniform.min;
newInput.max = this_uniform.max;
inputFunc = function(){
this_uniform.value = this.value;
numberDisplay.innerHTML = String( this.value );
renderGL();
}
if (this_uniform.isSliceCount) {
inputFunc = function(){
resMul = this.value;
this_uniform.value = this.value;
numberDisplay.innerHTML = String( this.value );
renderGL();
}
}
break;
}
this_uniform.element = newInput
if(this_uniform.default){
console.log(this_uniform.default)
newInput.value = this_uniform.default
}
if(this_uniform["showIf"]){
showUniformElementIf(uniforms,this_uniform,myDiv)
}
myDiv.appendChild(document.createTextNode(this_uniform.ui_name +": "))
myDiv.appendChild(newInput)
if (mySibling) {
myDiv.appendChild(mySibling)
}
myDiv.appendChild(document.createElement("br"))
uniDiv.appendChild(myDiv)
newInput.addEventListener("change",changeFunc)
newInput.addEventListener("input",inputFunc)
// Force set default values to GL uniforms
newInput.dispatchEvent(new Event("change"))
newInput.dispatchEvent(new Event("input"))
newInput.addEventListener("change",render)
}
}
function updateUniformsGL() {
if (!current_kernel) {
console.log("KERNEL UNDEFINED")
return
}
for (const key in current_kernel.uniforms) {
let uniform = current_kernel.uniforms[key]
switch(uniform["type"]) {
case "intEntry":
case "intSlider":
case "intOptions":
case "checkbox":
gl.uniform1i(uniform.uniloc,uniform.value)
break;
case "floatEntry":
case "floatSlider":
gl.uniform1f(uniform.uniloc,uniform.value)
break;
}
}
}
function setupUniformLocationsGL(uniforms,prog) {
for (const key in uniforms){
uniforms[key].uniloc = gl.getUniformLocation(prog,key)
}
}
function useShader(shadertext,uniforms){
fragmentShader = createShader(gl,gl.FRAGMENT_SHADER,shadertext)
let newProgram = createProgram(gl, vertexShader, fragmentShader)
if(newProgram){
program = newProgram
gl.useProgram( program );
setupUniformLocationsGL(uniforms,program)
}
}
var _render_id = 0
function renderGL() {
_render_id = -1
setCanvasResolution(canvasGL)
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
updateUniformsGL()
gl.drawArrays(gl.TRIANGLES, 0, 3);
setTimeout(updateBackground,100)
}
function renderJS() {
setCanvasResolution(canvasJSK)
if (!current_kernel.jsShader) {
didYouKnowEl.innerHTML = "No JS function defined for this kernel.<br>(see /voronoi.js for an example kernel with a JS implementation)"
return
}
let res = new vec2(1.0/canvasJSK.width,1.0/canvasJSK.height)
let img = ctx.createImageData(canvasJSK.width, 1);
let cur_y = 0
let drawLine = function() {
let offset = 0
for ( var x=0; x<canvasJSK.width; x++ ) {
let p = new vec2(x,cur_y)
let col = current_kernel.jsShader(ctx,p,p.mul(res),current_kernel.uniforms)
img.data[offset++] = col[0]
img.data[offset++] = col[1]
img.data[offset++] = col[2]
img.data[offset++] = col[3]
}
}
_render_id += 1
let _this_renderid = _render_id
let _last_now = (new Date).getTime()
let scanline = function() {
if ( _render_id != _this_renderid ) {
return;
}
var now = (new Date).getTime()
drawLine()
ctx.putImageData(img,0,cur_y)
cur_y++
if (cur_y < canvasJSK.height) {
if ( now - _last_now > 100 ) {
_last_now = now
setTimeout(scanline,0)
} else {
scanline();
}
} else {
hideDidYouKnow();
updateBackground();
}
}
showDidYouKnow();
scanline();
}
function render() {
if (rendererSelector.selectedIndex == 0) { renderGL() }
else { renderJS() }
}
function getCanvas() {
if (rendererSelector.selectedIndex == 0) {
return canvasGL;
}
else {
return canvasJSK;
}
}
var bgEl = document.getElementById("bg")
function updateBackground(){
let c = getCanvas()
let imag = c.toDataURL('image/png')
bgEl.style.backgroundImage = 'url('+ imag + ')'
positionBackground()
}
function positionBackground(){
let c = getCanvas()
let rect = c.getBoundingClientRect()
bgEl.style.backgroundPositionX = rect.x + "px"
bgEl.style.backgroundPositionY = rect.y + "px"
}
function showBackground(show) {
bgEl.style.opacity = 1.0
}
function hideBackground(show) {
bgEl.style.opacity = 0.0
}
canvasJSK.addEventListener("mouseenter",showBackground)
canvasJSK.addEventListener("mouseleave",hideBackground)
canvasGL.addEventListener("mouseenter",showBackground)
canvasGL.addEventListener("mouseleave",hideBackground)
var image_size_x = document.getElementById("imgsizeX")
var image_size_y = document.getElementById("imgsizeY")
var img_square_box = document.getElementById("squareXYcheckbox")
img_square_box.addEventListener("change",function(){
image_size_y.disabled = img_square_box.checked
if ( image_size_y.disabled ) {
image_size_y.value = image_size_x.value
}
image_size_y.dispatchEvent(new Event("change"))
})
function setCanvasResolution(c) {
c.width = image_size_x.value * resMul
c.height = image_size_y.value * resMul
}
function clampSize(self){
self.value = Math.min(self.max,self.value)
self.value = Math.max(0.0, self.value)
}
image_size_x.addEventListener("change",function(){
if (image_size_y.disabled) {
image_size_y.value = image_size_x.value
image_size_y.dispatchEvent(new Event("change"))
}
clampSize(this)
render()
})
image_size_y.addEventListener("change",function(){
clampSize(this)
render()
})
function setKernel(kernel){
current_kernel = kernel
console.log(kernel)
setupUniforms(kernel.uniforms)
useShader(current_kernel.glslShader,current_kernel.uniforms)
}
var shaderSelector = document.getElementById("shaderSelector")
shaderSelector.onchange = function(){
setKernel(kernels[this.selectedIndex])
render()
}
function updateShaderSelector() {
shaderSelector.innerHTML = ""
for (const i in kernels){
let newOpt = document.createElement("option")
newOpt.innerHTML = kernels[i].name
shaderSelector.appendChild(newOpt)
}
setKernel(kernels[0])
useShader(current_kernel.glslShader,current_kernel.uniforms)
render()
}
var default_kernels = [
"voronoi.js",
"fBm.js"
]
var kernels = []
var current_kernel = null
function loadKernelfromFile(file){
fetch(file)
.then(response => response.text())
.then(function(text){
var newScript = document.createElement("script")
newScript.innerHTML = text
document.body.appendChild(newScript)
})
}
function addKernel(kernel){
kernels.push(kernel)
updateShaderSelector()
}
for (const key in default_kernels){
loadKernelfromFile(default_kernels[key])
}
var dl = document.getElementById("dl")
dl.onmousedown = function(i){
let canvas;
if (rendererSelector.selectedIndex == 0) {
canvas = canvasGL;
} else { canvas = canvasJSK; }
var a = document.createElement('a')
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream")
a.href = image
var faux_hash = Math.floor(Math.random()*Date.now()*0.01)
a.download = "urnoise"+ faux_hash +".png"
document.body.appendChild(a)
a.click()
}
function setRenderer(rendererIndex) {
if (rendererIndex == 0) {
canvasJSK.style.display = "none"
canvasGL.style.display = "block"
} else {
canvasGL.style.display = "none"
canvasJSK.style.display = "block"
}
}
var rendererSelector = document.getElementById("rendererSelector")
rendererSelector.onchange = function(){
setRenderer(this.selectedIndex)
render()
}
setRenderer(rendererSelector.selectedIndex)