■ Viewport3D 클래스를 사용해 3D 장면을 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Canvas Name="canvas" Width="300" Height="300" /> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Media3D; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); #region 투시 카메라를 생성한다. PerspectiveCamera perspectiveCamera = new PerspectiveCamera(); perspectiveCamera.Position = new Point3D(0, 0, 2); perspectiveCamera.LookDirection = new Vector3D(0, 0, -1); perspectiveCamera.FieldOfView = 60; #endregion #region 지향성 조명을 생성한다. DirectionalLight directionalLight = new DirectionalLight(); directionalLight.Color = Colors.White; directionalLight.Direction = new Vector3D(-0.61, -0.5, -0.61); #endregion #region 위치 포인트 3D 컬렉션을 생성한다. Point3DCollection positionPoint3DCollection = new Point3DCollection(); positionPoint3DCollection.Add(new Point3D(-0.5, -0.5, 0.5)); positionPoint3DCollection.Add(new Point3D( 0.5, -0.5, 0.5)); positionPoint3DCollection.Add(new Point3D( 0.5, 0.5, 0.5)); positionPoint3DCollection.Add(new Point3D( 0.5, 0.5, 0.5)); positionPoint3DCollection.Add(new Point3D(-0.5, 0.5, 0.5)); positionPoint3DCollection.Add(new Point3D(-0.5, -0.5, 0.5)); #endregion #region 법선 벡터 3D 컬렉션을 생성한다. Vector3DCollection normalVector3DCollection = new Vector3DCollection(); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); normalVector3DCollection.Add(new Vector3D(0, 0, 1)); #endregion #region 텍스처 좌표 포인트 컬렉션을 생성한다. PointCollection textureCoordinatesPointCollection = new PointCollection(); textureCoordinatesPointCollection.Add(new Point(0, 0)); textureCoordinatesPointCollection.Add(new Point(1, 0)); textureCoordinatesPointCollection.Add(new Point(1, 1)); textureCoordinatesPointCollection.Add(new Point(1, 1)); textureCoordinatesPointCollection.Add(new Point(0, 1)); textureCoordinatesPointCollection.Add(new Point(0, 0)); #endregion #region 삼각형 인덱스 정수 32비트 컬렉션을 생성한다. Int32Collection triangleIndicesInt32Collection = new Int32Collection(); triangleIndicesInt32Collection.Add(0); triangleIndicesInt32Collection.Add(1); triangleIndicesInt32Collection.Add(2); triangleIndicesInt32Collection.Add(3); triangleIndicesInt32Collection.Add(4); triangleIndicesInt32Collection.Add(5); #endregion #region 메시 지오메트리 3D를 생성한다. MeshGeometry3D meshGeometry3D = new MeshGeometry3D(); meshGeometry3D.Positions = positionPoint3DCollection; meshGeometry3D.Normals = normalVector3DCollection; meshGeometry3D.TextureCoordinates = textureCoordinatesPointCollection; meshGeometry3D.TriangleIndices = triangleIndicesInt32Collection; #endregion #region 선형 그라디언트 브러시를 생성한다. LinearGradientBrush linearGradientBrush = new LinearGradientBrush(); linearGradientBrush.StartPoint = new Point(0, 0.5); linearGradientBrush.EndPoint = new Point(1, 0.5); linearGradientBrush.GradientStops.Add(new GradientStop(Colors.Yellow , 0.0 )); linearGradientBrush.GradientStops.Add(new GradientStop(Colors.Red , 0.25)); linearGradientBrush.GradientStops.Add(new GradientStop(Colors.Blue , 0.75)); linearGradientBrush.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0 )); #endregion #region 확산 재료를 생성한다. DiffuseMaterial diffuseMaterial = new DiffuseMaterial(linearGradientBrush); #endregion #region 축 각도 회전 3D를 생성한다. AxisAngleRotation3D axisAngleRotation3D = new AxisAngleRotation3D(); axisAngleRotation3D.Axis = new Vector3D(0,3,0); axisAngleRotation3D.Angle = 40; #endregion #region 회전 변환 3D를 생성한다. RotateTransform3D rotateTransform3D = new RotateTransform3D(); rotateTransform3D.Rotation = axisAngleRotation3D; #endregion #region 지오메트리 모델 3D를 생성한다. GeometryModel3D geometryModel3D = new GeometryModel3D(); geometryModel3D.Geometry = meshGeometry3D; geometryModel3D.Material = diffuseMaterial; geometryModel3D.Transform = rotateTransform3D; #endregion #region 모델 3D 그룹을 생성한다. Model3DGroup model3DGroup = new Model3DGroup(); model3DGroup.Children.Add(directionalLight); model3DGroup.Children.Add(geometryModel3D); #endregion #region 모델 비주얼 3D를 생성한다. ModelVisual3D modelVisual3D = new ModelVisual3D(); modelVisual3D.Content = model3DGroup; #endregion #region 뷰포트 3D를 생성한다. Viewport3D viewport3D = new Viewport3D(); viewport3D.Width = 300; viewport3D.Height = 300; viewport3D.Camera = perspectiveCamera; viewport3D.Children.Add(modelVisual3D); #endregion this.canvas.Children.Add(viewport3D); } #endregion } } |