public PxGhRigidStaticCMesh(Plane plane, List <Mesh> meshes, Material material)
        {
            ghMeshes = new List <GH_Mesh>();
            List <Mesh> initialMeshes = new List <Mesh>(meshes);

            actor = PhysXManager.Physics.CreateRigidStatic();

            foreach (Mesh initialMesh in initialMeshes)
            {
                Mesh meshLocal = initialMesh.DuplicateMesh();
                meshLocal.Transform(Transform.PlaneToPlane(plane, Plane.WorldXY));

                List <Vector3> points = new List <Vector3>();
                foreach (Point3d v in meshLocal.Vertices)
                {
                    points.Add(new Vector3((float)v.X, (float)v.Y, (float)v.Z));
                }

                List <int> faceVertexIndices = new List <int>();
                foreach (MeshFace face in meshLocal.Faces)
                {
                    faceVertexIndices.Add(face.A);
                    faceVertexIndices.Add(face.B);
                    faceVertexIndices.Add(face.C);
                }

                ConvexMeshDesc convexMeshDescription = new ConvexMeshDesc();
                convexMeshDescription.Flags = ConvexFlag.ComputeConvex;
                convexMeshDescription.SetPositions(points.ToArray());
                convexMeshDescription.SetTriangles(faceVertexIndices.ToArray());

                MemoryStream memoryStream = new MemoryStream();
                PhysXManager.Physics.CreateCooking().CookConvexMesh(convexMeshDescription, memoryStream);
                memoryStream.Position = 0;

                ConvexMesh         convexMesh         = PhysXManager.Physics.CreateConvexMesh(memoryStream);
                ConvexMeshGeometry convexMeshGeometry = new ConvexMeshGeometry(convexMesh);

                actor.CreateShape(convexMeshGeometry, material);

                ghMeshes.Add(new GH_Mesh(meshLocal));
            }

            actor.GlobalPose = plane.ToMatrix();
        }
Beispiel #2
0
        public PxGhRigidDynamiCompoundConvexMesh(List <Mesh> meshes, Plane frame, Material material, float mass, Vector3d initialLinearVelocity, Vector3d initialAngularVelocity)
            : base(frame, initialLinearVelocity, initialAngularVelocity)
        {
            DisplayMeshes = new List <Mesh>(meshes);

            foreach (Mesh mesh in DisplayMeshes)
            {
                mesh.Transform(Transform.PlaneToPlane(frame, Plane.WorldXY));

                Vector3[] vertices = new Vector3[mesh.Vertices.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = mesh.Vertices[i].ToSystemVector();
                }

                int[] faceVertexIndices = new int[mesh.Faces.Count * 3];
                for (int i = 0; i < mesh.Faces.Count; i++)
                {
                    MeshFace face = mesh.Faces[i];
                    faceVertexIndices[3 * i + 0] = face.A;
                    faceVertexIndices[3 * i + 1] = face.B;
                    faceVertexIndices[3 * i + 2] = face.C;
                }

                ConvexMeshDesc convexMeshDescription = new ConvexMeshDesc();
                convexMeshDescription.Flags = ConvexFlag.ComputeConvex;
                convexMeshDescription.SetPositions(vertices);
                convexMeshDescription.SetTriangles(faceVertexIndices);

                MemoryStream memoryStream = new MemoryStream();
                PxGhManager.Physics.CreateCooking().CookConvexMesh(convexMeshDescription, memoryStream);
                memoryStream.Position = 0;

                ConvexMesh         convexMesh         = PxGhManager.Physics.CreateConvexMesh(memoryStream);
                ConvexMeshGeometry convexMeshGeometry = new ConvexMeshGeometry(convexMesh);

                Actor.CreateShape(convexMeshGeometry, material);
            }

            Actor.SetMassAndUpdateInertia(mass);
        }
Beispiel #3
0
        private void CreateConvexMesh(Scene scene, Material material)
        {
            var colladaLoader = new ColladaLoader();
            var bunny         = colladaLoader.Load(@"Teapot.DAE", this.Engine.GraphicsDevice);

            var convexMeshDesc = new ConvexMeshDesc
            {
                Flags = ConvexFlag.ComputeConvex
            };

            convexMeshDesc.SetPositions(bunny.VertexPositions);
            convexMeshDesc.SetTriangles(bunny.Indices);

            var cooking = scene.Physics.CreateCooking();

            var stream     = new MemoryStream();
            var cookResult = cooking.CookConvexMesh(convexMeshDesc, stream);

            stream.Position = 0;

            var convexMesh = scene.Physics.CreateConvexMesh(stream);

            var convexMeshGeom = new ConvexMeshGeometry(convexMesh)
            {
                Scale = new MeshScale(new Vector3(0.3f, 0.3f, 0.3f), Quaternion.Identity)
            };

            var rigidActor = scene.Physics.CreateRigidDynamic();

            // TODO: The Shape created here is now also an owner of the ConvexMesh object,
            // this needs to be incorp into the ObjectTable ownership logic
            rigidActor.CreateShape(convexMeshGeom, material);

            rigidActor.GlobalPose =
                Matrix4x4.CreateRotationX(-(float)System.Math.PI / 2) *
                Matrix4x4.CreateTranslation(0, 80, 0);

            scene.AddActor(rigidActor);
        }
        public PxGhRigidStaticCompoundConvexMesh(Plane frame, List <Mesh> meshes, Material material)
        {
            DisplayMeshes    = new List <Mesh>(meshes);
            Actor.GlobalPose = frame.ToMatrix();

            foreach (Mesh mesh in DisplayMeshes)
            {
                mesh.Transform(Transform.PlaneToPlane(frame, Plane.WorldXY));

                Vector3[] vertices = new Vector3[mesh.Vertices.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = mesh.Vertices[i].ToSystemVector();
                }

                int[] faceVertexIndices = new int[mesh.Faces.Count * 3];
                for (int i = 0; i < mesh.Faces.Count; i++)
                {
                    MeshFace face = mesh.Faces[i];
                    faceVertexIndices[3 * i + 0] = face.A;
                    faceVertexIndices[3 * i + 1] = face.B;
                    faceVertexIndices[3 * i + 2] = face.C;
                }

                ConvexMeshDesc convexMeshDescription = new ConvexMeshDesc();
                convexMeshDescription.Flags = ConvexFlag.ComputeConvex;
                convexMeshDescription.SetPositions(vertices);
                convexMeshDescription.SetTriangles(faceVertexIndices);

                MemoryStream memoryStream = new MemoryStream();
                PxGhManager.Physics.CreateCooking().CookConvexMesh(convexMeshDescription, memoryStream);
                memoryStream.Position = 0;

                ConvexMeshGeometry convexMeshGeometry = new ConvexMeshGeometry(PxGhManager.Physics.CreateConvexMesh(memoryStream));

                Actor.CreateShape(convexMeshGeometry, material);
            }
        }
