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(); } }
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(); } }