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