Exemplo n.º 1
0
        /// <summary>
        /// Get the texture for this LBTerrainTexture instance. Takes into consideration
        /// if isTinted and/or isRotate enabled.
        /// </summary>
        /// <returns></returns>
        public Texture2D GetTexture2D()
        {
            Texture2D tempTexture = null;

            // If the Texture is tinted, use the tinted texture rather than the main texture
            if (isTinted)
            {
                // If it is Tinted AND rotated, rotate the tinted texture
                if (isRotated)
                {
                    tempTexture = LBTextureOperations.RotateTexture(tintedTexture, rotationAngle);
                }
                else
                {
                    tempTexture = tintedTexture;
                }
            }
            else if (isRotated)
            {
                tempTexture = rotatedTexture;
            }
            else
            {
                tempTexture = texture;
            }

            return(tempTexture);
        }
        private void UserDefinedLookup(LBTerrainGrass lbTerrainGrass, string sourceName)
        {
            if (lbGrassSetup == null)
            {
                Debug.LogWarning("LandscapeBuilderGrassEditor.UserDefinedLookup - Grass Setup is not defined");
            }
            else if (lbTerrainGrass == null)
            {
                Debug.LogWarning("LandscapeBuilderGrassEditor.UserDefinedLookup - Terrain Grass cannot be null");
            }
            else
            {
                string filePath = LBTextureOperations.GetTextureFilePath(lbTerrainGrass.texture);
                string textureNameWithExtension = LBTextureOperations.GetTextureFileNameFromPath(filePath);

                // Find a match in the current config list
                LBGrassConfig lbGrassConfig = lbGrassSetup.lbGrassConfigList.Find(g => g.grassTextureName == textureNameWithExtension && g.sourceName == sourceName);

                // If we didn't find a match, create a new one
                if (lbGrassConfig == null)
                {
                    AddGrassConfig(lbTerrainGrass, textureNameWithExtension);
                }
            }
        }
 /// <summary>
 /// Add a new LBGrassConfig using an existing LBTerrainGrass instance
 /// </summary>
 /// <param name="lbTerrainGrass"></param>
 private void AddGrassConfig(LBTerrainGrass lbTerrainGrass, string textureName)
 {
     if (lbGrassSetup == null)
     {
         Debug.LogWarning("LandscapeBuilderGrassEditor.AddGrassConfig - Grass Setup is not defined");
     }
     else if (lbTerrainGrass == null)
     {
         Debug.LogWarning("LandscapeBuilderGrassEditor.AddGrassConfig - Terrain Grass cannot be null");
     }
     else if (lbGrassSetup.lbGrassConfigList == null)
     {
         Debug.LogWarning("LandscapeBuilderGrassEditor.AddGrassConfig - GrassConfigList is not defined");
     }
     else
     {
         LBGrassConfig lbGrassConfigNew = new LBGrassConfig();
         if (lbGrassConfigNew != null)
         {
             lbGrassConfigNew.sourceName           = LBGrassSetup.UserDefinedSourceFilter;
             lbGrassConfigNew.texture2D            = lbTerrainGrass.texture;
             lbGrassConfigNew.grassTexturePath     = AssetDatabase.GetAssetPath(lbTerrainGrass.texture);
             lbGrassConfigNew.grassTextureName     = LBTextureOperations.GetTextureFileNameFromPath(lbGrassConfigNew.grassTexturePath);
             lbGrassConfigNew.detailRenderMode     = lbTerrainGrass.detailRenderMode;
             lbGrassConfigNew.dryColour            = lbTerrainGrass.dryColour;
             lbGrassConfigNew.grassPatchFadingMode = (LBGrassConfig.GrassPatchFadingMode)lbTerrainGrass.grassPatchFadingMode;
             lbGrassConfigNew.healthyColour        = lbTerrainGrass.healthyColour;
             lbGrassConfigNew.minWidth             = lbTerrainGrass.minWidth;
             lbGrassConfigNew.maxWidth             = lbTerrainGrass.maxWidth;
             lbGrassConfigNew.minHeight            = lbTerrainGrass.minHeight;
             lbGrassConfigNew.maxHeight            = lbTerrainGrass.maxHeight;
             lbGrassSetup.lbGrassConfigList.Add(lbGrassConfigNew);
             lbGrassSetup.Save(false);
             if (lbWindow != null)
             {
                 lbWindow.isGrassConfigListDirty = true;
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Load a PNG file from disk into rawHeightData (which is stored in LB as 16-bit little endian [windows] RAW)
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="showErrors"></param>
        /// <returns></returns>
        public static LBRaw ImportHeightmapPNG(string filePath, bool showErrors)
        {
            LBRaw lbRaw = null;

            string methodName = "LBRaw.ImportHeightmapPNG";

            // Perform basic validation
            if (string.IsNullOrEmpty(filePath))
            {
                if (showErrors)
                {
                    Debug.LogWarning("ERROR: " + methodName + " - PNG file is not available");
                }
            }
            else if (!System.IO.File.Exists(filePath))
            {
                if (showErrors)
                {
                    Debug.LogWarning("ERROR: " + methodName + " - PNG file does not exist: " + filePath);
                }
            }
            else if (!System.IO.Path.HasExtension(filePath))
            {
                if (showErrors)
                {
                    Debug.LogWarning("ERROR: " + methodName + " - the filename does not have the raw extension. File: " + filePath);
                }
            }
            else if (System.IO.Path.GetExtension(filePath).ToLower() != ".png")
            {
                if (showErrors)
                {
                    Debug.LogWarning("ERROR: " + methodName + " - the filename is not of type png. File: " + filePath);
                }
            }
            else
            {
                byte[] pngData = null;

                // RAW data is stored as 16-bit, consisting of a little and big endian (8bit + 8bit)
                try
                {
                    pngData = System.IO.File.ReadAllBytes(filePath);
                }
                catch (System.Exception ex)
                {
                    if (showErrors)
                    {
                        Debug.LogWarning("ERROR: " + methodName + " - could not read RAW file " + ex.Message);
                    }
                }

                if (pngData == null)
                {
                    if (showErrors)
                    {
                        Debug.LogWarning("ERROR: " + methodName + " - could not get heights from file: " + filePath);
                    }
                }
                else if (pngData.Length < 3)
                {
                    if (showErrors)
                    {
                        Debug.LogWarning("ERROR: " + methodName + " - PNG data file is empty or invalid: " + filePath);
                    }
                }
                else
                {
                    lbRaw = new LBRaw();
                    if (lbRaw == null)
                    {
                        if (showErrors)
                        {
                            Debug.LogWarning("ERROR: " + methodName + " - could not create LBRaw instance");
                        }
                    }
                    else
                    {
                        // This assumes that the RAW file is square
                        lbRaw.rawHeightData  = null;
                        lbRaw.dataSourceName = System.IO.Path.GetFileName(filePath);
                        lbRaw.sourceFileType = SourceFileType.PNG;

                        // Create a very small texture. Texture2D.LoadImage will auto-resize it.
                        Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, false, true);

                        if (texture == null)
                        {
                            if (showErrors)
                            {
                                Debug.LogWarning("ERROR: " + methodName + " - could not create Texture2D");
                            }
                        }
                        else
                        {
                            // Load the PNG raw data and auto-resize the texture
                            texture.LoadImage(pngData, false);

                            // Is the width 2 ^ n where n is an integer
                            float log2 = Mathf.Log(texture.width, 2);
                            if (log2 - (int)log2 < 0.0001f)
                            {
                                // Make the width (2 ^ n) + 1
                                lbRaw.rawSourceWidth = texture.width + 1;
                            }
                            else
                            {
                                // Scale down to the nearest 2 ^ n width where n is an integer - then add 1
                                lbRaw.rawSourceWidth = ((int)Mathf.Pow(2f, (int)log2)) + 1;
                            }

                            // Assume this is a square image
                            lbRaw.rawSourceLength = lbRaw.rawSourceWidth;

                            //Debug.Log("PNG tex size: " + texture.width + "x" + texture.height + " resize to " + lbRaw.rawSourceWidth + "x" + lbRaw.rawSourceWidth);

                            // resize to a square texture with width = 2^n + 1
                            LBTextureOperations.TexturePointScale(texture, lbRaw.rawSourceWidth, lbRaw.rawSourceLength);

                            // Get the pixel colours from the base texture (mipmap = 0)
                            Color[] colours = texture.GetPixels();

                            if (colours == null)
                            {
                                if (showErrors)
                                {
                                    Debug.LogWarning("ERROR: " + methodName + " - could not get pixels from imported PNG file");
                                }
                            }
                            else
                            {
                                // TEST CODE
                                //LBEditorHelper.SaveMapTexture(texture, filePath + "_2.png", 2048);

                                // RAW data is stored as 16-bit, consisting of a little and big endian (8bit + 8bit)
                                lbRaw.rawHeightData = new byte[lbRaw.rawSourceWidth * lbRaw.rawSourceLength * 2];

                                if (lbRaw.rawHeightData == null)
                                {
                                    if (showErrors)
                                    {
                                        Debug.LogWarning("ERROR: " + methodName + " - could not create rawHeightData array");
                                    }
                                }
                                else
                                {
                                    int rawHeightDataLength = lbRaw.rawHeightData.Length;

                                    ushort sampleMinValue = ushort.MaxValue;
                                    ushort sampleMaxValue = ushort.MinValue;
                                    ushort sample16bit    = 0;

                                    for (int byteIndex = 0; byteIndex < rawHeightDataLength - 1; byteIndex += 2)
                                    {
                                        //pixel = colours[byteIndex / 2].grayscale;
                                        sample16bit = (ushort)(colours[byteIndex / 2].grayscale * 65535f);

                                        // LB stores RAW data in little endian (Windows) format, which is more suited to Intel processors.
                                        // Windows: little endian
                                        lbRaw.rawHeightData[byteIndex + 1] = System.Convert.ToByte(sample16bit >> 8);
                                        lbRaw.rawHeightData[byteIndex]     = System.Convert.ToByte(sample16bit & 255);

                                        // keep track of the min/max values in the input RAW data
                                        if (sample16bit > sampleMaxValue)
                                        {
                                            sampleMaxValue = sample16bit;
                                        }
                                        if (sample16bit < sampleMinValue)
                                        {
                                            sampleMinValue = sample16bit;
                                        }
                                    }

                                    if (sampleMinValue == ushort.MaxValue)
                                    {
                                        sampleMinValue = ushort.MinValue;
                                    }

                                    lbRaw.rawMinHeight = sampleMinValue;
                                    lbRaw.rawMaxHeight = sampleMaxValue;

                                    //Debug.Log("PNG RAW min/max: " + sampleMinValue + "," + sampleMaxValue);
                                }
                            }
                        }
                    }
                }
            }

            return(lbRaw);
        }
        /// <summary>
        /// This will import grass settings from the latest LB version file
        /// which is located in Assets/LandscapeBuilder/Setup folder and gets
        /// updated with newer versions of LB.
        /// WARNING: It will overwrite any user settings made in this Editor
        /// </summary>
        private void UpgradeGrassSettings()
        {
            // Need to load the textures first.
            GetGrassSettings();

            // Import the file from the Assets folder which was downloaded with the latest package of LB
            LBGrassSetup lbGrassSetupUpgrade = new LBGrassSetup();

            if (lbGrassSetupUpgrade != null)
            {
                lbGrassSetupUpgrade.Retrieve(true);
                if (lbGrassSetupUpgrade.lbGrassConfigList != null)
                {
                    int    numConfigsToUpgrade = lbGrassSetupUpgrade.lbGrassConfigList.Count;
                    int    upgradingCount      = 0;
                    int    numAdded            = 0;
                    int    numUpgraded         = 0;
                    int    numSkipped          = 0;
                    float  upgradeProgress     = 0f;
                    string progressMsg         = string.Empty;
                    string textureFolder       = string.Empty;

                    // Loop throught the new grass configs
                    foreach (LBGrassConfig lbGrassConfigUpgrade in lbGrassSetupUpgrade.lbGrassConfigList)
                    {
                        if (lbGrassConfigUpgrade != null)
                        {
                            allowRepaint    = true;
                            progressMsg     = "Upgrading " + (upgradingCount++).ToString() + " of " + numConfigsToUpgrade.ToString() + " ... Please wait";
                            upgradeProgress = upgradingCount / numConfigsToUpgrade;
                            if (EditorUtility.DisplayCancelableProgressBar("Upgrading Grass Configurations", progressMsg, upgradeProgress))
                            {
                                break;
                            }

                            // Find a match in the current config list (used in the LandscapeBuilderGrassSelector)
                            LBGrassConfig lbGrassconfigCurrent = lbGrassSetup.lbGrassConfigList.Find(g => g.grassTextureName == lbGrassConfigUpgrade.grassTextureName &&
                                                                                                     g.sourceName == lbGrassConfigUpgrade.sourceName);

                            // Is this a new configuration to be added?
                            if (lbGrassconfigCurrent == null)
                            {
                                LBGrassConfig newGrassConfig = new LBGrassConfig();
                                if (newGrassConfig != null)
                                {
                                    // default configuration
                                    if (!string.IsNullOrEmpty(lbGrassConfigUpgrade.grassTextureName))
                                    {
                                        newGrassConfig = new LBGrassConfig(lbGrassConfigUpgrade);

                                        // Select the correct folder based on the source of the LBGrassConfig (i.e. which Unity package is it from)
                                        if (lbGrassConfigUpgrade.sourceName == sourceNameHQPhotoPackVol1)
                                        {
                                            textureFolder = "Assets/" + pathHQPhotoPackVol1;
                                        }
                                        else if (lbGrassConfigUpgrade.sourceName == sourceNameHQPhotoPackVol2)
                                        {
                                            textureFolder = "Assets/" + pathHQPhotoPackVol2;
                                        }
                                        else if (lbGrassConfigUpgrade.sourceName == sourceNameRusticGrass)
                                        {
                                            textureFolder = "Assets/" + pathRusticGrass;
                                        }
                                        else
                                        {
                                            textureFolder = "unknown";
                                        }

                                        // The paths may not match the current project
                                        //string[] lookFor = new string[] { textureFolder };

                                        if (textureFolder == "unknown")
                                        {
                                            numSkipped++;
                                        }
                                        else
                                        {
                                            //string shortName = lbGrassConfigUpgrade.grassTextureName.Substring(0, lbGrassConfigUpgrade.grassTextureName.LastIndexOf('.'));

                                            // If the folder doesn't exist this typically means it is not installed in the project, so use the default path.
                                            if (AssetDatabase.IsValidFolder(textureFolder))
                                            {
                                                //Debug.Log("valid: " + textureFolder);
                                                // TODO - LB Grass Editor - get the correct project folder path when upgrading rather than using the default.
                                                // Currently this can return multiple textures for names that have a space in them. For now
                                                // we're going to use the default path...

                                                //string[] textureGUIDArray = AssetDatabase.FindAssets(shortName + " t:texture2D", lookFor);

                                                //if (textureGUIDArray != null)
                                                //{
                                                //    foreach (string guidstr in textureGUIDArray)
                                                //    {
                                                //        string pathToTexture2D = AssetDatabase.GUIDToAssetPath(guidstr);
                                                //        //Debug.Log(" tx: " + pathToTexture2D);
                                                //    }
                                                //}
                                            }

                                            //Debug.Log("upgrade path: " + lbGrassConfigUpgrade.grassTexturePath);
                                        }

                                        lbGrassSetup.lbGrassConfigList.Add(newGrassConfig);
                                        numAdded++;
                                    }
                                }
                            }
                            else
                            {
                                // Existing configuration - so update settings
                                lbGrassconfigCurrent.grassPatchFadingMode = lbGrassConfigUpgrade.grassPatchFadingMode;
                                lbGrassconfigCurrent.healthyColour        = lbGrassConfigUpgrade.healthyColour;
                                lbGrassconfigCurrent.dryColour            = lbGrassConfigUpgrade.dryColour;
                                lbGrassconfigCurrent.detailRenderMode     = lbGrassConfigUpgrade.detailRenderMode;
                                lbGrassconfigCurrent.minWidth             = lbGrassConfigUpgrade.minWidth;
                                lbGrassconfigCurrent.maxWidth             = lbGrassConfigUpgrade.maxWidth;
                                lbGrassconfigCurrent.minHeight            = lbGrassConfigUpgrade.minHeight;
                                lbGrassconfigCurrent.maxHeight            = lbGrassConfigUpgrade.maxHeight;

                                // Update the texture settings - only update installed textures
                                if (lbGrassconfigCurrent.texture2D != null)
                                {
#if UNITY_5_5_OR_NEWER
                                    LBTextureOperations.SetTextureAttributes(lbGrassconfigCurrent.texture2D, TextureImporterCompression.CompressedHQ, FilterMode.Bilinear, false, 0, true);
#else
                                    LBTextureOperations.SetTextureAttributes(lbGrassconfigCurrent.texture2D, TextureImporterFormat.AutomaticCompressed, FilterMode.Bilinear, false, 0, true);
#endif
                                }
                                numUpgraded++;
                            }
                        }
                    }

                    // Save the updates
                    if (lbGrassSetup != null)
                    {
                        lbGrassSetup.Save();
                    }
                    Debug.Log("Landscape Builder Grass Editor: Added " + numAdded.ToString() + " new grass configurations added. " + numUpgraded.ToString() + " updated. " + numSkipped.ToString() + " skipped.");
                    allowRepaint = false;
                    // Refresh the on-screen list
                    GetGrassSettings();
                    EditorUtility.ClearProgressBar();
                }
            }
        }