public ModelViewer() { this.InitializeComponent(); // Create a camera and a ContainerVisual _compositor = Window.Current.Compositor; var root = _compositor.CreateContainerVisual(); root.Size = new Vector2(1000, 1000); ElementCompositionPreview.SetElementChildVisual(this, root); // create a SceneVisual and insert it into the visual tree _sceneVisual = SceneVisual.Create(_compositor); root.Children.InsertAtTop(_sceneVisual); // instantiate viewport and assign it "good" default values _viewport = new Viewport(_compositor); _viewport.AttachToVisual(_sceneVisual); _viewport.Size = Target.ActualSize; _viewport.Offset = new Vector3(_viewport.Size / 2f, 0f); // instantiate camera and assign it "good" default values _camera = new OrbitalCamera(_compositor); _camera.Target = new Vector3(0f, 0f, 0f); _camera.Radius = 600f; _camera.Theta = 0f; _camera.Phi = MathF.PI / 4; // instantiate projection and assign it "good" default values PerspectiveProjection projection = new PerspectiveProjection(_compositor); projection.Fov = MathF.PI / 2; _camera.Projection = projection; _viewport.Camera = _camera; // add event handler for chaning target size Target.SizeChanged += Target_SizeChanged; // event handlers for pointer events Target.PointerWheelChanged += Target_PointerWheelChanged; Target.PointerPressed += Target_PointerPressed; Target.PointerReleased += Target_PointerReleased; Target.PointerMoved += Target_PointerMoved; Window.Current.CoreWindow.PointerReleased += CoreWindow_PointerReleased; }
/// <summary> /// Creates a Viewport with default properties. /// Offset = Vector3.Zero /// Size = Vector2(100, 100) /// Stretch = Uniform /// StretchMatrix = Matrix4x4.Identity /// </summary> /// <param name="compositor"></param> /// <exception cref="System.ArgumentException">Thrown when constructor is passed a null value.</exception> public Viewport(Compositor compositor) { if (compositor == null) { throw new System.ArgumentException("Compositor cannot be null"); } _compositor = compositor; _propertySet = _compositor.CreatePropertySet(); // Create properties of viewport _propertySet.InsertVector3("Offset", Vector3.Zero); _propertySet.InsertVector2("Size", new Vector2(100, 100)); _propertySet.InsertScalar("Stretch", (int)Stretch.Uniform); _propertySet.InsertMatrix4x4("StretchMatrix", Matrix4x4.Identity); Camera = new OrbitalCamera(_compositor); StartAnimationsOnStretchMatrix(); }