96 lines
3.0 KiB
Plaintext
96 lines
3.0 KiB
Plaintext
|
#include "ColliderDefinitions.cginc"
|
||
|
#include "ContactHandling.cginc"
|
||
|
#include "Transform.cginc"
|
||
|
#include "Simplex.cginc"
|
||
|
#include "Bounds.cginc"
|
||
|
#include "SolverParameters.cginc"
|
||
|
#include "Optimization.cginc"
|
||
|
|
||
|
#pragma kernel GenerateContacts
|
||
|
|
||
|
StructuredBuffer<float4> positions;
|
||
|
StructuredBuffer<quaternion> orientations;
|
||
|
StructuredBuffer<float4> principalRadii;
|
||
|
StructuredBuffer<float4> velocities;
|
||
|
|
||
|
StructuredBuffer<int> simplices;
|
||
|
|
||
|
StructuredBuffer<transform> transforms;
|
||
|
StructuredBuffer<shape> shapes;
|
||
|
|
||
|
StructuredBuffer<uint2> contactPairs;
|
||
|
StructuredBuffer<int> contactOffsetsPerType;
|
||
|
|
||
|
RWStructuredBuffer<contact> contacts;
|
||
|
RWStructuredBuffer<uint> dispatchBuffer;
|
||
|
|
||
|
StructuredBuffer<transform> worldToSolver;
|
||
|
|
||
|
uint maxContacts;
|
||
|
|
||
|
struct Sphere : IDistanceFunction
|
||
|
{
|
||
|
shape s;
|
||
|
transform colliderToSolver;
|
||
|
|
||
|
void Evaluate(in float4 pos, in float4 radii, in quaternion orientation, inout SurfacePoint projectedPoint)
|
||
|
{
|
||
|
float4 center = s.center * colliderToSolver.scale;
|
||
|
float4 pnt = colliderToSolver.InverseTransformPointUnscaled(pos) - center;
|
||
|
|
||
|
if (s.is2D())
|
||
|
pnt[2] = 0;
|
||
|
|
||
|
float radius = s.size.x * cmax(colliderToSolver.scale.xyz);
|
||
|
float distanceToCenter = length(pnt);
|
||
|
|
||
|
float4 normal = pnt / (distanceToCenter + EPSILON);
|
||
|
|
||
|
projectedPoint.pos = colliderToSolver.TransformPointUnscaled(center + normal * (radius + s.contactOffset));
|
||
|
projectedPoint.normal = colliderToSolver.TransformDirection(normal);
|
||
|
projectedPoint.bary = float4(1,0,0,0);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
[numthreads(128, 1, 1)]
|
||
|
void GenerateContacts (uint3 id : SV_DispatchThreadID)
|
||
|
{
|
||
|
uint i = id.x;
|
||
|
|
||
|
// entry #11 in the dispatch buffer is the amount of pairs for the first shape type.
|
||
|
if (i >= dispatchBuffer[11 + 4*SPHERE_SHAPE]) return;
|
||
|
|
||
|
uint count = contacts.IncrementCounter();
|
||
|
if (count < maxContacts)
|
||
|
{
|
||
|
int firstPair = contactOffsetsPerType[SPHERE_SHAPE];
|
||
|
int simplexIndex = contactPairs[firstPair + i].x;
|
||
|
int colliderIndex = contactPairs[firstPair + i].y;
|
||
|
|
||
|
contact c = (contact)0;
|
||
|
|
||
|
Sphere sphereShape;
|
||
|
sphereShape.colliderToSolver = worldToSolver[0].Multiply(transforms[colliderIndex]);
|
||
|
sphereShape.s = shapes[colliderIndex];
|
||
|
|
||
|
int simplexSize;
|
||
|
int simplexStart = GetSimplexStartAndSize(simplexIndex, simplexSize);
|
||
|
|
||
|
float4 simplexBary = BarycenterForSimplexOfSize(simplexSize);
|
||
|
float4 simplexPoint;
|
||
|
|
||
|
SurfacePoint surfacePoint = Optimize(sphereShape, positions, orientations, principalRadii,
|
||
|
simplices, simplexStart, simplexSize, simplexBary, simplexPoint, surfaceCollisionIterations, surfaceCollisionTolerance);
|
||
|
|
||
|
c.pointB = surfacePoint.pos;
|
||
|
c.normal = surfacePoint.normal * sphereShape.s.isInverted();
|
||
|
c.pointA = simplexBary;
|
||
|
c.bodyA = simplexIndex;
|
||
|
c.bodyB = colliderIndex;
|
||
|
|
||
|
contacts[count] = c;
|
||
|
|
||
|
InterlockedMax(dispatchBuffer[0],(count + 1) / 128 + 1);
|
||
|
InterlockedMax(dispatchBuffer[3], count + 1);
|
||
|
}
|
||
|
}
|