-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Triadica/orbit
Orbits demo looped
- Loading branch information
Showing
9 changed files
with
569 additions
and
1,005 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }], | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.