public static SGT_ColourGradient Field(string handle, string tooltip, SGT_ColourGradient field, bool isField = true)
    {
        if (CanDraw == true && field != null)
        {
            var borderRect   = ReserveField(handle, tooltip);
            var gradientRect = SGT_RectHelper.RemovePx(borderRect, 3.0f);
            var borderStyle  = EditorStyles.objectFieldThumb;
            var overlayStyle = (GUIStyle)(EditorStyles.objectFieldThumb.name + "Overlay2");

            gradientRect.xMax -= 4.0f;

            var colours = field.CalculateColours(0.0f, 1.0f, 256);
            var texture = SGT_ColourGradient.AllocateTexture(256);

            for (var x = 0; x < 256; x++)
            {
                texture.SetPixel(x, 0, colours[x]);
            }

            texture.Apply();

            GUI.Box(borderRect, string.Empty, borderStyle);
            DrawTiledTexture(gradientRect, SGT_Helper.CheckerTexture);
            GUI.DrawTexture(gradientRect, texture);

            SGT_Helper.DestroyObject(texture);

            if (GUI.Button(borderRect, "Edit", overlayStyle) == true)
            {
                SGT_AuxWindow_ColourGradient.Create(field, isField);
            }
        }

        return(field);
    }
    public static SGT_ColourGradient Field(string handle, string tooltip, SGT_ColourGradient field, bool isField = true)
    {
        if (CanDraw == true && field != null)
        {
            var borderRect   = ReserveField(handle, tooltip);
            var gradientRect = SGT_RectHelper.RemovePx(borderRect, 3.0f);
            var borderStyle  = EditorStyles.objectFieldThumb;
            var overlayStyle = (GUIStyle)(EditorStyles.objectFieldThumb.name + "Overlay2");

            gradientRect.xMax -= 4.0f;

            var colours = field.CalculateColours(0.0f, 1.0f, 256);
            var texture = SGT_ColourGradient.AllocateTexture(256);

            for (var x = 0; x < 256; x++)
            {
                texture.SetPixel(x, 0, colours[x]);
            }

            texture.Apply();

            GUI.Box(borderRect, string.Empty, borderStyle);
            DrawTiledTexture(gradientRect, SGT_Helper.CheckerTexture);
            GUI.DrawTexture(gradientRect, texture);

            SGT_Helper.DestroyObject(texture);

            if (GUI.Button(borderRect, "Edit", overlayStyle) == true)
            {
                SGT_AuxWindow_ColourGradient.Create(field, isField);
            }
        }

        return field;
    }
예제 #3
0
        public void SetAlpha(SGT_ColourGradient parent, float newAlpha)
        {
            if (newAlpha != alpha)
            {
                alpha = newAlpha;

                parent.Modified = true;
            }
        }
	public void LateUpdate()
	{
		if (oblatenessGameObject == null) oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
		if (surfaceGameObject    == null) surfaceGameObject    = SGT_Helper.CreateGameObject("Surface", oblatenessGameObject);
		if (atmosphereGameObject == null) atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
		if (atmosphereMesh       == null) atmosphereMesh       = new SGT_Mesh();
		if (starObserver         == null) starObserver         = SGT_Helper.FindCamera();
		if (surfaceTexture       == null) surfaceTexture       = new SGT_SurfaceTexture();
		if (surfaceMultiMesh     == null) surfaceMultiMesh     = new SGT_SurfaceMultiMesh();
		
		SGT_Helper.SetParent(oblatenessGameObject, gameObject);
		SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
		SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);
		
		SGT_Helper.SetParent(surfaceGameObject, oblatenessGameObject);
		SGT_Helper.SetLayer(surfaceGameObject, oblatenessGameObject.layer);
		SGT_Helper.SetTag(surfaceGameObject, oblatenessGameObject.tag);
		
		SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
		SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
		SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);
		
		if (atmosphereDensityColour == null)
		{
			atmosphereDensityColour = new SGT_ColourGradient(false, true);
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 1.0f, 0.0f, 1.0f), 0.0f);
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.5f, 0.0f, 1.0f), 0.5f).Locked = true;
			atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.0f, 0.0f, 1.0f), 1.0f);
		}
		
		UpdateTransform();
		UpdateGradient();
		UpdateMaterial();
		UpdateShader();
		
		surfaceMultiMesh.GameObject           = surfaceGameObject;
		surfaceMultiMesh.HasMeshRenderers     = true;
		surfaceMultiMesh.MeshRenderersEnabled = true;
		surfaceMultiMesh.MeshCollidersEnabled = true;
		surfaceMultiMesh.SetSharedMaterials(surfaceMaterials);
		surfaceMultiMesh.Update(gameObject.layer, gameObject.tag);
		
		atmosphereMesh.GameObject          = atmosphereGameObject;
		atmosphereMesh.HasMeshRenderer     = true;
		atmosphereMesh.MeshRendererEnabled = true;
		atmosphereMesh.SharedMaterial      = atmosphereMaterial;
		atmosphereMesh.Update();
		
