예제 #1
0
        /// <summary>
        /// Generates frames and 3D texture
        /// </summary>
        /// <param name="noiseProfile">Noise profile</param>
        /// <param name="materials">Materials, one for each layer</param>
        /// <param name="textureAssetFileName">Texture file name</param>
        /// <param name="showConfirm">Show confirm dialog?</param>
        public static void GenerateFramesAnd3DTexture(WeatherMakerCloudNoiseProfileGroupScript noiseProfile, Material[] materials, string textureAssetFileName, bool showConfirm = false)
        {
            string tempPath = Path.GetTempPath();

            tempPath = System.IO.Path.Combine(tempPath, "WeatherMakerNoiseTexture");
            string texturesPath = WeatherMakerCloudNoiseGeneratorScript.GenerateFrameTextures(noiseProfile, materials, 0.0f, 0.5f, false);

            WeatherMakerCloudNoiseGeneratorScript.Generate3DTexture(texturesPath, textureAssetFileName, noiseProfile.FilterMode, noiseProfile.GenerateMips, progress: 0.5f, progressMultiplier: 0.5f);
        }
        private void OnGUI()
        {
            float textureHeight = Mathf.Max(position.height - 80.0f, 180.0f);
            float fieldWidth    = EditorGUIUtility.fieldWidth;

            EditorGUIUtility.fieldWidth = 800.0f;
            Rect rect = new Rect(4.0f, textureHeight + 8.0f, EditorGUIUtility.fieldWidth, EditorGUIUtility.singleLineHeight);

            noiseProfile = EditorGUI.ObjectField(rect, "Cloud Noise Profile", noiseProfile, typeof(WeatherMakerCloudNoiseProfileGroupScript), false) as WeatherMakerCloudNoiseProfileGroupScript;
            if (noiseProfile != null && noiseProfile.NoiseProfiles != null && noiseProfile.NoiseProfiles.Length <= materials.Length)
            {
                for (int i = 0; i < noiseProfile.NoiseProfiles.Length; i++)
                {
                    float x     = 4.0f + (i * textureHeight) + (i * 4.0f);
                    float y     = 4.0f;
                    Rect  rect2 = new Rect(x, y, textureHeight, textureHeight);
                    //GUI.DrawTexture(rect2, renderTextures[i], ScaleMode.ScaleToFit);
                    EditorGUI.DrawTextureTransparent(rect2, renderTextures[i], ScaleMode.ScaleToFit);
                }
                EditorPrefs.SetString("WeatherMakerCloudNoiseGeneratorLastProfile", AssetDatabase.GetAssetPath(noiseProfile));
            }

            rect.y    += EditorGUIUtility.singleLineHeight + 4.0f;
            rect.width = 100.0f;
            if (GUI.Button(rect, "Browse"))
            {
                string file = WeatherMakerCloudNoiseGeneratorScript.PickAssetFile(textureAssetPath);
                if (!string.IsNullOrEmpty(file))
                {
                    textureAssetPath = file;
                    GUI.FocusControl(null);
                }
            }
            rect.xMin        = rect.width + 8.0f;
            rect.width       = EditorGUIUtility.fieldWidth - 208.0f;
            textureAssetPath = EditorGUI.TextField(rect, string.Empty, textureAssetPath);
            rect.xMin        = rect.xMin + rect.width + 4.0f;
            rect.width       = 100.0f;
            if (GUI.Button(rect, "Export"))
            {
                WeatherMakerCloudNoiseGeneratorScript.GenerateFramesAnd3DTexture(noiseProfile, materials, textureAssetPath);
                Repaint();
            }
            EditorGUIUtility.fieldWidth = fieldWidth;
            rect.y    += EditorGUIUtility.singleLineHeight + 4.0f;
            rect.xMin  = 4.0f;
            rect.width = 200.0f;
            float labelWidth = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 68.0f;
            animated = EditorGUI.Toggle(rect, "Animated", animated);
            EditorGUIUtility.labelWidth = labelWidth;
        }
        private void OnEnable()
        {
            for (int i = 0; i < 4; i++)
            {
                string[] assets = AssetDatabase.FindAssets("WeatherMakerCloudNoiseGeneratorMaterial" + (i + 1));
                string   path   = AssetDatabase.GUIDToAssetPath(assets[0]);
                materials[i]      = AssetDatabase.LoadAssetAtPath <Material>(path);
                assets            = AssetDatabase.FindAssets("WeatherMakerCloudNoiseRenderTexture" + (i + 1));
                path              = AssetDatabase.GUIDToAssetPath(assets[0]);
                renderTextures[i] = AssetDatabase.LoadAssetAtPath <RenderTexture>(path);
            }
            textureAssetPath = EditorPrefs.GetString("WeatherMakerCloudNoiseGeneratorFileName");
            if (string.IsNullOrEmpty(textureAssetPath))
            {
                textureAssetPath = "Assets/WeatherMaker/Prefab/Textures/Clouds/CloudNoiseTexture.asset";
            }
            string lastNoiseAssetPath = EditorPrefs.GetString("WeatherMakerCloudNoiseGeneratorLastProfile");

            if (!string.IsNullOrEmpty(lastNoiseAssetPath))
            {
                noiseProfile = AssetDatabase.LoadAssetAtPath <WeatherMakerCloudNoiseProfileGroupScript>(lastNoiseAssetPath);
            }
        }
