Exemple #1
0
		public static VertexGrid CreateGrid(int rows, int columns)
		{
			int numVertsX = rows + 1;
			int numVertsZ = columns + 1;

			VertexGrid grid = new VertexGrid(new Vector3[numVertsX * numVertsZ], new int[rows * columns * 2 * 3]);

			{
				for (int r = 0; r < numVertsX; r++)
				{
					for (int c = 0; c < numVertsZ; c++)
					{
						grid.Points[r * numVertsZ + c] = new Vector3(r, 0, c);
					}
				}
			}

			{
				int count = 0;
				int vi = 0;
				for (int z = 0; z < columns; z++)
				{
					for (int x = 0; x < rows; x++)
					{
						// First triangle
						grid.Indices[count++] = vi;
						grid.Indices[count++] = vi + 1;
						grid.Indices[count++] = vi + numVertsX;

						// Second triangle
						grid.Indices[count++] = vi + numVertsX;
						grid.Indices[count++] = vi + 1;
						grid.Indices[count++] = vi + numVertsX + 1;

						vi++;
					}
					vi++;
				}
			}

			return grid;
		}
Exemple #2
0
        public static VertexGrid CreateGrid(int rows, int columns)
        {
            int numVertsX = rows + 1;
            int numVertsZ = columns + 1;

            VertexGrid grid = new VertexGrid(new Vector3[numVertsX * numVertsZ], new int[rows * columns * 2 * 3]);

            {
                for (int r = 0; r < numVertsX; r++)
                {
                    for (int c = 0; c < numVertsZ; c++)
                    {
                        grid.Points[r * numVertsZ + c] = new Vector3(r, 0, c);
                    }
                }
            }

            {
                int count  = 0;
                int vIndex = 0;
                for (int z = 0; z < columns; z++)
                {
                    for (int x = 0; x < rows; x++)
                    {
                        // first triangle
                        grid.Indices[count++] = vIndex;
                        grid.Indices[count++] = vIndex + 1;
                        grid.Indices[count++] = vIndex + numVertsX;

                        // second triangle
                        grid.Indices[count++] = vIndex + numVertsX;
                        grid.Indices[count++] = vIndex + 1;
                        grid.Indices[count++] = vIndex + numVertsX + 1;

                        vIndex++;
                    }
                    vIndex++;
                }
            }

            return(grid);
        }
Exemple #3
0
        protected override void LoadPhysics(Scene scene)
        {
            int w = 25;
            int h = 25;

            float hw = w / 2.0f;
            float hh = h / 2.0f;

            Vector3 p = new Vector3(0, 20, 0);

            // Create a Grid of Points
            int vertices, indices;

            ClothMesh clothMesh;
            {
                var grid = VertexGrid.CreateGrid(w, h);

                vertices = grid.Points.Length;
                indices  = grid.Indices.Length;

                ClothMeshDescription clothMeshDesc = new ClothMeshDescription();
                clothMeshDesc.AllocateVertices <Vector3>(vertices);
                clothMeshDesc.AllocateTriangles <int>(indices / 3);

                clothMeshDesc.VertexCount   = vertices;
                clothMeshDesc.TriangleCount = indices / 3;

                clothMeshDesc.VerticesStream.SetData(grid.Points);
                clothMeshDesc.TriangleStream.SetData(grid.Indices);

                // We are using 32 bit integers for our indices, so make sure the 16 bit flag is removed.
                // 32 bits are the default, so this isn't technically needed, but it's good to show in a sample
                clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;
                clothMeshDesc.Flags |= (MeshFlag)((int)clothMeshDesc.Flags | (int)ClothMeshFlag.Tearable);

                // Write the cooked data to memory
                using (var memoryStream = new MemoryStream())
                {
                    Cooking.InitializeCooking();
                    Cooking.CookClothMesh(clothMeshDesc, memoryStream);
                    Cooking.CloseCooking();

                    // Need to reset the position of the stream to the beginning
                    memoryStream.Position = 0;

                    clothMesh = Engine.Core.CreateClothMesh(memoryStream);
                }
            }

            //

            int j = vertices * 2;
            int k = indices * 3;

            var clothDesc = new ClothDescription()
            {
                ClothMesh  = clothMesh,
                GlobalPose =
                    Matrix.Translation(-hw, 0, -hh) *
                    Matrix.Translation(p),
                Flags            = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization | ClothFlag.Tearable,
                BendingStiffness = 0.1f,
                TearFactor       = 1.5f
            };

            clothDesc.MeshData.AllocatePositions <Vector3>(j);
            clothDesc.MeshData.AllocateIndices <int>(k);
            clothDesc.MeshData.AllocateNormals <Vector3>(j);

            clothDesc.MeshData.MaximumVertices = j;
            clothDesc.MeshData.MaximumIndices  = k;

            clothDesc.MeshData.NumberOfVertices = vertices;
            clothDesc.MeshData.NumberOfIndices  = indices;

            _cloth = scene.CreateCloth(clothDesc);

            //

            // Four corner boxes to hold it in place
            var positions = new[]
            {
                new Vector3(0, 0, -hh),                // Back
                new Vector3(0, 0, hh),                 // Front
                new Vector3(-hw, 0, 0),                // Left
                new Vector3(hw, 0, 0),                 // Right
            };

            var sizes = new[]
            {
                new Vector3(w, 1, 1),                 // Back
                new Vector3(w, 1, 1),                 // Front
                new Vector3(1, 1, h),                 // Left
                new Vector3(1, 1, h),                 //Right
            };

            for (int i = 0; i < 4; i++)
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose = Matrix.Translation(positions[i] + p),
                    Shapes     = { new BoxShapeDescription(sizes[i]) }
                };

                var actor = scene.CreateActor(actorDesc);

                _cloth.AttachToShape(actor.Shapes.First(), (ClothAttachmentFlag)0);
            }

            //

            // Something to drop on it
            {
                var actorDesc = new ActorDescription()
                {
                    GlobalPose      = Matrix.Translation(0, 100, 0),
                    Shapes          = { new SphereShapeDescription(2) },
                    BodyDescription = new BodyDescription(50)
                };

                var actor = scene.CreateActor(actorDesc);
            }
        }