#if UNITY_EDITOR == true
		SGT_Helper.HideGameObject(oblatenessGameObject);
		
		atmosphereMesh.HideInEditor();
		surfaceMultiMesh.HideInEditor();
#endif
	}
예제 #5
0
        public void SetPosition(SGT_ColourGradient parent, float newPosition)
        {
            newPosition = Mathf.Clamp01(newPosition);

            if (newPosition != position)
            {
                position = newPosition;

                parent.Modified = true;
            }
        }
예제 #6
0
        public void SetColour(SGT_ColourGradient parent, Color newColour)
        {
            if (parent.HasAlpha == false)
            {
                newColour.a = 1.0f;
            }

            if (parent.IsGreyscale == true)
            {
                var g = colour.grayscale;
                colour = new Color(g, g, g, colour.a);
            }

            if (newColour != colour)
            {
                colour = newColour;

                parent.Modified = true;
            }
        }
예제 #7
0
    private void UpdateGradient()
    {
        if (atmosphereLightingColour.Modified == true || atmosphereTwilightColour.Modified == true || atmosphereLimbColour.Modified == true)
        {
            lightingTexture = SGT_Helper.DestroyObject(lightingTexture);
        }

        atmosphereLightingColour.Modified = false;
        atmosphereTwilightColour.Modified = false;
        atmosphereLimbColour.Modified     = false;

        if (lightingTexture == null)
        {
            var lightingColours = atmosphereLightingColour.CalculateColours(0.0f, 1.0f, (int)lutSize);
            var twilightColours = atmosphereTwilightColour.CalculateColours(0.0f, 1.0f, (int)lutSize);
            var limbColours     = atmosphereLimbColour.CalculateColours(0.0f, 1.0f, (int)lutSize);

            lightingTexture = SGT_ColourGradient.AllocateTexture((int)lutSize, (int)lutSize);

            for (var y = 0; y < (int)lutSize; y++)
            {
                var limbPixel = limbColours[y];

                for (var x = 0; x < (int)lutSize; x++)
                {
                    var lightingPixel = lightingColours[x];
                    var twilightPixel = twilightColours[x];
                    var colour        = Color.white;

                    colour = SGT_Helper.AlphaBlend(colour, twilightPixel);
                    colour = SGT_Helper.AlphaBlend(colour, limbPixel);
                    colour = colour * lightingPixel;

                    lightingTexture.SetPixel(x, y, colour);
                }
            }

            lightingTexture.Apply();
        }
    }
예제 #8
0
    private void UpdateGradient()
    {
        if (atmosphereDensityColour.Modified == true)
        {
            atmosphereTexture = SGT_Helper.DestroyObject(atmosphereTexture);
        }

        atmosphereDensityColour.Modified = false;

        if (atmosphereTexture == null)
        {
            atmosphereDensityColour.Modified = false;

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

            var density        = atmosphereDensityColour.CalculateColours(1.0f, 0.5f, (int)lutSize);
            var surfaceDensity = atmosphereDensityColour.CalculateColours(0.0f, 0.5f, (int)lutSize);

            atmosphereTexture        = SGT_ColourGradient.AllocateTexture((int)lutSize);
            atmosphereSurfaceTexture = SGT_ColourGradient.AllocateTexture((int)lutSize);

            for (var x = 0; x < (int)lutSize; x++)
            {
                atmosphereTexture.SetPixel(x, 0, density[x]);
            }

            for (var x = 0; x < (int)lutSize; x++)
            {
                atmosphereSurfaceTexture.SetPixel(x, 0, surfaceDensity[x]);
            }

            atmosphereTexture.Apply();
            atmosphereSurfaceTexture.Apply();
        }
    }
