public void FixedUpdate()
    {
        if (GetComponent <Rigidbody>() != null)
        {
            gravitySources = SGT_CachedFind <SGT_GravitySource> .All(1.0f);

            foreach (var gravitySource in gravitySources)
            {
                GetComponent <Rigidbody>().AddForce(gravitySource.ForceAtPoint(transform.position) * Time.fixedDeltaTime, ForceMode.Acceleration);
            }
        }
    }
Ejemplo n.º 2
0
    public void Update()
    {
        if (GetComponent <Rigidbody>() != null)
        {
            translationVel += GetComponent <Rigidbody>().velocity;
            GetComponent <Rigidbody>().velocity        = Vector3.zero;
            GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
        }

        // Translation
        var moveMul = Input.GetKey(KeyCode.LeftShift) == true ? shiftSpeedMul : 1.0f;

        translationTgt.x = SGT_Input.MoveX * moveSpeed * moveMul;
        translationTgt.z = SGT_Input.MoveY * moveSpeed * moveMul;

        moveSpeed = Mathf.Max(0.0f, moveSpeed + SGT_Input.Zoom * moveSpeed * 2.0f);

        if (translationCur != translationTgt)
        {
            translationCur.x = Mathf.SmoothDamp(translationCur.x, translationTgt.x, ref translationVel.x, moveSmooth, float.PositiveInfinity, SGT_Helper.DeltaTime);
            translationCur.z = Mathf.SmoothDamp(translationCur.z, translationTgt.z, ref translationVel.z, moveSmooth, float.PositiveInfinity, SGT_Helper.DeltaTime);
        }

        var translation = translationCur * SGT_Helper.DeltaTime;

        if (translation != Vector3.zero)
        {
            switch (movePlane)
            {
            case MovementPlane.LocalXZ: transform.Translate(translation.x, 0.0f, translation.z); break;

            case MovementPlane.LocalXY: transform.Translate(translation.x, translation.z, 0.0f); break;
            }
        }

        // Prevent the camera from entering planets/stars
        if (repelBodies == true)
        {
            var currentPosition = transform.position;

            var planets = SGT_CachedFind <SGT_Planet> .All(1.0f);

            foreach (var planet in planets)
            {
                if (planet != null)
                {
                    var radius = planet.SurfaceRadiusAtPoint(currentPosition) * SGT_SurfaceHelper.FindDisplacement(planet.gameObject, currentPosition);

                    RepelByRadius(planet.transform.position, radius * planet.UniformScale);
                }
            }

            var stars = SGT_CachedFind <SGT_Star> .All(1.0f);

            foreach (var star in stars)
            {
                if (star != null)
                {
                    var radius = star.SurfaceRadiusAtPoint(currentPosition) * SGT_SurfaceHelper.FindDisplacement(star.gameObject, currentPosition);

                    RepelByRadius(star.transform.position, radius * star.UniformScale);
                }
            }
        }
    }
Ejemplo n.º 3
0
    // TODO: Support multiple intersection
    public static bool ColourToPoint(Vector3 observer, Vector3 point, float searchDelay, bool ignoreCase1, bool ignoreCase2, out Color finalColour)
    {
        finalColour = Color.clear;

        var gasGiants = SGT_CachedFind <SGT_GasGiant> .All(searchDelay);

        foreach (var gasGiant in gasGiants)
        {
            if (gasGiant != null && gasGiant.atmosphereGameObject != null)
            {
                var oInside = gasGiant.InsideGasGiant(observer);
                var pInside = gasGiant.InsideGasGiant(point);

                // The gas giant itself will provide the colour in these cases
                if (ignoreCase1 == true)
                {
                    if (oInside == true && pInside == false)
                    {
                        continue;
                    }
                }

                if (ignoreCase2 == true)
                {
                    if (oInside == false && pInside == false)
                    {
                        continue;
                    }
                }

                //var oblateFix = 1.0f / (1.0f - gasGiant.gasGiantOblateness);
                var near = gasGiant.atmosphereGameObject.transform.InverseTransformPoint(observer);
                var far  = gasGiant.atmosphereGameObject.transform.InverseTransformPoint(point);
                var far2 = far;                      // This is the actual far point on the gas giant's surface
                var ray  = (far - near).normalized;

                if (oInside == false)
                {
                    var dist = 0.0f;

                    if (SGT_Helper.IntersectRayToSphereA(near, ray, Vector3.zero, 1.0f, out dist) == false)
                    {
                        continue;
                    }

                    near = (near + ray * dist);
                }

                if (pInside == false)
                {
                    var dist = 0.0f;

                    if (SGT_Helper.IntersectRayToSphereA(far, -ray, Vector3.zero, 1.0f, out dist) == false)
                    {
                        continue;
                    }

                    far  = (far - ray * dist);
                    far2 = far;
                }
                else
                {
                    var dist = 0.0f;

                    if (SGT_Helper.IntersectRayToSphereB(far, -ray, Vector3.zero, 1.0f, out dist) == false)
                    {
                        continue;
                    }

                    far2 = (far2 + ray * dist).normalized;
                }

                var polar    = SGT_Helper.CartesianToPolarUV(near);
                var nearDir  = near.normalized;
                var lightDir = gasGiant.atmosphereGameObject.transform.InverseTransformDirection(gasGiant.GasGiantLightSourceDirection);
                var lightU   = Vector3.Dot(nearDir, lightDir) * 0.5f + 0.5f;
                var lightV   = 1.0f - Vector3.Dot(ray, far2);

                var day      = gasGiant.SampleAtmosphereDay(polar);
                var night    = gasGiant.SampleAtmosphereNight(polar);
                var lighting = gasGiant.SampleLighting(new Vector2(lightU, lightV));

                var falloff      = gasGiant.atmosphereDensityFalloff * gasGiant.atmosphereDensityFalloff;
                var depthRatio   = ((near - far).magnitude * gasGiant.gasGiantEquatorialRadius) / gasGiant.maxDepth;
                var opticalDepth = Mathf.Pow(Mathf.Clamp01(SGT_Helper.Expose(depthRatio)), falloff);

                finalColour   = SGT_Helper.Lerp(night, day, lighting);
                finalColour.a = opticalDepth;

                return(true);
            }
        }

        return(false);
    }