Beispiel #5
0
        private void AddCollider(RigidActor actor, ICollider collider)
        {
            if (collider is AggregateCollider agg)
            {
                foreach (var c in agg.ColliderComponents)
                {
                    AddCollider(actor, c);
                }
            }
            else if (collider is TriangleMeshCollider triCollider)
            {
                var desc = triCollider.GetDescriptor(GetMaterialIndices);

                // Avoiding offline cook path for now because
                //   1. Comments in Physx.Net imply memory leak using streams
                //   2. I don't want to deal with disk caching cooks yet
                var finalMesh = this.physxPhysics.CreateTriangleMesh(cooker, desc);

                var meshGeom = new TriangleMeshGeometry(finalMesh);

                RigidActorExt.CreateExclusiveShape(actor, meshGeom, globalMaterials, null);
            }
            else if (collider is TriangleModelCollider triModelCollider)
            {
                foreach (var mesh in triModelCollider.MeshColliders)
                {
                    var desc = mesh.GetDescriptor(GetMaterialIndices);

                    // Avoiding offline cook path for now because
                    //   1. Comments in Physx.Net imply memory leak using streams
                    //   2. I don't want to deal with disk caching cooks yet
                    var finalMesh = this.physxPhysics.CreateTriangleMesh(cooker, desc);

                    var meshGeom = new TriangleMeshGeometry(finalMesh);

                    RigidActorExt.CreateExclusiveShape(actor, meshGeom, globalMaterials, null);
                }
            }
            else if (collider is IVertexBasedCollider vertCollider)
            {
                var desc = new ConvexMeshDesc()
                {
                    Flags = ConvexFlag.ComputeConvex
                };
                desc.SetPositions(vertCollider.GetTransformedVertices());
                var mesh = this.physxPhysics.CreateConvexMesh(this.cooker, desc);
                var geom = new ConvexMeshGeometry(mesh);
                var mat  = this.GetOrCreateMaterial(vertCollider.PhysicsMaterial);
                // TODO: re-use shared shapes instead of creating exclusive
                RigidActorExt.CreateExclusiveShape(actor, geom, mat);
            }
            else if (collider is ConvexModelCollider modelCollider)
            {
                foreach (var verts in modelCollider.Meshes)
                {
                    var desc = new ConvexMeshDesc()
                    {
                        Flags = ConvexFlag.ComputeConvex
                    };
                    desc.SetPositions(verts);
                    var mesh = this.physxPhysics.CreateConvexMesh(this.cooker, desc);
                    var geom = new ConvexMeshGeometry(mesh);
                    var mat  = this.GetOrCreateMaterial(modelCollider.PhysicsMaterial);
                    // TODO: re-use shared shapes instead of creating exclusive
                    RigidActorExt.CreateExclusiveShape(actor, geom, mat);
                }
            }
        }
Beispiel #6
0
		private void CreateConvexMesh(Scene scene, Material material)
		{
			var colladaLoader = new ColladaLoader();
			var bunny = colladaLoader.Load(@"Teapot.DAE", this.Engine.GraphicsDevice);

			var convexMeshDesc = new ConvexMeshDesc()
			{
				Flags = ConvexFlag.ComputeConvex
			};
			convexMeshDesc.SetPositions(bunny.VertexPositions);
			convexMeshDesc.SetTriangles(bunny.Indices);

			var cooking = scene.Physics.CreateCooking();

			var stream = new MemoryStream();
			var cookResult = cooking.CookConvexMesh(convexMeshDesc, stream);

			stream.Position = 0;

			var convexMesh = scene.Physics.CreateConvexMesh(stream);

			var convexMeshGeom = new ConvexMeshGeometry(convexMesh)
			{
				Scale = new MeshScale(new Vector3(0.3f, 0.3f, 0.3f), Quaternion.Identity)
			};

			var rigidActor = scene.Physics.CreateRigidDynamic();

			// TODO: The Shape created here is now also an owner of the ConvexMesh object,
			// this needs to be incorp into the ObjectTable ownership logic
			rigidActor.CreateShape(convexMeshGeom, material);

			rigidActor.GlobalPose =
				Matrix.RotationX(-(float)System.Math.PI / 2) *
				Matrix.Translation(0, 80, 0);

			scene.AddActor(rigidActor);
		}