예제 #9
0
    public void LateUpdate()
    {
        if (surfaceGameObject == null)
        {
            surfaceGameObject = SGT_Helper.CreateGameObject("Surface", gameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", gameObject);
        }
        if (cloudsGameObject == null)
        {
            cloudsGameObject = SGT_Helper.CreateGameObject("Clouds", gameObject);
        }
        if (surfaceMesh == null)
        {
            surfaceMesh = new SGT_SurfaceMultiMesh();
        }
        if (surfaceTextureDay == null)
        {
            surfaceTextureDay = new SGT_SurfaceTexture();
        }
        if (surfaceTextureNight == null)
        {
            surfaceTextureNight = new SGT_SurfaceTexture();
        }
        if (surfaceTextureNormal == null)
        {
            surfaceTextureNormal = new SGT_SurfaceTexture();
        }
        if (surfaceTextureSpecular == null)
        {
            surfaceTextureSpecular = new SGT_SurfaceTexture();
        }
        if (planetObserver == null)
        {
            planetObserver = SGT_Helper.FindCamera();
        }
        if (planetLightSource == null)
        {
            planetLightSource = SGT_LightSource.Find();
        }

        SGT_Helper.SetParent(surfaceGameObject, gameObject);
        SGT_Helper.SetLayer(surfaceGameObject, gameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, gameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, gameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, gameObject.tag);

        SGT_Helper.SetParent(cloudsGameObject, gameObject);
        SGT_Helper.SetLayer(cloudsGameObject, gameObject.layer);
        SGT_Helper.SetTag(cloudsGameObject, gameObject.tag);

        surfaceTextureDay.Configuration      = surfaceMesh.Configuration;
        surfaceTextureNight.Configuration    = surfaceMesh.Configuration;
        surfaceTextureNormal.Configuration   = surfaceMesh.Configuration;
        surfaceTextureSpecular.Configuration = surfaceMesh.Configuration;

        if (surfaceTextureDay.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureDay;      surfaceTextureDay.Modified = false;
        }
        if (surfaceTextureNight.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureNight;    surfaceTextureNight.Modified = false;
        }
        if (surfaceTextureNormal.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureNormal;   surfaceTextureNormal.Modified = false;
        }
        if (surfaceTextureSpecular.Modified == true)
        {
            updateShader |= ShaderFlags.SurfaceTextureSpecular; surfaceTextureSpecular.Modified = false;
        }

        if (atmosphere == true)
        {
            if (atmosphereMesh == null)
            {
                atmosphereMesh = new SGT_Mesh();
            }
        }
        else
        {
            if (atmosphereMesh != null)
            {
                atmosphereMesh.Clear();
            }
        }

        if (clouds == true)
        {
            if (cloudsMesh == null)
            {
                cloudsMesh = new SGT_SurfaceMultiMesh();
            }
            if (cloudsTexture == null)
            {
                cloudsTexture = new SGT_SurfaceTexture();
            }

            if (cloudsTexture.Modified == true)
            {
                updateShader |= ShaderFlags.CloudsTexture; cloudsTexture.Modified = false;
            }
        }
        else
        {
            if (cloudsTexture != null)
            {
                cloudsTexture = null;
            }
            if (cloudsMesh != null)
            {
                cloudsMesh.Clear();
            }
        }

        if (planetLighting == null)
        {
            planetLighting = new SGT_ColourGradient(false, false);
            planetLighting.AddColourNode(Color.black, 0.45f);
            planetLighting.AddColourNode(Color.white, 0.55f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.45f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.65f);
            atmosphereTwilightColour.AddColourNode(new Color(1.0f, 0.19f, 0.0f, 1.0f), 0.5f);
        }

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.53f, 0.85f), 0.2f);
            atmosphereDensityColour.AddColourNode(Color.white, 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.51f, 1.0f), 1.0f);
        }

        if (cloudsLimbColour == null)
        {
            cloudsLimbColour = new SGT_ColourGradient(false, true);
            cloudsLimbColour.AddColourNode(Color.white, 0.5f);
        }

        // Rotate?
        if (Application.isPlaying == true)
        {
            if (cloudsRotationPeriod != 0.0f)
            {
                cloudsGameObject.transform.Rotate(0.0f, SGT_Helper.DegreesPerSecond(cloudsRotationPeriod) * Time.smoothDeltaTime, 0.0f);
            }
        }

        // Update scales
        SGT_Helper.SetLocalScale(surfaceGameObject.transform, surfaceRadius);

        if (atmosphere == true)
        {
            SGT_Helper.SetLocalScale(atmosphereGameObject.transform, AtmosphereRadius);

            // Point atmosphere at camera
            if (planetObserver != null)
            {
                var observerPosition  = planetObserver.transform.position;
                var observerDirection = (observerPosition - gameObject.transform.position).normalized;

                SGT_Helper.SetUp(atmosphereGameObject.transform, observerDirection);
            }
        }

        if (clouds == true)
        {
            SGT_Helper.SetLocalScale(cloudsGameObject.transform, CloudsRadius);

            UpdateCloudsOffset();
        }

        UpdateGradient();
        UpdateTechnique();
        UpdateShader();

        surfaceMesh.GameObject           = surfaceGameObject;
        surfaceMesh.HasMeshRenderers     = true;
        surfaceMesh.MeshRenderersEnabled = true;
        surfaceMesh.MeshCollidersEnabled = true;
        surfaceMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMesh.Update(gameObject.layer, gameObject.tag);

        if (atmosphere == true)
        {
            atmosphereMesh.GameObject          = atmosphereGameObject;
            atmosphereMesh.HasMeshRenderer     = true;
            atmosphereMesh.MeshRendererEnabled = true;
            atmosphereMesh.SharedMaterial      = atmosphereMaterial;
            atmosphereMesh.Update();
        }

        if (clouds == true)
        {
            cloudsMesh.GameObject           = cloudsGameObject;
            cloudsMesh.HasMeshRenderers     = true;
            cloudsMesh.MeshRenderersEnabled = true;
            cloudsMesh.SetSharedMaterials(cloudsMaterials);
            cloudsMesh.Update(gameObject.layer, gameObject.tag);
        }

