public void CookClothFabric_SimplestExample()
        {
            using (var physics = CreatePhysicsAndScene())
            {
                var points = new[]
                {
                    new Vector3(0, 5, 0),
                    new Vector3(0, 5, 1),
                    new Vector3(1, 5, 1)
                };

                var indices = new[] { 0, 1, 2 };

                using (var cooking = physics.Physics.CreateCooking())
                {
                    var clothMeshDesc = new ClothMeshDesc()
                    {
                        Points    = points,
                        Triangles = ArrayUtil.ToByteArray(indices)
                    };

                    var stream = new MemoryStream();

                    cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);
                }
            }
        }
Example #2
0
		public void CookClothFabric_SimplestExample()
		{
			using (var physics = CreatePhysicsAndScene())
			{
				var points = new[] 
				{
					new Vector3(0, 5, 0),
					new Vector3(0, 5, 1),
					new Vector3(1, 5, 1)
				};

				var indices = new[] { 0, 1, 2 };

				using (var cooking = physics.Physics.CreateCooking())
				{
					var clothMeshDesc = new ClothMeshDesc()
					{
						Points = points,
						Triangles = ArrayUtil.ToByteArray(indices)
					};

					var stream = new MemoryStream();

					cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);
				}
			}
		}
Example #3
0
		public void CreateFlag()
		{
			var clothGrid = new ClothTestGrid(10, 10);

			using (var physics = CreatePhysicsAndScene())
			{
				Cooking cooking = physics.Physics.CreateCooking();

				ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
				{
					Points = clothGrid.Points,
					Triangles = clothGrid.Indices
				};

				MemoryStream stream = new MemoryStream();

				bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

				ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

				ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

				ClothCollisionData collision = new ClothCollisionData();

				Cloth cloth = physics.Physics.CreateCloth(Matrix.Identity, clothFabric, particles, collision, 0);
			}
		}
Example #4
0
        public void CreateFlag()
        {
            var clothGrid = new ClothTestGrid(10, 10);

            using (var physics = CreatePhysicsAndScene())
            {
                using (var cooking = physics.Physics.CreateCooking())
                {
                    var clothMeshDesc = new ClothMeshDesc()
                    {
                        Points    = clothGrid.Points,
                        Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
                    };

                    MemoryStream stream = new MemoryStream();

                    cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

                    // After cooking the fabric, we must put the position of the written stream back to 0
                    // so that it can be read from the beginning in the CreateClothFabric method
                    stream.Position = 0;

                    ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

                    ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

                    Cloth cloth = physics.Physics.CreateCloth(Matrix4x4.Identity, clothFabric, particles, 0);
                }
            }
        }
        public void CreateFlag()
        {
            var clothGrid = new ClothTestGrid(10, 10);

            using (var physics = CreatePhysicsAndScene())
            {
                Cooking cooking = physics.Physics.CreateCooking();

                ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
                {
                    Points    = clothGrid.Points,
                    Triangles = clothGrid.Indices
                };

                MemoryStream stream = new MemoryStream();

                bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

                ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

                ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

                ClothCollisionData collision = new ClothCollisionData();

                Cloth cloth = physics.Physics.CreateCloth(Matrix.Identity, clothFabric, particles, collision, 0);
            }
        }
Example #6
0
		public void CreateFlag()
		{
			var clothGrid = new ClothTestGrid(10, 10);

			using (var physics = CreatePhysicsAndScene())
			{
				using (Cooking cooking = physics.Physics.CreateCooking())
				{
					var clothMeshDesc = new ClothMeshDesc()
					{
						Points = clothGrid.Points,
						Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
					};

					MemoryStream stream = new MemoryStream();

					cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

					// After cooking the fabric, we must put the position of the written stream back to 0
					// so that it can be read from the beginning in the CreateClothFabric method
					stream.Position = 0;

					ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

					ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

					Cloth cloth = physics.Physics.CreateCloth(Matrix.Identity, clothFabric, particles, 0);
				}
			}
		}
