Beispiel #1
0
            JobHandle ScheduleMeshPreview(PhysicsShapeAuthoring shape, NativeArray <BlobAssetReference <Collider> > output)
            {
                var points  = new NativeList <float3>(65535, Allocator.Temp);
                var indices = new NativeList <int>(65535, Allocator.Temp);

                shape.GetBakedMeshProperties(points, indices);

                // copy to NativeArray because NativeList not yet compatible with DeallocateOnJobCompletion
                var pointsArray = new NativeArray <float3>(
                    points.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory
                    );

                pointsArray.CopyFrom(points);
                var indicesArray = new NativeArray <int>(
                    indices.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory
                    );

                indicesArray.CopyFrom(indices);

                // TODO: if there is still an active job with the same input data hash, then just set it to be most recently scheduled job
                return(new CreateTempMeshJob
                {
                    Points = pointsArray,
                    Indices = indicesArray,
                    Output = output
                }.Schedule());
            }
 /// <summary>
 /// Retreives the Transforms and PhysicsShapeAuthorings' from the children of the GameObject
 /// </summary>
 /// <param name="rigComponent"></param>
 /// <param name="children"></param>
 /// <param name="childrenShapes"></param>
 void GetData(RigComponent rigComponent, out Transform[] children, out PhysicsShapeAuthoring[] childrenShapes)
 {
     children       = new Transform[rigComponent.Bones.Length];
     childrenShapes = new PhysicsShapeAuthoring[rigComponent.Bones.Length];
     for (int i = 0; i < rigComponent.Bones.Length; i++)
     {
         children[i]       = rigComponent.Bones[i];
         childrenShapes[i] = children[i].GetComponent <PhysicsShapeAuthoring>();
     }
 }
Beispiel #3
0
            public void SchedulePreviewIfChanged(PhysicsShapeAuthoring shape)
            {
                using (var currentPoints = new NativeList <float3>(65535, Allocator.Temp))
                {
                    var hash = GetInputHash(
                        shape, currentPoints, m_HashedPoints, m_HashedConvexParameters, out var currentConvexParameters
                        );
                    if (m_InputHash == hash)
                    {
                        return;
                    }

                    m_InputHash = hash;
                    m_HashedConvexParameters = currentConvexParameters;
                    m_HashedPoints.Dispose();
                    m_HashedPoints = new NativeArray <float3>(currentPoints.Length, Allocator.Persistent);
                    m_HashedPoints.CopyFrom(currentPoints);
                }

                if (shape.ShapeType != ShapeType.ConvexHull && shape.ShapeType != ShapeType.Mesh)
                {
                    return;
                }

                // TODO: cache results per input data hash, and simply use existing data (e.g., to make undo/redo faster)
                var output = new NativeArray <BlobAssetReference <Collider> >(1, Allocator.Persistent);

                m_MostRecentlyScheduledJob = shape.ShapeType == ShapeType.Mesh
                    ? ScheduleMeshPreview(shape, output)
                    : ScheduleConvexHullPreview(shape, output);
                m_PreviewJobsOutput.Add(m_MostRecentlyScheduledJob, output);
                if (m_PreviewJobsOutput.Count == 1)
                {
                    CheckPreviewJobsForCompletion();
                    EditorApplication.update    += CheckPreviewJobsForCompletion;
                    EditorApplication.delayCall += () =>
                    {
                        SceneViewUtility.DisplayProgressNotification(
                            Styles.PreviewGenerationNotification, () => float.PositiveInfinity
                            );
                    };
                }
            }
Beispiel #4
0
        PreviewMeshData GetPreviewData(PhysicsShapeAuthoring shape)
        {
            if (shape.ShapeType != ShapeType.ConvexHull && shape.ShapeType != ShapeType.Mesh)
            {
                return(null);
            }

            if (!m_PreviewData.TryGetValue(shape, out var preview))
            {
                preview = m_PreviewData[shape] = new PreviewMeshData();
                preview.SchedulePreviewIfChanged(shape);
            }

            // do not generate a new preview until the user has finished dragging a control handle (e.g., scale)
            if (m_DraggingControlID == 0 && !EditorGUIUtility.editingTextField)
            {
                preview.SchedulePreviewIfChanged(shape);
            }

            return(preview);
        }
Beispiel #5
0
        void UpdateGeometryState()
        {
            m_GeometryState = GeometryState.Okay;
            var skinnedPoints = new NativeList <float3>(8192, Allocator.Temp);

            foreach (PhysicsShapeAuthoring shape in targets)
            {
                // if a custom mesh is assigned, only check it
                using (var so = new SerializedObject(shape))
                {
                    var customMesh = so.FindProperty(m_CustomMesh.propertyPath).objectReferenceValue as UnityMesh;
                    if (customMesh != null)
                    {
                        m_GeometryState |= GetGeometryState(customMesh, shape.gameObject);
                        continue;
                    }
                }

                // otherwise check all mesh filters in the hierarchy that might be included
                var geometryState = GeometryState.Okay;
                foreach (var meshFilter in PhysicsShapeAuthoring.GetAllMeshFiltersInHierarchyBelongingToShape(shape, false))
                {
                    geometryState |= GetGeometryState(meshFilter.sharedMesh, shape.gameObject);
                }

                if (shape.ShapeType == ShapeType.Mesh)
                {
                    PhysicsShapeAuthoring.GetAllSkinnedPointsInHierarchyBelongingToShape(shape, skinnedPoints, false);
                    if (skinnedPoints.Length > 0)
                    {
                        geometryState |= GeometryState.MeshWithSkinnedPoints;
                    }
                }

                m_GeometryState |= geometryState;
            }
            skinnedPoints.Dispose();
        }
Beispiel #6
0
            unsafe uint GetInputHash(
                PhysicsShapeAuthoring shape,
                NativeList <float3> currentPoints,
                NativeArray <float3> hashedPoints,
                ConvexHullGenerationParameters hashedConvexParameters,
                out ConvexHullGenerationParameters currentConvexProperties
                )
            {
                currentConvexProperties = default;
                switch (shape.ShapeType)
                {
                case ShapeType.ConvexHull:
                    shape.GetBakedConvexProperties(currentPoints, out currentConvexProperties);

                    return(math.hash(
                               new uint3(
                                   (uint)shape.ShapeType,
                                   currentConvexProperties.GetStableHash(hashedConvexParameters),
                                   currentPoints.GetStableHash(hashedPoints)
                                   )
                               ));

                case ShapeType.Mesh:
                    var indices = new NativeList <int>(65535, Allocator.Temp);
                    shape.GetBakedMeshProperties(currentPoints, indices);

                    return(math.hash(
                               new uint3(
                                   (uint)shape.ShapeType,
                                   currentPoints.GetStableHash(hashedPoints),
                                   math.hash(indices.GetUnsafePtr(), UnsafeUtility.SizeOf <int>() * indices.Length)
                                   )
                               ));

                default:
                    return((uint)shape.ShapeType);
                }
            }
Beispiel #7
0
            JobHandle ScheduleConvexHullPreview(PhysicsShapeAuthoring shape, NativeArray <BlobAssetReference <Collider> > output)
            {
                var pointCloud = new NativeList <float3>(65535, Allocator.Temp);

                shape.GetBakedConvexProperties(pointCloud, out var generationParameters);

                // copy to NativeArray because NativeList not yet compatible with DeallocateOnJobCompletion
                var pointsArray = new NativeArray <float3>(
                    pointCloud.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory
                    );

                pointsArray.CopyFrom(pointCloud);

                // TODO: if there is still an active job with the same input data hash, then just set it to be most recently scheduled job
                var hullGenerationParameters = generationParameters;

                return(new CreateTempHullJob
                {
                    GenerationParameters = hullGenerationParameters.ToRunTime(),
                    Points = pointsArray,
                    Output = output
                }.Schedule());
            }
    /// <summary>
    /// Generates a collider using a PhysicsShapeAuthoring and PhysicsMaterialsExtensionComponent
    /// </summary>
    /// <param name="shape"></param>
    /// <param name="shapeExt"></param>
    /// <param name="offsetPosition"></param>
    /// <param name="offsetRotation"></param>
    /// <returns></returns>
    BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, PhysicsMaterialsExtensionComponent shapeExt, out float3 offsetPosition, out quaternion offsetRotation)
    {
        CollisionFilter filter = new CollisionFilter
        {
            CollidesWith = shape.CollidesWith.Value,
            BelongsTo    = shape.BelongsTo.Value,
            GroupIndex   = 0
        };

        Unity.Physics.Material material = new Unity.Physics.Material
        {
            CollisionResponse        = shape.CollisionResponse,
            CustomTags               = shape.CustomTags.Value,
            Friction                 = shape.Friction.Value,
            FrictionCombinePolicy    = shape.Friction.CombineMode,
            Restitution              = shape.Restitution.Value,
            RestitutionCombinePolicy = shape.Restitution.CombineMode,
            EnableMassFactors        = shapeExt != null ? shapeExt.EnableMassFactors : false,
            EnableSurfaceVelocity    = shapeExt != null ? shapeExt.EnableSurfaceVelocity : false
        };
        switch (shape.ShapeType)
        {
        case ShapeType.Box:
            var boxProperties = shape.GetBoxProperties();
            offsetPosition = boxProperties.Center;
            offsetRotation = boxProperties.Orientation;
            return(Unity.Physics.BoxCollider.Create(boxProperties, filter, material));

        case ShapeType.Capsule:
            var capsuleProperties = shape.GetCapsuleProperties();
            var capsuleGeometry   = new CapsuleGeometry
            {
                Radius  = capsuleProperties.Radius,
                Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius,
                Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius
            };
            offsetPosition = capsuleProperties.Center;
            offsetRotation = capsuleProperties.Orientation;
            return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry, filter, material));

        case ShapeType.Cylinder:
            var cylinderProperties = shape.GetCylinderProperties();
            offsetPosition = cylinderProperties.Center;
            offsetRotation = cylinderProperties.Orientation;
            return(CylinderCollider.Create(cylinderProperties, filter, material));

        case ShapeType.Sphere:
            var sphereProperties = shape.GetSphereProperties(out var orientation);
            var SphereGeometry   = new SphereGeometry
            {
                Center = sphereProperties.Center,
                Radius = sphereProperties.Radius
            };
            offsetPosition = sphereProperties.Center;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.SphereCollider.Create(SphereGeometry, filter, material));

        case ShapeType.ConvexHull:
            NativeList <float3> points = new NativeList <float3>(Allocator.Temp);
            shape.GetConvexHullProperties(points);
            var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters, filter, material);
            //    points.Dispose();
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(ConvexCollider);

        case ShapeType.Mesh:
            NativeList <float3> verts = new NativeList <float3>(Allocator.Temp);
            NativeList <int3>   tris  = new NativeList <int3>(Allocator.Temp);
            shape.GetMeshProperties(verts, tris);
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.MeshCollider.Create(verts, tris, filter, material));

        default:
            UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\"");
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(new BlobAssetReference <Unity.Physics.Collider>());
        }
    }
Beispiel #9
0
    BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, out float3 offsetPosition, out quaternion offsetRotation)
    {
        switch (shape.ShapeType)
        {
        case ShapeType.Box:
            var boxProperties = shape.GetBoxProperties();
            offsetPosition = boxProperties.Center;
            offsetRotation = boxProperties.Orientation;
            return(Unity.Physics.BoxCollider.Create(boxProperties));

        case ShapeType.Capsule:
            var capsuleProperties = shape.GetCapsuleProperties();
            var capsuleGeometry   = new CapsuleGeometry
            {
                Radius  = capsuleProperties.Radius,
                Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius,
                Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius
            };
            offsetPosition = capsuleProperties.Center;
            offsetRotation = capsuleProperties.Orientation;
            return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry));

        case ShapeType.Cylinder:
            var cylinderProperties = shape.GetCylinderProperties();
            offsetPosition = cylinderProperties.Center;
            offsetRotation = cylinderProperties.Orientation;
            return(CylinderCollider.Create(cylinderProperties));

        case ShapeType.Sphere:
            var sphereProperties = shape.GetSphereProperties(out var orientation);
            var SphereGeometry   = new SphereGeometry
            {
                Center = sphereProperties.Center,
                Radius = sphereProperties.Radius
            };
            offsetPosition = sphereProperties.Center;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.SphereCollider.Create(SphereGeometry));

        case ShapeType.ConvexHull:
            NativeList <float3> points = new NativeList <float3>(Allocator.Temp);
            shape.GetConvexHullProperties(points);
            var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters);
            //    points.Dispose();
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(ConvexCollider);

        case ShapeType.Mesh:
            NativeList <float3> verts = new NativeList <float3>(Allocator.Temp);
            NativeList <int3>   tris  = new NativeList <int3>(Allocator.Temp);
            shape.GetMeshProperties(verts, tris);
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(Unity.Physics.MeshCollider.Create(verts, tris));

        default:
            UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\"");
            offsetPosition = float3.zero;
            offsetRotation = quaternion.identity;
            return(new BlobAssetReference <Unity.Physics.Collider>());
        }
    }
Beispiel #10
0
 public void OneTimeSetUp()
 {
     m_Shape = new GameObject(GetType().Name, typeof(PhysicsShapeAuthoring)).GetComponent <PhysicsShapeAuthoring>();
 }
Beispiel #11
0
 public void SetUp() => m_Shape = new GameObject("Shape").AddComponent <PhysicsShapeAuthoring>();
Beispiel #12
0
 public CropSection3DClass(PhysicsShapeAuthoring shapeA, PhysicsShapeAuthoring shapeB, bool debug = false)
 {
     Bounds                  = new AColliderClass(shapeA);
     CropSectionBounds       = new AColliderClass(shapeB);
     CropSection             = new CropSection3D(Bounds.collider, CropSectionBounds.collider, float3.zero, float3.zero);
     TranslationOffsetSlider = new Float3Slider(CropSection.TranslationOffsetMin, CropSection.TranslationOffsetMax,