#if UNITY_EDITOR == true
        surfaceMesh.HideInEditor();
        if (atmosphereMesh != null)
        {
            atmosphereMesh.HideInEditor();
        }
        if (cloudsMesh != null)
        {
            cloudsMesh.HideInEditor();
        }

        SGT_Helper.HideGameObject(atmosphereGameObject);
        SGT_Helper.HideGameObject(cloudsGameObject);
#endif
    }
예제 #10
0
    public void LateUpdate()
    {
        if (oblatenessGameObject == null)
        {
            oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
        }
        if (surfaceGameObject == null)
        {
            surfaceGameObject = SGT_Helper.CreateGameObject("Surface", oblatenessGameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
        }
        if (atmosphereMesh == null)
        {
            atmosphereMesh = new SGT_Mesh();
        }
        if (starObserver == null)
        {
            starObserver = SGT_Helper.FindCamera();
        }
        if (surfaceTexture == null)
        {
            surfaceTexture = new SGT_SurfaceTexture();
        }
        if (surfaceMultiMesh == null)
        {
            surfaceMultiMesh = new SGT_SurfaceMultiMesh();
        }

        SGT_Helper.SetParent(oblatenessGameObject, gameObject);
        SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
        SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);

        SGT_Helper.SetParent(surfaceGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(surfaceGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, oblatenessGameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 1.0f, 0.0f, 1.0f), 0.0f);
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.5f, 0.0f, 1.0f), 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(1.0f, 0.0f, 0.0f, 1.0f), 1.0f);
        }

        UpdateTransform();
        UpdateGradient();
        UpdateMaterial();
        UpdateShader();

        surfaceMultiMesh.GameObject           = surfaceGameObject;
        surfaceMultiMesh.HasMeshRenderers     = true;
        surfaceMultiMesh.MeshRenderersEnabled = true;
        surfaceMultiMesh.MeshCollidersEnabled = true;
        surfaceMultiMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMultiMesh.Update(gameObject.layer, gameObject.tag);

        atmosphereMesh.GameObject          = atmosphereGameObject;
        atmosphereMesh.HasMeshRenderer     = true;
        atmosphereMesh.MeshRendererEnabled = true;
        atmosphereMesh.SharedMaterial      = atmosphereMaterial;
        atmosphereMesh.Update();

#if UNITY_EDITOR == true
        SGT_Helper.HideGameObject(oblatenessGameObject);

        atmosphereMesh.HideInEditor();
        surfaceMultiMesh.HideInEditor();
#endif
    }
