Пример #1
0
            private void Core_OnUpdateLightSource(object sender, ShadowMapCore.UpdateLightSourceEventArgs e)
            {
                CameraCore camera = LightCamera ?? null;

                if (LightCamera == null)
                {
                    var lights = e.Context.RenderHost.PerFrameLights.Take(Constants.MaxLights);
                    foreach (var light in lights)
                    {
                        if (light.LightType == LightType.Directional)
                        {
                            var dlight = light.RenderCore as DirectionalLightCore;
                            var dir    = Vector3.TransformNormal(dlight.Direction, dlight.ModelMatrix).Normalized() * distance;
                            orthoCamera.LookDirection = dir;
                            orthoCamera.Position      = -dir;
                            orthoCamera.UpDirection   = Vector3.UnitZ;
                            orthoCamera.Width         = orthoWidth;
                            camera = orthoCamera;
                            break;
                        }
                        else if (light.LightType == LightType.Spot)
                        {
                            var splight = light.RenderCore as SpotLightCore;
                            persCamera.Position = (splight.Position + splight.ModelMatrix.Row4.ToVector3());
                            var look = Vector3.TransformNormal(splight.Direction, splight.ModelMatrix);
                            persCamera.LookDirection    = look;
                            persCamera.FarPlaneDistance = (float)splight.Range;
                            persCamera.FieldOfView      = (float)splight.OuterAngle;
                            persCamera.UpDirection      = Vector3.UnitZ;
                            camera = persCamera;
                            break;
                        }
                    }
                }
                if (camera == null)
                {
                    shadowCore.FoundLightSource = false;
                }
                else
                {
                    shadowCore.FoundLightSource       = true;
                    shadowCore.LightViewProjectMatrix = camera.CreateViewMatrix() * camera.CreateProjectionMatrix(shadowCore.Width / shadowCore.Height);
                }
            }
Пример #2
0
            /// <summary>
            /// Rotates using turntable.
            /// </summary>
            /// <param name="cameraMode">The camera mode.</param>
            /// <param name="delta">The delta.</param>
            /// <param name="rotateAround">The rotate around.</param>
            /// <param name="sensitivity">The sensitivity.</param>
            /// <param name="viewportWidth">Width of the viewport.</param>
            /// <param name="viewportHeight">Height of the viewport.</param>
            /// <param name="camera">The camera.</param>
            /// <param name="invertFactor">The invert factor. Right Handed = 1; Left Handed = -1;</param>
            /// <param name="modelUpDirection">The model up direction.</param>
            /// <param name="newPosition">The new position.</param>
            /// <param name="newLookDirection">The new look direction.</param>
            /// <param name="newUpDirection">The new up direction.</param>
            public static void RotateTurntable(CameraMode cameraMode, ref Vector2 delta, ref Vector3 rotateAround,
                                               float sensitivity,
                                               int viewportWidth, int viewportHeight,
                                               CameraCore camera, int invertFactor,
                                               Vector3 modelUpDirection,
                                               out Vector3 newPosition, out Vector3 newLookDirection, out Vector3 newUpDirection)
            {
                var relativeTarget   = rotateAround - camera.Target;
                var relativePosition = rotateAround - camera.Position;
                var cUp   = Vector3.Normalize(camera.UpDirection);
                var up    = modelUpDirection;
                var dir   = Vector3.Normalize(camera.LookDirection);
                var right = Vector3.Normalize(Vector3.Cross(dir, cUp));

                float d = -0.5f;

                if (cameraMode != CameraMode.Inspect)
                {
                    d *= -0.2f;
                }

                d *= sensitivity;

                var        q1 = Quaternion.RotationAxis(up, d * invertFactor * delta.X / 180 * (float)Math.PI);
                var        q2 = Quaternion.RotationAxis(right, d * delta.Y / 180 * (float)Math.PI);
                Quaternion q  = q1 * q2;

                var m = Matrix.RotationQuaternion(q);

                newUpDirection = Vector3.TransformNormal(cUp, m);

                var newRelativeTarget   = Vector3.TransformCoordinate(relativeTarget, m);
                var newRelativePosition = Vector3.TransformCoordinate(relativePosition, m);

                var newTarget = rotateAround - newRelativeTarget;

                newPosition = rotateAround - newRelativePosition;

                newLookDirection = (newTarget - newPosition);
                if (cameraMode != CameraMode.Inspect)
                {
                    newPosition = camera.Position;
                }
            }