Example #7
0
        private Cloth CreateCloth(Scene scene, RigidActor poleActor)
        {
            // Create a grid of triangles to be our cloth
            var clothGrid = VertexGrid.CreateGrid(40, 40, 0.4f);

            // Setup the grid for cooking
            var clothMeshDesc = new ClothMeshDesc()
            {
                Points    = clothGrid.Points,
                Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
            };

            // Cook
            var clothFabricStream = new MemoryStream();

            using (var cooking = scene.Physics.CreateCooking())
            {
                cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);
            }

            // Reset the seek position of the stream so we can read it form the beginning
            clothFabricStream.Position = 0;

            var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

            var poleBoxGeom = poleActor.GetShape(0).GetBoxGeometry();
            var boxPosition = new Vector3(poleActor.GlobalPose.M41, poleActor.GlobalPose.M42, poleActor.GlobalPose.M43);

            var particles = from p in clothGrid.Points
                            select new ClothParticle()
            {
                Position = p,
                // Setting the inverse weight of a particle to 0 will pin it in place, any other value will be its weight
                InverseWeight = IsPointInBox(poleBoxGeom, boxPosition, p) ? 0 : 1
            };

            // Create the cloth mesh from the cooked stream
            var cloth = scene.Physics.CreateCloth
                        (
                Matrix4x4.CreateTranslation(0, 30, 0),
                clothFabric,
                particles.ToArray(),
                0
                        );

            // Enable collision with other scene geometry
            //cloth.Flags |= ClothFlag.SceneCollision;

            // GPU cloth if desired
            // The Engine class needs to create a CudaContextManager for this to work. Define 'GPU' on
            // the Engine project to enable it.
            cloth.Flags |= ClothFlag.GPU;

            scene.AddActor(cloth);

            return(cloth);
        }
Example #8
0
		private Cloth CreateCloth(Scene scene, RigidActor poleActor)
		{
			// Create a grid of triangles to be our cloth
			var clothGrid = VertexGrid.CreateGrid(40, 40, 0.4f);

			// Setup the grid for cooking
			var clothMeshDesc = new ClothMeshDesc()
			{
				Points = clothGrid.Points,
				Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
			};

			// Cook
			var clothFabricStream = new MemoryStream();

			using (var cooking = scene.Physics.CreateCooking())
			{
				cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);
			}

			// Reset the seek position of the stream so we can read it form the beginning
			clothFabricStream.Position = 0;

			var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

			var poleBoxGeom = poleActor.GetShape(0).GetBoxGeometry();
			var boxPosition = new Vector3(poleActor.GlobalPose.M41, poleActor.GlobalPose.M42, poleActor.GlobalPose.M43);

			var particles = from p in clothGrid.Points
							select new ClothParticle()
							{
								Position = p,
								// Setting the inverse weight of a particle to 0 will pin it in place, any other value will be its weight
								InverseWeight = IsPointInBox(poleBoxGeom, boxPosition, p) ? 0 : 1
							};

			// Create the cloth mesh from the cooked stream
			var cloth = scene.Physics.CreateCloth
			(
				Matrix4x4.CreateTranslation(0, 30, 0),
				clothFabric,
				particles.ToArray(),
				0
			);

			// Enable collision with other scene geometry
			//cloth.Flags |= ClothFlag.SceneCollision;

			// GPU cloth if desired
			// The Engine class needs to create a CudaContextManager for this to work. Define 'GPU' on
			// the Engine project to enable it.
			cloth.Flags |= ClothFlag.GPU;

			scene.AddActor(cloth);

			return cloth;
		}
Example #9
0
        private Cloth CreateCloth(Scene scene)
        {
            // Create a grid of triangles to be our cloth
            var clothGrid = new VertexGrid(25, 25);

            // Setup the grid for cooking
            var clothMeshDesc = new ClothMeshDesc()
            {
                Points    = clothGrid.Points,
                Triangles = clothGrid.Indices
            };

            // Cook
            var clothFabricStream = new MemoryStream();

            using (var cooking = scene.Physics.CreateCooking())
            {
                bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);

                if (!result)
                {
                    throw new Exception("Failed to cook deformable mesh");
                }
            }

            // Reset the seek position of the stream so we can read it form the beginning
            clothFabricStream.Position = 0;

            var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

            var collisionData = new ClothCollisionData();

            var particles = from p in clothGrid.Points
                            select new ClothParticle()
            {
                Position      = p,
                InverseWeight = 0.1f
            };

            // Create the cloth mesh from the cooked stream
            var cloth = scene.Physics.CreateCloth(
                Matrix.Identity,
                clothFabric,
                particles.ToArray(),
                collisionData,
                0);

            scene.AddActor(cloth);

            return(cloth);
        }
