■ 큐브 변형하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800"

    Height="600"

    Title="큐브 변형하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <Border

            Margin="10"

            BorderThickness="1"

            BorderBrush="Black">

            <Viewport3D>

                <ModelVisual3D>

                    <ModelVisual3D.Content>

                        <Model3DGroup>

                            <GeometryModel3D>

                                <GeometryModel3D.Geometry>

                                    <MeshGeometry3D x:Name="cube"

                                        Positions="-0.5  0.5  0.5,  0.5  0.5  0.5,

                                                   -0.5 -0.5  0.5,  0.5 -0.5  0.5,

                                                    0.5  0.5 -0.5, -0.5  0.5 -0.5,

                                                    0.5 -0.5 -0.5, -0.5 -0.5 -0.5,

                                                   -0.5  0.5 -0.5, -0.5  0.5  0.5,

                                                   -0.5 -0.5 -0.5, -0.5 -0.5  0.5,

                                                    0.5  0.5  0.5,  0.5  0.5 -0.5,

                                                    0.5 -0.5  0.5,  0.5 -0.5 -0.5,

                                                   -0.5  0.5 -0.5,  0.5  0.5 -0.5,

                                                   -0.5  0.5  0.5,  0.5  0.5  0.5,

                                                    0.5 -0.5 -0.5, -0.5 -0.5 -0.5,

                                                    0.5 -0.5  0.5, -0.5 -0.5  0.5"

                                       TriangleIndices=" 0  2  1,  1  2  3

                                                         4  6  5,  5  6  7,

                                                         8 10  9,  9 10 11,

                                                        12 14 13, 13 14 15,

                                                        16 18 17, 17 18 19,

                                                        20 22 21, 21 22 23" />

                                </GeometryModel3D.Geometry>

                                <GeometryModel3D.Material>

                                    <DiffuseMaterial Brush="Blue" />

                                </GeometryModel3D.Material>

                            </GeometryModel3D>

                            <AmbientLight Color="#404040" />

                            <DirectionalLight

                                Color="#C0C0C0"

                                Direction="2, -3 -1" />

                        </Model3DGroup>

                    </ModelVisual3D.Content>

                </ModelVisual3D>

                <Viewport3D.Camera>

                    <PerspectiveCamera

                        Position="2 3 4"

                        LookDirection="-2 -3 -4"

                        UpDirection="0 1 0"

                        FieldOfView="45" />

                </Viewport3D.Camera>

            </Viewport3D>

        </Border>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Windows;

using System.Windows.Media.Media3D;

using System.Windows.Threading;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 주기 시간 범위

        /// </summary>

        private static readonly TimeSpan _intervalTimeSpan = TimeSpan.FromSeconds(0.1);

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 변경 방향

        /// </summary>

        private const int CHANGE_DIRECTION = 10;

 

        /// <summary>

        /// 벡터 인자

        /// </summary>

        private const double VECTOR_FACTOR = 0.005;

 

        /// <summary>

        /// 메시

        /// </summary>

        private MeshGeometry3D mesh;

 

        /// <summary>

        /// 꼭지점 배열

        /// </summary>

        private Point3D[] vertexArray = new Point3D[8];

 

        /// <summary>

        /// 인덱스 배열

        /// </summary>

        private int[,] indexArray  = new int[8, 3];

 

        /// <summary>

        /// 난수

        /// </summary>

        private Random random = new Random();

 

        /// <summary>

        /// 벡터 배열

        /// </summary>

        private Vector3D[] vectorArray  = new Vector3D[8];

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.mesh = FindName("cube") as MeshGeometry3D;

 

            int   foundVertexCount = 0;

            int[] foundIndexArray  = new int[8];

 

            for(int i = 0; i < this.mesh.Positions.Count; i++)

            {

                Point3D point = this.mesh.Positions[i];

 

                int j;

 

                for(j = 0; j < foundVertexCount; j++)

                {

                    if(point == this.vertexArray[j])

                    {

                        this.indexArray[j, foundIndexArray[j]++] = i;

 

                        break;

                    }

                }

 

                if(j == foundVertexCount)

                {

                    this.vertexArray[foundVertexCount] = point;

 

                    this.indexArray[foundVertexCount++, foundIndexArray[j]++] = i;

                }

            }

 

            DispatcherTimer dispatcherTimer = new DispatcherTimer();

 

            dispatcherTimer.Interval = _intervalTimeSpan;

 

            dispatcherTimer.Tick += dispatcherTimer_Tick;

 

            dispatcherTimer.Start();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 디스패처 타이머 틱 처리하기 - dispatcherTimer_Tick(sender, e)

 

        /// <summary>

        /// 디스패처 타이머 틱 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 이낮</param>

        private void dispatcherTimer_Tick(object sender, EventArgs e)

        {

            Point3DCollection collection = mesh.Positions;

 

            mesh.Positions = null;

 

            for(int i = 0; i < 8; i++)

            {

                if(random.Next(CHANGE_DIRECTION) == 0)

                {

                    Vector3D vector = new Vector3D

                    (

                        random.NextDouble() - 0.5,

                        random.NextDouble() - 0.5,

                        random.NextDouble() - 0.5

                    );

 

                    vector.Normalize();

 

                    this.vectorArray[i] = vector;

                }

 

                this.vertexArray[i] += VECTOR_FACTOR * this.vectorArray[i];

 

                for(int j = 0; j < 3; j++)

                {

                    collection[this.indexArray[i, j]] = this.vertexArray[i];

                }

            }

 

            mesh.Positions = collection;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요