Skip to content

Commit

Permalink
Merge pull request #18 from Triadica/orbit
Browse files Browse the repository at this point in the history
Orbits demo looped
  • Loading branch information
NoEgAm authored Apr 24, 2024
2 parents 5701253 + 73fc15e commit ec21db7
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 1,005 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/upload.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@ jobs:
node-version: 20
cache: 'yarn'

- uses: supplypike/setup-bin@v3
- uses: calcit-lang/setup-[email protected]
with:
uri: 'https://github.com/calcit-lang/calcit/releases/download/0.8.19/cr'
name: 'cr'
version: '0.8.19'

- uses: supplypike/setup-bin@v3
with:
uri: 'https://github.com/calcit-lang/calcit/releases/download/0.8.19/caps'
name: 'caps'
version: '0.8.19'
version: "0.8.50"

- name: "compiles calcit to js"
run: caps --ci && cr --emit-js --once
Expand Down
739 changes: 18 additions & 721 deletions calcit.cirru

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
def skip-rendering? $ = "\"true" (get-env "\"skip" "\"false")
|tabs $ %{} :CodeEntry (:doc |)
:code $ quote
def tabs $ [] (:: :fireworks |Fireworks :dark) (:: :lorenz |Lorenz :dark) (:: :aizawa |Aizawa :dark) (:: :fourwing "|Four Wing" :dark) (:: :fractal |Fractal :dark) (:: :collision |Collision :dark) (:: :bounce |Bounce :dark) (:: :feday |FEDAY :dark) (:: :bifurcation "\"Bifurcation" :dark) (:: :ball-spin "\"Ball Spin" :dark) (:: :lifegame "\"Lifegame" :dark) (:: :lifegame-trail "\"Lifegame Trail" :dark) (:: :bounce-trail "|Bounce Trail" :dark) (:: :orbit-spark "|Orbit Spark" :dark) (:: :chen |Chen :dark) (:: :sprott |Sprott :dark) (:: :lorenz83 |Lorenz83 :dark) (:: :orbits |Orbits :dark) (:: :lamps |Lamps :dark) (:: :debug-grid "|Debug Grid" :dark) (:: :den-tsucs "\"Den Tsucs" :dark) (:: :bouali "\"Bouali" :dark)
def tabs $ [] (:: :fireworks |Fireworks :dark) (:: :lorenz |Lorenz :dark) (:: :aizawa |Aizawa :dark) (:: :fourwing "|Four Wing" :dark) (:: :fractal |Fractal :dark) (:: :collision |Collision :dark) (:: :bounce |Bounce :dark) (:: :feday |FEDAY :dark) (:: :bifurcation "\"Bifurcation" :dark) (:: :ball-spin "\"Ball Spin" :dark) (:: :lifegame "\"Lifegame" :dark) (:: :lifegame-trail "\"Lifegame Trail" :dark) (:: :bounce-trail "|Bounce Trail" :dark) (:: :orbit-spark "|Orbit Spark" :dark) (:: :chen |Chen :dark) (:: :sprott |Sprott :dark) (:: :lorenz83 |Lorenz83 :dark) (:: :orbits |Orbits :dark) (:: :lamps |Lamps :dark) (:: :debug-grid "|Debug Grid" :dark) (:: :den-tsucs "\"Den Tsucs" :dark) (:: :bouali "\"Bouali" :dark) (:: :orbits2 "\"Orbits 2" :dark)
|threshold $ %{} :CodeEntry (:doc |)
:code $ quote
def threshold $ js/parseFloat
Expand Down Expand Up @@ -141,6 +141,7 @@
:sprott $ sprott/loadRenderer canvas
:lorenz83 $ lorenz-83/loadRenderer canvas
:orbits $ orbits/loadRenderer canvas
:orbits2 $ orbits-2/loadRenderer canvas
:lamps $ lamps/loadRenderer canvas
:debug-grid $ debug-grid/loadRenderer canvas
:den-tsucs $ den-tsucs/loadRenderer canvas
Expand Down Expand Up @@ -209,6 +210,7 @@
"\"../src/apps/lifegame-trail" :as lifegame-trail
"\"../src/apps/orbit-spark" :as orbit-spark
"\"../src/apps/orbits" :as orbits
"\"../src/apps/orbits-2" :as orbits-2
"\"../src/apps/lamps" :as lamps
"\"../src/apps/debug-grid" :as debug-grid
"\"../src/apps/attractor-den-tsucs" :as den-tsucs
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"@webgpu/types": "^0.1.40",
"bottom-tip": "^0.1.5",
"query-string": "^9.0.0",
"typescript": "^5.4.2",
"vite": "^5.1.6"
"typescript": "^5.4.5",
"vite": "^5.2.9"
},
"dependencies": {
"@calcit/procs": "^0.8.40",
"@calcit/procs": "^0.8.50",
"@triadica/touch-control": "^0.0.4-a1",
"ismobilejs": "^1.1.1"
},
Expand Down
106 changes: 106 additions & 0 deletions src/apps/orbits-2.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { createRenderer } from "../index.mjs";
import computeOrbits from "./orbits-2.wgsl?raw";
import { fiboGridN } from "../math.mjs";

