Skip to content

Commit

Permalink
Prevent liquid emitters from emitting if the neighbourhood is already…
Browse files Browse the repository at this point in the history
… heavily compressed. This prevents severe overpressure from developing in the sim.
  • Loading branch information
Chris Lewin committed Aug 20, 2024
1 parent 03c25d9 commit e0536f8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 83 deletions.
138 changes: 63 additions & 75 deletions scenes/seedLogo.json
Original file line number Diff line number Diff line change
@@ -1,213 +1,196 @@
{
"version": 2,
"resolution": [
2560,
1439
2335,
1313
],
"shapes": [
{
"id": "shape5",
"position": {
"x": 1211.0477881842492,
"y": 666.6613012601011
"x": 1104.6080411758678,
"y": 608.2879003158532
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "162"
"x": "22",
"y": "148"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape6",
"position": {
"x": 1105.7975719211247,
"y": 487.9908916003117
"x": 1008.6083322014946,
"y": 445.2620157548362
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "210"
"x": "22",
"y": "192"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape7",
"position": {
"x": 1243.682168465429,
"y": 33.683296837780404
"x": 1134.3741653776472,
"y": 30.733960214041467
},
"rotation": 0.7293645483352325,
"halfSize": {
"x": "24",
"y": "323"
"x": "22",
"y": "295"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape8",
"position": {
"x": 1203.890348362763,
"y": 529.8223679438969
"x": 1098.079673213692,
"y": 483.43069430878154
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "162"
"x": "22",
"y": "148"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape9",
"position": {
"x": 1341.2651503943116,
"y": 673.5735978781664
"x": 1223.3805180354366,
"y": 614.5949506699322
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "154"
"x": "22",
"y": "140"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape10",
"position": {
"x": 1120.9251350966076,
"y": 774.650258578502
"x": 1022.4063243947573,
"y": 706.8212574798979
},
"rotation": 135.18005264926117,
"halfSize": {
"x": "24",
"y": "217"
"x": "22",
"y": "198"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape11",
"position": {
"x": 1940.6367996123654,
"y": 1337.4679469605649
"x": 1770.073018396435,
"y": 1220.3581753712451
},
"rotation": 0,
"halfSize": {
"x": "540",
"y": "31"
"x": "493",
"y": "28"
},
"shape": "0",
"function": "0",
"emitMaterial": "0",
"emissionRate": "8.3",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape2",
"position": {
"x": 1352.1649756842492,
"y": 808.9613012601013
"x": 1233.3223508682506,
"y": 738.127997605638
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "136"
"x": "22",
"y": "124"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape1",
"position": {
"x": 1458.761853846608,
"y": 565.1710919118352
"x": 1330.550362785871,
"y": 515.6842555109379
},
"rotation": 135.18005264926117,
"halfSize": {
"x": "24",
"y": "226"
"x": "22",
"y": "206"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape12",
"position": {
"x": 1475.4561656711248,
"y": 842.2721416003118
"x": 1345.777401110186,
"y": 768.5221139132797
},
"rotation": 45.382572231599404,
"halfSize": {
"x": "24",
"y": "193"
"x": "22",
"y": "176"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
"radius": 59.2918936029378
},
{
"id": "shape3",
"position": {
"x": 1343.6485747154288,
"y": 1278.4159357266694
"x": 1225.5544617033306,
"y": 1166.4768058437228
},
"rotation": 0.7293645483352325,
"halfSize": {
"x": "24",
"y": "323"
"x": "22",
"y": "295"
},
"shape": "0",
"function": "1",
"emitMaterial": "0",
"emissionRate": "1",
"radius": 64.99349546821817
},
{
"id": "shape4",
"position": {
"x": 1217.692631708736,
"y": -75.58915584227748
},
"rotation": 89.74484793854614,
"halfSize": {
"x": "129",
"y": "226"
},
"shape": "0",
"function": "2",
"emitMaterial": "0",
"emissionRate": "4.8",
"radius": 64.99349546821817
"radius": 59.2918936029378
}
],
"settings": [
Expand All @@ -219,7 +202,7 @@
{
"name": "fixedPointMultiplierExponent",
"type": "range",
"value": "6"
"value": "8"
},
{
"name": "gravityStrength",
Expand All @@ -231,6 +214,11 @@
"type": "range",
"value": "0"
},
{
"name": "borderFriction",
"type": "range",
"value": "0"
},
{
"name": "renderMode",
"type": "combo",
Expand Down
2 changes: 1 addition & 1 deletion shaders/g2p2g.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ fn csMain( @builtin(local_invocation_index) indexInGroup: u32, @builtin(workgrou
}


if(g_simConstants.iteration != g_simConstants.iterationCount-1)
//if(g_simConstants.iteration != g_simConstants.iterationCount-1)
{
// Particle update
if(particle.material == MaterialLiquid)
Expand Down
16 changes: 13 additions & 3 deletions shaders/particleEmit.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@group(0) @binding(2) var<storage, read_write> g_particles : array<Particle>;
@group(0) @binding(3) var<storage> g_shapes : array<SimShape>;
@group(0) @binding(4) var<storage, read_write> g_freeIndices : array<atomic<i32>>;
@group(0) @binding(5) var<storage> g_grid : array<i32>;

fn createParticle(position: vec2f, material: f32, mass: f32, volume: f32, color: vec3f) -> Particle
{
Expand Down Expand Up @@ -93,7 +94,9 @@ fn csMain( @builtin(global_invocation_id) id: vec3u )
let gridSize = g_simConstants.gridSize;
let pos = vec2f(id.xy);


let weightInfo = quadraticWeightInit(pos);
let nearestCell = vec2i(weightInfo.cellIndex) + vec2i(1,1);
let nearestCellVolume = decodeFixedPoint(g_grid[gridVertexIndex(vec2u(nearestCell), g_simConstants.gridSize) + 3], g_simConstants.fixedPointMultiplier);

for(var shapeIndex = 0u; shapeIndex < g_simConstants.shapeCount; shapeIndex++)
{
Expand All @@ -107,6 +110,12 @@ fn csMain( @builtin(global_invocation_id) id: vec3u )
continue;
}

// Skip emission if we are spewing liquid into an already compressed space
if(isEmitter && shape.emitMaterial == MaterialLiquid && nearestCellVolume > 1.5)
{
continue;
}

let particleCountPerCellAxis = u32(g_simConstants.particlesPerCellAxis);
let volumePerParticle = 1.0f / f32(particleCountPerCellAxis*particleCountPerCellAxis);

Expand All @@ -115,7 +124,6 @@ fn csMain( @builtin(global_invocation_id) id: vec3u )
{
let emitEvery = u32(1.0 / (shape.emissionRate * g_simConstants.deltaTime));


for(var i = 0u; i < particleCountPerCellAxis; i++)
{
for(var j = 0u; j < particleCountPerCellAxis; j++)
Expand All @@ -127,9 +135,11 @@ fn csMain( @builtin(global_invocation_id) id: vec3u )
let emitDueToMyTurnHappening = isEmitter && 0 == ((hashCode + g_simConstants.simFrame) % emitEvery);
let emitDueToInitialEmission = isInitialEmitter && g_simConstants.simFrame == 0;

let emitPos = pos + vec2f(f32(i),f32(j))/f32(particleCountPerCellAxis);

if(emitDueToInitialEmission || emitDueToMyTurnHappening)
{
addParticle(pos + vec2f(f32(i),f32(j))/f32(particleCountPerCellAxis), shape.emitMaterial, volumePerParticle, 1.0, 1.0/f32(particleCountPerCellAxis));
addParticle(emitPos, shape.emitMaterial, volumePerParticle, 1.0, 1.0/f32(particleCountPerCellAxis));
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/sim.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export function init(insertHandlers)
g_shapeFactory = shapeFactory;
}

function doEmission(gpuContext, simUniformBuffer, inputs, shapeBuffer)
function doEmission(gpuContext, simUniformBuffer, inputs, shapeBuffer, gridBuffer)
{
const threadGroupCountX = gpu.divUp(inputs.gridSize[0], DispatchSizes.GridDispatchSize);
const threadGroupCountY = gpu.divUp(inputs.gridSize[1], DispatchSizes.GridDispatchSize);
const gridThreadGroupCounts = [threadGroupCountX, threadGroupCountY, 1];

gpu.computeDispatch(Shaders.particleEmit, [simUniformBuffer, gpuContext.particleCountBuffer, gpuContext.particleBuffer, shapeBuffer, gpuContext.particleFreeIndicesBuffer], gridThreadGroupCounts);
gpu.computeDispatch(Shaders.particleEmit, [simUniformBuffer, gpuContext.particleCountBuffer, gpuContext.particleBuffer, shapeBuffer, gpuContext.particleFreeIndicesBuffer, gridBuffer], gridThreadGroupCounts);
gpu.computeDispatch(Shaders.setIndirectArgs, [gpuContext.particleCountBuffer, gpuContext.particleSimDispatchBuffer, gpuContext.particleRenderDispatchBuffer], [1,1,1]);
}

Expand Down Expand Up @@ -183,8 +183,7 @@ export function update(gpuContext, inputs)
for(let substepIdx = 0; substepIdx < substepCount; ++substepIdx)
{
var simUniformBuffer = constructSimUniformBuffer(gpuContext, inputs, bukkitSystem, 0);
doEmission(gpuContext, simUniformBuffer, inputs, shapeBuffer);
bukkitizeParticles(gpuContext, simUniformBuffer, bukkitSystem);


for(let iterationIdx = 0; iterationIdx < inputs.iterationCount; ++iterationIdx)
{
Expand All @@ -198,6 +197,9 @@ export function update(gpuContext, inputs)
gpu.computeDispatch(Shaders.g2p2g, [simUniformBuffer, gpuContext.particleBuffer, currentGrid, nextGrid, nextNextGrid, bukkitSystem.threadData, bukkitSystem.particleData, shapeBuffer, gpuContext.particleFreeIndicesBuffer], bukkitSystem.dispatch)
}

doEmission(gpuContext, simUniformBuffer, inputs, shapeBuffer, gridBuffers[bufferIdx]);
bukkitizeParticles(gpuContext, simUniformBuffer, bukkitSystem);

g_substepIndex = (g_substepIndex + 1);
}
}
Expand Down

0 comments on commit e0536f8

Please sign in to comment.