private void UpdateVisibleBoxes() { if (!(CullingCheckBox.IsChecked ?? false) || _frustumStatuses == null) { return; } // // IMPORTANT TIP: // // When you have many 3D objects, do not check each object if it is visible or not. // Instead group the objects into lower number of groups (into ModelVisual3D, Model3DGroup or some other group). // Then calculate the bounding box of each group. // Finally check each group if it is visible or not and remove the hidden groups from visible objects. // Get camera's viewProjection matrix System.Windows.Media.Media3D.Matrix3D view, projection; Camera1.GetCameraMatrixes(out view, out projection); var viewProjection = view * projection; // Create BoundingFrustum from camera view-projection matrix (we need to convert the matrix from WPF to SharpDX format) var boundingFrustum = new SharpDX.BoundingFrustum(viewProjection.ToMatrix()); // We could also get the ViewProjection from DXScene (but when we are called from CameraChanged we may get an older version) //if (MainDXViewportView.DXScene == null) // return; //SharpDX.Matrix worldViewProjectionMatrix = MainDXViewportView.DXScene.Camera.GetViewProjection(); //var boundingFrustum = new SharpDX.BoundingFrustum(worldViewProjectionMatrix); bool hasChanged = false; for (var i = 0; i < _frustumStatuses.Length; i++) // Use for instead of foreach to avoid creating enumerator object on each call of the method (on every frame) { var newVisibility = boundingFrustum.Contains(_frustumStatuses[i].Bounds); if (newVisibility != _frustumStatuses[i].Visibility) { Material newMaterial; switch (newVisibility) { case ContainmentType.Disjoint: newMaterial = _hiddenMaterial; break; case ContainmentType.Contains: newMaterial = _fullyVisibleMaterial; break; case ContainmentType.Intersects: newMaterial = _partiallyVisibleMaterial; break; default: newMaterial = null; break; } _frustumStatuses[i].BoxVisual3D.Material = newMaterial; FrustumVisibilityStatus.SetVisibility(ref _frustumStatuses[i], newVisibility); hasChanged = true; } } if (hasChanged) { UpdateStatistics(); } }
public BoundingFrustum(Matrix viewProj) { this.sbf = new SharpDX.BoundingFrustum(viewProj); }
public BoundingFrustum(SharpDX.BoundingFrustum bf) { this.sbf = bf; }