예제 #11
0
    public void LateUpdate()
    {
        if (oblatenessGameObject == null)
        {
            oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
        }
        if (atmosphereGameObject == null)
        {
            atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
        }
        if (atmosphereMesh == null)
        {
            atmosphereMesh = new SGT_Mesh();
        }
        if (gasGiantLightSource == null)
        {
            gasGiantLightSource = SGT_LightSource.Find();
        }
        if (gasGiantObserver == null)
        {
            gasGiantObserver = SGT_Helper.FindCamera();
        }

        SGT_Helper.SetParent(oblatenessGameObject, gameObject);
        SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
        SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);

        if (atmosphereLightingColour == null)
        {
            atmosphereLightingColour = new SGT_ColourGradient(false, false);
            atmosphereLightingColour.AddColourNode(Color.black, 0.4f);
            atmosphereLightingColour.AddColourNode(Color.white, 0.7f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.4f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.5f);
            atmosphereTwilightColour.AddColourNode(Color.red, 0.5f);
        }

        if (atmosphereLimbColour == null)
        {
            atmosphereLimbColour = new SGT_ColourGradient(false, true);
            atmosphereLimbColour.AddAlphaNode(0.0f, 0.75f);
            atmosphereLimbColour.AddAlphaNode(1.0f, 1.0f);
            atmosphereLimbColour.AddColourNode(Color.blue, 0.4f);
        }

        if (shadowAutoUpdate == true)
        {
            switch (shadowType)
            {
            case SGT_ShadowOccluder.Ring:
            {
                var fill = new SGT_FillRingDimensions();

                SendMessage("FillRingDimensions", fill, SendMessageOptions.DontRequireReceiver);

                shadowRadius = fill.Radius;
                shadowWidth  = fill.Width;
            }
            break;

            case SGT_ShadowOccluder.Planet:
            {
                if (shadowGameObject != null)
                {
                    var fill = new SGT_FillFloat();

                    shadowGameObject.SendMessage("FillShadowRadius", fill, SendMessageOptions.DontRequireReceiver);

                    shadowRadius = fill.Float;
                }
            }
            break;
            }
        }

        UpdateTransform();
        UpdateGradient();
        UpdateMaterial();
        UpdateShader();

        // Update mesh
        atmosphereMesh.GameObject          = atmosphereGameObject;
        atmosphereMesh.HasMeshRenderer     = true;
        atmosphereMesh.MeshRendererEnabled = true;
        atmosphereMesh.SharedMaterial      = atmosphereMaterial;
        atmosphereMesh.Update();

#if UNITY_EDITOR == true
        SGT_Helper.HideGameObject(oblatenessGameObject);

        atmosphereMesh.HideInEditor();
#endif
    }
