public VoxelHitData(Unity.Physics.RaycastHit _hit, Unity.Entities.Entity _entity) { rayInfo = _hit; hitTransform = GameMaster.Instance.entitiesToObjectMap[_entity]; component = hitTransform.GetComponent <VoxelComponent>(); if (component == null) { return; } voxelObject = component.voxelObject; _hit.Position += (_hit.SurfaceNormal * 0.1f); // Pad the ray to penetrate into the voxel's space worldNormal = _hit.SurfaceNormal; worldNormal.Normalize(); localNormal = hitTransform.InverseTransformDirection(_hit.SurfaceNormal); localNormal.Normalize(); voxelPosition = hitTransform.InverseTransformPoint(_hit.Position); voxelPosition -= (component.pivotPoint + (Vector3.one * 0.5f)); // Offset to match voxel coordinates voxelPosition.x = (float)Math.Round(voxelPosition.x); voxelPosition.y = (float)Math.Round(voxelPosition.y); voxelPosition.z = (float)Math.Round(voxelPosition.z); voxelPosition -= localNormal; // Reverse centering localPosition = voxelPosition + component.pivotPoint + (Vector3.one * 0.5f); worldPosition = hitTransform.position + (hitTransform.rotation * localPosition); localFacePosition = (localPosition + localNormal); voxelFacePosition = (voxelPosition + localNormal); worldFacePosition = (worldPosition + worldNormal); atHit = Coord3D.fromVector3(voxelPosition); // The coord of the voxel that was hit atFace = Coord3D.fromVector3(voxelFacePosition); // The coord of the voxel at the face that was hit }
// This should be a job rather than happening on the main thread. However, it doesn't really take a long time, and happens infrequently. protected override void OnUpdate() { var physicsWorldSystem = World.Active.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; Entities.With(mRaycastRequestsQuery).ForEach((Entity entity, ref RayRequest r) => { Unity.Physics.RaycastInput input = new Unity.Physics.RaycastInput() { Start = r.Value.Origin, End = r.Value.Displacement, Filter = Unity.Physics.CollisionFilter.Default, }; Debug.DrawLine(input.Start, input.End, Color.blue, 0.5f); var hit = new Unity.Physics.RaycastHit(); if (collisionWorld.CastRay(input, out hit)) { var e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; if (EntityManager.HasComponent <IFCGuidUIPosition>(e)) { PostUpdateCommands.RemoveComponent <IFCGuidUIPosition>(e); PostUpdateCommands.AddComponent <DeselectedTag>(e); } else { PostUpdateCommands.AddComponent(e, new IFCGuidUIPosition { Value = hit.Position }); } } PostUpdateCommands.DestroyEntity(entity); }); }