Пример #3
0
        /// <summary>
        /// Zooms the camera to the specified rectangle.
        /// </summary>
        /// <param name="camera">
        /// The camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="zoomRectangle">
        /// The zoom rectangle.
        /// </param>
        public static void ZoomToRectangle(this CameraCore camera, ViewportCore viewport, RectangleF zoomRectangle)
        {
            if (camera is ProjectionCameraCore pcam)
            {
                if (viewport.UnProject(new Vector2(zoomRectangle.Top, zoomRectangle.Left), out var topLeftRay) &&
                    viewport.UnProject(new Vector2(zoomRectangle.Top, zoomRectangle.Right), out var topRightRay) &&
                    viewport.UnProject(
                        new Vector2(
                            (zoomRectangle.Left + zoomRectangle.Right) * 0.5f,
                            (zoomRectangle.Top + zoomRectangle.Bottom) * 0.5f), out var centerRay))
                {
                    var u = topLeftRay.Direction;
                    var v = topRightRay.Direction;
                    var w = centerRay.Direction;
                    u.Normalize();
                    v.Normalize();
                    w.Normalize();
                    if (camera is PerspectiveCameraCore perspectiveCamera)
                    {
                        var distance = pcam.LookDirection.Length();

                        // option 1: change distance
                        var newDistance      = distance * zoomRectangle.Width / viewport.ViewportRectangle.Width;
                        var newLookDirection = (float)newDistance * w;
                        var newPosition      = perspectiveCamera.Position + ((distance - (float)newDistance) * w);
                        var newTarget        = newPosition + newLookDirection;
                        LookAt(pcam, newTarget, newLookDirection, 200);
                    }
                    else if (camera is OrthographicCameraCore orthographicCamera)
                    {
                        orthographicCamera.Width *= zoomRectangle.Width / viewport.ViewportRectangle.Width;
                        var oldTarget = pcam.Position + pcam.LookDirection;
                        var distance  = pcam.LookDirection.Length();

                        if (centerRay.PlaneIntersection(oldTarget, w, out var newTarget))
                        {
                            orthographicCamera.LookDirection = w * distance;
                            orthographicCamera.Position      = newTarget - orthographicCamera.LookDirection;
                        }
                    }
                }
            }
        }
Пример #4
0
        protected override void OnMouse3DDown(object sender, RoutedEventArgs e)
        {
            base.OnMouse3DDown(sender, e);

            var args = e as Mouse3DEventArgs;

            if (args == null)
            {
                return;
            }
            if (args.Viewport == null)
            {
                return;
            }

            this.isCaptured     = true;
            this.viewport       = args.Viewport;
            this.viewportCamera = args.Viewport.Camera;
            this.lastHitPos     = args.HitTestResult.PointHit;
        }
Пример #5
0
    //DRAW Camera Limits Zone ( Red Box )
    void OnDrawGizmos()
    {
        cam = GetComponent <CameraCore>();


        if (boundToLimits)
        {
            float temp;



            cameraLimits.SetMinMax(new Vector3(limiterPos.x - width / 2, limiterPos.y - height / 2, 0), new Vector3(limiterPos.x + width / 2, limiterPos.y + height / 2, 0));

            Gizmos.color = Color.red;
            Gizmos.DrawLine(cameraLimits.min, new Vector3(cameraLimits.min.x, cameraLimits.max.y, 0));
            Gizmos.DrawLine(new Vector3(cameraLimits.min.x, cameraLimits.max.y, 0), cameraLimits.max);
            Gizmos.DrawLine(cameraLimits.max, new Vector3(cameraLimits.max.x, cameraLimits.min.y, 0));
            Gizmos.DrawLine(new Vector3(cameraLimits.max.x, cameraLimits.min.y, 0), cameraLimits.min);
        }
    }
