// continue generating maps for this planet public float GenerateMaps() { var progress = m_planetGenerator.Process(); if (m_planetGenerator.m_abort) { m_mapsGenerated = true; } else if (m_planetGenerator.m_mapsGenerated) { m_mapsGenerated = true; m_material.SetTexture("_MainTex", m_planetGenerator.m_albedoTexture); m_material.SetTexture("SF_SpecularMap", m_planetGenerator.m_specularTexture); m_material.SetTexture("SF_NormalMap", m_planetGenerator.m_normalTexture); m_material.SetTexture("SF_WaterMaskMap", m_planetGenerator.m_waterMaskTexture); // is this a gas giant? if (m_planet.IsGasGiant()) { // yes - turn off the detail normal map m_material.DisableKeyword("SF_DETAILNORMALMAP_ON"); } else { // no - turn on the detail normal map m_material.EnableKeyword("SF_DETAILNORMALMAP_ON"); } // does this planet have an atmosphere? if (m_planet.HasAtmosphere()) { // yes - allow full detail normal map strength m_material.SetFloat("SF_DetailNormalMapStrength", 1.0f); } else { // no - make detail normal map strength weak so craters are more apparent m_material.SetFloat("SF_DetailNormalMapStrength", 0.05f); } m_meshRenderer.material = m_material; SpaceflightController.m_instance.m_inOrbit.MaterialUpdated(); } return(progress); }
public void AsyncProcess(byte[] bytes) { var progressStepSize = 0.5f / 12.0f; m_progress += progressStepSize; // decompress the planet data using (var memoryStream = new MemoryStream(bytes)) { using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress, false)) { var binaryReader = new BinaryReader(gZipStream); var version = binaryReader.ReadInt32(); if (version != c_versionNumber) { m_abort = true; } else { m_minimumElevation = binaryReader.ReadSingle(); m_waterElevation = binaryReader.ReadSingle(); m_snowElevation = binaryReader.ReadSingle(); var r = binaryReader.ReadSingle(); var g = binaryReader.ReadSingle(); var b = binaryReader.ReadSingle(); m_waterColor = new Color(r, g, b); r = binaryReader.ReadSingle(); g = binaryReader.ReadSingle(); b = binaryReader.ReadSingle(); m_groundColor = new Color(r, g, b); r = binaryReader.ReadSingle(); g = binaryReader.ReadSingle(); b = binaryReader.ReadSingle(); m_snowColor = new Color(r, g, b); var preparedMapWidth = binaryReader.ReadInt32(); var preparedMapHeight = binaryReader.ReadInt32(); m_preparedHeightMap = new float[preparedMapHeight, preparedMapWidth]; for (var y = 0; y < preparedMapHeight; y++) { for (var x = 0; x < preparedMapWidth; x++) { m_preparedHeightMap[y, x] = binaryReader.ReadSingle(); } } m_preparedColorMap = new Color[preparedMapHeight, preparedMapWidth]; for (var y = 0; y < preparedMapHeight; y++) { for (var x = 0; x < preparedMapWidth; x++) { r = binaryReader.ReadSingle(); g = binaryReader.ReadSingle(); b = binaryReader.ReadSingle(); m_preparedColorMap[y, x] = new Color(r, g, b); } } if (m_planet.IsGasGiant()) { m_textureMapWidth = c_gasGiantTextureMapWidth; m_textureMapHeight = c_gasGiantTextureMapHeight; } else { m_textureMapWidth = c_nonGasGiantTextureMapWidth; m_textureMapHeight = c_nonGasGiantTextureMapHeight; m_minimumDifference = binaryReader.ReadSingle(); m_maximumDifference = binaryReader.ReadSingle(); var differenceBufferSize = m_textureMapWidth * m_textureMapHeight; m_differenceBuffer = new byte[differenceBufferSize]; gZipStream.Read(m_differenceBuffer, 0, differenceBufferSize); } } } } // gas giant or not? if (m_planet.IsGasGiant()) { // yes - do color bicubic scale m_progress += progressStepSize; var bicubicScaleColor = new PG_BicubicScaleColor(); m_albedoMap = bicubicScaleColor.Process(m_preparedColorMap, m_textureMapWidth, m_textureMapHeight); // do color gaussian blur (this becomes our albedo map) m_progress += progressStepSize; var gaussianBlurColor = new PG_GaussianBlurColor(); m_albedoMap = gaussianBlurColor.Process(m_albedoMap, c_xBlurRadiusGasGiant, c_yBlurRadiusGasGiant); // do elevation bicubic scale m_progress += progressStepSize; var bicubicScaleElevation = new PG_BicubicScaleElevation(); m_elevation = bicubicScaleElevation.Process(m_preparedHeightMap, m_textureMapWidth, m_textureMapHeight); // do elevation gaussian blur m_progress += progressStepSize; var gaussianBlurElevation = new PG_GaussianBlurElevation(); m_elevation = gaussianBlurElevation.Process(m_elevation, c_xBlurRadiusGasGiant, c_yBlurRadiusGasGiant); // build specular map m_progress += progressStepSize; m_specularMap = new Color[m_textureMapHeight, m_textureMapWidth]; for (var y = 0; y < m_textureMapHeight; y++) { for (var x = 0; x < m_textureMapWidth; x++) { var elevation = m_elevation[y, x]; m_specularMap[y, x] = new Color(elevation, elevation, elevation, 0.25f); } } // build water mask map m_progress += progressStepSize; m_waterMaskMap = new Color[4, 4]; for (var y = 0; y < 4; y++) { for (var x = 0; x < 4; x++) { m_waterMaskMap[y, x] = Color.black; } } // build normal map m_progress += progressStepSize; m_normalMap = new Color[4, 4]; var defaultNormal = new Color(0.5f, 0.5f, 1.0f); for (var y = 0; y < 4; y++) { for (var x = 0; x < 4; x++) { m_normalMap[y, x] = defaultNormal; } } } else { // do elevation bicubic scale m_progress += progressStepSize; var bicubicScaleElevation = new PG_BicubicScaleElevation(); m_elevation = bicubicScaleElevation.Process(m_preparedHeightMap, m_textureMapWidth, m_textureMapHeight); // do craters m_progress += progressStepSize; if (m_planet.m_atmosphereDensityId == 0) { var craters = new PG_Craters(); m_elevation = craters.Process(m_elevation, m_planet.m_id, 0.1f, m_waterElevation); } // factor in elevation difference map m_progress += progressStepSize; m_maximumElevation = 0.0f; var elevationScale = (m_maximumDifference - m_minimumDifference) / 255.0f; for (var y = 0; y < m_textureMapHeight; y++) { for (var x = 0; x < m_textureMapWidth; x++) { var difference = m_differenceBuffer[y * m_textureMapWidth + x]; m_elevation[y, x] += (difference * elevationScale) + m_minimumDifference; m_maximumElevation = Mathf.Max(m_maximumElevation, m_elevation[y, x]); } } // build albedo map m_progress += progressStepSize; var albedoMap = new PG_AlbedoMap(); m_albedoMap = albedoMap.Process(m_elevation, m_preparedColorMap, m_waterElevation, m_waterColor, m_groundColor); // build specular map m_progress += progressStepSize; var waterSpecularColor = m_planet.IsMolten() ? new Color(0.75f, 0.125f, 0.125f) : new Color(1.0f, 1.0f, 1.0f); var waterSpecularPower = m_planet.IsMolten() ? 0.4f : 0.75f; var specularMap = new PG_SpecularMap(); m_specularMap = specularMap.Process(m_elevation, m_albedoMap, m_waterElevation, waterSpecularColor, waterSpecularPower, 4); // build water mask map m_progress += progressStepSize; var waterMaskMap = new PG_WaterMaskMap(); m_waterMaskMap = waterMaskMap.Process(m_elevation, m_waterElevation, 4); // build normal map m_progress += progressStepSize; var normalMap = new PG_NormalMap(); m_normalMap = normalMap.Process(m_elevation, c_normalScale, m_waterElevation, 2); } // get albedo pixels m_progress += progressStepSize; m_albedoPixels = new Color[m_textureMapWidth * m_textureMapHeight]; var index = 0; for (var y = 0; y < m_textureMapHeight; y++) { for (var x = 0; x < m_textureMapWidth; x++) { m_albedoPixels[index++] = m_albedoMap[y, x]; } } // get specular pixels m_progress += progressStepSize; var textureMapWidth = m_specularMap.GetLength(1); var textureMapHeight = m_specularMap.GetLength(0); m_specularPixels = new Color[textureMapWidth * textureMapHeight]; index = 0; for (var y = 0; y < textureMapHeight; y++) { for (var x = 0; x < textureMapWidth; x++) { m_specularPixels[index++] = m_specularMap[y, x]; } } // get water mask pixels m_progress += progressStepSize; textureMapWidth = m_waterMaskMap.GetLength(1); textureMapHeight = m_waterMaskMap.GetLength(0); m_waterMaskPixels = new Color[textureMapWidth * textureMapHeight]; index = 0; for (var y = 0; y < textureMapHeight; y++) { for (var x = 0; x < textureMapWidth; x++) { m_waterMaskPixels[index++] = m_waterMaskMap[y, x]; } } // get normal pixels m_progress += progressStepSize; textureMapWidth = m_normalMap.GetLength(1); textureMapHeight = m_normalMap.GetLength(0); m_normalPixels = new Color[textureMapWidth * textureMapHeight]; index = 0; for (var y = 0; y < textureMapHeight; y++) { for (var x = 0; x < textureMapWidth; x++) { m_normalPixels[index++] = new Color(0.0f, m_normalMap[y, x].g, 0.0f, m_normalMap[y, x].r); } } m_step = 20; }