添加插件

This commit is contained in:
2025-11-10 00:08:26 +08:00
parent 4059c207c0
commit 76f80db694
2814 changed files with 436400 additions and 178 deletions

View File

@@ -0,0 +1,44 @@
using System;
using UnityEngine;
namespace Obi
{
public class ComputeDistanceConstraints : ComputeConstraintsImpl<ComputeDistanceConstraintsBatch>
{
public ComputeShader constraintsShader;
public int projectKernel;
public int applyKernel;
public ComputeDistanceConstraints(ComputeSolverImpl solver) : base(solver, Oni.ConstraintType.Distance)
{
constraintsShader = GameObject.Instantiate(Resources.Load<ComputeShader>("Compute/DistanceConstraints"));
projectKernel = constraintsShader.FindKernel("Project");
applyKernel = constraintsShader.FindKernel("Apply");
}
public override IConstraintsBatchImpl CreateConstraintsBatch()
{
var dataBatch = new ComputeDistanceConstraintsBatch(this);
batches.Add(dataBatch);
return dataBatch;
}
public override void RemoveBatch(IConstraintsBatchImpl batch)
{
batches.Remove(batch as ComputeDistanceConstraintsBatch);
batch.Destroy();
}
public void RequestDataReadback()
{
foreach (var batch in batches)
batch.RequestDataReadback();
}
public void WaitForReadback()
{
foreach (var batch in batches)
batch.WaitForReadback();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c56c65d479be44432ba437d871d4892d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
using UnityEngine;
namespace Obi
{
public class ComputeDistanceConstraintsBatch : ComputeConstraintsBatchImpl, IDistanceConstraintsBatchImpl
{
GraphicsBuffer restLengthsBuffer;
GraphicsBuffer stiffnessesBuffer;
public ComputeDistanceConstraintsBatch(ComputeDistanceConstraints constraints)
{
m_Constraints = constraints;
m_ConstraintType = Oni.ConstraintType.Distance;
}
public void SetDistanceConstraints(ObiNativeIntList particleIndices, ObiNativeFloatList restLengths, ObiNativeVector2List stiffnesses, ObiNativeFloatList lambdas, int count)
{
this.particleIndices = particleIndices.AsComputeBuffer<int>();
this.restLengthsBuffer = restLengths.AsComputeBuffer<float>();
this.stiffnessesBuffer = stiffnesses.AsComputeBuffer<Vector2>();
this.lambdas = lambdas.AsComputeBuffer<float>();
this.lambdasList = lambdas;
m_ConstraintCount = count;
}
public override void Evaluate(float stepTime, float substepTime, int steps, float timeLeft)
{
if (m_ConstraintCount > 0)
{
var shader = ((ComputeDistanceConstraints)m_Constraints).constraintsShader;
int projectKernel = ((ComputeDistanceConstraints)m_Constraints).projectKernel;
shader.SetBuffer(projectKernel, "particleIndices", particleIndices);
shader.SetBuffer(projectKernel, "restLengths", restLengthsBuffer);
shader.SetBuffer(projectKernel, "stiffnesses", stiffnessesBuffer);
shader.SetBuffer(projectKernel, "lambdas", lambdas);
shader.SetBuffer(projectKernel, "positions", solverImplementation.positionsBuffer);
shader.SetBuffer(projectKernel, "invMasses", solverImplementation.invMassesBuffer);
shader.SetBuffer(projectKernel, "deltasAsInt", solverImplementation.positionDeltasIntBuffer);
shader.SetBuffer(projectKernel, "positionConstraintCounts", solverImplementation.positionConstraintCountBuffer);
shader.SetInt("activeConstraintCount", m_ConstraintCount);
shader.SetFloat("deltaTime", substepTime);
int threadGroups = ComputeMath.ThreadGroupCount(m_ConstraintCount, 128);
shader.Dispatch(projectKernel, threadGroups, 1, 1);
}
}
public override void Apply(float deltaTime)
{
if (m_ConstraintCount > 0)
{
var parameters = solverAbstraction.GetConstraintParameters(m_ConstraintType);
var shader = ((ComputeDistanceConstraints)m_Constraints).constraintsShader;
int applyKernel = ((ComputeDistanceConstraints)m_Constraints).applyKernel;
shader.SetBuffer(applyKernel, "particleIndices", particleIndices);
shader.SetBuffer(applyKernel, "positions", solverImplementation.positionsBuffer);
shader.SetBuffer(applyKernel, "deltasAsInt", solverImplementation.positionDeltasIntBuffer);
shader.SetBuffer(applyKernel, "positionConstraintCounts", solverImplementation.positionConstraintCountBuffer);
shader.SetInt("activeConstraintCount", m_ConstraintCount);
shader.SetFloat("sorFactor", parameters.SORFactor);
int threadGroups = ComputeMath.ThreadGroupCount(m_ConstraintCount, 128);
shader.Dispatch(applyKernel, threadGroups, 1, 1);
}
}
public void RequestDataReadback()
{
lambdasList.Readback();
}
public void WaitForReadback()
{
lambdasList.WaitForReadback();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e197e97b0c8b94a84bce0a448e62e5fb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: