コード例 #1
0
        void OnGUI()
        {
            //don't actually care about something being selected but if its null curve field throws errors
            if (Selection.activeGameObject == null)
            {
                Selection.activeGameObject = Editor.FindObjectOfType <GameObject>();
            }


            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Grayscale"))
            {
                mode = Mode.GrayScale;
            }

            if (GUILayout.Button("Gradient"))
            {
                mode = Mode.FullColour;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            if (mode == Mode.GrayScale)
            {
                curve = EditorGUILayout.CurveField("curve", curve);
            }
            else if (mode == Mode.FullColour)
            {
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(serialColGrad, true, null);
                if (EditorGUI.EndChangeCheck())
                {
                    serialColGradObj.ApplyModifiedProperties();
                }
            }
            textureWidth = (TextureWidth)EditorGUILayout.EnumPopup("Texture ramp width", (System.Enum)textureWidth);



            EditorGUILayout.Space();
            saveAs = EditorGUILayout.TextField("Save as: ", saveAs);

            if (GUILayout.Button("Create?"))
            {
                CreateTexture();
            }
        }
コード例 #2
0
        public override IEnumerator ReceivePayload(VisualPayload payload)
        {
            var currentWidth = 0f;

            var flagColorImpact = FlagColorImpact.GetFirstValue(payload.Data);

            var sections = SectionScope.GetEntries(payload.Data).ToList();

            var totalSize = (float)(from entry in SectionScope.GetEntries(payload.Data)
                                    select
                                    SectionMemorySize.GetValue(entry)).Aggregate((a, b) => a + b);

            ColorGradient sectionGradient = new ColorGradient(2 * sections.Count);

            foreach (var section in SectionScope.GetEntries(payload.Data))
            {
                var bandColor = Color.Lerp(
                    SectionColorMapping.GetSectionTypeColor(SectionType.GetValue(section)),
                    SectionColorMapping.GetSectionFlagColor(SectionFlag.GetValue(section)),
                    flagColorImpact);
                sectionGradient.AddColorKey(new GradientColorKey(
                                                bandColor,
                                                currentWidth));

                currentWidth += SectionMemorySize.GetValue(section);

                sectionGradient.AddColorKey(new GradientColorKey(
                                                bandColor,
                                                currentWidth));

                currentWidth += Epsilon * totalSize;
            }

            var textureWidth  = TextureWidth.GetFirstValue(payload.Data);
            var textureHeight = TextureHeight.GetFirstValue(payload.Data);

            sectionGradient.RescaleColorKeys(currentWidth);


            var resultTexture = new Texture2D(textureWidth, textureHeight);

            var outColors = new Color[textureWidth * textureHeight];

            for (int x = 0; x < textureWidth; x++)
            {
                var localColor = sectionGradient.Evaluate(x / (float)textureWidth);
                for (int y = 0; y < textureHeight; y++)
                {
                    outColors[x + textureWidth * y] = localColor;
                }
            }

            resultTexture.SetPixels(outColors);
            resultTexture.Apply();

            var quadMaterial = GameObject.Instantiate(MaterialFactory.GetDefaultMaterial());

            quadMaterial.mainTexture = resultTexture;

            MaterialTarget.SetValue(quadMaterial, payload.Data);

            var iterator = Router.TransmitAll(payload);

            while (iterator.MoveNext())
            {
                yield return(null);
            }
        }
コード例 #3
0
        private void OnGUI()
        {
            serializedObject.Update();
            EditorGUILayout.LabelField("V 0.1");
            EditorGUILayout.LabelField("This tool creates ramp maps based on a gradient.", flowTextStyle);
            Divider();

            EditorGUILayout.LabelField("How to use:", UnityEditor.EditorStyles.boldLabel);
            EditorGUILayout.LabelField("1. Create your gradient.");
            EditorGUILayout.LabelField("2. Setup the output settings.");
            EditorGUILayout.LabelField("3. Run the operation via \"Create Ramp\".");
            EditorGUILayout.LabelField("Existing files will be overwritten!", UnityEditor.EditorStyles.boldLabel);
            Divider();

            EditorGUILayout.LabelField("Input:", UnityEditor.EditorStyles.boldLabel);
            EditorGUILayout.LabelField("Dark -> Bright");
            EditorGUILayout.PropertyField(_outputGradientProperty, new GUIContent("Gradient"));
            Divider();

            EditorGUILayout.LabelField("Output Settings:", UnityEditor.EditorStyles.boldLabel);
            _outputFormat = (TextureFormat)EditorGUILayout.EnumPopup("Format", _outputFormat);
            EditorGUILayout.BeginHorizontal();
            float labelWidthBase = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 37;
            _outputWidth = (TextureWidth)EditorGUILayout.EnumPopup("Size", _outputWidth);
            EditorGUIUtility.labelWidth = 42;
            EditorGUIUtility.labelWidth = labelWidthBase;
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            _filePath = EditorGUILayout.TextField("Output Folder", _filePath);
            if (GUILayout.Button("Select"))
            {
                _filePath = EditorUtility.SaveFolderPanel("Output Folder", _filePath, "");
            }
            if (_filePath == "")
            {
                _filePath = _defaultFilePath;
            }
            EditorGUILayout.EndHorizontal();
            _filename = EditorGUILayout.TextField("Filename", _filename);
            Divider();

            serializedObject.ApplyModifiedProperties();

            if (_filename != "")
            {
                if (GUILayout.Button("Create Ramp"))
                {
                    EditorUtility.DisplayProgressBar("Texture progress", "Creating new texture...", 0.5f);
                    Texture2D _outputTexture = new Texture2D((int)_outputWidth, 4);

                    for (int i = 0; i < (int)_outputWidth; i++)
                    {
                        float pPos       = (float)i / (float)_outputWidth;
                        Color pixelColor = _outputGradient.Evaluate(pPos);
                        _outputTexture.SetPixel(i, 0, pixelColor);
                        _outputTexture.SetPixel(i, 1, pixelColor);
                        _outputTexture.SetPixel(i, 2, pixelColor);
                        _outputTexture.SetPixel(i, 3, pixelColor);
                    }

                    _outputTexture.Apply();
                    SaveOutputTexture(_outputTexture, _filePath, FilterFilename(_filename), _outputFormat, _outputWidth);
                    EditorUtility.ClearProgressBar();
                }
            }
        }
コード例 #4
0
        private static void SaveOutputTexture(Texture2D tex, string path, string name, TextureFormat format, TextureWidth width)
        {
            //path = System.Text.RegularExpressions.Regex.Replace(path, ".*:Assets:", "");
            path  = path.Substring(path.IndexOf("Assets"));
            path += "/";

            string fileSuffix;

            if (format == TextureFormat.PNG)
            {
                fileSuffix = ".png";
            }
            #if UNITY_2018_3_OR_NEWER
            else if (format == TextureFormat.TGA)
            {
                fileSuffix = ".tga";
            }
            #endif
            else
            {
                fileSuffix = ".jpg";
            }

            if (format == TextureFormat.PNG)
            {
                System.IO.File.WriteAllBytes(path + name + fileSuffix, tex.EncodeToPNG());
            }
            #if UNITY_2018_3_OR_NEWER
            else if (format == TextureFormat.TGA)
            {
                System.IO.File.WriteAllBytes(path + name + fileSuffix, tex.EncodeToTGA());
            }
            #endif
            else
            {
                System.IO.File.WriteAllBytes(path + name + fileSuffix, tex.EncodeToJPG());
            }

            AssetDatabase.Refresh();

            string          texturePath     = path + name + fileSuffix;
            TextureImporter textureImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath);
            textureImporter.wrapMode           = TextureWrapMode.Clamp;
            textureImporter.maxTextureSize     = (int)width;
            textureImporter.alphaSource        = TextureImporterAlphaSource.None;
            textureImporter.textureCompression = TextureImporterCompression.CompressedHQ;
            textureImporter.mipmapEnabled      = true;
            AssetDatabase.SaveAssets();
            AssetDatabase.ImportAsset(texturePath);
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = AssetDatabase.LoadAssetAtPath(texturePath, typeof(UnityEngine.Object));
            EditorGUIUtility.PingObject(Selection.activeObject);
        }