コード例 #1
0
 private void Start()
 {
     //initlialize KopE loggers
     KopernicusExpansionLogger.InitializeKopernicusExpansionLoggers();
 }
コード例 #2
0
ファイル: Utils.cs プロジェクト: StollD/KopernicusExpansion
        public static Texture2D[] BuildMaps(CelestialBody body, Texture2D specularMap, int resolution, float normalStrength, bool writeFiles, bool generateSpecFromOcean, bool colorFromOcean, Color oceanColor, bool updateScaled)
        {
            if (body.pqsController == null)
            {
                Utils.LogError("Body has no PQS");
                return(null);
            }
            var scaled = GetScaled(body.name);

            if (scaled == null && updateScaled)
            {
                Utils.LogError("Body has no ScaledVersion");
                return(null);
            }

            var pqs = body.pqsController;

            if (resolution % 2 != 0)
            {
                Utils.LogError("Resolution not divisible by 2!");
                return(null);
            }
            var yResolution = (resolution / 2);

            Utils.Log("Starting ScaledVerion map build for body " + body.bodyName + " at " + resolution + "x" + yResolution + " pixels");
            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();

            bool suppliedSpecMap = specularMap != null;

            var colorMap  = new Texture2D(resolution, yResolution, TextureFormat.ARGB32, true);
            var heightMap = new Texture2D(resolution, yResolution, TextureFormat.RGB24, false);
            var normalMap = new Texture2D(resolution, yResolution, TextureFormat.ARGB32, true);

            if (body.ocean && generateSpecFromOcean && specularMap == null)
            {
                specularMap = new Texture2D(resolution, yResolution, TextureFormat.RGB24, false);
            }

            pqs.SetupExternalRender();

            var modVertexHeightMethod = typeof(PQS).GetMethod("Mod_OnVertexBuildHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            var modVertexMethod       = typeof(PQS).GetMethod("Mod_OnVertexBuild", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            double[,] heightScalarField = new double[resolution, yResolution];
            for (int x = 0; x < resolution; x++)
            {
                for (int y = 0; y < yResolution; y++)
                {
                    //get height and color data from PQS
                    var data = new PQS.VertexBuildData();
                    data.directionFromCenter = (QuaternionD.AngleAxis((360d / resolution) * x, Vector3d.up) * QuaternionD.AngleAxis(90d - (180d / yResolution) * y, Vector3d.right)) * Vector3d.forward;
                    data.vertHeight          = pqs.radius;

                    modVertexHeightMethod.Invoke(pqs, new object[] { data });
                    modVertexMethod.Invoke(pqs, new object[] { data });

                    //write height to the scalar field
                    var height = data.vertHeight - pqs.radius;
                    if (body.ocean && generateSpecFromOcean && height < 0)                     //make ocean flat if there is an ocean, and it is requested
                    {
                        height = 0;
                    }
                    heightScalarField [x, y] = height;

                    //calculate color
                    var color = data.vertColor;
                    if (colorFromOcean && body.ocean && height <= 0f)
                    {
                        color = oceanColor;
                    }

                    //calculate specularity
                    float spec = 1f;
                    if (!generateSpecFromOcean && specularMap != null)
                    {
                        spec = specularMap.GetPixelBilinear((float)x / (float)resolution, (float)y / (float)yResolution).grayscale;
                    }
                    //generate ocean specularity if requested
                    if (body.ocean && generateSpecFromOcean && specularMap != null)
                    {
                        if (height <= 0)
                        {
                            specularMap.SetPixel(x, y, Color.white);
                            spec = 1f;
                        }
                        else
                        {
                            specularMap.SetPixel(x, y, Color.black);
                            spec = 0f;
                        }
                    }

                    //write color/spec into colormap
                    colorMap.SetPixel(x, y, new Color(color.r, color.g, color.b, spec));
                }
            }

            pqs.CloseExternalRender();

            //finish color/spec maps
            colorMap.Apply();
            if (body.ocean && generateSpecFromOcean && specularMap != null)
            {
                specularMap.Apply();
            }

            //calculate highest and lowest heights
            double lowestHeight  = double.MaxValue;
            double highestHeight = double.MinValue;

            for (int x = 0; x < resolution; x++)
            {
                for (int y = 0; y < yResolution; y++)
                {
                    var height = heightScalarField [x, y];
                    if (height < lowestHeight)
                    {
                        lowestHeight = height;
                    }
                    if (height > highestHeight)
                    {
                        highestHeight = height;
                    }
                }
            }

            //fill with grey if the surface is completely flat
            if (highestHeight == lowestHeight)
            {
                for (int x = 0; x < resolution; x++)
                {
                    for (int y = 0; y < yResolution; y++)
                    {
                        heightMap.SetPixel(x, y, new Color(0.5f, 0.5f, 0.5f));
                    }
                }
            }
            //otherwise write an actual height map
            else
            {
                for (int x = 0; x < resolution; x++)
                {
                    for (int y = 0; y < yResolution; y++)
                    {
                        var height = heightScalarField [x, y];
                        //center to 0 -> 1
                        height -= lowestHeight;
                        height /= highestHeight - lowestHeight;

                        heightMap.SetPixel(x, y, new Color((float)height, (float)height, (float)height));
                    }
                }
            }
            heightMap.Apply();

            //generate normal map from height map
            BumpToNormal(heightMap, normalMap, normalStrength);

            //write files if requested
            if (writeFiles)
            {
                string path = KSPUtil.ApplicationRootPath + "GameData/KopernicusExpansion/Exports/";
                Directory.CreateDirectory(path);
                File.WriteAllBytes(path + body.bodyName + "_Color.png", colorMap.EncodeToPNG());
                File.WriteAllBytes(path + body.bodyName + "_Height.png", heightMap.EncodeToPNG());
                File.WriteAllBytes(path + body.bodyName + "_Normal.png", normalMap.EncodeToPNG());
                if (body.ocean && generateSpecFromOcean && specularMap != null)
                {
                    File.WriteAllBytes(path + body.bodyName + "_Spec.png", specularMap.EncodeToPNG());
                }
            }

            //update scaled if requested
            if (updateScaled)
            {
                scaled.renderer.sharedMaterial.SetTexture("_MainTex", colorMap);
                scaled.renderer.sharedMaterial.SetTexture("_BumpMap", normalMap);
            }

            stopwatch.Stop();
            Utils.Log("Finished build in " + stopwatch.ElapsedMilliseconds + "ms");
            KopernicusExpansionLogger logger = new KopernicusExpansionLogger(body.bodyName + ".ScaledExport");

            logger.Log("Map Build Time: " + stopwatch.ElapsedMilliseconds + " milliseconds");
            logger.Log("Exported Image Size: " + resolution + "x" + yResolution + " pixels");
            logger.Log("Highest Terrain Altitude: " + highestHeight + " meters");
            logger.Log("Lowest Terrain Altitude: " + lowestHeight + " meters");
            logger.Log("Supplied Specularity Map: " + suppliedSpecMap);
            if (suppliedSpecMap)
            {
                logger.Log("Specularity Map Image Size: " + specularMap.width + "x" + specularMap.height + " pixels");
            }
            logger.Log("Has Ocean: " + body.ocean);
            if (body.ocean)
            {
                logger.Log("Rendering Oceans: " + colorFromOcean);
                logger.Log("Generating Spec from Oceans: " + generateSpecFromOcean);
            }
            logger.Flush();
            logger.Close();

            return(new Texture2D[] { colorMap, heightMap, specularMap, normalMap });
        }
コード例 #3
0
        private void TextureViewerWindow(int id)
        {
            GUILayout.BeginHorizontal ();

            GUILayout.BeginVertical (GUILayout.Width(500f));
            textureViewScroll = GUILayout.BeginScrollView (textureViewScroll);
            if(selectedTexture != null)
            {
                GUILayout.Label ("<b>" + selectedTexture.name + "</b>");
                GUILayout.Label (selectedTexture, GUILayout.Width (textureDefaultWidth * textureZoom), GUILayout.Height (textureDefaultWidth * textureZoom));
            }
            else
            {
                GUILayout.Space (100f);
                GUILayout.Label ("<b><color=#aaaaaa>select a texture</color></b>", GUILayout.Height(textureDefaultWidth));
            }
            GUILayout.EndScrollView ();
            textureZoom = DrawManipulationSlider ("textureZoom", "Zoom", textureZoom, 0f, 10f, false);
            GUILayout.EndVertical ();

            GUILayout.BeginVertical (GUILayout.Width(400f));
            //draw pages of textures
            textureListScroll = GUILayout.BeginScrollView (textureListScroll, false, true);
            foreach (var texture in thisTexturePage)
            {
                if (texture == null)
                    continue;
                var textureName = string.IsNullOrEmpty (texture.name) ? "<color=orange><unnamed></color>" : texture.name;
                if (texture == selectedTexture)
                {
                    GUILayout.Toggle (true, textureName, smallButton);
                }
                else
                {
                    if (GUILayout.Button (textureName, smallButton))
                    {
                        selectedTexture = texture;
                    }
                }
            }
            GUILayout.EndScrollView ();

            GUILayout.BeginHorizontal ();
            bool hasNextPage = thisPageNumber < Mathf.FloorToInt((float)numberOfTextures / (float)pageLength);
            bool hasPreviousPage = thisPageNumber > 0;
            if (hasPreviousPage)
            {
                if (GUILayout.Button ("<<", GUILayout.Width (80f)))
                {
                    GetPageOfTextures (thisPageNumber - 1);
                }
            }
            else
            {
                GUILayout.Toggle (true, "<<", skin.button, GUILayout.Width (80f));
            }
            GUILayout.Label ("<color=white>Page " + (thisPageNumber + 1) + " of " + (Mathf.FloorToInt((float)numberOfTextures / (float)pageLength) + 1) + "</color>", GUILayout.ExpandWidth (true));
            if (hasNextPage)
            {
                if (GUILayout.Button (">>", GUILayout.Width (80f)))
                {
                    GetPageOfTextures (thisPageNumber + 1);
                }
            }
            else
            {
                GUILayout.Toggle (true, ">>", skin.button, GUILayout.Width (80f));
            }
            GUILayout.EndHorizontal ();

            GUILayout.BeginHorizontal ();
            GUI.color = textBoxTextureNameColor;
            textBoxTextureName = GUILayout.TextField (textBoxTextureName, GUILayout.ExpandWidth(true));
            GUI.color = Color.white;
            if (GUILayout.Button ("Select", GUILayout.Width (120f)))
            {
                var allTextures = UnityEngine.Resources.FindObjectsOfTypeAll<Texture2D> ();
                if (allTextures.Any (t => t.name == textBoxTextureName))
                {
                    var texture = allTextures.First (t => t.name == textBoxTextureName);
                    if (texture == null)
                    {
                        textBoxTextureNameColor = Color.red;
                    }
                    else
                    {
                        selectedTexture = texture;
                        textBoxTextureNameColor = Color.white;
                    }
                }
                else
                {
                    textBoxTextureNameColor = Color.red;
                }
            }
            GUILayout.EndHorizontal ();
            GUILayout.BeginHorizontal ();
            GUI.color = pageJumpStringColor;
            pageJumpString = GUILayout.TextField (pageJumpString, GUILayout.ExpandWidth(true));
            GUI.color = Color.white;
            if (GUILayout.Button ("Jump to page", GUILayout.Width (120f)))
            {
                int pageToJumpTo = 0;
                if (int.TryParse (pageJumpString, out pageToJumpTo))
                {
                    pageToJumpTo -= 1;
                    bool isValidPage = pageToJumpTo >= 0 && pageToJumpTo <= Mathf.FloorToInt ((float)numberOfTextures / (float)pageLength);
                    if (isValidPage)
                    {
                        GetPageOfTextures (pageToJumpTo);
                        pageJumpStringColor = Color.white;
                    }
                    else
                    {
                        pageJumpStringColor = Color.red;
                    }
                }
                else
                {
                    pageJumpStringColor = Color.red;
                }
            }
            GUILayout.EndHorizontal ();

            if (GUILayout.Button ("Print list of texture names", smallButton))
            {
                KopernicusExpansionLogger logger = new KopernicusExpansionLogger ("TextureList");
                var allTextures = UnityEngine.Resources.FindObjectsOfTypeAll<Texture2D> ();
                logger.Log ("Number of textures: " + numberOfTextures);
                logger.Log ("");
                foreach (var texture in allTextures)
                {
                    logger.Log ("Texture: " + texture.name);
                }
                logger.Flush ();
                logger.Close ();
            }
            GUILayout.Label ("<color=white>Outputs a list of all texture names to <color=orange>Logs/KopernicusExpansion/TextureList.log</color></color>");

            GUILayout.EndVertical ();

            GUILayout.EndHorizontal ();

            GUI.DragWindow ();
        }