Refactor random

This commit is contained in:
Andras Schmelczer 2023-05-27 19:48:45 +01:00
parent 752e1ae425
commit d83d5a8d4f
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 10 additions and 172 deletions

View file

@ -2,9 +2,10 @@
## todo
- add info page
- add info page description
- add share link
- settings page
add reset link
- shareable settings
- query max agent count
- graceful error messages when no support
- fix up generation id automatically

View file

@ -1,93 +0,0 @@
import { Random } from '../../random';
import { setUpFullScreenQuad } from '../full-screen-quad/full-screen-quad';
import random from '../random.wgsl';
import { smartCompile } from '../smart-compile';
import noise from './fbm-noise.wgsl';
const textureCache = new Map<string, GPUTexture>();
export const generateFbmNoise = ({
device,
width = 1024,
height = 1024,
octaves = 8,
lacunarity = 2,
amplitude = 0.5,
gain = 0.5,
}: {
device: GPUDevice;
width?: number;
height?: number;
octaves?: number;
lacunarity?: number;
amplitude?: number;
gain?: number;
}): GPUTextureView => {
const constants = {
octaves,
lacunarity,
amplitude,
gain,
seedR: Random.getRandom(),
seedG: Random.getRandom(),
seedB: Random.getRandom(),
seedA: Random.getRandom(),
};
const cacheKey = `${width}x${height}x${JSON.stringify(constants)}`;
if (!textureCache.has(cacheKey)) {
const { buffer, vertex } = setUpFullScreenQuad(device);
const vertexBuffer = buffer;
const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex,
fragment: {
module: smartCompile(device, random, noise),
entryPoint: 'fragment',
constants,
targets: [
{
format: 'rgba16float',
},
],
},
primitive: {
topology: 'triangle-strip',
},
});
const colorTexture = device.createTexture({
size: {
width,
height,
depthOrArrayLayers: 1,
},
format: 'rgba16float',
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT,
});
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: colorTexture.createView(),
clearValue: { r: 1.0, g: 1.0, b: 1.0, a: 1.0 },
loadOp: 'clear',
storeOp: 'store',
},
],
};
const commandEncoder = device.createCommandEncoder();
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(pipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(4, 1);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
textureCache.set(cacheKey, colorTexture);
}
return textureCache.get(cacheKey).createView();
};

View file

@ -1,54 +0,0 @@
override octaves: i32;
override amplitude: f32;
override gain: f32;
override lacunarity: f32;
override seedR: f32;
override seedG: f32;
override seedB: f32;
override seedA: f32;
@fragment
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
return vec4(
fbm(uv, seedR),
fbm(uv, seedG),
fbm(uv, seedB),
fbm(uv, seedA),
);
}
fn fbm(uv: vec2<f32>, seed: f32) -> f32 {
var st = uv;
var v = 0.0;
var a = amplitude;
let shift = vec2(100.0);
let rot = mat2x2(cos(0.5), sin(0.5),
-sin(0.5), cos(0.50));
for (var i = 0; i < octaves; i++) {
v += a * noise(st, seed);
st *= rot * lacunarity;
st += shift;
a *= gain;
}
return v;
}
fn noise (st: vec2<f32>, seed: f32) -> f32 {
let i = floor(st);
let f = fract(st);
let a = random_with_seed(i, seed);
let b = random_with_seed(i + vec2(1.0, 0.0), seed);
let c = random_with_seed(i + vec2(0.0, 1.0), seed);
let d = random_with_seed(i + vec2(1.0, 1.0), seed);
let u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}

View file

@ -1,6 +1,5 @@
import { setUpFullScreenQuad } from '../full-screen-quad/full-screen-quad';
import random from '../random.wgsl';
import { smartCompile } from '../smart-compile';
import { setUpFullScreenQuad } from './full-screen-quad/full-screen-quad';
import { smartCompile } from './smart-compile';
const textureCache = new Map<string, GPUTexture>();
@ -24,8 +23,11 @@ export const generateNoise = ({
fragment: {
module: smartCompile(
device,
random,
/* wgsl */ `
fn random_with_seed(uv: vec2<f32>, seed: f32) -> f32 {
return fract(sin(dot(uv, vec2(12.9898 + seed, 78.233 + seed)))* 43758.5453123 + seed);
}
@fragment
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
return vec4(

View file

@ -1,18 +0,0 @@
fn random_with_seed(uv: vec2<f32>, seed: f32) -> f32 {
return fract(sin(dot(uv, vec2(12.9898 + seed, 78.233 + seed)))* 43758.5453123 + seed);
}
fn random(uv: vec2<f32>) -> f32 {
return fract(sin(dot(uv, vec2(12.9898, 78.233)))* 43758.5453123);
}
fn hash(state0 : u32) -> f32 {
var state : u32 = state0;
state = state ^ 2747636419u;
state = state * 2654435769u;
state = state ^ (state >> 16u);
state = state * 2654435769u;
state = state ^ (state >> 16u);
state = state * 2654435769u;
return f32(state) / 4294967295.0;
}