System to visually inspect PhysicsJoint
and PhysicsConstrainedBodyPair
data
Written for entities 1.0.16, physics 1.0.16
// src* https://gist.github.com/andrew-raphael-lukasik/aa88cf6f32634126849dfe176f11a955 | |
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Collections; | |
using Unity.Mathematics; | |
using Unity.Transforms; | |
using Unity.Physics; | |
using Unity.Physics.Extensions; | |
using Unity.Physics.Systems; | |
using Unity.Jobs; | |
namespace EditorOnly.Authoring | |
{ | |
[Unity.Burst.BurstCompile] | |
[RequireMatchingQueriesForUpdate] | |
[WorldSystemFilter( WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor )] | |
[UpdateInGroup( typeof(Unity.Physics.Authoring.PhysicsDisplayDebugGroup) )] | |
public partial struct PhysicsJointDebugSystem : ISystem | |
{ | |
ComponentLookup<LocalToWorld> _lookupLTW; | |
[Unity.Burst.BurstCompile] | |
public void OnCreate ( ref SystemState state ) | |
{ | |
_lookupLTW = state.GetComponentLookup<LocalToWorld>( isReadOnly:true ); | |
state.RequireForUpdate<SystemStartRequest>(); | |
} | |
[Unity.Burst.BurstCompile] | |
public void OnDestroy ( ref SystemState state ) {} | |
[Unity.Burst.BurstCompile] | |
public void OnUpdate ( ref SystemState state ) | |
{ | |
_lookupLTW.Update( ref state ); | |
state.EntityManager.CompleteDependencyBeforeRO<LocalToWorld>(); | |
foreach( var( pj , pcb ) in SystemAPI.Query< RefRO<PhysicsJoint> , RefRO<PhysicsConstrainedBodyPair> >() ) | |
{ | |
Entity entityA = pcb.ValueRO.EntityA; | |
Entity entityB = pcb.ValueRO.EntityB; | |
var ltwA = _lookupLTW[entityA]; | |
var ltwB = _lookupLTW[entityB]; | |
var bodyAFromJoint = pj.ValueRO.BodyAFromJoint; | |
var bodyBFromJoint = pj.ValueRO.BodyBFromJoint; | |
Debug.DrawLine( ltwA.Position , ltwA.Value.TransformPoint(bodyAFromJoint.Position) , Color.cyan ); | |
Debug.DrawRay( ltwA.Position , ltwA.Value.TransformDirection(bodyAFromJoint.Axis) , Color.red ); | |
Debug.DrawRay( ltwA.Position , ltwA.Value.TransformDirection(bodyAFromJoint.PerpendicularAxis) , Color.green ); | |
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.Position) , Color.yellow ); | |
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.Axis) , Color.red ); | |
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.PerpendicularAxis) , Color.green ); | |
} | |
} | |
public struct SystemStartRequest : IComponentData {} | |
} | |
} |