Beispiel #1
0
        /// <summary>
        /// Gets the number of shapes in the list for the particular shape
        /// </summary>
        /// <param name="phmo">the serialized phmo from which to get the counts</param>
        /// <param name="type">the shape for which to get the count</param>
        /// <returns></returns>
        private int GetNumberOfShapes(PhysicsModel phmo, BlamShapeType type)
        {
            switch (type)
            {
            case BlamShapeType.Box:
                return(phmo.Boxes != null ? phmo.Boxes.Count : 0);

            case BlamShapeType.List:
                return(phmo.Lists != null ? phmo.Lists.Count : 0);

            case BlamShapeType.Mopp:
                return(phmo.Mopps != null ? phmo.Mopps.Count : 0);

            case BlamShapeType.Pill:
                return(phmo.Pills != null ? phmo.Pills.Count : 0);

            case BlamShapeType.Polyhedron:
                return(phmo.Polyhedra != null ? phmo.Polyhedra.Count : 0);

            case BlamShapeType.Sphere:
                return(phmo.Spheres != null ? phmo.Spheres.Count : 0);

            default:
                return(0);
            }
        }
Beispiel #2
0
        public bool ParseFromFile(string fname)
        {
            BlenderPhmoReader reader = new BlenderPhmoReader(fname);

            fileStruct = reader.ReadFile();
            if (fileStruct == null)
            {
                Console.WriteLine("Could not parse file.");
                return(false);
            }

            //create a rigidbody for the phmo
            var rigidbody = new PhysicsModel.RigidBody
            {
                Mass         = 100f,                                            //probably not important
                CenterOfMass = new RealVector3d(0.0f, 0.0f, 0.25f),             //probably not important
                ShapeIndex   = 0,                                               //important
                MotionType   = PhysicsModel.RigidBody.MotionTypeValue.Keyframed // keyframed movement for now.
            };

            var shapedefs = fileStruct.AsArray;

            if (shapedefs.Count < 1)
            {
                Console.WriteLine("No shapes!");
                return(false);
            }
            else if (shapedefs.Count < 2)
            {
                //optimise the phmo by avoiding the use of the list-shape
                // as a level of indirection for multiple shapes.
                //Also for ease of shape encoding, AddShape returns the
                //shape-type added. 'Unused0' is used to represent failure.
                if (AddShape(_phmo, shapedefs[0]) == BlamShapeType.TriangleMesh)
                {
                    return(false);
                }

                rigidbody.ShapeType = BlamShapeType.Polyhedron;

                //this field does not have any influence
                //rigidbody.BoundingSphereRadius = 0.5f;
            }
            else
            {
                MemoryStream moppCodeBlockStream = BlenderPhmoMoppUtil.GenerateForBlenderPhmoJsonFile(fname);

                rigidbody.ShapeType  = BlamShapeType.Mopp;
                rigidbody.ShapeIndex = 0;

                //phmo needs to use shapelist and listelements reflexives
                _phmo.Lists      = new List <PhysicsModel.List>();
                _phmo.ListShapes = new List <PhysicsModel.ListShape>();
                _phmo.Mopps      = new List <HkpBvMoppTreeShape>();

                var moppTagblock = new HkpBvMoppTreeShape
                {
                    ReferencedObject = new HkpReferencedObject {
                        ReferenceCount = 0x80
                    },
                    Type  = 27,
                    Child = new HkpSingleShapeContainer {
                        Type = BlamShapeType.List, Index = 0
                    },
                };

                _phmo.MoppData = moppCodeBlockStream.ToArray();


                _phmo.Mopps.Add(moppTagblock);

                //_phmo.MoppCodes = new
                PhysicsModel.List shapeList = new PhysicsModel.List();

                Console.WriteLine("Loading multiple shapes");
                int amountAdded = 0;
                foreach (JSONNode listelem in fileStruct.AsArray)
                {
                    BlamShapeType typeAdded = AddShape(_phmo, listelem);
                    if (typeAdded == BlamShapeType.TriangleMesh)
                    {
                        Console.WriteLine("Failed loading shape.");
                        return(false);
                    }

                    PhysicsModel.ListShape shapeElem = new PhysicsModel.ListShape
                    {
                        ShapeType = typeAdded,
                        //assumes the shape added should be at the end of the respected list.
                        ShapeIndex = (short)(GetNumberOfShapes(_phmo, typeAdded) - 1)
                    };
                    _phmo.ListShapes.Add(shapeElem);
                    amountAdded++;
                }
                shapeList.Count               = 128;
                shapeList.ChildShapesSize     = amountAdded;
                shapeList.ChildShapesCapacity = (uint)(amountAdded + 0x80000000);
                _phmo.Lists.Add(shapeList);
                Console.WriteLine("Added {0} shapes.", amountAdded);
            }

            _phmo.RigidBodies.Add(rigidbody);

            return(true);
        }