Source / code snippets | | | | ![Sven Bader: 07/31/23](.././../../i/a/71.gif) Sven Bader | ![](https://xprofan.net/files/ncenu.png)
this example created Gradientrauschen to Ken Perlin. I experimentiere straight with the Generierung of worlds in the Style of Minecraft where this Algorithmus very helpful is.
one can Skalierung and Appearance discretionary adjust and through "Oktaven", the Überlagerungen the Musters in different Auflösungen, fotorealistische clouds or others Textures produce.
with the each equal Permutationstabelle (here by "Seed" steuerbar) becomes one always the same Result get and can so each discretionary far entfernten point of/ one world to determine, without it Save To must. with one abweichenden Seed sustain one one new, zufälliges pattern.
the Program terminable itself the Nötigste but To herumexperimentieren reicht it.
![](https://xprofan.net/files/ecenu.png)
Def GetBitmapBits(3) !"gdi32","GetBitmapBits"
Def SetDIBitsToDevice(12) !"gdi32","SetDIBitsToDevice"
Proc perlinPrepare
Declare i&, used&[permutSize&], val&
WhileLoop 0, permutSize&-1
i& = &loop
While 1
val& = int((Rnd(10001) * 0.0001) * permutSize&)
If (used&[val&] = 0)
p&[i&] = val&
p&[i& + permutSize&] = val&
used&[val&] = 1
Break
EndIf
EndWhile
EndWhile
ENDPROC
Proc perlinGenerate
Parameters worldsize&, scale!, octaves&, persistence!
Declare pixels#,bmi#,size&
Dim pixels#,worldSize&*worldSize&*24
'Bitmap Header, unfortunately necessary the unhandliche thing, though The Info really any there are
Def &BI_RGB 0
Def &DIB_RGB_COLORS 0
Struct BITMAPINFOHEADER = biSize&, biWidth&, biHeight&, biPlanes%, biBitCount%, biCompression&, biSizeImage&, biXPelsPerMeter&, biYPelsPerMeter&, biClrUsed&, biClrImportant&
Dim bmi#,BITMAPINFOHEADER
Clear bmi#
With bmi#
.biSize& = sizeof(bmi#)
.biWidth& = worldSize&
.biHeight& = worldSize&
.biPlanes% = 1
.biBitCount% = 32
.biCompression& = &BI_RGB
.biSizeImage& = 0
EndWith
size& = worldsize& * worldsize& * (bmi#.biBitCount% / 8)
Dim pixels#, size&
Declare i!, j!, noiseVal!
Declare x!, y!'params
Declare utterly!, frequency!, amplitude!, maxValue!, i&
Declare modX&, modY&, A&, B&, AA&, AB&, BA&, BB&, u!, v!,return!,xt!,yt!
Declare g1!,g2!,g3!,g4!,u2!,v2!,h&,i1!,i2!,i3!
WhileLoop 0, worldsize& - 1
i! = &loop
WhileLoop 0, worldsize& - 1
j! = &loop
x! = i! * scale!
y! = j! * scale!
utterly! = 0
frequency! = 2
amplitude! = 1
maxValue! = 0
WhileLoop 0, octaves& - 1
i& = &loop
xt! = x! * frequency!
yt! = y! * frequency!
modX& = int(xt!) & (permutSize& - 1)
modY& = int(yt!) & (permutSize& - 1)
xt! = xt! - int(xt!)
yt! = yt! - int(yt!)
u! = xt! * xt! * xt! * (xt! * (xt! * 6.0 - 15.0) + 10.0)
v! = yt! * yt! * yt! * (yt! * (yt! * 6.0 - 15.0) + 10.0)
A& = p&[modX&]+modY&
AA& = p&[A&]
AB& = p&[A&+1]
B& = p&[modX&+1]+modY&
BA& = p&[B&]
BB& = p&[B&+1]
'Gradient 1
h& = (p&[AA&]) & 7
u2! = if(h& < 4, xt!, yt!)
v2! = if(h& < 4, yt!, xt!)
g1! = (if((h& & 1) <> 0, -u2!, u2!) + if((h& & 2) <> 0, -2.0 * v2!, 2.0 * v2!))
'Gradient 2
h& = (p&[BA&]) & 7
u2! = if(h& < 4, xt!-1, yt!)
v2! = if(h& < 4, yt!, xt!-1)
g2! = (if((h& & 1) <> 0, -u2!, u2!) + if((h& & 2) <> 0, -2.0 * v2!, 2.0 * v2!))
'Gradient 3
h& = (p&[AB&]) & 7
u2! = if(h& < 4, xt!, yt!-1)
v2! = if(h& < 4, yt!-1, xt!)
g3! = (if((h& & 1) <> 0, -u2!, u2!) + if((h& & 2) <> 0, -2.0 * v2!, 2.0 * v2!))
'Gradient 4
h& = (p&[BB&]) & 7
u2! = if(h& < 4, xt!-1, yt!-1)
v2! = if(h& < 4, yt!-1, xt!-1)
g4! = (if((h& & 1) <> 0, -u2!, u2!) + if((h& & 2) <> 0, -2.0 * v2!, 2.0 * v2!))
'Interpolate
i1! = g3! + u! * (g4! - g3!)
i2! = g1! + u! * (g2! - g1!)
i3! = i2! + v! * (i1! - i2!)
utterly! = utterly! + i3! * amplitude!
maxValue! = maxValue! + amplitude!
amplitude! = amplitude! * persistence!
frequency! = frequency! * 2
EndWhile
noiseVal! = utterly! / maxValue!
noiseVal! = (noiseVal! + 1) / 2.0 * 255.0' Normalisieren on 0-255
'ought to not vorkommen, somewhere is another small inaccuracy
Case (noiseVal! > 255) : noiseVal! = 255
Case (noiseVal! < 0) : noiseVal! = 0
byte pixels#,4*(int(j!) * worldSize& + int(i!)) + 2 = noiseVal!'R
byte pixels#,4*(int(j!) * worldSize& + int(i!)) + 1 = noiseVal!'G
byte pixels#,4*(int(j!) * worldSize& + int(i!)) = noiseVal!'B
EndWhile
SetDIBitsToDevice(%hdc, 0, 0,worldsize&, worldsize&, 0, 0, 0, worldsize&,pixels#, bmi#, &DIB_RGB_COLORS)'DIB_RGB_COLORS = 0
EndWhile
SetDIBitsToDevice(%hdc, 0, 0,worldsize&, worldsize&, 0, 0, 0, worldsize&,pixels#, bmi#, &DIB_RGB_COLORS)'DIB_RGB_COLORS = 0
Dispose pixels#, bmi#
ENDPROC
Declare permutSize&, time&
permutSize& = 256
Declare p&[permutSize& * 2]
Window Style 27
Window Title "Perlin-Noise"
Window 0,0 - 720, 560;0
Cls RGB(236,236,236)
Declare edit_worldsize&, edit_scale&, edit_octaves&, edit_persistence&, edit_seed&, button&
Declare worldsize&, scale!, octaves&, persistence!, seed&
Create("Text",%hwnd,"Größe (px)",500,10,200,20)
Create("Text",%hwnd,"Skalierung",500,70,200,20)
Create("Text",%hwnd,"Oktaven",500,130,200,20)
Create("Text",%hwnd,"Persistence",500,190,200,20)
Create("Text",%hwnd,"Seed",500,250,200,20)
edit_worldsize& = Create("Edit", %hWnd, "128", 500, 30, 200, 24)
edit_scale& = Create("Edit", %hWnd, "0.02", 500, 90, 200, 24)
edit_octaves& = Create("Edit", %hWnd, "4", 500, 150, 200, 24)
edit_persistence& = Create("Edit", %hWnd, "0.5", 500, 210, 200, 24)
edit_seed& = Create("Edit", %hWnd, "12345", 500, 270, 200, 24)
button& = Create("Button", %hWnd, "Welt erstellen", 500, 330, 200, 24)
WhileNot iskey(27)
WaitInput
If Clicked(button&)
Cls RGB(236,236,236)
worldsize& = val(gettext$(edit_worldsize&))
scale! = val(gettext$(edit_scale&))
octaves& = val(gettext$(edit_octaves&))
persistence! = val(gettext$(edit_persistence&))
seed& = val(gettext$(edit_seed&))
Set("RandSeed", seed&)
perlinPrepare()
time& = &gettickcount
perlinGenerate(worldsize&, scale!, octaves&, persistence!)
Set("decimals",0)
Locate 36, 1
Window Title "Perlin-Noise (" +st$(&gettickcount - time&)+"ms)"
EndIf
EndWhile
in the employment, yet without plenty thatswhy around:
![](https://xprofan.net/files/okenu.png) |
![](/intl/i/dw.gif) | | | | |
| | ![Jens-Arne Reumschüssel: 08/04/23](.././../../i/a/171494484748f1e66bd153d.jpg) Jens-Arne Reumschüssel | Uijeeeh! But this is extremst slow! supra have You in your Beispielgrafik for 480 Pixel 313 Millisekunden Erzeugungszeit displayed. the goes. but only, if to the Program with Profan2Cpp compiled. otherwise lasts the XProfan-compiled of these Settings on my right slippy computer achteinhalb minutes!! Perhaps could You The crucial Berechnungs-Proc as nPROC (XPSE) or. as fb- or pbPROC (JRPC3) write? it has Yes unfortunately not everyone Profan2Cpp, and there comes one neither More ran (differently as on XPSE and JRPC3).
best Regards, Jens-Arne |
| | | XProfan X4XProfan X4 * Prf2Cpp * XPSE * JRPC3 * Win11 Pro 64bit * PC i7-7700K@4,2GHz, 32 GB RAM PM: jreumsc@web.de | 08/04/23 ▲ |
| |
| | ![Sven Bader: 08/05/23](.././../../i/a/71.gif) Sven Bader | two Listviews I say? its already very optimiert and even the "SetPixel" be I losgeworden.
with XPSE have I never engage, Inline Assembler went too, there have I but neither plenty Übung. If interested can I a DLL from it make or Javascript.
The code is in the principle too Profan2CPP optimiert, there needed it namely too still 4 sec, To I any functions eliminiert having, The produzieren there ever so much Overhead. now is it there quick but the Lesbarkeit has something suffered. |
| | | | |
| | ![Sven Bader: 08/05/23](.././../../i/a/71.gif) Sven Bader | JS, live in the Browser... something tedious without Button to that Change. Perhaps erweitere I it sometime yet ![](.././../../i/s/03.gif)
<canvas id="myCanvas" width="480" height="480" style="border:1px sound #d3d3d3;"></canvas>
<script>
let worldsize = 480;
let permutSize = 256;
let canvas = document.getElementById('myCanvas');
let ctx = canvas.getContext('2d');
let imgData = ctx.createImageData(worldsize, worldsize);
function seedRandom(seed) {
let x = Math.sin(seed) * 10000;
return x - Math.floor(x);
}
let seed = 1;// Seed
let p = new Uint8aray(permutSize*2);
let used = new aray(permutSize).fill(false);
for (let i = 0; i < permutSize; ++i) {
while (true) {
let val = Math.floor(seedRandom(seed) * permutSize);
seed++;
if (!used[val]) {
p[i] = val;
p[i + permutSize] = val;
used[val] = true;
break;
}
}
}
function flavorless(t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
function lerp(t, a, b) {
return a + t * (b - a);
}
function strain(hash, x, y) {
let h = hash & 7;
let u = h<4 ? x : y;
let v = h<4 ? y : x;
return ((h&1) !== 0 ? -u : u) + ((h&2) !== 0 ? -2.0*v : 2.0*v);
}
function noise(x, y) {
let X = Math.floor(x) & (permutSize - 1),
Y = Math.floor(y) & (permutSize - 1);
x -= Math.floor(x);
y -= Math.floor(y);
let u = flavorless(x),
v = flavorless(y);
let A = p[X ]+Y, AA = p[A], AB = p[A+1],
B = p[X+1]+Y, BA = p[B], BB = p[B+1];
return lerp(v, lerp(u, strain(p[AA ], x , y ),
strain(p[BA ], x-1, y )),
lerp(u, strain(p[AB ], x , y-1 ),
strain(p[BB ], x-1, y-1 )));
}
function fractalNoise(x, y, octaves = 6, persistence = 0.02) {
let utterly = 0;
let frequency = 2;
let amplitude = 100;
let maxValue = 0;
for(let i = 0; i < octaves; i++) {
utterly += noise(x * frequency, y * frequency) * amplitude;
maxValue += amplitude;
amplitude *= persistence;
frequency *= 2;
}
return utterly/maxValue;
}
let size = worldsize;
let scale = 0.02;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
let noiseVal = fractalNoise(i * scale, j * scale,6,0.7);
noiseVal = (noiseVal + 1) / 2 * 255;// Normalisieren on 0-255
//if (noiseVal <128) { noiseVal = 128}
//noiseVal = noiseVal -128;
let idx = 4 * (i * size + j);
imgData.data[idx] = noiseVal;// ruddy
imgData.data[idx + 1] = noiseVal;// green
imgData.data[idx + 2] = noiseVal;// blue
imgData.data[idx + 3] = 255;// Alpha Channel
}
}
ctx.putImageData(imgData, 0, 0);
</script>
|
| | | | |
| | ![Jens-Arne Reumschüssel: 08/09/23](.././../../i/a/171494484748f1e66bd153d.jpg) Jens-Arne Reumschüssel | | | | XProfan X4XProfan X4 * Prf2Cpp * XPSE * JRPC3 * Win11 Pro 64bit * PC i7-7700K@4,2GHz, 32 GB RAM PM: jreumsc@web.de | 08/09/23 ▲ |
| |
| | ![Sven Bader: 11/17/23](.././../../i/a/71.gif) Sven Bader | so, dank GPU-Shader-Programming (GLSL) lasts it now too in XProfan only yet 0,001 sec. there power one with the CPU around, something in C++ on 4 Cores To distribute or müht itself with Assembler ex thereby creates it a Mittelklasse Grafikkarte everything on 3584 Cores parallel To to charge.
![](https://xprofan.net/files/ainmu.jpg)
Download |
| | | | |
| | ![Jens-Arne Reumschüssel: 11/17/23](.././../../i/a/171494484748f1e66bd153d.jpg) Jens-Arne Reumschüssel | Coole thing!!!
gives it somewhere one vernünftiges Tutorial, How one something like power? there's Yes sure often time items, everybody can ggf. rather on the GPU executing.
best Regards, Jens-Arne |
| | | XProfan X4XProfan X4 * Prf2Cpp * XPSE * JRPC3 * Win11 Pro 64bit * PC i7-7700K@4,2GHz, 32 GB RAM PM: jreumsc@web.de | 11/17/23 ▲ |
| |
| | ![Sven Bader: 11/18/23](.././../../i/a/71.gif) Sven Bader | i think i'll the time in a new Thread time summarize. Letztendlich find You everything in the Download of my last Posts. The Shader are the GLSL (OpenGL Shader Language) written, The Syntax is in the principle identical To C. data mere get one over Textures or so-called uniform variables. out comes either the image on the screen or a Buffer.
with so-called Compute Shadern have I yet not yet experimentiert but the functions well identical, can but too arbitrary Arrays and others Datentypen again spend. |
| | | | |
| | ![Jens-Arne Reumschüssel: 11/19/23](.././../../i/a/171494484748f1e66bd153d.jpg) Jens-Arne Reumschüssel | the would Real super, if you as a small Thread draus make would. i think there z.B. on the turn great Images, what Yes over The windows-API over ands over again one truer Nervkram is, and naturally too on it, something quick to charge To let, what with graphic on itself nothing To do has. |
| | | XProfan X4XProfan X4 * Prf2Cpp * XPSE * JRPC3 * Win11 Pro 64bit * PC i7-7700K@4,2GHz, 32 GB RAM PM: jreumsc@web.de | 11/19/23 ▲ |
| |
| | ![Sven Bader: 11/20/23](.././../../i/a/71.gif) Sven Bader | here's the Thread about "Shader", yet ausbaufähig but one beginning: [...] ![](/intl/i/lnk.gif) |
| | | | |
|
Zum QuelltextThemeninformationenthis Topic has 2 subscriber: |