Example #1
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 #2
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 #3
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 #4
0
        //private void CreateFlagPole(Cloth cloth)
        //{
        //    var polePosition = new Vector3(0, 30, 0);

        //    // Create a flag pole to attach the cloth to
        //    var material = this.Scene.Physics.CreateMaterial(0.1f, 0.1f, 0.1f);

        //    var rigidActor = this.Scene.Physics.CreateRigidStatic();

        //    var boxGeom = new BoxGeometry(2, 30, 2);
        //    var boxShape = rigidActor.CreateShape(boxGeom, material);

        //    rigidActor.GlobalPose = Matrix.Translation(polePosition);
        //    //rigidActor.SetMassAndUpdateInertia(10);
        //    //rigidActor.Flags = RigidDynamicFlags.Kinematic;

        //    this.Scene.AddActor(rigidActor);

        //    // Attach the cloth
        //    var clothGrid = new VertexGrid(25, 25);

        //    // TODO: Clean up the below code
        //    // I'd like this to use the FindPointsInsideBoxGeom method instead (more generic)
        //    Vector3[] p;
        //    int[] i;
        //    FindPointsInsideBoxGeom(boxGeom, polePosition, clothGrid, out p, out i);

        //    p = clothGrid.Points.Take(75).ToArray();
        //    i = Enumerable.Range(0, 75).ToArray();
        //    var f = new int[p.Length];

        //    var attachment = this.Scene.Physics.CreateAttachment(cloth, boxShape, i, p, f);
        //}

        private void FindPointsInsideBoxGeom(BoxGeometry box, Vector3 boxPosition, VertexGrid grid, out Vector3[] points, out int[] indices)
        {
            var i = new List <Vector3>();
            var j = new List <int>();

            foreach (int index in grid.Indices)
            {
                Vector3 p = grid.Points[index] + new Vector3(0, 5, 0);

                if (IsPointInBox(box, boxPosition, p))
                {
                    i.Add(p);
                    j.Add(index);
                }
            }

            points  = i.ToArray();
            indices = j.ToArray();
        }
Example #5
0
		//private void CreateFlagPole(Cloth cloth)
		//{
		//    var polePosition = new Vector3(0, 30, 0);

		//    // Create a flag pole to attach the cloth to
		//    var material = this.Scene.Physics.CreateMaterial(0.1f, 0.1f, 0.1f);

		//    var rigidActor = this.Scene.Physics.CreateRigidStatic();

		//    var boxGeom = new BoxGeometry(2, 30, 2);
		//    var boxShape = rigidActor.CreateShape(boxGeom, material);

		//    rigidActor.GlobalPose = Matrix.Translation(polePosition);
		//    //rigidActor.SetMassAndUpdateInertia(10);
		//    //rigidActor.Flags = RigidDynamicFlags.Kinematic;

		//    this.Scene.AddActor(rigidActor);

		//    // Attach the cloth
		//    var clothGrid = new VertexGrid(25, 25);

		//    // TODO: Clean up the below code
		//    // I'd like this to use the FindPointsInsideBoxGeom method instead (more generic)
		//    Vector3[] p;
		//    int[] i;
		//    FindPointsInsideBoxGeom(boxGeom, polePosition, clothGrid, out p, out i);

		//    p = clothGrid.Points.Take(75).ToArray();
		//    i = Enumerable.Range(0, 75).ToArray();
		//    var f = new int[p.Length];

		//    var attachment = this.Scene.Physics.CreateAttachment(cloth, boxShape, i, p, f);
		//}

		private void FindPointsInsideBoxGeom(BoxGeometry box, Vector3 boxPosition, VertexGrid grid, out Vector3[] points, out int[] indices)
		{
			var i = new List<Vector3>();
			var j = new List<int>();

			foreach (int index in grid.Indices)
			{
				Vector3 p = grid.Points[index] + new Vector3(0, 5, 0);

				if (IsPointInBox(box, boxPosition, p))
				{
					i.Add(p);
					j.Add(index);
				}
			}

			points = i.ToArray();
			indices = j.ToArray();
		}