リソースモデルはこんな感じ。
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 |
private void Sample() { this.viewPort3D1.Name = "ZAM3DViewport3D"; viewPort3D1.ClipToBounds = true; viewPort3D1.Width = 400; viewPort3D1.Height = 300; ResourceDictionary resourceDictionary = new ResourceDictionary(); //// MaterialGroup materialGroup = new MaterialGroup(); DiffuseMaterial diffuseMaterial = new DiffuseMaterial(); diffuseMaterial.Brush = new SolidColorBrush(Color.FromRgb(0xd3, 0xc8, 0xad)); materialGroup.Children.Add(diffuseMaterial); SpecularMaterial specularMaterial = new SpecularMaterial(); specularMaterial.SpecularPower = 93f; specularMaterial.Brush = new SolidColorBrush(Color.FromRgb(0x33, 0x33, 0x33)); materialGroup.Children.Add(specularMaterial); resourceDictionary.Add("ER___Default_MaterialMR1", materialGroup); //// Transform3DGroup transform3DGroup = new Transform3DGroup(); TranslateTransform3D translateTransform3D = new TranslateTransform3D(0f, 0f, 0f); ScaleTransform3D scaleTransform3D = new ScaleTransform3D(1f, 1f, 1f); AxisAngleRotation3D axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(0f, 1f, 0f), 0f); RotateTransform3D rotateTransform3D = new RotateTransform3D(); rotateTransform3D.Rotation = axisAngleRotation3D; TranslateTransform3D translateTransform3D2 = new TranslateTransform3D(0f, 0f, 0f); transform3DGroup.Children.Add(translateTransform3D); transform3DGroup.Children.Add(scaleTransform3D); transform3DGroup.Children.Add(rotateTransform3D); transform3DGroup.Children.Add(translateTransform3D2); resourceDictionary.Add("SceneTR7", transform3DGroup); //// Transform3DGroup transform3DGroupBox = new Transform3DGroup(); TranslateTransform3D translateTransform3DBox = new TranslateTransform3D(0f, 0f, 0f); ScaleTransform3D scaleTransform3DBox = new ScaleTransform3D(1f, 1f, 1f); AxisAngleRotation3D axisAngleRotation3DBox = new AxisAngleRotation3D(new Vector3D(0f, 1f, 0f), 0f); RotateTransform3D rotateTransform3DBox = new RotateTransform3D(); rotateTransform3DBox.Rotation = axisAngleRotation3DBox; TranslateTransform3D translateTransform3DBox2 = new TranslateTransform3D(0, 0, 0); transform3DGroupBox.Children.Add(translateTransform3DBox); transform3DGroupBox.Children.Add(scaleTransform3DBox); transform3DGroupBox.Children.Add(rotateTransform3DBox); transform3DGroupBox.Children.Add(translateTransform3DBox2); resourceDictionary.Add("BoxOR9TR8", transform3DGroupBox); //// MeshGeometry3D meshGeometry3D = new MeshGeometry3D(); TypeConverter i32ColConv = TypeDescriptor.GetConverter(typeof(Int32Collection)); meshGeometry3D.TriangleIndices = (Int32Collection)i32ColConv.ConvertFromString("0,1,2 2,3,0 4,5,6 6,7,4 8,9,10 10,11,8 12,13,14 14,15,12 16,17,18 18,19,16 20,21,22 22,23,20"); TypeConverter vector3DColConv = TypeDescriptor.GetConverter(typeof(Vector3DCollection)); meshGeometry3D.Normals = (Vector3DCollection)vector3DColConv.ConvertFromString("0,0,-1 0,0,-1 0,0,-1 0,0,-1 0,0,1 0,0,1 0,0,1 0,0,1 0,-1,0 0,-1,0 0,-1,0 0,-1,0 1,0,0 1,0,0 1,0,0 1,0,0 0,1,0 0,1,0 0,1,0 0,1,0 -1,0,0 -1,0,0 -1,0,0 -1,0,0 "); TypeConverter point3DColConv = TypeDescriptor.GetConverter(typeof(Point3DCollection)); meshGeometry3D.Positions = (Point3DCollection)point3DColConv.ConvertFromString("-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 "); resourceDictionary.Add("BoxOR9GR10", meshGeometry3D); //// PerspectiveCamera camera = new PerspectiveCamera(); camera.FarPlaneDistance = 10f; camera.LookDirection = new Vector3D(2.05609, -1.21509, -2.52021); camera.UpDirection = new Vector3D(0f, 1f, 0f); camera.NearPlaneDistance = 2f; camera.Position = new Point3D(-2.05609, 1.21509, 2.52021); camera.FieldOfView = 39f; viewPort3D1.Camera = camera; //// ModelVisual3D modelVisual3D = new ModelVisual3D(); Model3DGroup model3DGroup = new Model3DGroup(); model3DGroup.Transform = (Transform3D)resourceDictionary["SceneTR7"]; ////// AmbientLight ambientLight = new AmbientLight(Color.FromRgb(0x33, 0x33, 0x33)); model3DGroup.Children.Add(ambientLight); DirectionalLight directionalLight1 = new DirectionalLight(Colors.White, new Vector3D(-0.6f, -0.5f, -0.6f)); model3DGroup.Children.Add(directionalLight1); DirectionalLight directionalLight2 = new DirectionalLight(Colors.White, new Vector3D(0.6f, -0.5f, -0.6f)); model3DGroup.Children.Add(directionalLight2); //// MeshGeometry3D meshGeometry3D2 = (MeshGeometry3D)resourceDictionary["BoxOR9GR10"]; GeometryModel3D geometryModel3D = new GeometryModel3D(); geometryModel3D.Geometry = meshGeometry3D2; geometryModel3D.Material = resourceDictionary["ER___Default_MaterialMR1"] as Material; geometryModel3D.BackMaterial = (Material)resourceDictionary["ER___Default_MaterialMR1"]; model3DGroup.Children.Add(geometryModel3D); modelVisual3D.Content = model3DGroup; this.viewPort3D1.Children.Add(modelVisual3D); } |