Example #10
0
		private Cloth CreateCloth(Scene scene)
		{
			// Create a grid of triangles to be our cloth
			var clothGrid = new VertexGrid(25, 25);

			// Setup the grid for cooking
			var clothMeshDesc = new ClothMeshDesc()
			{
				Points = clothGrid.Points,
				Triangles = clothGrid.Indices
			};

			// Cook
			var clothFabricStream = new MemoryStream();

			using (var cooking = scene.Physics.CreateCooking())
			{
				bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -1, 0), clothFabricStream);

				if (!result)
					throw new Exception("Failed to cook deformable mesh");
			}

			// Reset the seek position of the stream so we can read it form the beginning
			clothFabricStream.Position = 0;

			var clothFabric = scene.Physics.CreateClothFabric(clothFabricStream);

			var collisionData = new ClothCollisionData();

			var particles = from p in clothGrid.Points
							select new ClothParticle()
							{
								Position = p,
								InverseWeight = 0.1f
							};

			// Create the cloth mesh from the cooked stream
			var cloth = scene.Physics.CreateCloth(
				Matrix.Identity,
				clothFabric,
				particles.ToArray(),
				collisionData,
				0);

			scene.AddActor(cloth);

			return cloth;
		}
Example #11
0
        public void Quadifier()
        {
            var foundation = new Foundation();

            // Indices - 2 triangles

            /*
             *      1_____2
             |    /|
             |   / |
             |  /  |
             | /   |
             |      0 ____3
             */
            var indices = new short[]
            {
                0, 1, 2,
                0, 2, 3
            };

            var clothMeshDesc = new ClothMeshDesc
            {
                Flags         = MeshFlag.FlipNormals | MeshFlag.Indices16Bit,
                InverseMasses = new float[] { 1, 2, 3, 4 },
                Points        = new[]
                {
                    new Vector3(0, 0, 0),
                    new Vector3(0, 10, 0),
                    new Vector3(10, 10, 0),
                    new Vector3(10, 0, 0)
                },
                Triangles = Util.ArrayUtil.ToByteArray(indices)
            };

            // This should turn the two triangles into a single quad
            var quad = ClothMeshQuadifier.GetDescriptor(clothMeshDesc);

            Assert.IsNotNull(quad);
            Assert.IsNotNull(quad.Quads);

            // Quad indices are (currently) always Int32
            var quads = quad.GetQuads32();

            Assert.AreEqual(4, quads.Length);
            Assert.AreEqual(0, quads[0]);
            Assert.AreEqual(1, quads[1]);
            Assert.AreEqual(2, quads[2]);
            Assert.AreEqual(3, quads[3]);
        }
Example #12
0
        public void LockParticleDataToReadPositions()
        {
            var clothGrid = new ClothTestGrid(10, 10);

            using (var physics = CreatePhysicsAndScene())
            {
                using (var cooking = physics.Physics.CreateCooking())
                {
                    var clothMeshDesc = new ClothMeshDesc()
                    {
                        Points    = clothGrid.Points,
                        Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
                    };

                    MemoryStream stream = new MemoryStream();

                    cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

                    // After cooking the fabric, we must put the position of the written stream back to 0
                    // so that it can be read from the beginning in the CreateClothFabric method
                    stream.Position = 0;

                    ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

                    ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

                    Cloth cloth = physics.Physics.CreateCloth(Matrix4x4.Identity, clothFabric, particles, 0);

                    var read = cloth.LockParticleData();

                    // Are the particle positions the right length
                    Assert.AreEqual(121, read.Particles.Length);

                    // And initially is what we read back the same as what we started with
                    for (int i = 0; i < 121; i++)
                    {
                        Assert.AreEqual(clothGrid.Points[i], read.Particles[i].Position);
                    }
                }
            }
        }
