private void FindFracturedBlocks(HkdBreakableBodyInfo b)
        {
            ProfilerShort.Begin("DBHelper");
            var dbHelper = new HkdBreakableBodyHelper(b);
            ProfilerShort.BeginNextBlock("GetRBMatrix");
            var bodyMatrix = dbHelper.GetRigidBodyMatrix();
            ProfilerShort.BeginNextBlock("SearchChildren");
            dbHelper.GetChildren(m_children);
            foreach (var child in m_children)
            {
                if (!child.IsFracturePiece())
                    continue;
                //var blockPosWorld = ClusterToWorld(Vector3.Transform(child.GetTransform().Translation, bodyMatrix));
                var bShape = child.Shape;
                HkVec3IProperty pProp = bShape.GetProperty(HkdBreakableShape.PROPERTY_GRID_POSITION);
                var blockPos = pProp.Value; //Vector3I.Round(child.GetTransform().Translation / m_grid.GridSize);
                if (!m_grid.CubeExists(blockPos))
                {
                    //Debug.Fail("FindFracturedBlocks:Fracture piece missing block");//safe to ignore
                    continue;
                }

                if (MyFakes.ENABLE_FRACTURE_COMPONENT)
                {
                    var block = m_grid.GetCubeBlock(blockPos);
                    if (block == null)
                        continue;

                    if (!FindFractureComponentBlocks(block, child))
                        continue;
                }
                else
                {
                    if (!m_fracturedBlocksShapes.ContainsKey(blockPos))
                        m_fracturedBlocksShapes[blockPos] = new List<HkdShapeInstanceInfo>();
                    m_fracturedBlocksShapes[blockPos].Add(child);
                }
            }
            ProfilerShort.BeginNextBlock("CreateFreacturedBlocks");
            if (!MyFakes.ENABLE_FRACTURE_COMPONENT)
            {
                foreach (var key in m_fracturedBlocksShapes.Keys)
                {
                    HkdBreakableShape shape;
                    var shapeList = m_fracturedBlocksShapes[key];
                    foreach (var s in shapeList)
                    {
                        var matrix = s.GetTransform();
                        matrix.Translation = Vector3.Zero;
                        s.SetTransform(ref matrix);
                    }
                    ProfilerShort.Begin("CreateShape");
                    HkdBreakableShape compound = new HkdCompoundBreakableShape(null, shapeList);
                    ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                    var mp = new HkMassProperties();
                    compound.BuildMassProperties(ref mp);
                    shape = compound;
                    var sh = compound.GetShape();
                    shape = new HkdBreakableShape(sh, ref mp);
                    //shape.SetMassProperties(mp); //important! pass mp to constructor
                    foreach (var si in shapeList)
                    {
                        var siRef = si;
                        shape.AddShape(ref siRef);
                    }
                    compound.RemoveReference();
                    ProfilerShort.BeginNextBlock("Connect");
                    //shape.SetChildrenParent(shape);
                    ConnectPiecesInBlock(shape, shapeList);
                    ProfilerShort.End();

                    var info = new MyFracturedBlock.Info()
                    {
                        Shape = shape,
                        Position = key,
                        Compound = true,
                    };
                    var originalBlock = m_grid.GetCubeBlock(key);
                    if (originalBlock == null)
                    {
                        //Debug.Fail("Missing fracture piece original block.");//safe to ignore
                        shape.RemoveReference();
                        continue;
                    }
                    Debug.Assert(originalBlock != null);
                    if (originalBlock.FatBlock is MyFracturedBlock)
                    {
                        var fractured = originalBlock.FatBlock as MyFracturedBlock;
                        info.OriginalBlocks = fractured.OriginalBlocks;
                        info.Orientations = fractured.Orientations;
                        info.MultiBlocks = fractured.MultiBlocks;
                    }
                    else if (originalBlock.FatBlock is MyCompoundCubeBlock)
                    {
                        info.OriginalBlocks = new List<MyDefinitionId>();
                        info.Orientations = new List<MyBlockOrientation>();
                        MyCompoundCubeBlock compoundBlock = originalBlock.FatBlock as MyCompoundCubeBlock;
                        bool hasMultiBlockPart = false;
                        var blocksInCompound = compoundBlock.GetBlocks();
                        foreach (var block in blocksInCompound)
                        {
                            info.OriginalBlocks.Add(block.BlockDefinition.Id);
                            info.Orientations.Add(block.Orientation);

                            hasMultiBlockPart = hasMultiBlockPart || block.IsMultiBlockPart;
                        }

                        if (hasMultiBlockPart)
                        {
                            info.MultiBlocks = new List<MyFracturedBlock.MultiBlockPartInfo>();

                            foreach (var block in blocksInCompound)
                            {
                                if (block.IsMultiBlockPart)
                                    info.MultiBlocks.Add(new MyFracturedBlock.MultiBlockPartInfo() { MultiBlockDefinition = block.MultiBlockDefinition.Id, MultiBlockId = block.MultiBlockId });
                                else
                                    info.MultiBlocks.Add(null);
                            }
                        }
                    }
                    else
                    {
                        info.OriginalBlocks = new List<MyDefinitionId>();
                        info.Orientations = new List<MyBlockOrientation>();
                        info.OriginalBlocks.Add(originalBlock.BlockDefinition.Id);
                        info.Orientations.Add(originalBlock.Orientation);

                        if (originalBlock.IsMultiBlockPart)
                        {
                            info.MultiBlocks = new List<MyFracturedBlock.MultiBlockPartInfo>();
                            info.MultiBlocks.Add(new MyFracturedBlock.MultiBlockPartInfo() { MultiBlockDefinition = originalBlock.MultiBlockDefinition.Id, MultiBlockId = originalBlock.MultiBlockId });
                        }
                    }
                    m_fractureBlocksCache.Add(info);
                }
            }
            m_fracturedBlocksShapes.Clear();
            m_children.Clear();

            ProfilerShort.End();
        }
        /// <summary>
        /// Searches for blocks which will create fracture components from cached m_fracturedSlimBlocksShapes
        /// </summary>
        private void FindFractureComponentBlocks()
        {
            Debug.Assert(MyFakes.ENABLE_FRACTURE_COMPONENT);

            foreach (var pair in m_fracturedSlimBlocksShapes)
            {
                var slimBlock = pair.Key;
                var shapeList = pair.Value;

                if (slimBlock.FatBlock.Components.Has<MyFractureComponentBase>())
                {
                    // Block has fracture component - ignore
                    continue;
                }
                else
                {
                    int totalBreakableShapesCountForModel = slimBlock.GetTotalBreakableShapeChildrenCount();
                    Debug.Assert(shapeList.Count <= totalBreakableShapesCountForModel);
                    // No removed pieces? Then ignore.
                    if (slimBlock.BlockDefinition.CreateFracturedPieces && totalBreakableShapesCountForModel == shapeList.Count)
                        continue;

                    foreach (var s in shapeList)
                    {
                        s.SetTransform(ref Matrix.Identity);
                    }
                    ProfilerShort.Begin("CreateShapeComponent");
                    HkdBreakableShape compound = new HkdCompoundBreakableShape(null, shapeList);
                    ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                    var mp = new HkMassProperties();
                    compound.BuildMassProperties(ref mp);
                    HkdBreakableShape shape = compound;
                    var sh = compound.GetShape();
                    shape = new HkdBreakableShape(sh, ref mp);
                    //shape.SetMassProperties(mp); //important! pass mp to constructor
                    foreach (var si in shapeList)
                    {
                        var siRef = si;
                        shape.AddShape(ref siRef);
                    }
                    compound.RemoveReference();

                    ProfilerShort.BeginNextBlock("Connect");
                    //shape.SetChildrenParent(shape);
                    ConnectPiecesInBlock(shape, shapeList);

                    MyFractureComponentBase.Info info = new MyFractureComponentBase.Info()
                    {
                        Entity = slimBlock.FatBlock,
                        Shape = shape,
                        Compound = true
                    };

                    m_fractureBlockComponentsCache.Add(info);

                    ProfilerShort.End();
                }
            }

            m_fracturedSlimBlocksShapes.Clear();
        }