Пример #6
0
        /// <summary>
        /// Zooms to fit the specified sphere.
        /// </summary>
        /// <param name="camera">
        /// The camera.
        /// </param>
        /// <param name="viewport">
        /// The viewport.
        /// </param>
        /// <param name="center">
        /// The center of the sphere.
        /// </param>
        /// <param name="radius">
        /// The radius of the sphere.
        /// </param>
        /// <param name="animationTime">
        /// The animation time.
        /// </param>
        public static void ZoomExtents(
            this CameraCore camera, ViewportCore viewport, Vector3 center, float radius, float animationTime = 0)
        {
            // var target = Camera.Position + Camera.LookDirection;
            if (camera is PerspectiveCameraCore pcam)
            {
                float disth = radius / (float)Math.Tan(0.5 * pcam.FieldOfView * Math.PI / 180);
                float vfov  = pcam.FieldOfView / viewport.ViewportRectangle.Width * viewport.ViewportRectangle.Height;
                float distv = radius / (float)Math.Tan(0.5 * vfov * Math.PI / 180);

                float dist = Math.Max(disth, distv);
                var   dir  = camera.LookDirection;
                dir.Normalize();
                LookAt(camera, center, dir * dist, animationTime);
            }
            else if (camera is OrthographicCameraCore orth)
            {
                orth.LookAt(center, 0);
                var newWidth = radius * 2;
                orth.AnimateWidth(newWidth, animationTime);
            }
        }
Пример #7
0
        public static Matrix GetInversedViewMatrix(this CameraCore camera)
        {
            var viewMatrix = GetViewMatrix(camera);

            return(MatrixExtensions.PsudoInvert(ref viewMatrix));
        }
Пример #8
0
 /// <summary>
 /// Obtains the view transform matrix for a camera. (see page 327)
 /// </summary>
 /// <param name="camera">
 /// Camera to obtain the ViewMatrix for
 /// </param>
 /// <returns>
 /// A Matrix object with the camera view transform matrix, or a Matrix with all zeros if the "camera" is null.
 /// </returns>
 public static Matrix GetViewMatrix(this CameraCore camera)
 {
     return(camera.CreateViewMatrix());
 }
Пример #9
0
 /// <summary>
 /// Gets the projection matrix for the specified camera.
 /// </summary>
 /// <param name="camera">The camera.</param>
 /// <param name="aspectRatio">The aspect ratio.</param>
 /// <returns>The projection matrix.</returns>
 public static Matrix GetProjectionMatrix(this CameraCore camera, double aspectRatio)
 {
     return(camera.CreateProjectionMatrix((float)aspectRatio));
 }
Пример #10
0
            /// <summary>
            /// Rotates the trackball.
            /// </summary>
            /// <param name="cameraMode">The camera mode.</param>
            /// <param name="p1">The p1.</param>
            /// <param name="p2">The p2.</param>
            /// <param name="rotateAround">The rotate around.</param>
            /// <param name="sensitivity">The sensitivity.</param>
            /// <param name="viewportWidth">Width of the viewport.</param>
            /// <param name="viewportHeight">Height of the viewport.</param>
            /// <param name="camera">The camera.</param>
            /// <param name="invertFactor">The invert factor. Right Handed System = 1; LeftHandedSystem = -1;</param>
            /// <param name="newPosition">The new position.</param>
            /// <param name="newLookDirection">The new look direction.</param>
            /// <param name="newUpDirection">The new up direction.</param>
            public static void RotateTrackball(CameraMode cameraMode, ref Vector2 p1, ref Vector2 p2, ref Vector3 rotateAround,
                                               float sensitivity,
                                               int viewportWidth, int viewportHeight, CameraCore camera, int invertFactor,
                                               out Vector3 newPosition, out Vector3 newLookDirection, out Vector3 newUpDirection)
            {
                // http://viewport3d.com/trackball.htm
                // http://www.codeplex.com/3DTools/Thread/View.aspx?ThreadId=22310
                var v1  = ProjectToTrackball(p1, viewportWidth, viewportHeight);
                var v2  = ProjectToTrackball(p2, viewportWidth, viewportHeight);
                var cUP = Vector3.Normalize(camera.UpDirection);
                // transform the trackball coordinates to view space
                var viewZ = Vector3.Normalize(camera.LookDirection * invertFactor);
                var viewX = Vector3.Normalize(Vector3.Cross(cUP, viewZ)) * invertFactor;
                var viewY = Vector3.Cross(viewX, viewZ);
                var u1    = (viewZ * v1.Z) + (viewX * v1.X) + (viewY * v1.Y);
                var u2    = (viewZ * v2.Z) + (viewX * v2.X) + (viewY * v2.Y);

                // Could also use the Camera ViewMatrix
                // var vm = Viewport3DHelper.GetViewMatrix(this.ActualCamera);
                // vm.Invert();
                // var ct = new MatrixTransform3D(vm);
                // var u1 = ct.Transform(v1);
                // var u2 = ct.Transform(v2);

                // Find the rotation axis and angle
                var axis = Vector3.Cross(u1, u2);

                if (axis.LengthSquared() < 1e-8)
                {
                    newPosition      = camera.Position;
                    newLookDirection = camera.LookDirection;
                    newUpDirection   = camera.UpDirection;
                    return;
                }

                var angle = VectorExtensions.AngleBetween(u1, u2);

                // Create the transform
                var rotate = Matrix.RotationAxis(Vector3.Normalize(axis), -angle * sensitivity * 5);

                // Find vectors relative to the rotate-around point
                var relativeTarget   = rotateAround - camera.Target;
                var relativePosition = rotateAround - camera.Position;

                // Rotate the relative vectors
                var newRelativeTarget   = Vector3.TransformCoordinate(relativeTarget, rotate);
                var newRelativePosition = Vector3.TransformCoordinate(relativePosition, rotate);

                newUpDirection = Vector3.TransformNormal(cUP, rotate);

                // Find new camera position
                var newTarget = rotateAround - newRelativeTarget;

                newPosition = rotateAround - newRelativePosition;

                newLookDirection = newTarget - newPosition;
                if (cameraMode != CameraMode.Inspect)
                {
                    newPosition = camera.Position;
                }
            }