Exemple #4
0
        public static PhysX.Cloth FlagOfCloth(Scene scene)
        {
            Core core = scene.Core;

            // Create a Grid of Points
            VertexGrid grid = VertexGrid.CreateGrid(10, 10);

            ClothMeshDescription clothMeshDesc = new ClothMeshDescription();

            clothMeshDesc.AllocateVertices <Vector3>(grid.Points.Length);
            clothMeshDesc.AllocateTriangles <int>(grid.Indices.Length / 3);

            clothMeshDesc.VertexCount   = grid.Points.Length;
            clothMeshDesc.TriangleCount = grid.Indices.Length / 3;

            clothMeshDesc.VerticesStream.SetData(grid.Points);
            clothMeshDesc.TriangleStream.SetData(grid.Indices);

            // We are using 32 bit integers, so make sure the 16 bit flag is removed.
            // 32 bits are the default, so this isn't technically needed
            clothMeshDesc.Flags &= ~MeshFlag.Indices16Bit;

            // Write the cooked data to memory
            MemoryStream memoryStream = new MemoryStream();

            Cooking.InitializeCooking();
            Cooking.CookClothMesh(clothMeshDesc, memoryStream);
            Cooking.CloseCooking();

            // Need to reset the position of the stream to the beginning
            memoryStream.Position = 0;

            ClothMesh clothMesh = core.CreateClothMesh(memoryStream);

            //

            ClothDescription clothDesc = new ClothDescription()
            {
                ClothMesh  = clothMesh,
                Flags      = ClothFlag.Gravity | ClothFlag.Bending | ClothFlag.CollisionTwoway | ClothFlag.Visualization,
                GlobalPose =
                    Matrix.RotationYawPitchRoll(0, (float)Math.PI / 2.0f, (float)Math.PI / 2.0f) *
                    Matrix.Translation(0, 20, 0)
            };

            clothDesc.MeshData.AllocatePositions <Vector3>(grid.Points.Length);
            clothDesc.MeshData.AllocateIndices <int>(grid.Indices.Length);

            clothDesc.MeshData.MaximumVertices = grid.Points.Length;
            clothDesc.MeshData.MaximumIndices  = grid.Indices.Length;

            clothDesc.MeshData.NumberOfVertices = grid.Points.Length;
            clothDesc.MeshData.NumberOfIndices  = grid.Indices.Length;

            var flag = scene.CreateCloth(clothDesc);

            // Flag Pole
            ActorDescription flagPoleActorDesc = new ActorDescription()
            {
                GlobalPose = Matrix.Translation(0, 10, 0),
                Shapes     = { new BoxShapeDescription(1.0f, 20.0f, 1.0f) }
            };

            Actor flagPoleActor = scene.CreateActor(flagPoleActorDesc);

            flag.AttachToShape(flagPoleActor.Shapes[0], 0);
            flag.WindAcceleration = new Vector3(10, 10, 10);
            flag.BendingStiffness = 0.1f;

            return(flag);
        }