修改水
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
public struct BurstHeightField : BurstLocalOptimization.IDistanceFunction
|
||||
public struct BurstHeightField : BurstLocalOptimization.IDistanceFunction, IBurstCollider
|
||||
{
|
||||
|
||||
public BurstColliderShape shape;
|
||||
public BurstAffineTransform colliderToSolver;
|
||||
public BurstAffineTransform solverToWorld;
|
||||
public float dt;
|
||||
public float collisionMargin;
|
||||
|
||||
public BurstMath.CachedTri tri;
|
||||
public float4 triNormal;
|
||||
@@ -21,7 +23,7 @@ namespace Obi
|
||||
{
|
||||
point = colliderToSolver.InverseTransformPoint(point);
|
||||
|
||||
float4 nearestPoint = BurstMath.NearestPointOnTri(tri, point, out _);
|
||||
float4 nearestPoint = BurstMath.NearestPointOnTri(tri, point, out float4 bary);
|
||||
float4 normal = math.normalizesafe(point - nearestPoint);
|
||||
|
||||
// flip the contact normal if it points below ground: (doesn't work with holes)
|
||||
@@ -31,123 +33,30 @@ namespace Obi
|
||||
projectedPoint.normal = colliderToSolver.TransformDirection(normal);
|
||||
}
|
||||
|
||||
public static JobHandle GenerateContacts(ObiColliderWorld world,
|
||||
BurstSolverImpl solver,
|
||||
NativeList<Oni.ContactPair> contactPairs,
|
||||
NativeQueue<BurstContact> contactQueue,
|
||||
NativeArray<int> contactOffsetsPerType,
|
||||
float deltaTime,
|
||||
JobHandle inputDeps)
|
||||
public void Contacts(int colliderIndex,
|
||||
int rigidbodyIndex,
|
||||
NativeArray<BurstRigidbody> rigidbodies,
|
||||
|
||||
NativeArray<float4> positions,
|
||||
NativeArray<quaternion> orientations,
|
||||
NativeArray<float4> velocities,
|
||||
NativeArray<float4> radii,
|
||||
|
||||
NativeArray<int> simplices,
|
||||
in BurstAabb simplexBounds,
|
||||
int simplexIndex,
|
||||
int simplexStart,
|
||||
int simplexSize,
|
||||
|
||||
NativeQueue<BurstContact>.ParallelWriter contacts,
|
||||
int optimizationIterations,
|
||||
float optimizationTolerance)
|
||||
{
|
||||
int pairCount = contactOffsetsPerType[(int)Oni.ShapeType.Heightmap + 1] - contactOffsetsPerType[(int)Oni.ShapeType.Heightmap];
|
||||
if (pairCount == 0) return inputDeps;
|
||||
if (shape.dataIndex < 0) return;
|
||||
|
||||
var job = new GenerateHeightFieldContactsJob
|
||||
{
|
||||
contactPairs = contactPairs,
|
||||
triNormal = float4.zero;
|
||||
|
||||
positions = solver.positions,
|
||||
orientations = solver.orientations,
|
||||
velocities = solver.velocities,
|
||||
invMasses = solver.invMasses,
|
||||
radii = solver.principalRadii,
|
||||
|
||||
simplices = solver.simplices,
|
||||
simplexCounts = solver.simplexCounts,
|
||||
simplexBounds = solver.simplexBounds,
|
||||
|
||||
transforms = world.colliderTransforms.AsNativeArray<BurstAffineTransform>(),
|
||||
shapes = world.colliderShapes.AsNativeArray<BurstColliderShape>(),
|
||||
rigidbodies = world.rigidbodies.AsNativeArray<BurstRigidbody>(),
|
||||
|
||||
heightFieldHeaders = world.heightFieldContainer.headers.AsNativeArray<HeightFieldHeader>(),
|
||||
heightFieldSamples = world.heightFieldContainer.samples.AsNativeArray<float>(),
|
||||
|
||||
contactsQueue = contactQueue.AsParallelWriter(),
|
||||
|
||||
solverToWorld = solver.inertialFrame,
|
||||
worldToSolver = solver.worldToSolver,
|
||||
deltaTime = deltaTime,
|
||||
parameters = solver.abstraction.parameters,
|
||||
firstPair = contactOffsetsPerType[(int)Oni.ShapeType.Heightmap]
|
||||
};
|
||||
|
||||
inputDeps = job.Schedule(pairCount, 1, inputDeps);
|
||||
return inputDeps;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
struct GenerateHeightFieldContactsJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeList<Oni.ContactPair> contactPairs;
|
||||
|
||||
// particle arrays:
|
||||
[ReadOnly] public NativeArray<float4> velocities;
|
||||
[ReadOnly] public NativeArray<float4> positions;
|
||||
[ReadOnly] public NativeArray<quaternion> orientations;
|
||||
[ReadOnly] public NativeArray<float> invMasses;
|
||||
[ReadOnly] public NativeArray<float4> radii;
|
||||
|
||||
// simplex arrays:
|
||||
[ReadOnly] public NativeArray<int> simplices;
|
||||
[ReadOnly] public SimplexCounts simplexCounts;
|
||||
[ReadOnly] public NativeArray<BurstAabb> simplexBounds;
|
||||
|
||||
// collider arrays:
|
||||
[ReadOnly] public NativeArray<BurstAffineTransform> transforms;
|
||||
[ReadOnly] public NativeArray<BurstColliderShape> shapes;
|
||||
[ReadOnly] public NativeArray<BurstRigidbody> rigidbodies;
|
||||
|
||||
// height field data:
|
||||
[ReadOnly] public NativeArray<HeightFieldHeader> heightFieldHeaders;
|
||||
[ReadOnly] public NativeArray<float> heightFieldSamples;
|
||||
|
||||
[WriteOnly]
|
||||
[NativeDisableParallelForRestriction]
|
||||
public NativeQueue<BurstContact>.ParallelWriter contactsQueue;
|
||||
|
||||
// auxiliar data:
|
||||
[ReadOnly] public int firstPair;
|
||||
[ReadOnly] public BurstInertialFrame solverToWorld;
|
||||
[ReadOnly] public BurstAffineTransform worldToSolver;
|
||||
[ReadOnly] public float deltaTime;
|
||||
[ReadOnly] public Oni.SolverParameters parameters;
|
||||
|
||||
public void Execute(int i)
|
||||
{
|
||||
|
||||
int simplexIndex = contactPairs[firstPair + i].bodyA;
|
||||
int colliderIndex = contactPairs[firstPair + i].bodyB;
|
||||
var shape = shapes[colliderIndex];
|
||||
|
||||
if (shape.dataIndex < 0)
|
||||
return;
|
||||
|
||||
var header = heightFieldHeaders[shape.dataIndex];
|
||||
int rigidbodyIndex = shapes[colliderIndex].rigidbodyIndex;
|
||||
|
||||
int simplexStart = simplexCounts.GetSimplexStartAndSize(simplexIndex, out int simplexSize);
|
||||
var simplexBound = simplexBounds[simplexIndex];
|
||||
|
||||
BurstAffineTransform colliderToSolver = worldToSolver * transforms[colliderIndex];
|
||||
|
||||
// invert a full matrix here to accurately represent collider bounds scale.
|
||||
var solverToCollider = math.inverse(float4x4.TRS(colliderToSolver.translation.xyz, colliderToSolver.rotation, colliderToSolver.scale.xyz));
|
||||
var simplexBoundsCS = simplexBound.Transformed(solverToCollider);
|
||||
|
||||
BurstHeightField triangleMeshShape = new BurstHeightField()
|
||||
{
|
||||
colliderToSolver = colliderToSolver,
|
||||
shape = shapes[colliderIndex],
|
||||
header = heightFieldHeaders[shapes[colliderIndex].dataIndex],
|
||||
heightFieldSamples = heightFieldSamples
|
||||
};
|
||||
|
||||
float4 triNormal = float4.zero;
|
||||
|
||||
var co = new BurstContact { bodyA = simplexIndex, bodyB = colliderIndex };
|
||||
var co = new BurstContact() { bodyA = simplexIndex, bodyB = colliderIndex };
|
||||
|
||||
int resolutionU = (int)shape.center.x;
|
||||
int resolutionV = (int)shape.center.y;
|
||||
@@ -157,8 +66,8 @@ namespace Obi
|
||||
float cellHeight = shape.size.z / (resolutionV - 1);
|
||||
|
||||
// calculate particle bounds min/max cells:
|
||||
int2 min = new int2((int)math.floor(simplexBoundsCS.min[0] / cellWidth), (int)math.floor(simplexBoundsCS.min[2] / cellHeight));
|
||||
int2 max = new int2((int)math.floor(simplexBoundsCS.max[0] / cellWidth), (int)math.floor(simplexBoundsCS.max[2] / cellHeight));
|
||||
int2 min = new int2((int)math.floor(simplexBounds.min[0] / cellWidth), (int)math.floor(simplexBounds.min[2] / cellHeight));
|
||||
int2 max = new int2((int)math.floor(simplexBounds.max[0] / cellWidth), (int)math.floor(simplexBounds.max[2] / cellHeight));
|
||||
|
||||
for (int su = min[0]; su <= max[0]; ++su)
|
||||
{
|
||||
@@ -197,11 +106,11 @@ namespace Obi
|
||||
float4 v2 = new float4(max_x, h4, max_z, 0);
|
||||
float4 v3 = new float4(min_x, h1, min_z, 0);
|
||||
|
||||
triangleMeshShape.tri.Cache(v1, v2, v3);
|
||||
tri.Cache(v1, v2, v3);
|
||||
triNormal.xyz = math.normalizesafe(math.cross((v2 - v1).xyz, (v3 - v1).xyz));
|
||||
|
||||
var colliderPoint = BurstLocalOptimization.Optimize(ref triangleMeshShape, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out convexPoint, parameters.surfaceCollisionIterations, parameters.surfaceCollisionTolerance);
|
||||
var colliderPoint = BurstLocalOptimization.Optimize<BurstHeightField>(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out convexPoint, optimizationIterations, optimizationTolerance);
|
||||
|
||||
float4 velocity = float4.zero;
|
||||
float simplexRadius = 0;
|
||||
@@ -219,12 +128,12 @@ namespace Obi
|
||||
float dAB = math.dot(convexPoint - colliderPoint.point, colliderPoint.normal);
|
||||
float vel = math.dot(velocity - rbVelocity, colliderPoint.normal);
|
||||
|
||||
if (vel * deltaTime + dAB <= simplexRadius + shape.contactOffset + parameters.collisionMargin)
|
||||
if (vel * dt + dAB <= simplexRadius + shape.contactOffset + collisionMargin)
|
||||
{
|
||||
co.pointB = colliderPoint.point;
|
||||
co.normal = colliderPoint.normal * triangleMeshShape.shape.sign;
|
||||
co.normal = colliderPoint.normal;
|
||||
co.pointA = simplexBary;
|
||||
contactsQueue.Enqueue(co);
|
||||
contacts.Enqueue(co);
|
||||
}
|
||||
|
||||
// ------contact against the second triangle------:
|
||||
@@ -232,11 +141,11 @@ namespace Obi
|
||||
v2 = new float4(max_x, h4, max_z, 0);
|
||||
v3 = new float4(max_x, h2, min_z, 0);
|
||||
|
||||
triangleMeshShape.tri.Cache(v1, v2, v3);
|
||||
tri.Cache(v1, v2, v3);
|
||||
triNormal.xyz = math.normalizesafe(math.cross((v2 - v1).xyz, (v3 - v1).xyz));
|
||||
|
||||
colliderPoint = BurstLocalOptimization.Optimize(ref triangleMeshShape, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out convexPoint, parameters.surfaceCollisionIterations, parameters.surfaceCollisionTolerance);
|
||||
colliderPoint = BurstLocalOptimization.Optimize<BurstHeightField>(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out convexPoint, optimizationIterations, optimizationTolerance);
|
||||
|
||||
velocity = float4.zero;
|
||||
simplexRadius = 0;
|
||||
@@ -254,19 +163,21 @@ namespace Obi
|
||||
dAB = math.dot(convexPoint - colliderPoint.point, colliderPoint.normal);
|
||||
vel = math.dot(velocity - rbVelocity, colliderPoint.normal);
|
||||
|
||||
if (vel * deltaTime + dAB <= simplexRadius + shape.contactOffset + parameters.collisionMargin)
|
||||
if (vel * dt + dAB <= simplexRadius + shape.contactOffset + collisionMargin)
|
||||
{
|
||||
co.pointB = colliderPoint.point;
|
||||
co.normal = colliderPoint.normal * triangleMeshShape.shape.sign;
|
||||
co.normal = colliderPoint.normal;
|
||||
co.pointA = simplexBary;
|
||||
|
||||
contactsQueue.Enqueue(co);
|
||||
contacts.Enqueue(co);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user