예제 #12
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();
        }
    }
    public void LateUpdate()
    {
        if (oblatenessGameObject == null) oblatenessGameObject = SGT_Helper.CreateGameObject("Oblateness", gameObject);
        if (atmosphereGameObject == null) atmosphereGameObject = SGT_Helper.CreateGameObject("Atmosphere", oblatenessGameObject);
        if (atmosphereMesh       == null) atmosphereMesh      = new SGT_Mesh();
        if (gasGiantLightSource  == null) gasGiantLightSource = SGT_LightSource.Find();
        if (gasGiantObserver     == null) gasGiantObserver    = SGT_Helper.FindCamera();

        SGT_Helper.SetParent(oblatenessGameObject, gameObject);
        SGT_Helper.SetLayer(oblatenessGameObject, gameObject.layer);
        SGT_Helper.SetTag(oblatenessGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, oblatenessGameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, oblatenessGameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, oblatenessGameObject.tag);

        if (atmosphereLightingColour == null)
        {
            atmosphereLightingColour = new SGT_ColourGradient(false, false);
            atmosphereLightingColour.AddColourNode(Color.black, 0.4f);
            atmosphereLightingColour.AddColourNode(Color.white, 0.7f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.4f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.5f);
            atmosphereTwilightColour.AddColourNode(Color.red, 0.5f);
        }

        if (atmosphereLimbColour == null)
        {
            atmosphereLimbColour = new SGT_ColourGradient(false, true);
            atmosphereLimbColour.AddAlphaNode(0.0f, 0.75f);
            atmosphereLimbColour.AddAlphaNode(1.0f, 1.0f);
            atmosphereLimbColour.AddColourNode(Color.blue, 0.4f);
        }

        if (shadowAutoUpdate == true)
        {
            switch (shadowType)
            {
                case SGT_ShadowOccluder.Ring:
                {
                    var fill = new SGT_FillRingDimensions();

                    SendMessage("FillRingDimensions", fill, SendMessageOptions.DontRequireReceiver);

                    shadowRadius = fill.Radius;
                    shadowWidth  = fill.Width;
                }
                break;

                case SGT_ShadowOccluder.Planet:
                {
                    if (shadowGameObject != null)
                    {
                        var fill = new SGT_FillFloat();

                        shadowGameObject.SendMessage("FillShadowRadius", fill, SendMessageOptions.DontRequireReceiver);

                        shadowRadius = fill.Float;
                    }
                }
                break;
            }
        }

        UpdateTransform();
        UpdateGradient();
        UpdateMaterial();
        UpdateShader();

        // Update mesh
        atmosphereMesh.GameObject          = atmosphereGameObject;
        atmosphereMesh.HasMeshRenderer     = true;
        atmosphereMesh.MeshRendererEnabled = true;
        atmosphereMesh.SharedMaterial      = atmosphereMaterial;
        atmosphereMesh.Update();

        #if UNITY_EDITOR == true
        SGT_Helper.HideGameObject(oblatenessGameObject);

        atmosphereMesh.HideInEditor();
        #endif
    }
    public void LateUpdate()
    {
        if (surfaceGameObject      == null) surfaceGameObject      = SGT_Helper.CreateGameObject("Surface", gameObject);
        if (atmosphereGameObject   == null) atmosphereGameObject   = SGT_Helper.CreateGameObject("Atmosphere", gameObject);
        if (cloudsGameObject       == null) cloudsGameObject       = SGT_Helper.CreateGameObject("Clouds", gameObject);
        if (surfaceMesh            == null) surfaceMesh            = new SGT_SurfaceMultiMesh();
        if (surfaceTextureDay      == null) surfaceTextureDay      = new SGT_SurfaceTexture();
        if (surfaceTextureNight    == null) surfaceTextureNight    = new SGT_SurfaceTexture();
        if (surfaceTextureNormal   == null) surfaceTextureNormal   = new SGT_SurfaceTexture();
        if (surfaceTextureSpecular == null) surfaceTextureSpecular = new SGT_SurfaceTexture();
        if (planetObserver         == null) planetObserver         = SGT_Helper.FindCamera();
        if (planetLightSource      == null) planetLightSource      = SGT_LightSource.Find();

        SGT_Helper.SetParent(surfaceGameObject, gameObject);
        SGT_Helper.SetLayer(surfaceGameObject, gameObject.layer);
        SGT_Helper.SetTag(surfaceGameObject, gameObject.tag);

        SGT_Helper.SetParent(atmosphereGameObject, gameObject);
        SGT_Helper.SetLayer(atmosphereGameObject, gameObject.layer);
        SGT_Helper.SetTag(atmosphereGameObject, gameObject.tag);

        SGT_Helper.SetParent(cloudsGameObject, gameObject);
        SGT_Helper.SetLayer(cloudsGameObject, gameObject.layer);
        SGT_Helper.SetTag(cloudsGameObject, gameObject.tag);

        surfaceTextureDay.Configuration      = surfaceMesh.Configuration;
        surfaceTextureNight.Configuration    = surfaceMesh.Configuration;
        surfaceTextureNormal.Configuration   = surfaceMesh.Configuration;
        surfaceTextureSpecular.Configuration = surfaceMesh.Configuration;

        if (surfaceTextureDay.Modified      == true) { updateShader |= ShaderFlags.SurfaceTextureDay;      surfaceTextureDay.Modified      = false; }
        if (surfaceTextureNight.Modified    == true) { updateShader |= ShaderFlags.SurfaceTextureNight;    surfaceTextureNight.Modified    = false; }
        if (surfaceTextureNormal.Modified   == true) { updateShader |= ShaderFlags.SurfaceTextureNormal;   surfaceTextureNormal.Modified   = false; }
        if (surfaceTextureSpecular.Modified == true) { updateShader |= ShaderFlags.SurfaceTextureSpecular; surfaceTextureSpecular.Modified = false; }

        if (atmosphere == true)
        {
            if (atmosphereMesh == null) atmosphereMesh = new SGT_Mesh();
        }
        else
        {
            if (atmosphereMesh != null) atmosphereMesh.Clear();
        }

        if (clouds == true)
        {
            if (cloudsMesh    == null) cloudsMesh    = new SGT_SurfaceMultiMesh();
            if (cloudsTexture == null) cloudsTexture = new SGT_SurfaceTexture();

            if (cloudsTexture.Modified == true) { updateShader |= ShaderFlags.CloudsTexture; cloudsTexture.Modified = false; }
        }
        else
        {
            if (cloudsTexture != null) cloudsTexture = null;
            if (cloudsMesh    != null) cloudsMesh.Clear();
        }

        if (planetLighting == null)
        {
            planetLighting = new SGT_ColourGradient(false, false);
            planetLighting.AddColourNode(Color.black, 0.45f);
            planetLighting.AddColourNode(Color.white, 0.55f);
        }

        if (atmosphereTwilightColour == null)
        {
            atmosphereTwilightColour = new SGT_ColourGradient(false, true);
            atmosphereTwilightColour.AddAlphaNode(1.0f, 0.45f);
            atmosphereTwilightColour.AddAlphaNode(0.0f, 0.65f);
            atmosphereTwilightColour.AddColourNode(new Color(1.0f, 0.19f, 0.0f, 1.0f), 0.5f);
        }

        if (atmosphereDensityColour == null)
        {
            atmosphereDensityColour = new SGT_ColourGradient(false, true);
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.53f, 0.85f), 0.2f);
            atmosphereDensityColour.AddColourNode(Color.white, 0.5f).Locked = true;
            atmosphereDensityColour.AddColourNode(new Color(0.17f, 0.51f, 1.0f), 1.0f);
        }

        if (cloudsLimbColour == null)
        {
            cloudsLimbColour = new SGT_ColourGradient(false, true);
            cloudsLimbColour.AddColourNode(Color.white, 0.5f);
        }

        // Rotate?
        if (Application.isPlaying == true)
        {
            if (cloudsRotationPeriod != 0.0f)
            {
                cloudsGameObject.transform.Rotate(0.0f, SGT_Helper.DegreesPerSecond(cloudsRotationPeriod) * Time.smoothDeltaTime, 0.0f);
            }
        }

        // Update scales
        SGT_Helper.SetLocalScale(surfaceGameObject.transform, surfaceRadius);

        if (atmosphere == true)
        {
            SGT_Helper.SetLocalScale(atmosphereGameObject.transform, AtmosphereRadius);

            // Point atmosphere at camera
            if (planetObserver != null)
            {
                var observerPosition  = planetObserver.transform.position;
                var observerDirection = (observerPosition - gameObject.transform.position).normalized;

                SGT_Helper.SetUp(atmosphereGameObject.transform, observerDirection);
            }
        }

        if (clouds == true)
        {
            SGT_Helper.SetLocalScale(cloudsGameObject.transform, CloudsRadius);

            UpdateCloudsOffset();
        }

        UpdateGradient();
        UpdateTechnique();
        UpdateShader();

        surfaceMesh.GameObject           = surfaceGameObject;
        surfaceMesh.HasMeshRenderers     = true;
        surfaceMesh.MeshRenderersEnabled = true;
        surfaceMesh.MeshCollidersEnabled = true;
        surfaceMesh.SetSharedMaterials(surfaceMaterials);
        surfaceMesh.Update(gameObject.layer, gameObject.tag);

        if (atmosphere == true)
        {
            atmosphereMesh.GameObject          = atmosphereGameObject;
            atmosphereMesh.HasMeshRenderer     = true;
            atmosphereMesh.MeshRendererEnabled = true;
            atmosphereMesh.SharedMaterial      = atmosphereMaterial;
            atmosphereMesh.Update();
        }

        if (clouds == true)
        {
            cloudsMesh.GameObject           = cloudsGameObject;
            cloudsMesh.HasMeshRenderers     = true;
            cloudsMesh.MeshRenderersEnabled = true;
            cloudsMesh.SetSharedMaterials(cloudsMaterials);
            cloudsMesh.Update(gameObject.layer, gameObject.tag);
        }

        #if UNITY_EDITOR == true
        surfaceMesh.HideInEditor();
        if (atmosphereMesh != null) atmosphereMesh.HideInEditor();
        if (cloudsMesh     != null) cloudsMesh.HideInEditor();

        SGT_Helper.HideGameObject(atmosphereGameObject);
        SGT_Helper.HideGameObject(cloudsGameObject);
        #endif
    }
예제 #15
0
 public void OffsetPosition(SGT_ColourGradient parent, float offset)
 {
     SetPosition(parent, position + offset);
 }