private void RecreateTargetModel() { MainViewport.Children.Clear(); Visual3D targetModelVisual3D; // Setup VisualBrush as the material for the model var visualBrush = new VisualBrush(DrawingCanvas); var material = new DiffuseMaterial(visualBrush); if (HeightMapModelTypeRadioButton.IsChecked ?? false) { var heightMapVisual3D = new Ab3d.Visuals.HeightMapVisual3D() { Size = new Size3D(100, 50, 100), IsWireframeShown = false, IsSolidSurfaceShown = true, Material = material, BackMaterial = new DiffuseMaterial(Brushes.DimGray) }; // Create height data from bitmap string heightMapFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\HeightMaps\\simpleHeightMap.png"); BitmapImage heightImage = new BitmapImage(new Uri(heightMapFileName, UriKind.RelativeOrAbsolute)); double[,] heightData = Ab3d.PowerToys.Samples.Objects3D.HeightMapSample.OpenHeightMapDataFile(heightImage, invertData: false); if (heightData != null) { heightMapVisual3D.HeightData = heightData; } targetModelVisual3D = heightMapVisual3D; } else if (BoxModelTypeRadioButton.IsChecked ?? false) { var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { Size = new Size3D(100, 50, 100), Material = material }; targetModelVisual3D = boxVisual3D; } else //if (SphereModelTypeRadioButton.IsChecked ?? false) { var sphereVisual3D = new Ab3d.Visuals.SphereVisual3D() { Radius = 40, Material = material }; targetModelVisual3D = sphereVisual3D; } MainViewport.Children.Add(targetModelVisual3D); SetupMouseEventHandlers(targetModelVisual3D); }
private void CreateSampleScene() { _physicallyBasedMaterial = new PhysicallyBasedMaterial(); // We need to dispose the PhysicallyBasedMaterial when this sample is uloaded _disposables.Add(_physicallyBasedMaterial); UpdateBaseColor(); UpdateMetalness(); UpdateRoughness(); var normalMapShaderResourceView = GetNormalMapShaderResourceView(); if (normalMapShaderResourceView != null) { _physicallyBasedMaterial.SetTextureMap(TextureMapTypes.NormalMap, normalMapShaderResourceView, "bricks_normal.png"); } var ambientOcclusionShaderResourceView = AmbientOcclusionShaderResourceView(); if (ambientOcclusionShaderResourceView != null) { _physicallyBasedMaterial.SetTextureMap(TextureMapTypes.AmbientOcclusion, ambientOcclusionShaderResourceView, "bricks_ao.png"); } var wpfMaterial = new DiffuseMaterial(Brushes.Red); wpfMaterial.SetUsedDXMaterial(_physicallyBasedMaterial); ModelPlaceholder.Content = null; ModelPlaceholder.Children.Clear(); var sphereVisual3D = new Ab3d.Visuals.SphereVisual3D() { CenterPosition = new Point3D(40, 12, 0), Radius = 12, Segments = 50, Material = wpfMaterial, FreezeMeshGeometry3D = false, UseCachedMeshGeometry3D = false }; ModelPlaceholder.Children.Add(sphereVisual3D); var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(-40, 10, 0), Size = new Size3D(20, 20, 20), Material = wpfMaterial, FreezeMeshGeometry3D = false, UseCachedMeshGeometry3D = false }; ModelPlaceholder.Children.Add(boxVisual3D); var readerObj = new Ab3d.ReaderObj(); var readModel3D = (GeometryModel3D)readerObj.ReadModel3D(_modelsFolder + "teapot-hires.obj"); Ab3d.Utilities.ModelUtils.CenterAndScaleModel3D(readModel3D, new Point3D(0, 10, 0), new Size3D(40, 40, 40), true); //// This code is called for each GeometryModel3D inside Plane1 //var tangentVectors = Ab3d.DirectX.Utilities.MeshUtils.CalculateTangentVectors((MeshGeometry3D)readModel3D.Geometry); ////// Assign tangent array to the MeshGeometry3D //readModel3D.Geometry.SetDXAttribute(DXAttributeType.MeshTangentArray, tangentVectors); readModel3D.Material = wpfMaterial; //ModelPlaceholder.Content = null; ModelPlaceholder.Children.Add(readModel3D.CreateModelVisual3D()); // Rendering normal (bump) maps require tangent vectors. // The following code will generate tangent vectors and assign them to the MeshGeometry3D that form our 3D model. // If tangent vectors are not provided, they will be calculated on-demand in the pixel shader (slightly reducing performance). Ab3d.Utilities.ModelIterator.IterateGeometryModel3DObjects(ModelPlaceholder, delegate(GeometryModel3D geometryModel3D, Transform3D transform3D) { // This code is called for each GeometryModel3D inside Plane1 var tangentVectors = Ab3d.DirectX.Utilities.MeshUtils.CalculateTangentVectors((MeshGeometry3D)geometryModel3D.Geometry); // Assign tangent array to the MeshGeometry3D geometryModel3D.Geometry.SetDXAttribute(DXAttributeType.MeshTangentArray, tangentVectors); }); Camera1.Distance = 150; UpdateLights(); }