export let loadRenderer = async (canvas: HTMLCanvasElement) => {
let seedSize = 4194240;
// 4194304

let renderFrame = await createRenderer(
canvas,
{
seedSize,
seedData: makeSeed(seedSize, 0),
params: [
0.004, // deltaT
0.6, // height
0.2, // width
0.8, // opacity
],
// computeShader: updateSpritesWGSL,
// computeShader: computeGravityWgsl,
computeShader: computeOrbits,
},
{
vertexCount: 1,
vertexData: [0, 1, 2, 3],
indexData: [0, 1, 2, 1, 2, 3],
vertexBufferLayout: vertexBufferLayout,
// topology: "line-list",
bgColor: [0.1, 0.0, 0.2, 1.0],
}
);

return renderFrame;
};

// let vv: [number, number] = [5.46, 4.55];
// // vv = [0.848, 4.783];
// // vv = [1.158, 1.93];
// // vv = [3.667, 3.934];
// // vv = [3.536, 5.575];
// // vv = [5.655, 5.16];
// // vv = [5.937, 5.482];
// // vv = [2.3, 2.72];
// // vv = [5.46, 4.55];
// // vv = [3.65, 4.58];
// // vv = [0.02, 1.07];
// vv = [5.68, 5.02];

// let iterate = (p: [number, number]): [number, number] => {
// let x = p[0];
// let y = p[1];
// let next_x = Math.sin(x * x - y * y + vv[0]);
// let next_y = Math.cos(2 * x * y + vv[1]);
// return [next_x, next_y];
// };

function makeSeed(numParticles: number, _s: number): Float32Array {
const unit = 8;
const buf = new Float32Array(numParticles * unit);
// let scale = 200 * (Math.random() * 0.5 + 0.5);
let scale_base = 1;
// let p = [-0.4, 0.6] as [number, number];
// p = [Math.random(), Math.random()];
for (let i = 0; i < numParticles; ++i) {
// let q = randPointInSphere(100);
let p = fiboGridN(i, numParticles).slice(0, 2) as [number, number];
// for (let j = 0; j < 20; ++j) {
// p = iterate(p);
// }
let b = unit * i;
// buf[b + 0] = p[0] * scale;
// buf[b + 1] = p[1] * scale;
buf[b + 0] = p[0] * scale_base;
buf[b + 1] = p[1] * scale_base;
buf[b + 2] = Math.random();
buf[b + 3] = 0; // times
buf[b + 4] = p[0] * scale_base;
buf[b + 5] = p[1] * scale_base;
buf[b + 6] = Math.random();
buf[b + 7] = 0; // times
// p = iterate(p);
}

return buf;
}

let vertexBufferLayout: GPUVertexBufferLayout[] = [
{
// instanced particles buffer
arrayStride: 8 * 4,
stepMode: "instance",
attributes: [
{ shaderLocation: 0, offset: 0, format: "float32x3" },
{ shaderLocation: 1, offset: 3 * 4, format: "float32" },
{ shaderLocation: 2, offset: 4 * 4, format: "float32x3" },
{ shaderLocation: 3, offset: 7 * 4, format: "float32" },
],
},
{
// vertex buffer
arrayStride: 1 * 4,
stepMode: "vertex",
attributes: [{ shaderLocation: 4, offset: 0, format: "uint32" }],
},
];
160 changes: 160 additions & 0 deletions src/apps/orbits-2.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@

#import protea::perspective
#import protea::colors

struct Particle {
pos: vec3<f32>,
times: f32,
pos0: vec3<f32>,
times2: f32,
// p1: f32,
// p2: f32,
// p3: f32,
// p4: f32,
}

struct Params {
delta_t: f32,
height: f32,
width: f32,
opacity: f32,
}

struct Particles {
particles: array<Particle>,
}

@group(0) @binding(1) var<uniform> params: Params;
@group(1) @binding(0) var<storage, read> pass_in: Particles;
@group(1) @binding(1) var<storage, read_write> pass_out: Particles;

fn rand(n: f32) -> f32 { return fract(sin(n) * 43758.5453123); }

