Exemplo n.º 1
0
        public void CookConvexMesh()
        {
            string teapotXml;

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PhysX.Test.Resources.Teapot.DAE"))
            {
                var streamReader = new StreamReader(stream);

                teapotXml = streamReader.ReadToEnd();
            }

            using (var physics = CreatePhysicsAndScene())
            {
                var colladaLoader = new ColladaLoader();

                // Read the vertices and indices from the Teapot.DAE collada file
                // This file is copied out to the TestResults folder using the DeploymentItem attribute on this class
                var teapot = colladaLoader.Load(teapotXml);

                using (var cooking = physics.Physics.CreateCooking())
                {
                    var desc = new ConvexMeshDesc();
                    desc.SetTriangles(teapot.Indices);
                    desc.SetPositions(teapot.Vertices);
                    desc.Flags = ConvexFlag.ComputeConvex;

                    var stream = new MemoryStream();

                    var result = cooking.CookConvexMesh(desc, stream);

                    Assert.IsFalse(physics.ErrorOutput.HasErrors, physics.ErrorOutput.LastError);
                    Assert.AreEqual(ConvexMeshCookingResult.Success, result);
                }
            }
        }
Exemplo n.º 2
0
		public void CookConvexMesh()
		{
			string teapotXml;
			using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PhysX.Test.Resources.Teapot.DAE"))
			{
				var streamReader = new StreamReader(stream);

				teapotXml = streamReader.ReadToEnd();
			}

			using (var physics = CreatePhysicsAndScene())
			{
				var colladaLoader = new ColladaLoader();

				// Read the vertices and indices from the Teapot.DAE collada file
				// This file is copied out to the TestResults folder using the DeploymentItem attribute on this class
				var teapot = colladaLoader.Load(teapotXml);

				using (var cooking = physics.Physics.CreateCooking())
				{
					var desc = new ConvexMeshDesc();
					desc.SetTriangles(teapot.Indices);
					desc.SetPositions(teapot.Vertices);
					desc.Flags = ConvexFlag.ComputeConvex;

					var stream = new MemoryStream();

					var result = cooking.CookConvexMesh(desc, stream);

					Assert.IsFalse(physics.ErrorOutput.HasErrors, physics.ErrorOutput.LastError);
					Assert.AreEqual(ConvexMeshCookingResult.Success, result);
				}
			}
		}
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
		public void CookConvexMesh()
		{
			using (var physics = CreatePhysicsAndScene())
			{
				var vertices = Cuboid.CubeVertices();
				var indices = Cuboid.CubeIndices();

				var cooking = physics.Physics.CreateCooking();

				var desc = new ConvexMeshDesc();
				desc.SetTriangles(indices);
				desc.SetPositions(vertices);
				desc.Flags = ConvexFlag.Indices16Bit | ConvexFlag.ComputeConvex;

				var stream = new MemoryStream();

				bool result = cooking.CookConvexMesh(desc, stream);

				Assert.IsFalse(physics.ErrorOutput.HasErrors, physics.ErrorOutput.LastError);
				Assert.IsTrue(result);
			}
		}
Exemplo n.º 7
0
        public void CookConvexMesh()
        {
            using (var physics = CreatePhysicsAndScene())
            {
                var vertices = Cuboid.CubeVertices();
                var indices  = Cuboid.CubeIndices();

                var cooking = physics.Physics.CreateCooking();

                var desc = new ConvexMeshDesc();
                desc.SetTriangles(indices);
                desc.SetPositions(vertices);
                desc.Flags = ConvexFlag.Indices16Bit | ConvexFlag.ComputeConvex;

                var stream = new MemoryStream();

                bool result = cooking.CookConvexMesh(desc, stream);

                Assert.IsFalse(physics.ErrorOutput.HasErrors, physics.ErrorOutput.LastError);
                Assert.IsTrue(result);
            }
        }
        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);
            }
        }
Exemplo n.º 9
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);
                }
            }
        }
Exemplo n.º 10
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);
		}