浏览代码

Clean up arrays to stop leaks and stack overflows

/dots-physics
Steve Ewart 5 年前
当前提交
fec1adf2
共有 4 个文件被更改,包括 56 次插入31 次删除
  1. 30
      Assets/Unity Physics Items/BuoyancyVisualizer.cs
  2. 8
      Assets/Unity Physics Items/Systems/ApplyBuoyancyForceSystem.cs
  3. 31
      Assets/Unity Physics Items/Systems/DriveSystem.cs
  4. 18
      Assets/Unity Physics Items/Systems/GertsnerSystem.cs

30
Assets/Unity Physics Items/BuoyancyVisualizer.cs


private void OnDrawGizmos()
{
if (!Application.isPlaying)
if (!Application.isPlaying || (boat == Entity.Null))
Gizmos.color = Color.red;
// engine at first position
{
Gizmos.color = Color.green;
var enginePos = transform.TransformPoint(offsets[0].Value);
var waterPos = transform.TransformPoint(offsets[0].Value.x, heights[0].Value.y, offsets[0].Value.z);
Gizmos.DrawSphere(waterPos, .15f);
Gizmos.DrawLine(waterPos, enginePos);
}
for (int i = 0; i < heights.Length; i++)
Gizmos.DrawSphere(new Vector3(transform.position.x + offsets[i].Value.x, heights[i].Value.y, offsets[i].Value.z + transform.position.z), .1f);
}
{
Gizmos.color = Color.red;
for (int i = 1; i < heights.Length; i++)
{
var waterPos = transform.TransformPoint(offsets[0].Value.x, heights[0].Value.y, offsets[0].Value.z);
Gizmos.DrawSphere(transform.TransformPoint(offsets[i].Value.x, heights[i].Value.y, offsets[i].Value.z), .1f);
}
}
}
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
if (boat == Entity.Null)
{
boat = conversionSystem.GetPrimaryEntity(transform.parent);

8
Assets/Unity Physics Items/Systems/ApplyBuoyancyForceSystem.cs


[UpdateAfter(typeof(GertsnerSystem)), UpdateAfter(typeof(ExportPhysicsWorld))]
public class ApplyBuoyancyForceSystem : JobComponentSystem
{
float lastTime = 0;
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var offsets = GetBufferFromEntity<VoxelOffset>(false);

normals = GetComponentDataFromEntity<BuoyancyNormal>(true),
heightBuffer = heights
};
var simpleHandle = simpleJob.Schedule(simpleEntities.Length, 32, inputDeps);

{
[ReadOnly] public float dt;
[DeallocateOnJobCompletion]
[ReadOnly] public NativeArray<Entity> entities;
[ReadOnly] public ComponentDataFromEntity<Translation> translations;
[ReadOnly] public ComponentDataFromEntity<Rotation> rotations;

{
public float dt;
[ReadOnly] public NativeArray<Entity> entities;
[DeallocateOnJobCompletion]
[ReadOnly] public NativeArray<Entity> entities;
[ReadOnly] public ComponentDataFromEntity<BuoyancyNormal> normals;
[ReadOnly] public BufferFromEntity<VoxelHeight> heightBuffer;

31
Assets/Unity Physics Items/Systems/DriveSystem.cs


{
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var job = new DriveWithInputJob()
{
dt = Time.deltaTime,
var heights = GetBufferFromEntity<VoxelHeight>(false);
var job = new DriveWithInputJob()
{
dt = Time.deltaTime,
HeightBuffers = heights,
};
return job.Schedule(this, inputDeps);

{
public float dt;
[ReadOnly]
public BufferFromEntity<VoxelHeight> HeightBuffers;
public void Execute(Entity entity, int index, [ReadOnly] ref Translation pos, [ReadOnly] ref Rotation rot, ref PhysicsVelocity vel, [ReadOnly] ref PhysicsMass mass, ref DrivingData driveData, ref InputData inputData)
public void Execute(Entity entity, int index, [ReadOnly] ref Translation pos, [ReadOnly] ref Rotation rot, ref PhysicsVelocity vel, [ReadOnly] ref PhysicsMass mass, ref DrivingData driveData, ref InputData inputData)
float velMag = math.dot(vel.Linear, vel.Linear);
DynamicBuffer<VoxelHeight> heights = HeightBuffers[entity];
float velMag = math.dot(vel.Linear, vel.Linear);
var wp = math.transform(entityTransform, driveData.engineOffset);// + mass.CenterOfMass);
//if (wp.y <= -0.1f) // if the engine is deeper than 0.1
//{
inputData.throttle = Mathf.Clamp(inputData.throttle, 0f, 1f); // clamp for reasonable values
var engineWP = math.transform(entityTransform, driveData.engineOffset);
var waterWP = math.rotate(rot.Value, heights[0].Value) + engineWP;
var yHeight = heights[0].Value.y - engineWP.y;
//Debug.Log($"yHeight:{yHeight}");
//if (yHeight < -0.1f) // if the engine is deeper than 0.1
{
inputData.throttle = Mathf.Clamp(inputData.throttle, 0f, 1f); // clamp for reasonable values
float3 forward = math.forward(rot.Value);
forward.y = 0f;
forward = math.normalize(forward);

inputData.steering = Mathf.Clamp(inputData.steering, -1f, 1f); // clamp for reasonable values
var sTorque = new float3(0f, driveData.steeringTorque, -driveData.steeringTorque * .5f) * inputData.steering / mass.InverseInertia;
vel.ApplyAngularImpulse(mass, sTorque * dt);
//}
}
}
}
}

18
Assets/Unity Physics Items/Systems/GertsnerSystem.cs


protected override void OnCreate()
{
_waveCount = Water.Instance._waves.Length;
base.OnCreate();
_waveCount = Water.Instance._waves.Length;
}
base.OnCreate();
}
protected override void OnDestroy()
{
if(waveData != null)
waveData.Dispose();
protected override JobHandle OnUpdate(JobHandle inputDeps)
base.OnDestroy();
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var query = GetEntityQuery(typeof(Translation), typeof(Rotation), typeof(BuoyancyNormal));
var entities = query.ToEntityArray(Allocator.TempJob);

[ReadOnly] public float time;
[ReadOnly] public NativeArray<Entity> entities;
[DeallocateOnJobCompletion]
[ReadOnly] public NativeArray<Entity> entities;
[ReadOnly] public ComponentDataFromEntity<Translation> translations;
[ReadOnly] public ComponentDataFromEntity<Rotation> rotations;
[ReadOnly] public BufferFromEntity<VoxelOffset> offsetBuffer;

正在加载...
取消
保存