using GarmentButton.PhysicFillters; using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Mathematics; using Unity.Physics; using UnityEngine; namespace GarmentButton.PathFinding.Editor { [DisableAutoCreation] [BurstCompile] public partial struct GetPointsHeightsSystemEditor : ISystem { [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); state.RequireForUpdate(); } public void OnDestroy(ref SystemState state) { if (SystemAPI.TryGetSingletonRW(out var index)) if (index.ValueRW.Value.IsCreated) index.ValueRW.Value.Dispose(); if (SystemAPI.TryGetSingletonRW(out var height)) if (height.ValueRW.Value.IsCreated) height.ValueRW.Value.Dispose(); } [BurstCompile] public void OnUpdate(ref SystemState state) { var physic = SystemAPI.GetSingleton().PhysicsWorld; var entity = SystemAPI.GetSingletonEntity(); var worldIndex = SystemAPI.GetSingletonRW(); var lenght = worldIndex.ValueRO.Value.Length; var heights = new NativeArray(lenght, Allocator.Persistent); var Direction = new NativeArray(8, Allocator.Persistent); Direction[0] = new float2(0, 0.1f); Direction[1] = new float2(0.1f, 0.1f); Direction[2] = new float2(0, 0.1f); Direction[3] = new float2(-0.1f, 0.1f); Direction[4] = new float2(-0.1f, 0); Direction[5] = new float2(-0.1f, -0.1f); Direction[6] = new float2(0, -0.1f); Direction[7] = new float2(0.1f, -0.1f); for (int i = 0; i < lenght; i++) { var position = worldIndex.ValueRO.Value[i]; var filler = CollisionFilter.Default; filler.CollidesWith = (uint)CollisionLayer.Ground | (uint)(CollisionLayer.AIGround); float height = float.MinValue; for (global::System.Int32 d = 0; d < Direction.Length; d++) { var offest = Direction[d]; var ray = new RaycastInput { Start = new float3(position.x + offest.x, 100, position.y + offest.y), End = new float3(position.x + offest.x, -100, position.y + offest.y), Filter = filler, }; var raycast = physic.CastRay(ray, out var hit); if (raycast && hit.Position.y > height) { height = hit.Position.y; Debug.DrawLine(hit.Position, hit.Position + new float3(0, - 5 , 0),Color.red,10); } } if(height == float.MinValue) height = 0; heights[i] = height; } worldIndex.ValueRW.Value.Dispose(); state.EntityManager.RemoveComponent(entity); state.EntityManager.AddComponentData(entity, new WorldHeights { Value = heights }); } } public struct WorldIndexs : IComponentData { public NativeArray Value; } public struct WorldHeights : IComponentData { public NativeArray Value; } }