示例#1
0
        private Ab3d.DirectX.Material GeneratePositionColorMaterial(Color4[] positionColorsArray, DXDevice dxDevice)
        {
            Ab3d.DirectX.Material dxMaterial;

            if (positionColorsArray != null)
            {
                dxMaterial = new Ab3d.DirectX.Materials.VertexColorMaterial()
                {
                    PositionColors      = positionColorsArray, // The PositionColors property is used to specify colors for each vertex
                    CreateDynamicBuffer = false,               // We will not update the colors frequently

                    // To show specular effect set the specular data here:
                    //SpecularPower = 16,
                    //SpecularColor = Color3.White,
                    //HasSpecularColor = true
                };
            }
            else
            {
                // Solid color material:
                var diffuseMaterial = new DiffuseMaterial(Brushes.Green);

                // Texture material:
                //var imageBrush = new ImageBrush();
                //imageBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/GrassTexture.jpg"));
                //var diffuseMaterial = new DiffuseMaterial(imageBrush);

                dxMaterial = new Ab3d.DirectX.Materials.WpfMaterial(diffuseMaterial);
            }

            // If DXDevice is already initialized, then we can also initialize (create DirectX resources) for the SimpleMesh.
            // This will create the DirectX resources and send them to the GPU
            if (dxDevice != null)
            {
                dxMaterial.InitializeResources(dxDevice);
            }

            return(dxMaterial);
        }
        private void GenerateHeightMapObject(float[,] heightData, Color4[] positionColorsArray)
        {
            PositionNormalTexture[] vertexBuffer;
            int[] indexBuffer;

            CreateHeightVertexAndIndexBuffer(heightData,
                                             centerPosition: new Vector3(0, 0, 0),
                                             size: new Vector3(1000, 20, 200),
                                             vertexBuffer: out vertexBuffer,
                                             indexBuffer: out indexBuffer);

            var simpleMesh = new SimpleMesh <PositionNormalTexture>(vertexBuffer,
                                                                    indexBuffer,
                                                                    inputLayoutType: InputLayoutType.Position | InputLayoutType.Normal | InputLayoutType.TextureCoordinate,
                                                                    name: "HeightSimpleMesh");

            _disposables.Add(simpleMesh);


            Ab3d.DirectX.Material dxMaterial;

            if (positionColorsArray != null)
            {
                dxMaterial = new Ab3d.DirectX.Materials.VertexColorMaterial()
                {
                    PositionColors      = positionColorsArray, // The PositionColors property is used to specify colors for each vertex
                    CreateDynamicBuffer = false,               // We will not update the colors frequently

                    // To show specular effect set the specular data here:
                    //SpecularPower = 16,
                    //SpecularColor = Color3.White,
                    //HasSpecularColor = true
                };
            }
            else
            {
                // Solid color material:
                var diffuseMaterial = new DiffuseMaterial(Brushes.Green);

                // Texture material:
                //var imageBrush = new ImageBrush();
                //imageBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Resources/GrassTexture.jpg"));
                //var diffuseMaterial = new DiffuseMaterial(imageBrush);

                dxMaterial = new Ab3d.DirectX.Materials.WpfMaterial(diffuseMaterial);
            }

            _disposables.Add(dxMaterial);

            var meshObjectNode = new Ab3d.DirectX.MeshObjectNode(simpleMesh, dxMaterial);

            meshObjectNode.Name = "HeightMeshObjectNode";

            _disposables.Add(meshObjectNode);

            var sceneNodeVisual3D = new SceneNodeVisual3D(meshObjectNode);

            MainViewport.Children.Add(sceneNodeVisual3D);


            // If you also want to render back faces of the height map you need to create another MeshObjectNode and set its IsBackFaceMaterial to true.
            // You can reuse the mesh. But this still requires almost twice the GPU power.
            var backDiffuseMaterial = new DiffuseMaterial(Brushes.Gray);
            var backDXMaterial      = new Ab3d.DirectX.Materials.WpfMaterial(backDiffuseMaterial);

            meshObjectNode = new Ab3d.DirectX.MeshObjectNode(simpleMesh, backDXMaterial);
            meshObjectNode.IsBackFaceMaterial = true;
            meshObjectNode.Name = "HeightBackMeshObjectNode";

            _disposables.Add(meshObjectNode);

            sceneNodeVisual3D = new SceneNodeVisual3D(meshObjectNode);
            MainViewport.Children.Add(sceneNodeVisual3D);
        }