Ejemplo n.º 3
0
        public override void Init(MyObjectBuilder_CubeBlock builder, MyCubeGrid cubeGrid)
        {
            base.Init(builder, cubeGrid);
            ProfilerShort.Begin("FP.Init");
            CheckConnectionAllowed = true;
            var ob = builder as MyObjectBuilder_FracturedBlock;
            if (ob.Shapes.Count == 0)
            {
                ProfilerShort.End();
                return;
            }
            
            OriginalBlocks = new List<MyDefinitionId>();
            Orientations = new List<MyBlockOrientation>();
            var lst = new List<HkdShapeInstanceInfo>();
            foreach (var def in ob.BlockDefinitions)
            {
                var blockDef = MyDefinitionManager.Static.GetCubeBlockDefinition(def);
                if (MyModels.GetModelOnlyData(blockDef.Model).HavokBreakableShapes == null)
                {
                    MyDestructionData.Static.LoadModelDestruction(blockDef, false, Vector3.One);
                }
                var shape = MyModels.GetModelOnlyData(blockDef.Model).HavokBreakableShapes[0];
                var si = new HkdShapeInstanceInfo(shape, null, null);
                lst.Add(si);
                m_children.Add(si);
                shape.GetChildren(m_children);
                OriginalBlocks.Add(def);
            }
            foreach (var or in ob.BlockOrientations)
            {
                Orientations.Add(or);
            }
            m_shapes.AddRange(ob.Shapes);
            for (int i = 0; i < m_children.Count; i++)
            {
                var child = m_children[i];
                Func<MyObjectBuilder_FracturedBlock.ShapeB, bool> x = s => s.Name == child.ShapeName;
                var result = m_shapes.Where(x);
                if (result.Count() > 0)
                {
                    var found = result.First();
                    var m = Matrix.CreateFromQuaternion(found.Orientation);
                    m.Translation = child.GetTransform().Translation;
                    var si = new HkdShapeInstanceInfo(child.Shape.Clone(), m);
                    if(found.Fixed)
                        si.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                    lst.Add(si);
                    m_shapeInfos.Add(si);
                    m_shapes.Remove(found);
                }
                else
                {
                    child.GetChildren(m_children);
                }
            }

            if (m_shapeInfos.Count == 0)
            {
                m_children.Clear();
                ProfilerShort.End();
                Debug.Fail("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
            }

            foreach (var shape in m_shapeInfos)
            {
                if(! string.IsNullOrEmpty(shape.Shape.Name))
                    Render.AddPiece(shape.Shape.Name, Matrix.CreateFromQuaternion(Quaternion.CreateFromRotationMatrix(shape.GetTransform().GetOrientation())));
            }

            if (CubeGrid.CreatePhysics)
            {
                HkdBreakableShape compound = new HkdCompoundBreakableShape(null, m_shapeInfos);
                ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                Shape = compound;
                var mp = new HkMassProperties();
                compound.BuildMassProperties(ref mp);
                Shape = new HkdBreakableShape(compound.GetShape(), ref mp);
                compound.RemoveReference();
                foreach (var si in m_shapeInfos)
                {
                    var siRef = si;
                    Shape.AddShape(ref siRef);
                }
                Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                CreateMountPoints();
            }
            m_children.Clear();
            foreach (var si in m_shapeInfos)
                si.Shape.RemoveReference();
            foreach (var si in lst)
                si.RemoveReference();
            m_shapeInfos.Clear();

            ProfilerShort.End();
        }
        protected override void RecreateShape(List<MyObjectBuilder_FractureComponentBase.FracturedShape> shapeList)
        {
            Debug.Assert(m_tmpChildren.Count == 0);
            Debug.Assert(m_tmpShapeInfos.Count == 0);

            ProfilerShort.Begin("FractureComponent.RecreateShape");

            if (Shape.IsValid())
            {
                Shape.RemoveReference();
                Shape = new HkdBreakableShape();
            }

            var render = Block.FatBlock.Render as MyRenderComponentFracturedPiece;
            if (render != null)
            {
                render.ClearModels();
                render.UpdateRenderObject(false);
            }
            else
            {
                Debug.Fail("Invalid render type");
            }

            if (shapeList.Count == 0)
            {
                ProfilerShort.End();
                return;
            }

            var removeRefsList = new List<HkdShapeInstanceInfo>();

            {
                var blockDef = Block.BlockDefinition;
                var model = blockDef.Model;
                if (VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                    MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);

                var shape = VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                var si = new HkdShapeInstanceInfo(shape, null, null);
                removeRefsList.Add(si);
                m_tmpChildren.Add(si);
                shape.GetChildren(m_tmpChildren);

                if (blockDef.BuildProgressModels != null)
                {
                    foreach (var progress in blockDef.BuildProgressModels)
                    {
                        model = progress.File;

                        if (VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                            MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);

                        shape = VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                        si = new HkdShapeInstanceInfo(shape, null, null);
                        removeRefsList.Add(si);
                        m_tmpChildren.Add(si);
                        shape.GetChildren(m_tmpChildren);
                    }
                }
            }

            Debug.Assert(m_tmpShapeListInit.Count == 0);
            m_tmpShapeListInit.Clear();
            m_tmpShapeListInit.AddList(shapeList);

            for (int i = 0; i < m_tmpChildren.Count; i++)
            {
                var child = m_tmpChildren[i];
                var result = m_tmpShapeListInit.Where(s => s.Name == child.ShapeName);
                if (result.Count() > 0)
                {
                    var found = result.First();
                    var si = new HkdShapeInstanceInfo(child.Shape.Clone(), Matrix.Identity);
                    if (found.Fixed)
                        si.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                    removeRefsList.Add(si);
                    m_tmpShapeInfos.Add(si);
                    m_tmpShapeListInit.Remove(found);
                }
                else
                {
                    child.GetChildren(m_tmpChildren);
                }
            }

            m_tmpShapeListInit.Clear();

            if (shapeList.Count > 0 && m_tmpShapeInfos.Count == 0)
            {
                ProfilerShort.End();
                m_tmpChildren.Clear();
                Debug.Fail("No relevant shape was found for fractured block. It was probably reexported and names changed. Block definition: " + Block.BlockDefinition.Id.ToString());
                throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed. Block definition: " + Block.BlockDefinition.Id.ToString());
            }

            if (render != null)
            {
                foreach (var shape in m_tmpShapeInfos)
                {
                    if (!string.IsNullOrEmpty(shape.Shape.Name))
                        render.AddPiece(shape.Shape.Name, Matrix.Identity);
                }

                render.UpdateRenderObject(true);
            }

            m_tmpChildren.Clear();

            if (Block.CubeGrid.CreatePhysics)
            {
                HkdBreakableShape compound = new HkdCompoundBreakableShape(null, m_tmpShapeInfos);
                ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                var mp = new HkMassProperties();
                compound.BuildMassProperties(ref mp);
                Shape = new HkdBreakableShape(compound.GetShape(), ref mp);
                compound.RemoveReference();
                foreach (var si in m_tmpShapeInfos)
                {
                    var siRef = si;
                    Shape.AddShape(ref siRef);
                }
                Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                CreateMountPoints();

                // Update neighbours for parent block
                var blockOnPosition = Block.CubeGrid.GetCubeBlock(Block.Position);
                if (blockOnPosition != null)
                    blockOnPosition.CubeGrid.UpdateBlockNeighbours(blockOnPosition);

                if (Block.CubeGrid.Physics != null)
                    Block.CubeGrid.Physics.AddDirtyBlock(Block);
            }

            foreach (var si in m_tmpShapeInfos)
                si.Shape.RemoveReference();
            m_tmpShapeInfos.Clear();

            foreach (var si in removeRefsList)
                si.RemoveReference();

            ProfilerShort.End();
        }
Ejemplo n.º 5
0
        private HkdBreakableShape? CreateBlockShape(Sandbox.Game.Entities.Cube.MySlimBlock b, out Matrix blockTransform)
        {
            ProfilerShort.Begin("CreateBlockShape");
            blockTransform = Matrix.Identity;
            if (b.FatBlock == null)
            {
                Debug.Fail("Armor blocks are not allowed in medieval");
                ProfilerShort.End();
                return null;
            }

            HkdBreakableShape breakableShape;
            Matrix compoundChildTransform = Matrix.Identity;

            if (b.FatBlock is MyCompoundCubeBlock)
            {
                ProfilerShort.Begin("Cmpnd");
                blockTransform.Translation = b.FatBlock.PositionComp.LocalMatrix.Translation;
                var cb = b.FatBlock as MyCompoundCubeBlock;
                if (cb.GetBlocksCount() == 1)
                {
                    ProfilerShort.Begin("SingleBlock");
                    var block = cb.GetBlocks()[0];
                    var defId = block.FatBlock.BlockDefinition;
                    Matrix m;
                    var model = block.CalculateCurrentModel(out m);
                    if (MyFakes.LAZY_LOAD_DESTRUCTION || HasBreakableShape(model, defId))
                    {
                        ProfilerShort.Begin("Clone");
                        breakableShape = GetBreakableShape(model, defId);
                        ProfilerShort.End();
                    }

                    block.Orientation.GetMatrix(out compoundChildTransform);
                    blockTransform = compoundChildTransform * blockTransform;
                    ProfilerShort.End();
                }
                else
                {
                    var pos = b.Position * m_grid.GridSize;

                    float mass = 0;
                    ProfilerShort.Begin("GetBlocks");
                    foreach (var block in cb.GetBlocks())
                    {
                        block.Orientation.GetMatrix(out compoundChildTransform);
                        compoundChildTransform.Translation = Vector3.Zero;
                        var blockDef = block.BlockDefinition;
                        Matrix m;
                        var model = block.CalculateCurrentModel(out m);
                        if (MyFakes.LAZY_LOAD_DESTRUCTION || HasBreakableShape(model, blockDef))
                        {
                            ProfilerShort.Begin("Clone");

                            breakableShape = GetBreakableShape(model, blockDef);
                            breakableShape.UserObject |= (uint)HkdBreakableShape.Flags.FRACTURE_PIECE;
                            System.Diagnostics.Debug.Assert(breakableShape.IsValid(), "Invalid breakableShape");

                            ProfilerShort.End();
                            mass += blockDef.Mass;
                            m_shapeInfosList2.Add(new HkdShapeInstanceInfo(breakableShape, compoundChildTransform));
                        }
                    }

                    if (m_shapeInfosList2.Count == 0)
                    {
                        ProfilerShort.End();
                        return null;
                    }

                    ProfilerShort.BeginNextBlock("CreateCompoundBlockShape");
                    //HkShape hkpShape = new HkListShape(m_khpShapeList.ToArray(), m_khpShapeList.Count, HkReferencePolicy.None);
                    //m_khpShapeList.Clear();
                    HkdBreakableShape compound = new HkdCompoundBreakableShape(null, m_shapeInfosList2);
                    ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                    var mp = new HkMassProperties();
                    compound.BuildMassProperties(ref mp);
                    breakableShape = new HkdBreakableShape(compound.GetShape(), ref mp);
                    compound.RemoveReference();
                    foreach (var si in m_shapeInfosList2)
                    {
                        var siRef = si;
                        breakableShape.AddShape(ref siRef);
                    }

                    ProfilerShort.BeginNextBlock("Connect");
                    //slow slow slow
                    //breakableShape.AutoConnect(MyDestructionData.Static.TemporaryWorld);
                    //slow wrong
                    //breakableShape.ConnectSemiAccurate(MyDestructionData.Static.TemporaryWorld);
                    //fast frong
                    for (int i = 0; i < m_shapeInfosList2.Count; i++)
                    {
                        for (int j = 0; j < m_shapeInfosList2.Count; j++)
                        {
                            if (i != j)
                            {
                                ConnectShapesWithChildren(breakableShape, m_shapeInfosList2[i].Shape, m_shapeInfosList2[j].Shape);
                            }
                        }
                    }
                    ProfilerShort.BeginNextBlock("Cleanup");
                    foreach (var si in m_shapeInfosList2)
                    {
                        si.Shape.RemoveReference();
                        si.RemoveReference();
                    }
                    m_shapeInfosList2.Clear();
                    ProfilerShort.End();
                }
                ProfilerShort.End();
            }
            else
            {
                ProfilerShort.Begin("SingleBlock");
                b.Orientation.GetMatrix(out blockTransform);
                blockTransform.Translation = b.FatBlock.PositionComp.LocalMatrix.Translation;
                Matrix m;
                var model = b.CalculateCurrentModel(out m);
                if (b.FatBlock is MyFracturedBlock)
                {
                    ProfilerShort.Begin("CloneFracture");
                    breakableShape = (b.FatBlock as MyFracturedBlock).Shape;
                    if(!breakableShape.IsValid())
                    {
                        Debug.Fail("Fractured block Breakable shape invalid!");
                        throw new Exception("Fractured block Breakable shape invalid!");
                    }
                    breakableShape.AddReference();
                    ProfilerShort.End();
                }
                else if (MyFakes.LAZY_LOAD_DESTRUCTION || HasBreakableShape(model, b.BlockDefinition))
                {
                    ProfilerShort.Begin("Clone");
                    breakableShape = GetBreakableShape(model, b.BlockDefinition);
                    ProfilerShort.End();
                }
                ProfilerShort.End();
            }
            ProfilerShort.Begin("Property");
            HkPropertyBase posProp = new HkVec3IProperty(b.Position);
            Debug.Assert(breakableShape.IsValid());
            breakableShape.SetPropertyRecursively(HkdBreakableShape.PROPERTY_GRID_POSITION, posProp);
            posProp.RemoveReference();
            ProfilerShort.End();
            //breakableShape.DisableRefCountRecursively();
            ProfilerShort.End();
            return breakableShape;
        }
Ejemplo n.º 6
0
        public override void Init(MyObjectBuilder_CubeBlock builder, MyCubeGrid cubeGrid)
        {
            base.Init(builder, cubeGrid);
            ProfilerShort.Begin("FP.Init");
            CheckConnectionAllowed = true;
            var ob = builder as MyObjectBuilder_FracturedBlock;

            if (ob.Shapes.Count == 0)
            {
                ProfilerShort.End();
                if (ob.CreatingFracturedBlock)
                {
                    return;
                }
                Debug.Fail("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
            }

            OriginalBlocks = new List <MyDefinitionId>();
            Orientations   = new List <MyBlockOrientation>();
            var lst = new List <HkdShapeInstanceInfo>();

            foreach (var def in ob.BlockDefinitions)
            {
                var blockDef = MyDefinitionManager.Static.GetCubeBlockDefinition(def);

                var model = blockDef.Model;
                if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                {
                    MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);
                }
                var shape = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                var si    = new HkdShapeInstanceInfo(shape, null, null);
                lst.Add(si);
                m_children.Add(si);
                shape.GetChildren(m_children);
                if (blockDef.BuildProgressModels != null)
                {
                    foreach (var progress in blockDef.BuildProgressModels)
                    {
                        model = progress.File;
                        if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                        {
                            MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);
                        }
                        shape = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                        si    = new HkdShapeInstanceInfo(shape, null, null);
                        lst.Add(si);
                        m_children.Add(si);
                        shape.GetChildren(m_children);
                    }
                }


                OriginalBlocks.Add(def);
            }

            foreach (var or in ob.BlockOrientations)
            {
                Orientations.Add(or);
            }

            if (ob.MultiBlocks.Count > 0)
            {
                MultiBlocks = new List <MultiBlockPartInfo>();
                foreach (var mbpart in ob.MultiBlocks)
                {
                    if (mbpart != null)
                    {
                        MultiBlocks.Add(new MultiBlockPartInfo()
                        {
                            MultiBlockDefinition = mbpart.MultiBlockDefinition, MultiBlockId = mbpart.MultiBlockId
                        });
                    }
                    else
                    {
                        MultiBlocks.Add(null);
                    }
                }
            }

            m_shapes.AddRange(ob.Shapes);
            for (int i = 0; i < m_children.Count; i++)
            {
                var child = m_children[i];
                Func <MyObjectBuilder_FracturedBlock.ShapeB, bool> x = s => s.Name == child.ShapeName;
                var result = m_shapes.Where(x);
                if (result.Count() > 0)
                {
                    var found = result.First();
                    var m     = Matrix.CreateFromQuaternion(found.Orientation);
                    m.Translation = child.GetTransform().Translation;
                    var si = new HkdShapeInstanceInfo(child.Shape.Clone(), m);
                    if (found.Fixed)
                    {
                        si.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                    }
                    lst.Add(si);
                    m_shapeInfos.Add(si);
                    m_shapes.Remove(found);
                }
                else
                {
                    child.GetChildren(m_children);
                }
            }

            if (m_shapeInfos.Count == 0)
            {
                m_children.Clear();
                ProfilerShort.End();
                Debug.Fail("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
            }

            foreach (var shape in m_shapeInfos)
            {
                if (!string.IsNullOrEmpty(shape.Shape.Name))
                {
                    Render.AddPiece(shape.Shape.Name, Matrix.CreateFromQuaternion(Quaternion.CreateFromRotationMatrix(shape.GetTransform().GetOrientation())));
                }
            }

            if (CubeGrid.CreatePhysics)
            {
                HkdBreakableShape compound = new HkdCompoundBreakableShape(null, m_shapeInfos);
                ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                Shape = compound;
                var mp = new HkMassProperties();
                compound.BuildMassProperties(ref mp);
                Shape = new HkdBreakableShape(compound.GetShape(), ref mp);
                compound.RemoveReference();
                foreach (var si in m_shapeInfos)
                {
                    var siRef = si;
                    Shape.AddShape(ref siRef);
                }
                Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                CreateMountPoints();
            }
            m_children.Clear();
            foreach (var si in m_shapeInfos)
            {
                si.Shape.RemoveReference();
            }
            foreach (var si in lst)
            {
                si.RemoveReference();
            }
            m_shapeInfos.Clear();

            ProfilerShort.End();
        }
        protected override void RecreateShape(List <MyObjectBuilder_FractureComponentBase.FracturedShape> shapeList)
        {
            Debug.Assert(m_tmpChildren.Count == 0);
            Debug.Assert(m_tmpShapeInfos.Count == 0);

            ProfilerShort.Begin("FractureComponent.RecreateShape");

            if (Shape.IsValid())
            {
                Shape.RemoveReference();
                Shape = new HkdBreakableShape();
            }

            var render = Block.FatBlock.Render as MyRenderComponentFracturedPiece;

            if (render != null)
            {
                render.ClearModels();
                render.UpdateRenderObject(false);
            }
            else
            {
                Debug.Fail("Invalid render type");
            }

            if (shapeList.Count == 0)
            {
                ProfilerShort.End();
                return;
            }

            var removeRefsList = new List <HkdShapeInstanceInfo>();

            {
                var blockDef = Block.BlockDefinition;
                var model    = blockDef.Model;
                if (VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                {
                    MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);
                }

                var shape = VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                var si    = new HkdShapeInstanceInfo(shape, null, null);
                removeRefsList.Add(si);
                m_tmpChildren.Add(si);
                shape.GetChildren(m_tmpChildren);

                if (blockDef.BuildProgressModels != null)
                {
                    foreach (var progress in blockDef.BuildProgressModels)
                    {
                        model = progress.File;

                        if (VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                        {
                            MyDestructionData.Static.LoadModelDestruction(model, blockDef, Vector3.One);
                        }

                        shape = VRage.Game.Models.MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                        si    = new HkdShapeInstanceInfo(shape, null, null);
                        removeRefsList.Add(si);
                        m_tmpChildren.Add(si);
                        shape.GetChildren(m_tmpChildren);
                    }
                }
            }

            Debug.Assert(m_tmpShapeListInit.Count == 0);
            m_tmpShapeListInit.Clear();
            m_tmpShapeListInit.AddList(shapeList);

            for (int i = 0; i < m_tmpChildren.Count; i++)
            {
                var child  = m_tmpChildren[i];
                var result = m_tmpShapeListInit.Where(s => s.Name == child.ShapeName);
                if (result.Count() > 0)
                {
                    var found = result.First();
                    var si    = new HkdShapeInstanceInfo(child.Shape.Clone(), Matrix.Identity);
                    if (found.Fixed)
                    {
                        si.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                    }
                    removeRefsList.Add(si);
                    m_tmpShapeInfos.Add(si);
                    m_tmpShapeListInit.Remove(found);
                }
                else
                {
                    child.GetChildren(m_tmpChildren);
                }
            }

            m_tmpShapeListInit.Clear();

            if (shapeList.Count > 0 && m_tmpShapeInfos.Count == 0)
            {
                ProfilerShort.End();
                m_tmpChildren.Clear();
                Debug.Fail("No relevant shape was found for fractured block. It was probably reexported and names changed. Block definition: " + Block.BlockDefinition.Id.ToString());
                throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed. Block definition: " + Block.BlockDefinition.Id.ToString());
            }

            if (render != null)
            {
                foreach (var shape in m_tmpShapeInfos)
                {
                    if (!string.IsNullOrEmpty(shape.Shape.Name))
                    {
                        render.AddPiece(shape.Shape.Name, Matrix.Identity);
                    }
                }

                render.UpdateRenderObject(true);
            }

            m_tmpChildren.Clear();

            if (Block.CubeGrid.CreatePhysics)
            {
                HkdBreakableShape compound = new HkdCompoundBreakableShape(null, m_tmpShapeInfos);
                ((HkdCompoundBreakableShape)compound).RecalcMassPropsFromChildren();
                var mp = new HkMassProperties();
                compound.BuildMassProperties(ref mp);
                Shape = new HkdBreakableShape(compound.GetShape(), ref mp);
                compound.RemoveReference();
                foreach (var si in m_tmpShapeInfos)
                {
                    var siRef = si;
                    Shape.AddShape(ref siRef);
                }
                Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                CreateMountPoints();

                // Update neighbours for parent block
                var blockOnPosition = Block.CubeGrid.GetCubeBlock(Block.Position);
                if (blockOnPosition != null)
                {
                    blockOnPosition.CubeGrid.UpdateBlockNeighbours(blockOnPosition);
                }

                if (Block.CubeGrid.Physics != null)
                {
                    Block.CubeGrid.Physics.AddDirtyBlock(Block);
                }
            }

            foreach (var si in m_tmpShapeInfos)
            {
                si.Shape.RemoveReference();
            }
            m_tmpShapeInfos.Clear();

            foreach (var si in removeRefsList)
            {
                si.RemoveReference();
            }

            ProfilerShort.End();
        }