@@ -36,6 +36,7 @@ class FieldsShader(
3636 val velocities = storage<KslFloat4 >(" velocities" )
3737 val forces = storage<KslFloat4 >(" forces" )
3838 val localNeighbours = storage<KslFloat1 >(" localNeighbours" )
39+ val velocityData = storage<KslFloat1 >(" velocityData" )
3940
4041 val particleTypes = storage<KslInt1 >(" particleTypes" )
4142
@@ -188,10 +189,23 @@ class FieldsShader(
188189 `if `(length(nextVelocity) gt params.maxVelocity.ksl) {
189190 nextVelocity set normalize(nextVelocity) * params.maxVelocity.ksl
190191 }
192+ val target = params.targetVelocity.ksl
193+ val totalSqrtVelocities = float1Var(velocityData[0 .const])
194+ val average = totalSqrtVelocities / count.toFloat1()
195+ // val halved = totalSqrtVelocities/2f.const
196+ // val degreesOfFreedom = 2f.const
197+ val strength = params.targetVelocityFixStrength.ksl
198+ // nudge particles towards target velocity
199+ nextVelocity set nextVelocity * sqrt(
200+ 1f .const + (dT * strength) * ((target) / max(
201+ average,
202+ 0.1f .const
203+ ) - 1f .const)
204+ )
191205
192206 forces[id] = float4Value(nextForce, 0f )
193207 velocities[id] = float4Value(nextVelocity, 0f )
194- localNeighbours[id] = localCount
208+ localNeighbours[id] = totalSqrtVelocities
195209 }
196210 }
197211
@@ -214,6 +228,7 @@ class FieldsShader(
214228 var localNeighbours by shader.storage(" localNeighbours" )
215229 var forces by shader.storage(" forces" )
216230 var particleTypes by shader.storage(" particleTypes" )
231+ var velocityData by shader.storage(" velocityData" )
217232}
218233//
219234// context(scope: KslScopeBuilder)
0 commit comments