コード例 #1
0
    public ConvexDecompositionSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;
      GraphicsScreen.ClearBackground = true;
      GraphicsScreen.BackgroundColor = Color.CornflowerBlue;
      SetCamera(new Vector3F(3, 3, 3), 0.8f, -0.6f);

      // Load model.
      _modelNode = ContentManager.Load<ModelNode>("Saucer/Saucer").Clone();

      // Combine all meshes of the model into a single TriangleMesh.
      TriangleMesh mesh = new TriangleMesh();
      foreach (var meshNode in _modelNode.GetChildren().OfType<MeshNode>())
      {
        var childMesh = MeshHelper.ToTriangleMesh(meshNode.Mesh);
        childMesh.Transform(meshNode.PoseWorld * Matrix44F.CreateScale(meshNode.ScaleWorld));
        mesh.Add(childMesh);
      }

      // Start convex decomposition on another thread.
      _convexDecomposition = new ConvexDecomposition();
      _convexDecomposition.ProgressChanged += OnProgressChanged;
      _convexDecomposition.AllowedConcavity = 0.8f;
      _convexDecomposition.IntermediateVertexLimit = 65536;
      _convexDecomposition.VertexLimit = 64;

      // 0 gives optimal results but is the slowest. Small positive values improve
      // speed but the result might be less optimal.
      _convexDecomposition.SmallIslandBoost = 0.02f;

      _convexDecomposition.SampleTriangleCenters = true;
      _convexDecomposition.SampleTriangleVertices = true;

      // Experimental multithreading. Enable at own risk ;-)
      _convexDecomposition.EnableMultithreading = true;

      _convexDecomposition.DecomposeAsync(mesh);
    }