Example #13
0
        public void GetActors_Cloth()
        {
            using (var physics = CreatePhysicsAndScene())
            {
                PhysX.Cloth cloth;

                using (var cooking = physics.Physics.CreateCooking())
                {
                    var clothGrid = new ClothTestGrid(10, 10);

                    var clothMeshDesc = new ClothMeshDesc
                    {
                        Points    = clothGrid.Points,
                        Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
                    };

                    var stream = new MemoryStream();

                    cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

                    // After cooking the fabric, we must put the position of the written stream back to 0
                    // so that it can be read from the beginning in the CreateClothFabric method
                    stream.Position = 0;

                    var clothFabric = physics.Physics.CreateClothFabric(stream);

                    var particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

                    cloth = physics.Physics.CreateCloth(Matrix4x4.Identity, clothFabric, particles, 0);

                    physics.Scene.AddActor(cloth);
                }

                //

                var actors = physics.Scene.GetActors(ActorTypeSelectionFlag.Cloth);

                Assert.AreEqual(1, actors.Count);
                Assert.AreEqual(cloth, actors[0]);
            }
        }
Example #14
0
		public void LockParticleDataToReadPositions()
		{
			var clothGrid = new ClothTestGrid(10, 10);

			using (var physics = CreatePhysicsAndScene())
			{
				using (Cooking cooking = physics.Physics.CreateCooking())
				{
					var clothMeshDesc = new ClothMeshDesc()
					{
						Points = clothGrid.Points,
						Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
					};

					MemoryStream stream = new MemoryStream();

					cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

					// After cooking the fabric, we must put the position of the written stream back to 0
					// so that it can be read from the beginning in the CreateClothFabric method
					stream.Position = 0;

					ClothFabric clothFabric = physics.Physics.CreateClothFabric(stream);

					ClothParticle[] particles = clothGrid.Points.Select(p => new ClothParticle(p, 2)).ToArray();

					Cloth cloth = physics.Physics.CreateCloth(Matrix4x4.Identity, clothFabric, particles, 0);

					var read = cloth.LockParticleData();

					// Are the particle positions the right length
					Assert.AreEqual(121, read.Particles.Length);

					// And initially is what we read back the same as what we started with
					for (int i = 0; i < 121; i++)
					{
						Assert.AreEqual(clothGrid.Points[i], read.Particles[i].Position);
					}
				}
			}
		}
Example #15
0
		public void CookClothFabric()
		{
			using (var physics = CreatePhysicsAndScene())
			{
				// Create a grid of triangles to be our cloth
				var clothGrid = new VertexGrid(25, 25);

				using (var cooking = physics.Physics.CreateCooking())
				{
					ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
					{
						Points = clothGrid.Points,
						Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
					};

					var stream = new MemoryStream();

					cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);
				}
			}
		}
        public void CookClothFabric()
        {
            using (var physics = CreatePhysicsAndScene())
            {
                // Create a grid of triangles to be our cloth
                var clothGrid = new VertexGrid(25, 25);

                using (var cooking = physics.Physics.CreateCooking())
                {
                    ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
                    {
                        Points    = clothGrid.Points,
                        Triangles = ArrayUtil.ToByteArray(clothGrid.Indices)
                    };

                    var stream = new MemoryStream();

                    cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);
                }
            }
        }
        public void CookClothFabric()
        {
            using (var physics = CreatePhysicsAndScene())
            {
                // Create a grid of triangles to be our cloth
                var clothGrid = new VertexGrid(25, 25);

                Cooking cooking = physics.Physics.CreateCooking();

                ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
                {
                    Points    = clothGrid.Points,
                    Triangles = clothGrid.Indices
                };

                var stream = new MemoryStream();

                bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

                Assert.IsTrue(result);
            }
        }
Example #18
0
		public void CookClothFabric()
		{
			using (var physics = CreatePhysicsAndScene())
			{
				// Create a grid of triangles to be our cloth
				var clothGrid = new VertexGrid(25, 25);

				Cooking cooking = physics.Physics.CreateCooking();

				ClothMeshDesc clothMeshDesc = new ClothMeshDesc()
				{
					Points = clothGrid.Points,
					Triangles = clothGrid.Indices
				};

				var stream = new MemoryStream();

				bool result = cooking.CookClothFabric(clothMeshDesc, new Vector3(0, -9.81f, 0), stream);

				Assert.IsTrue(result);
			}
		}