Ejemplo n.º 1
0
        public PlanarShadows()
        {
            InitializeComponent();


            //var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Resources/10x10-texture.png"));
            //GroundPlane.Material = new DiffuseMaterial(new ImageBrush(bitmapImage));

            _solidShadowMaterial       = new DiffuseMaterial(Brushes.Black);
            _transparentShadowMaterial = new DiffuseMaterial(new SolidColorBrush(System.Windows.Media.Color.FromArgb(180, 0, 0, 0)));

            _loadedModel3D = LoadModel();

            MainViewport.Children.Add(_loadedModel3D.CreateModelVisual3D());

            // Create PlanarShadowMeshCreator
            // If your model is very complex, then it is recommended to use a simplified 3D model for shadow generation
            _planarShadowMeshCreator = new PlanarShadowMeshCreator(_loadedModel3D);
            _planarShadowMeshCreator.SetPlane(GroundPlane.CenterPosition, GroundPlane.Normal, GroundPlane.HeightDirection, GroundPlane.Size);


            _lightHorizontalAngle = -25;
            _lightVerticalAngle   = 22;
            _lightDistance        = 500;

            _shadowPointLight       = new PointLight();
            _shadowDirectionalLight = new DirectionalLight();

            Camera1.ShowCameraLight = ShowCameraLightType.Never;

            SetShadowLight(isDirectionalLight: true);

            UpdateLights();

            UpdateShadowModel();

            this.PreviewKeyDown += OnPreviewKeyDown;

            // This will allow receiving keyboard events
            this.Focusable = true;
            this.Focus();
        }
        public PlanarShadows()
        {
            InitializeComponent();


            ShadowOffsetComboBox.ItemsSource       = new double[] { 0, 0.001, 0.01, 0.05, 0.1, 1, 5 };
            ShadowOffsetComboBox.SelectedIndex     = 3; // 0.05
            ShadowOffsetComboBox.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
            {
                if (_shadowVisual3D == null)
                {
                    return;
                }

                var offset = (double)ShadowOffsetComboBox.SelectedValue;
                _shadowVisual3D.Transform = new TranslateTransform3D(0, offset, 0); // Lift the shadow 3D model slightly above the ground
            };

            ShadowOffsetInfoControl.InfoText =
                @"Plane offset sets the value that specify for how much the plane is lifter above the green plane. This is needed to prevent z-fighting (artifact that appears when the same 3D objects occupy the same 3D space after the projection multiplication and because of limited floating point precession).

The default value for this sample is 0.05. If using smaller values than for some camera positions (when moving camera closer to the shadow) z-fighting artifacts can appear. When using to big values the shadow appears to be lifter above the plane.
The best value depends on the size of the scene. 
In Ab3d.DXEngine this problem is solved by using depth bias.";


            //var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Resources/10x10-texture.png"));
            //GroundPlane.Material = new DiffuseMaterial(new ImageBrush(bitmapImage));

            _solidShadowMaterial       = new DiffuseMaterial(Brushes.Black);
            _transparentShadowMaterial = new DiffuseMaterial(new SolidColorBrush(System.Windows.Media.Color.FromArgb(180, 0, 0, 0)));

            _loadedModel3D = LoadModel();

            MainViewport.Children.Add(_loadedModel3D.CreateModelVisual3D());

            // Create PlanarShadowMeshCreator
            // If your model is very complex, then it is recommended to use a simplified 3D model for shadow generation
            _planarShadowMeshCreator = new PlanarShadowMeshCreator(_loadedModel3D);
            _planarShadowMeshCreator.SetPlane(GroundPlane.CenterPosition, GroundPlane.Normal, GroundPlane.HeightDirection, GroundPlane.Size);


            _lightHorizontalAngle = -25;
            _lightVerticalAngle   = 22;
            _lightDistance        = 500;

            _shadowPointLight       = new PointLight();
            _shadowDirectionalLight = new DirectionalLight();

            Camera1.ShowCameraLight = ShowCameraLightType.Never;

            SetShadowLight(isDirectionalLight: true);

            UpdateLights();

            UpdateShadowModel();

            this.PreviewKeyDown += OnPreviewKeyDown;

            // This will allow receiving keyboard events
            this.Focusable = true;
            this.Focus();
        }