Ejemplo n.º 1
0
    private void UpdateGradient()
    {
        // See if any gradient has been modified
        if (planetLighting.Modified == true)
        {
            planetLighting.Modified = false;

            surfaceLightingTexture   = SGT_Helper.DestroyObject(surfaceLightingTexture);
            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
            cloudsLightingTexture    = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        if (atmosphereTwilightColour.Modified == true)
        {
            atmosphereTwilightColour.Modified = false;

            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
            cloudsLightingTexture    = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        if (atmosphereDensityColour.Modified == true)
        {
            atmosphereDensityColour.Modified = false;

            atmosphereTexture        = SGT_Helper.DestroyObject(atmosphereTexture);
            atmosphereSurfaceTexture = SGT_Helper.DestroyObject(atmosphereSurfaceTexture);
        }

        if (cloudsLimbColour.Modified == true)
        {
            cloudsLimbColour.Modified = false;

            cloudsLightingTexture = SGT_Helper.DestroyObject(cloudsLightingTexture);
        }

        // Build colour arrays
        var lighting = surfaceLightingTexture == null || atmosphereTexture == null || atmosphereSurfaceTexture == null || cloudsLightingTexture == null?planetLighting.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        var twilight = atmosphereTexture == null || atmosphereSurfaceTexture == null?atmosphereTwilightColour.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        var cloudsTwilight = cloudsLightingTexture == null?atmosphereTwilightColour.CalculateColours(cloudsTwilightOffset, cloudsTwilightOffset + 1.0f, (int)planetLutSize) : null;

        var density = atmosphereTexture == null?atmosphereDensityColour.CalculateColours(1.0f, 0.5f, (int)planetLutSize) : null;

        var surfaceDensity = atmosphereSurfaceTexture == null?atmosphereDensityColour.CalculateColours(0.0f, 0.5f, (int)planetLutSize) : null;

        var cloudsLimb = cloudsLightingTexture == null?cloudsLimbColour.CalculateColours(0.0f, 1.0f, (int)planetLutSize) : null;

        // Rebuild textures
        if (surfaceLightingTexture == null)
        {
            updateShader          |= ShaderFlags.SurfaceTextureLighting;
            surfaceLightingTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize);

            for (var x = 0; x < (int)planetLutSize; x++)
            {
                surfaceLightingTexture.SetPixel(x, 0, lighting[x]);
            }

            surfaceLightingTexture.anisoLevel = 8;
            surfaceLightingTexture.filterMode = FilterMode.Trilinear;
            surfaceLightingTexture.Apply();
        }

        if (atmosphereTexture == null)
        {
            updateShader     |= ShaderFlags.AtmosphereTexture;
            atmosphereTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var densityColour = density[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var lightingColour = lighting[x];
                    //var twilightColour   = twilight[x];
                    lightingColour.a = Mathf.Lerp(atmosphereNightOpacity, 1.0f, lightingColour.grayscale);
                    //var colour           = SGT_Helper.AlphaBlend(densityColour, twilightColour) * lightingColour;

                    var twilightColour = SGT_Helper.AlphaBlend(Color.white, twilight[x]);
                    var colour         = SGT_Helper.BlendRGB(densityColour * lightingColour, twilightColour, SGT_Helper.ChannelBlendMode.Multiply);

                    atmosphereTexture.SetPixel(x, y, SGT_Helper.PreventZeroRGB(colour));
                    atmosphereTexture.filterMode = FilterMode.Trilinear;
                }
            }

            atmosphereTexture.Apply();
        }

        if (atmosphereSurfaceTexture == null)
        {
            updateShader            |= ShaderFlags.SurfaceTextureAtmosphere;
            atmosphereSurfaceTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var densityColour = surfaceDensity[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var lightingColour = lighting[x];
                    var twilightColour = twilight[x];
                    lightingColour.a = Mathf.Lerp(atmosphereNightOpacity, 1.0f, lightingColour.grayscale);
                    var colour = SGT_Helper.AlphaBlend(densityColour, twilightColour) * lightingColour;

                    atmosphereSurfaceTexture.SetPixel(x, y, SGT_Helper.PreventZeroRGB(colour));
                    atmosphereSurfaceTexture.filterMode = FilterMode.Trilinear;
                }
            }

            atmosphereSurfaceTexture.Apply();
        }

        if (cloudsLightingTexture == null)
        {
            updateShader         |= ShaderFlags.CloudsTextureLighting;
            cloudsLightingTexture = SGT_ColourGradient.AllocateTexture((int)planetLutSize, (int)planetLutSize);

            for (var y = 0; y < (int)planetLutSize; y++)
            {
                var limb = cloudsLimb[y];

                for (var x = 0; x < (int)planetLutSize; x++)
                {
                    var colour = lighting[x];

                    if (atmosphere == true)
                    {
                        colour *= SGT_Helper.AlphaBlend(limb, cloudsTwilight[x]);
                    }
                    else
                    {
                        colour *= limb;
                    }

                    cloudsLightingTexture.SetPixel(x, y, colour);
                    cloudsLightingTexture.filterMode = FilterMode.Trilinear;
                }
            }

            cloudsLightingTexture.Apply();
        }
    }