public override void Update(double deltaTime)
        {
            var p = GeometryUtility.CalculateFrustumPlanes(GetComponent <Camera>());

            for (int i = 0; i < 6; i++)
            {
                // is broken maybe, furstum culling works but this doesnt make much sense
                //gos[i].transform.position = p[i].normal * p[i].distance;
                gos[i].transform.rotation = QuaternionUtility.LookRotation(p[i].normal);
            }
        }
예제 #2
0
        void Update(float deltaTime)
        {
            if (deltaTime > 1 / 30f)
            {
                deltaTime = 1 / 30f;
            }

            var rotation = Transform.Rotation;
            var position = Transform.Position;


            if (Debug.GetCVar("game / camera position 1 / save").EatBoolIfTrue())
            {
                savedPosition1 = position;
                savedRotation1 = rotation;
            }
            if (Debug.GetCVar("game / camera position 1 / load").EatBoolIfTrue())
            {
                position = Transform.Position = savedPosition1;
                rotation = Transform.Rotation = savedRotation1;
            }

            if (Debug.GetCVar("game / camera position 2 / save").EatBoolIfTrue())
            {
                savedPosition2 = position;
                savedRotation2 = rotation;
            }
            if (Debug.GetCVar("game / camera position 2 / load").EatBoolIfTrue())
            {
                position = Transform.Position = savedPosition2;
                rotation = Transform.Rotation = savedRotation2;
            }


            var planet = planets?.GetClosestPlanet(position);


            Debug.AddValue("camera / speed modifier", cameraSpeedModifier);
            Debug.AddValue("camera / position", position);

            var mouse = Mouse.GetState();

            var mouseDelta = new Point(mouse.X - lastMousePos.X, mouse.Y - lastMousePos.Y);

            lastMousePos = new Point(mouse.X, mouse.Y);

            int scrollWheelDelta = mouse.ScrollWheelValue - scrollWheelValue;

            scrollWheelValue = mouse.ScrollWheelValue;


            if (Input.GetKeyDown(Key.Escape))
            {
                if (Scene.Engine.WindowState == WindowState.Fullscreen)
                {
                    Scene.Engine.WindowState = WindowState.Normal;
                }
                else if (Input.LockCursor)
                {
                    Input.LockCursor    = false;
                    Input.CursorVisible = true;
                }
                else
                {
                    Scene.Engine.Exit();
                }
            }

            if (Input.GeMouseButtonDown(MouseButton.Left))
            {
                if (Input.LockCursor == false)
                {
                    Input.LockCursor    = true;
                    Input.CursorVisible = false;
                }
            }


            float planetSpeedModifier = 1;

            if (planet != null)
            {
                var planetLocalPosition          = planet.Transform.Position.Towards(position).ToVector3d();
                var sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
                var onPlanetSurfaceHeight        = planet.GetSurfaceHeight(planetLocalPosition);
                var onPlanetDistanceToSurface    = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;

                {
                    Debug.AddValue("camera / distance to surface", onPlanetDistanceToSurface);
                    {
                        var s = MyMath.SmoothStep(1, 30000, (float)onPlanetDistanceToSurface);
                        cam.NearClipPlane = 1000 * s + 0.5f;
                        cam.FarClipPlane  = 5000000 * s + 100000;
                    }
                }


                if (speedBasedOnDistanceToPlanet)
                {
                    var s = MyMath.Clamp(onPlanetDistanceToSurface, 1, 30000);
                    planetSpeedModifier = (1 + (float)s / 5.0f);
                }
            }

            if (Input.LockCursor)
            {
                if (scrollWheelDelta > 0)
                {
                    cameraSpeedModifier *= 1.3f;
                }
                if (scrollWheelDelta < 0)
                {
                    cameraSpeedModifier /= 1.3f;
                }
                cameraSpeedModifier = MyMath.Clamp(cameraSpeedModifier, 1, 100000);
                float currentSpeed = cameraSpeedModifier * planetSpeedModifier;



                if (Input.GetKey(Key.ShiftLeft))
                {
                    currentSpeed *= 5;
                }

                Debug.AddValue("camera / real speed", currentSpeed);

                var targetVelocity = Vector3.Zero;
                if (Input.GetKey(Key.W))
                {
                    targetVelocity += currentSpeed * Constants.Vector3Forward;
                }
                if (Input.GetKey(Key.S))
                {
                    targetVelocity -= currentSpeed * Constants.Vector3Forward;
                }
                if (Input.GetKey(Key.D))
                {
                    targetVelocity += currentSpeed * Constants.Vector3Right;
                }
                if (Input.GetKey(Key.A))
                {
                    targetVelocity -= currentSpeed * Constants.Vector3Right;
                }
                if (Input.GetKey(Key.Space))
                {
                    targetVelocity += currentSpeed * Constants.Vector3Up;
                }
                if (Input.GetKey(Key.ControlLeft))
                {
                    targetVelocity -= currentSpeed * Constants.Vector3Up;
                }

                //var pos = Matrix4.CreateTranslation(targetVelocity);


                float pitchDelta = 0;
                float yawDelta   = 0;
                float rollDelta  = 0;

                float c = mouseSensitivty * (float)deltaTime;
                yawDelta   += mouseDelta.X * c;
                pitchDelta += mouseDelta.Y * c;

                if (Input.GetKey(Key.Q))
                {
                    rollDelta -= c;
                }
                if (Input.GetKey(Key.E))
                {
                    rollDelta += c;
                }


                if (planet != null && Input.GetKeyDown(Key.C))
                {
                    rotation = position.Towards(planet.Transform.Position).ToVector3d().Normalized().ToVector3().LookRot();
                }

                if (planet != null && WalkOnPlanet.Bool)
                {
                    var up      = planet.Center.Towards(position).ToVector3().Normalized();
                    var forward = walkOnSphere_lastForward;

                    if (walkOnSphere_isFirstRun)
                    {
                        walkOnSphere_lastUp = up;

                        var pointOnPlanet = planet.Center.Towards(position).ToVector3d();
                        var s             = planet.CalestialToSpherical(pointOnPlanet);
                        //s.latitude += 0.1f;
                        //var forwardToPole = pointOnPlanet.Towards(planet.SphericalToCalestial(s)).Normalized().ToVector3().Normalized();
                        forward = Constants.Vector3Forward.RotateBy(rotation);
                    }
                    else
                    {
                        var upDeltaAngle = up.Angle(walkOnSphere_lastUp);
                        var upDeltaRot   = Quaternion.FromAxisAngle(up.Cross(walkOnSphere_lastUp), upDeltaAngle).Inverted();

                        forward = forward.RotateBy(upDeltaRot);
                    }


                    var left = up.Cross(forward);

                    var rotDelta =
                        Quaternion.FromAxisAngle(up, -yawDelta) *
                        Quaternion.FromAxisAngle(left, pitchDelta);


                    forward = forward.RotateBy(rotDelta);

                    {
                        // clamping up down rotation
                        var maxUpDownAngle = 80;
                        var minUp          = MyMath.ToRadians(90 - maxUpDownAngle);
                        var maxDown        = MyMath.ToRadians(90 + maxUpDownAngle);
                        var angle          = forward.Angle(up);
                        if (angle < minUp)
                        {
                            forward = up.RotateBy(Quaternion.FromAxisAngle(left, minUp));
                        }
                        else if (angle > maxDown)
                        {
                            forward = up.RotateBy(Quaternion.FromAxisAngle(left, maxDown));
                        }
                    }


                    forward.Normalize();

                    rotation = QuaternionUtility.LookRotation(forward, up);

                    walkOnSphere_lastForward = forward;
                    walkOnSphere_lastUp      = up;
                    walkOnSphere_isFirstRun  = false;
                }
                else
                {
                    var rotDelta =
                        Quaternion.FromAxisAngle(-Vector3.UnitX, pitchDelta) *
                        Quaternion.FromAxisAngle(-Vector3.UnitY, yawDelta) *
                        Quaternion.FromAxisAngle(-Vector3.UnitZ, rollDelta);

                    rotation = rotation * rotDelta;

                    walkOnSphere_isFirstRun = true;
                }



                Transform.Rotation = rotation;

                targetVelocity  = targetVelocity.RotateBy(rotation);
                currentVelocity = Vector3.Lerp(currentVelocity, targetVelocity, velocityChangeSpeed * (float)deltaTime);

                position += currentVelocity * (float)deltaTime;

                // make cam on top of the planet
                if (planet != null && collideWithPlanetSurface)
                {
                    var planetLocalPosition          = planet.Transform.Position.Towards(position).ToVector3d();
                    var sphericalPlanetLocalPosition = planet.CalestialToSpherical(planetLocalPosition);
                    var onPlanetSurfaceHeight        = planet.GetSurfaceHeight(planetLocalPosition);
                    var onPlanetDistanceToSurface    = sphericalPlanetLocalPosition.altitude - onPlanetSurfaceHeight;

                    var h = onPlanetSurfaceHeight + 2;
                    if (sphericalPlanetLocalPosition.altitude <= h || WalkOnPlanet.Bool)
                    {
                        sphericalPlanetLocalPosition.altitude = h;
                        position = planet.Transform.Position + planet.SphericalToCalestial(sphericalPlanetLocalPosition);
                    }
                }


                Transform.Position = position;                 // += Entity.Transform.Position.Towards(position).ToVector3d() * deltaTime * 10;

                //Log.Info(entity.transform.position);
            }
        }
        public override void Update(double deltaTime)
        {
            var        ray = new Ray(this.transform.position, this.transform.forward);
            RaycastHit raycastHit;

            if (Physics.Raycast(ray, out raycastHit))
            {
                visualiseHitTarget.transform.position = raycastHit.point;
                visualiseHitTarget.transform.rotation = QuaternionUtility.LookRotation(raycastHit.normal);
                var legoPiece = raycastHit.collider.gameObject.GetComponent <LegoPiece>();
                if (legoPiece)
                {
                    if (legoPiece != lastLegoPiece)
                    {
                        if (lastLegoPiece)
                        {
                            lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                        }
                        lastLegoPiece = legoPiece;

                        var m = lastLegoPiece.GetComponent <MeshRenderer>().material;
                        lastAlbedo = m.albedo;
                        const float T = 1.0f;
                        const float F = 0.3f;
                        if (clickedLegoPiece && clickedLegoPiece != lastLegoPiece)
                        {
                            m.albedo = new Vector4(F, T, F, 1);
                        }
                        else
                        {
                            m.albedo = new Vector4(T, F, F, 1);
                        }
                    }

                    if (clickedLegoPiece && lastLegoPiece && clickedLegoPiece != lastLegoPiece)
                    {
                        clickedLegoPiece.VizualizeConnectionTo(lastLegoPiece, visualiseHitTarget.transform.position);
                    }
                }
                else
                {
                    if (lastLegoPiece)
                    {
                        lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                    }
                    lastLegoPiece = null;
                }
            }
            else
            {
                if (lastLegoPiece)
                {
                    lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                }
                lastLegoPiece = null;
            }


            if (Input.GeMouseButtonDown(MouseButton.Left))
            {
                if (lastLegoPiece == null)
                {
                    if (clickedLegoPiece != null)
                    {
                        clickedLegoPiece.EndVisualise();
                        var p = visualiseHitTarget.transform.position - clickedLegoPiece.GetComponent <MeshRenderer>().mesh.bounds.center;
                        p.Y += 0.5f;
                        clickedLegoPiece.transform.position = p;
                        clickedLegoPiece.transform.rotation = visualiseHitTarget.transform.rotation;
                        clickedLegoPiece = null;
                    }
                }
                else
                {
                    if (clickedLegoPiece != null)
                    {
                        if (clickedLegoPiece != lastLegoPiece)
                        {
                            clickedLegoPiece.VizualizeConnectionTo(lastLegoPiece, visualiseHitTarget.transform.position);
                            clickedLegoPiece.ConnectTo(lastLegoPiece);
                            clickedLegoPiece = null;
                        }
                    }
                    else
                    {
                        clickedLegoPiece = lastLegoPiece;
                        clickedLegoPiece.StartVisualise();
                    }
                }
            }


            if (Input.GeMouseButton(MouseButton.Right))
            {
                if (lastLegoPiece)
                {
                    var rb = lastLegoPiece.GetComponent <Rigidbody>();
                    if (rb)
                    {
                        rb.AddForceAtPosition(transform.forward * 100, transform.position);
                    }
                }
            }

            if (Input.GetKeyDown(Key.L))
            {
                Debug.Info("a");
                Physics.IgnoreCollision(lastLegoPiece.GetComponent <Collider>(), clickedLegoPiece.GetComponent <Collider>(), true);
            }



            if (Input.GetKey(Key.AltLeft))
            {
                MyEngine.ParticleSimulation.Manager.instance.GenerateParticles(1000, visualiseHitTarget.transform.position, new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), 1000, 1, 1);
            }
        }