Exemple #1
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);
    }