예제 #4
0
        /// <summary>
        /// Generates all the individual frame textures
        /// </summary>
        /// <param name="profile">Noise profile</param>
        /// <param name="materials">Materials, should be one for each layer</param>
        /// <param name="progress">Current progress</param>
        /// <param name="progressMultiplier">Progress multiplier</param>
        /// <param name="clearProgress">Whether to restart progress</param>
        /// <returns>Texture path</returns>
        public static string GenerateFrameTextures(WeatherMakerCloudNoiseProfileGroupScript profile, Material[] materials, float progress = 0.0f, float progressMultiplier = 1.0f, bool clearProgress = true)
        {
            if (profile == null || profile.NoiseProfiles == null || materials == null || profile.NoiseProfiles.Length > materials.Length)
            {
                return(null);
            }

            string tempPath    = Path.GetTempPath();
            string texturePath = System.IO.Path.Combine(tempPath, "WeatherMakerNoiseTexture");

            UnityEditor.EditorUtility.DisplayProgressBar("Progress...", "", progress);
            try
            {
                RenderTexture[] renderTextures = new RenderTexture[profile.NoiseProfiles.Length];
                Texture2D[]     textures       = new Texture2D[profile.NoiseProfiles.Length];
                float           currentProgress;
                for (int channelIndex = 0; channelIndex < profile.NoiseProfiles.Length; channelIndex++)
                {
                    RenderTexture renderTexture = new RenderTexture(profile.Size, profile.Size, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                    renderTexture.autoGenerateMips = false;
                    renderTexture.name             = "WeatherMakerCloudRenderTextureTemp" + channelIndex;
                    renderTextures[channelIndex]   = renderTexture;

                    Texture2D tex2D = new Texture2D(profile.Size, profile.Size, TextureFormat.ARGB32, false, false);
                    tex2D.filterMode       = FilterMode.Bilinear;
                    tex2D.wrapMode         = TextureWrapMode.Clamp;
                    textures[channelIndex] = tex2D;
                }

                float frame = 0.0f;
                float step  = (profile.Step <= 0.0f ? 1.0f / (float)profile.Count : profile.Step);
                if (Directory.Exists(texturePath))
                {
                    Directory.Delete(texturePath, true);
                    System.Threading.Thread.Sleep(100);
                }
                Directory.CreateDirectory(texturePath);
                string[] dirs = new string[profile.NoiseProfiles.Length];
                for (int channelIndex = 0; channelIndex < profile.NoiseProfiles.Length; channelIndex++)
                {
                    profile.NoiseProfiles[channelIndex].ApplyToMaterial(materials[channelIndex]);
                    dirs[channelIndex] = Path.Combine(texturePath, channelIndex.ToString());
                    Directory.CreateDirectory(dirs[channelIndex]);
                }
                for (int frameIndex = 0; frameIndex < profile.Count; frameIndex++)
                {
                    frame = (float)frameIndex * step;
                    for (int channelIndex = 0; channelIndex < profile.NoiseProfiles.Length; channelIndex++)
                    {
                        materials[channelIndex].SetFloat(WMS._CloudNoiseFrame, frame);
                        Graphics.Blit(null, renderTextures[channelIndex], materials[channelIndex], 0);
                        RenderTexture.active = renderTextures[channelIndex];
                        textures[channelIndex].ReadPixels(new Rect(0, 0, profile.Size, profile.Size), 0, 0, false);
                        textures[channelIndex].Apply();
                        RenderTexture.active = null;
                        GL.Flush();
                        byte[] imageData = textures[channelIndex].EncodeToPNG();
                        System.IO.File.WriteAllBytes(Path.Combine(dirs[channelIndex], "WeatherMakerNoiseTexture_" + frameIndex.ToString("D4") + ".png"), imageData);
                    }
                    currentProgress = progress + ((((float)(frameIndex + 1) / (float)profile.Count) * progressMultiplier));
                    UnityEditor.EditorUtility.DisplayProgressBar("Progress...", "", currentProgress);
                }
                for (int i = 0; i < renderTextures.Length; i++)
                {
                    renderTextures[i].Release();
                    DestroyImmediate(renderTextures[i]);
                    DestroyImmediate(textures[i]);
                }
            }
            finally
            {
                if (clearProgress)
                {
                    UnityEditor.EditorUtility.ClearProgressBar();
                }
            }
            return(texturePath);
        }