模仿Charles Petzold的PointCollectionAnimation寫了一個Point3DCollectionAnimation,自己按照理解的寫了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using Petzold.AnimationExtensions;
class Point3DCollectionAnimation:Point3DCollectionAnimationBase
{
Point3DCollection p3dcDst = new Point3DCollection();
public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(Point3DCollection), typeof(Point3DCollectionAnima
$False$
tion));
public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Point3DCollection), typeof(Point3DCollectionAnimation));
public Point3DCollection From
{
set { SetValue(FromProperty, value); }
get { return (Point3DCollection)GetValue(FromProperty); }
}
public Point3DCollection To
{
set { SetValue(ToProperty, value); }
get { return (Point3DCollection)GetValue(ToProperty); }
}
protected override Point3DCollection GetCurrentValueCore(Point3DCollection defaultOriginValue, Point3DCollection defaultDestinationValue, AnimationClock animationClock)
{
if (animationClock.CurrentProgress == null)
return null;
double progress = animationClock.CurrentProgress.Value;
Point3DCollection p3dcFrom = From ?? defaultOriginValue;
Point3DCollection p3dcTo = To ?? defaultDestinationValue;
p3dcDst.Clear();
int="COLOR: #000000"> count = defaultOriginValue.Count;
for (int i = 0; i < count; i++)
{
p3dcDst.Add(new Point3D((1 - progress) * p3dcFrom[i].X + progress * p3dcTo[i].X,
(1 - progress) * p3dcFrom[i].Y + progress * p3dcTo[i].Y,
(1 - progress) * p3dcFrom[i].Z + progress * p3dcTo[i].Z));
}
return p3dcDst;
}
protected override System.Windows.Freezable CreateInstanceCore()
{
return new Point3DCollectionAnimation();
}
}
但實際的效果卻總出不來,我設想的曲面動畫總是不出現,弄了很久,把這些語句一個個調試過之後發現,似乎是p3dcDst.Clear出的問題,Clear之後再Add一批新點後,這些值在debug時看著是好的,但返回給WPF繪圖時它就是沒反應,而如果用p3dcDst = new Point3DCollection()替代效果才能出來。翻查原來Charles Petzold寫的PointCollectionAnimation,才發現他用了一個flip的變量,同時定義了兩個PointCollection實例,在操作時不停地flip。原來人家是知道這個問題的,奇怪怎麼沒有看到這方面的資料。。
於是給我的類加入了同樣的機制:
using System;000000"> System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using Petzold.AnimationExtensions;
class Point3DCollectionAnimation:Point3DCollectionAnimationBase
{
Point3DCollection p3dcDst1 = new Point3DCollection();
Point3DCollection p3dcDst2 = new Point3DCollection();
bool flip = true;
public static DependencyProperty FromProperty = DependencyProperty.Register(">From", typeof(Point3DCollection), typeof(Point3DCollectionAnimation));
public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Point3DCollection), typeof(Point3DCollectionAnimation));
public Point3DCollection From
{
set { SetValue(FromProperty, value); }
get { return (Point3DCollection)GetValue(FromProperty); }
}
public Point3DCollection To
{
set { SetValue(ToProperty, value); }
get { return (Point3DCollection)GetValue(ToProperty); }
}
protected override Point3DCollection GetCurrentValueCore(Point3DCollection defaultOriginValue, Point3DCollection defaultDestinationValue, AnimationClock animationClock)
{
if (animationClock.CurrentProgress == null)
return null;
double progress = animationClock.CurrentProgress.Value;
Point3DCollection p3dcFrom = From ?? defaultOriginValue;
Point3DCollection p3dcTo = To ?? defaultDestinationValue;
Point3DCollection p3dcDst=flip ? p3dcDst1 : p3dcDst2 ;
flip = !flip;
p3dcDst.Clear();
int count = defaultOriginValue.Count;
for (int i = 0; i < count; i++)
{
p3dcDst.Add(new Point3D((1 - progress) * p3dcFrom[i].X yle="COLOR: #000000">+ progress * p3dcTo[i].X,
(1 - progress) * p3dcFrom[i].Y + progress * p3dcTo[i].Y,
(1 - progress) * p3dcFrom[i].Z + progress * p3dcTo[i].Z));
}
return p3dcDst;
}
protected override System.Windows.Freezable CreateInstanceCore()
{
效果就出來了。