Silverlight3D文字效果

Silverlight3D文字效果

2010091914453868.png

 

步骤1。3D画笔

 public class Point3D
    {
        public double x;
        public double y;
        public double z;

        public Point3D()
        {

        }

        public Point3D(double x, double y, double z){
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }

 

步骤二。3D文字类

  public Point3D point3D = new Point3D();

        public Text3D()
        {
            InitializeComponent();
        }

        public double x {
            set {
                this.SetValue(Canvas.LeftProperty, value);
            }
            get
            {
                return (double)this.GetValue(Canvas.LeftProperty);
            }
        }

        public double y
        {
            set
            {
                this.SetValue(Canvas.TopProperty, value);
            }
            get
            {
                return (double)this.GetValue(Canvas.TopProperty);
            }
        }

 

步骤三。3D空间类

public partial class TextSpace : UserControl
    {
        private List<Text3D> _text3Ds= new List<Text3D>();
        private Canvas _holder = new Canvas();

        public int SPACE_LENGTH = 400;
        public Point3D camera = new Point3D(); // camera
        public Point3D destination = new Point3D(0, 0, -500); // move desination

  private String _allTexts = "吴建强 李秀婷 张美静 吴美英 丁魁 吗昭通 马超 别高 许媜 北极光 赵建 刘冬 艾尼 巴音 艾买 那比江 韩枫 韩冬 ";
  private double _xMul = 10;   // springness toward x axis
  private double _yMul = 10;   // springness toward y axis
  private double _zMul = 30;  // springness toward z axis
  private double _zMovement = 2; // speed toward z axis

        public TextSpace()
        {
            InitializeComponent();

            // add the holder to the canvas
            _holder.SetValue(Canvas.LeftProperty, Width/ 2);
            _holder.SetValue(Canvas.TopProperty, Height / 2);
            LayoutRoot.Children.Add(_holder);

        }

  /////////////////////////////////////////////////////       
  // Handlers
  ///////////////////////////////////////////////////// 

        void text3D_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Text3D text3D = sender as Text3D;
   
   // set the camera to the position of the text
   destination.z = text3D.point3D.z + SPACE_LENGTH;
   destination.y = text3D.point3D.y;
   destination.x = text3D.point3D.x;
        }

        // Enter Frame Event
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            // move the camera automatically
            destination.z += _zMovement;

            camera.z += (destination.z - camera.z) / _zMul;
            camera.x += (destination.x - camera.x) / _xMul;
            camera.y += (destination.y - camera.y) / _yMul;

            // rearrange the position of the texts
            postTexts();
        }

  /////////////////////////////////////////////////////       
  // Private Methods
  ///////////////////////////////////////////////////// 

        private void addTexts(){
            int seed = (int)DateTime.Now.Ticks;
            String[] texts = _allTexts.Split(new Char [] {' '});

            for (int i = 0; i < texts.Length; i++)
            {
                seed += (int)DateTime.Now.Ticks;
                Random r = new Random(seed);

                String text = texts[i];
    Text3D text3D = new Text3D();
                text3D.text.FontSize = 12;
    text3D.text.Text = text;

                text3D.point3D.x = r.NextDouble() * Width - Width / 2;
                text3D.point3D.y = r.NextDouble() * Height - Height / 2;
                text3D.x = text3D.point3D.x;
                text3D.y = text3D.point3D.y;
                text3D.point3D.z = r.NextDouble() * SPACE_LENGTH * 2 - SPACE_LENGTH;
                text3D.MouseLeftButtonDown += new MouseButtonEventHandler(text3D_MouseLeftButtonDown);
                _holder.Children.Add(text3D);

                _text3Ds.Add(text3D);
            }
        }

 

        private void postTexts(){
   for( int i = 0; i < _text3Ds.Count; i++){
    Text3D text3D = _text3Ds[i];
    
    double zActual = SPACE_LENGTH + (text3D.point3D.z - camera.z);
                double scale = SPACE_LENGTH / zActual;

    if(scale > 0){
     text3D.x = (text3D.point3D.x - camera.x) * scale;
     text3D.y = (text3D.point3D.y - camera.y) * scale;

                    ScaleTransform scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleX = scale;
                    scaleTransform.ScaleY = scale;
                    text3D.RenderTransform = scaleTransform;

     text3D.Opacity = 1 - 0.99 * zActual / SPACE_LENGTH * 0.5 ;
    }else{
     // if text move over the screen, place it at the back
     text3D.point3D.z += SPACE_LENGTH * 2; 
    }   
   }
        }


        /////////////////////////////////////////////////////       
        // Public Methods
        ///////////////////////////////////////////////////// 

        public void Start()
        {

            // add all the texts to the stage
            addTexts();

            Application.Current.Host.Settings.MaxFrameRate = 24;
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

    }

 

步骤四。应用

前台。

<UserControl x:Class="_3DText.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="550" Height="400">
    <Grid x:Name="LayoutRoot" Background="White">
    <Canvas x:Name="Cover">
            <Canvas.Background>
                <SolidColorBrush Color="#FFFFFF" Opacity="0.6"/>
            </Canvas.Background>
            <TextBlock Text="单击开始" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Blue" FontFamily="Verdana" FontWeight="Bold" FontSize="48" Canvas.Left="100" Canvas.Top="170"/>
        </Canvas>
    </Grid>
</UserControl>

后台

 public partial class Page : UserControl
    {
        private TextSpace _textSpace;
        public Page()
        {
            InitializeComponent();
          
            _textSpace = new TextSpace();
            LayoutRoot.Children.Insert(0, _textSpace);

            Cover.MouseLeftButtonDown += new MouseButtonEventHandler(Cover_MouseLeftButtonDown);
        }

        void Cover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutRoot.Children.Remove(Cover);
            _textSpace.Start();
        }
    }


源码下载

转载于:https://www.cnblogs.com/salam/archive/2010/09/19/1831029.html

相关文章
相关标签/搜索