Пример #11
0
        private void InitializeScene()
        {
            camera = new PerspectiveCameraCore()
            {
                LookDirection     = new Vector3(0, 0, 50),
                Position          = new Vector3(0, 0, -50),
                FarPlaneDistance  = 1000,
                NearPlaneDistance = 0.1f,
                FieldOfView       = 45,
                UpDirection       = new Vector3(0, 1, 0)
            };
            viewport.CameraCore = camera;
            viewport.Items.Add(new DirectionalLightNode()
            {
                Direction = new Vector3(0, -1, 1), Color = Color.White
            });
            viewport.Items.Add(new PointLightNode()
            {
                Position = new Vector3(0, 0, -20), Color = Color.Yellow, Range = 20, Attenuation = Vector3.One
            });

            var builder = new MeshBuilder(true, true, true);

            builder.AddSphere(Vector3.Zero, 1, 12, 12);
            sphere  = builder.ToMesh();
            builder = new MeshBuilder(true, true, true);
            builder.AddBox(Vector3.Zero, 1, 1, 1);
            box    = builder.ToMesh();
            points = new PointGeometry3D()
            {
                Positions = sphere.Positions
            };
            var lineBuilder = new LineBuilder();

            lineBuilder.AddBox(Vector3.Zero, 2, 2, 2);
            lines       = lineBuilder.ToLineGeometry3D();
            groupSphere = new GroupNode();
            groupBox    = new GroupNode();
            groupLines  = new GroupNode();
            groupPoints = new GroupNode();
            InitializeMaterials();
            materialList = materials.Values.ToArray();
            var materialCount = materialList.Length;

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                groupSphere.AddChildNode(new MeshNode()
                {
                    Geometry = sphere, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupBox.AddChildNode(new MeshNode()
                {
                    Geometry = box, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            //for(int i=0; i< NumItems; ++i)
            //{
            //    var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
            //    groupPoints.AddChildNode(new PointNode() { Geometry = points, ModelMatrix = transform, Material = new PointMaterialCore() { PointColor = Color.Red } });
            //}

            //for (int i = 0; i < NumItems; ++i)
            //{
            //    var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
            //    groupLines.AddChildNode(new LineNode() { Geometry = lines, ModelMatrix = transform, Material = new LineMaterialCore() { LineColor = Color.LightBlue } });
            //}

            viewport.Items.Add(groupSphere);
            groupSphere.AddChildNode(groupBox);
            groupSphere.AddChildNode(groupPoints);
            groupSphere.AddChildNode(groupLines);

            var viewbox = new ViewBoxNode();

            viewport.Items.Add(viewbox);
            var imGui = new ImGuiNode();

            viewport.Items.Add(imGui);
            imGui.UpdatingImGuiUI       += ImGui_UpdatingImGuiUI;
            io.KeyMap[GuiKey.Tab]        = (int)Keys.Tab;
            io.KeyMap[GuiKey.LeftArrow]  = (int)Keys.Left;
            io.KeyMap[GuiKey.RightArrow] = (int)Keys.Right;
            io.KeyMap[GuiKey.UpArrow]    = (int)Keys.Up;
            io.KeyMap[GuiKey.DownArrow]  = (int)Keys.Down;
            io.KeyMap[GuiKey.PageUp]     = (int)Keys.PageUp;
            io.KeyMap[GuiKey.PageDown]   = (int)Keys.PageDown;
            io.KeyMap[GuiKey.Home]       = (int)Keys.Home;
            io.KeyMap[GuiKey.End]        = (int)Keys.End;
            io.KeyMap[GuiKey.Delete]     = (int)Keys.Delete;
            io.KeyMap[GuiKey.Backspace]  = (int)Keys.Back;
            io.KeyMap[GuiKey.Enter]      = (int)Keys.Enter;
            io.KeyMap[GuiKey.Escape]     = (int)Keys.Escape;
        }
        private void InitializeScene()
        {
            camera = new PerspectiveCameraCore()
            {
                LookDirection     = new Vector3(0, 0, 50),
                Position          = new Vector3(0, 0, -50),
                FarPlaneDistance  = 1000,
                NearPlaneDistance = 0.1f,
                FieldOfView       = 45,
                UpDirection       = new Vector3(0, 1, 0)
            };
            viewport.CameraCore = camera;
            viewport.Items.Add(new DirectionalLightNode()
            {
                Direction = new Vector3(0, -1, 1), Color = Color.White
            });
            viewport.Items.Add(new PointLightNode()
            {
                Position = new Vector3(0, 0, -20), Color = Color.Yellow, Range = 20, Attenuation = Vector3.One
            });

            var builder = new MeshBuilder(true, true, true);

            builder.AddSphere(Vector3.Zero, 1, 12, 12);
            sphere  = builder.ToMesh();
            builder = new MeshBuilder(true, true, true);
            builder.AddBox(Vector3.Zero, 1, 1, 1);
            box    = builder.ToMesh();
            points = new PointGeometry3D()
            {
                Positions = sphere.Positions
            };
            var lineBuilder = new LineBuilder();

            lineBuilder.AddBox(Vector3.Zero, 2, 2, 2);
            lines       = lineBuilder.ToLineGeometry3D();
            groupSphere = new GroupNode();
            groupBox    = new GroupNode();
            groupLines  = new GroupNode();
            groupPoints = new GroupNode();
            InitializeMaterials();
            materialList = materials.Values.ToArray();
            var materialCount = materialList.Length;

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20), rnd.NextFloat(-20, 20)));
                groupSphere.AddChildNode(new MeshNode()
                {
                    Geometry = sphere, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupBox.AddChildNode(new MeshNode()
                {
                    Geometry = box, Material = materialList[i % materialCount], ModelMatrix = transform, CullMode = SharpDX.Direct3D11.CullMode.Back
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupPoints.AddChildNode(new PointNode()
                {
                    Geometry = points, ModelMatrix = transform, Color = Color.Red, Size = new Size2F(0.5f, 0.5f)
                });
            }

            for (int i = 0; i < NumItems; ++i)
            {
                var transform = Matrix.Translation(new Vector3(rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50), rnd.NextFloat(-50, 50)));
                groupLines.AddChildNode(new LineNode()
                {
                    Geometry = lines, ModelMatrix = transform, Color = Color.LightBlue, Thickness = 0.5f
                });
            }

            viewport.Items.Add(groupSphere);
            groupSphere.AddChildNode(groupBox);
            groupSphere.AddChildNode(groupPoints);
            groupSphere.AddChildNode(groupLines);

            var viewbox = new ViewBoxNode();

            viewport.Items.Add(viewbox);
        }
Пример #13
0
 void Start()
 {
     cam = GetComponent <CameraCore>();
 }