fn pick_param(n: u32) -> vec2f {
switch n {
case 0u: {
return vec2f(0.1, 0.6);
}
case 1u: {
return vec2f(3.667, 3.934);
}
case 2u: {
return vec2f(3.536, 5.575);
}
case 3u: {
return vec2f(5.655, 5.16);
}
case 4u: {
return vec2f(5.937, 5.482);
}
case 5u: {
return vec2f(2.3, 2.72);
}
case 6u: {
return vec2f(5.46, 4.55);
}
case 7u: {
return vec2f(3.65, 4.58);
}
case 8u: {
return vec2f(2.65, 1.58);
}
case 9u: {
return vec2f(-2.65, -.8);
}
default: {
return vec2f(1.158, 1.93);
}
}
}

fn iterate(p: vec3<f32>, n: u32) -> vec3<f32> {
let x = p[0];
let y = p[1];
let z = p[2];

var vv = pick_param(n);
let next_x = sin(x * x - y * y + vv.x);
let next_y = cos(2. * x * y + vv.y);
return vec3(next_y, next_x, z);
}

// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
var index = GlobalInvocationID.x;
let item = pass_in.particles[index];

let duration = 50.;
let try_idx = u32(item.times2 / duration) % 10u;

let next = iterate(item.pos, try_idx);
if item.times < duration {
pass_out.particles[index].pos.x = next.x;
pass_out.particles[index].pos.y = next.y;
pass_out.particles[index].pos.z = item.pos.z;
pass_out.particles[index].times = item.times + 1.;
pass_out.particles[index].times2 = item.times2 + 1.;
} else {
pass_out.particles[index].pos.x = item.pos0.x;
pass_out.particles[index].pos.y = item.pos0.y;
pass_out.particles[index].times = 0.;
pass_out.particles[index].times2 = item.times2 + 1.;
// pass_out.particles[index].pos = item.pos;
}
}

struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(4) color: vec4<f32>,
}

@vertex
fn vert_main(
@location(0) position0: vec3<f32>,
@location(1) point_idx: f32,
@location(2) velocity: vec3<f32>,
@location(3) times: f32,
@location(4) idx: u32,
) -> VertexOutput {
let position = position0 * 100.;
var pos: vec3<f32>;
let rightward: vec3<f32> = uniforms.rightward;
let upward: vec3<f32> = uniforms.upward;
let right = normalize(rightward);
let up = normalize(upward);

// let front = params.length;
var width = params.width * 0.4;

if idx == 0u {
pos = position + right * width;
// pos += vec3(1.,1.,1.) * 100.0;
} else if idx == 1u {
pos = position;
} else if idx == 2u {
pos = position + right * width + up * width;
} else if idx == 3u {
pos = position + up * width;
} else {
pos = position;
}
pos.z = 0.0;

var output: VertexOutput;
let p0 = vec4(pos * 10.0, 1.0);

let p: vec3<f32> = transform_perspective(p0.xyz).point_position;
let scale: f32 = 0.002;

output.position = vec4(p[0] * scale, p[1] * scale, p[2] * scale, 1.0);
let c3: vec3<f32> = hsl(fract(point_idx / 4000000.), 0.8, max(0.1, 0.9 - 0.2));
output.color = vec4(c3, params.opacity);
return output;
}

@fragment
fn frag_main(@location(4) color: vec4<f32>) -> @location(0) vec4<f32> {
return color;
// return vec4<f32>(1., 0., 0., 1.0);
}
12 changes: 0 additions & 12 deletions src/apps/orbits.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ struct Particles {
@group(1) @binding(0) var<storage, read> particles_a: Particles;
@group(1) @binding(1) var<storage, read_write> particles_b: Particles;

struct SphereConstraint {
center: vec3<f32>,
radius: f32,
inside: bool,
}

fn rand(n: f32) -> f32 { return fract(sin(n) * 43758.5453123); }

fn iterate(p: vec3<f32>) -> vec3<f32> {
Expand All @@ -50,12 +44,6 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
let item = particles_a.particles[index];
let write_target = &particles_b.particles[index];

let constraints = array<SphereConstraint, 3>(
SphereConstraint(vec3<f32>(0.0, 0.0, 0.0), 100.0, true),
SphereConstraint(vec3<f32>(0.0, 100.0, 0.0), 180.0, true),
SphereConstraint(vec3<f32>(0.0, -150.0, 0.0), 20.0, false)
);

var v_pos = item.pos;

let next = iterate(v_pos * 0.01) * 101.;
Expand Down
1 change: 1 addition & 0 deletions src/demo-entry.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {} from "./apps/lamps.mjs";
import {} from "./apps/debug-grid.mjs";
import {} from "./apps/attractor-den-tsucs.mjs";
import {} from "./apps/attractor-bouali.mjs";
import {} from "./apps/orbits-2.mjs";

let instanceRenderer: (t: number, skipComputing: boolean) => void;
let canvas = document.querySelector("#canvas-container") as HTMLCanvasElement;
Expand Down
Loading

0 comments on commit ec21db7

Please sign in to comment.