Exemplo n.º 1
0
        internal static BepuColliderShape Compose(IReadOnlyList <IBepuAssetColliderShapeDesc> descs, BepuUtilities.Memory.BufferPool bufferPool)
        {
            if (descs == null)
            {
                return(null);
            }

            BepuColliderShape res = null;

            if (descs.Count == 1) // Single shape case
            {
                res = CreateShape(descs[0], bufferPool);
                if (res == null)
                {
                    return(null);
                }
                res.IsPartOfAsset = true;
            }
            else if (descs.Count > 1) // Need a compound shape in this case
            {
                var compound = new BepuCompoundColliderShape();
                foreach (var desc in descs)
                {
                    var subShape = CreateShape(desc, bufferPool);
                    if (subShape == null)
                    {
                        continue;
                    }
                    compound.AddChildShape(subShape);
                }
                res = compound;
                res.IsPartOfAsset = true;
            }

            return(res);
        }
Exemplo n.º 2
0
        public BepuColliderShape CreateShape(BepuUtilities.Memory.BufferPool bufferPool)
        {
            if (ConvexHulls == null)
            {
                return(null);
            }
            BepuColliderShape shape;

            // Optimize performance and focus on less shapes creation since this shape could be nested

            if (ConvexHulls.Count == 1)
            {
                var curMeshConvexHulls        = ConvexHulls[0];
                var curMeshConvexHullsIndices = ConvexHullsIndices[0];
                if (curMeshConvexHulls.Count == 1 && curMeshConvexHullsIndices[0].Count > 0)
                {
                    shape = new BepuConvexHullColliderShape(curMeshConvexHulls[0], curMeshConvexHullsIndices[0], Scaling, bufferPool)
                    {
                        NeedsCustomCollisionCallback = true,
                    };

                    //shape.UpdateLocalTransformations();
                    shape.Description = this;

                    return(shape);
                }

                if (curMeshConvexHulls.Count <= 1)
                {
                    return(null);
                }

                var subCompound = new BepuCompoundColliderShape
                {
                    NeedsCustomCollisionCallback = true,
                };

                for (int i = 0; i < curMeshConvexHulls.Count; i++)
                {
                    var verts   = curMeshConvexHulls[i];
                    var indices = curMeshConvexHullsIndices[i];

                    if (indices.Count == 0)
                    {
                        continue;
                    }

                    var subHull = new BepuConvexHullColliderShape(verts, indices, Scaling, bufferPool);
                    //subHull.UpdateLocalTransformations();
                    subCompound.AddChildShape(subHull);
                }

                //subCompound.UpdateLocalTransformations();
                subCompound.Description = this;

                return(subCompound);
            }

            if (ConvexHulls.Count <= 1)
            {
                return(null);
            }

            var compound = new BepuCompoundColliderShape
            {
                NeedsCustomCollisionCallback = true,
            };

            for (int i = 0; i < ConvexHulls.Count; i++)
            {
                var curMeshConvexHulls        = ConvexHulls[i];
                var curMeshConvexHullsIndices = ConvexHullsIndices[i];

                if (curMeshConvexHulls.Count == 1)
                {
                    if (curMeshConvexHullsIndices[0].Count == 0)
                    {
                        continue;
                    }

                    var subHull = new BepuConvexHullColliderShape(curMeshConvexHulls[0], curMeshConvexHullsIndices[0], Scaling, bufferPool);
                    //subHull.UpdateLocalTransformations();
                    compound.AddChildShape(subHull);
                }
                else if (curMeshConvexHulls.Count > 1)
                {
                    var subCompound = new BepuCompoundColliderShape();

                    // Loop through each hulls
                    for (int b = 0; b < curMeshConvexHulls.Count; b++)
                    {
                        var verts   = curMeshConvexHulls[b];
                        var indices = curMeshConvexHullsIndices[b];

                        if (indices.Count == 0)
                        {
                            continue;
                        }

                        var subHull = new BepuConvexHullColliderShape(verts, indices, Scaling, bufferPool);
                        //subHull.UpdateLocalTransformations();
                        subCompound.AddChildShape(subHull);
                    }

                    //subCompound.UpdateLocalTransformations();

                    compound.AddChildShape(subCompound);
                }
            }

            //compound.UpdateLocalTransformations();
            compound.Description = this;

            return(compound);
        }