Пример #1
0
        // COLOR PICKER //
        //
        public void ColorPicker(ref Vector4 inputColor)
        {
            Vector3 hsv           = Vector3.zero;
            Vector3 hsvLDR        = Vector3.zero;
            Color   rgb           = Color.green;
            Vector4 matV          = Vector4.one;
            Color   colorConstant = new Vector4(inputColor.x, inputColor.y, inputColor.z, inputColor.w);

            shouldRepaintPicker = false;

            Material colorPickerSVMaterial = AssetDatabase.LoadAssetAtPath("Assets/Amps/Materials/ColorPicker_SV.mat", typeof(Material)) as Material;
            GUIStyle colorPickerSVStyle    = GUI.skin.GetStyle("colorPickerSV");
            Rect     colorPickerSVRect     = new Rect(colorPickerSVStyle.contentOffset.x,
                                                      colorPickerSVStyle.contentOffset.y,
                                                      colorPickerSVStyle.fixedWidth,
                                                      colorPickerSVStyle.fixedHeight);
            Material colorPickerHMaterial = AssetDatabase.LoadAssetAtPath("Assets/Amps/Materials/ColorPicker_H.mat", typeof(Material)) as Material;
            GUIStyle colorPickerHStyle    = GUI.skin.GetStyle("colorPickerH");
            Rect     colorPickerHRect     = new Rect(colorPickerHStyle.contentOffset.x,
                                                     colorPickerHStyle.contentOffset.y,
                                                     colorPickerHStyle.fixedWidth,
                                                     colorPickerHStyle.fixedHeight);
            Material colorPreviewMaterial = AssetDatabase.LoadAssetAtPath("Assets/Amps/Materials/ColorPreview.mat", typeof(Material)) as Material;
            GUIStyle colorPreviewStyle    = GUI.skin.GetStyle("colorPreview");
            Rect     colorPreviewRect     = new Rect(colorPreviewStyle.contentOffset.x,
                                                     colorPreviewStyle.contentOffset.y,
                                                     colorPreviewStyle.fixedWidth,
                                                     colorPreviewStyle.fixedHeight);

            GUILayout.BeginVertical("valuePickerBox");

            GUILayout.Box("", GUILayout.Height(colorPickerSVRect.height), GUILayout.ExpandWidth(true));             // Making room for the Saturation/Value picker.
            colorPickerSVRect = GUILayoutUtility.GetLastRect();
            EditorGUI.DrawPreviewTexture(colorPickerSVRect, dummyTexture, colorPickerSVMaterial);
            GUILayout.Space(8);

            GUILayout.Box("", GUILayout.Height(colorPickerHRect.height), GUILayout.ExpandWidth(true));             // Making room for the Hue picker.
            colorPickerHRect = GUILayoutUtility.GetLastRect();
            EditorGUI.DrawPreviewTexture(colorPickerHRect, dummyTexture, colorPickerHMaterial);
            GUILayout.Space(8);

            GUILayout.Box("", GUILayout.Height(colorPreviewRect.height), GUILayout.ExpandWidth(true));             // Making room for the preview.
            colorPreviewRect = GUILayoutUtility.GetLastRect();
            EditorGUI.DrawPreviewTexture(colorPreviewRect, dummyTexture, colorPreviewMaterial);
            GUILayout.Space(8);

            // RGB sliders.
            // Check which component has HDR value.
            if (colorPickerHdrToggles == null)
            {
                colorPickerHdrToggles = new bool[] { false, false, false, false }
            }
            ;
            if (colorConstant.r > 1)
            {
                colorPickerHdrToggles[0] = true;
            }
            if (colorConstant.g > 1)
            {
                colorPickerHdrToggles[1] = true;
            }
            if (colorConstant.b > 1)
            {
                colorPickerHdrToggles[2] = true;
            }

            // Show HDR capable controls for RGB.
            colorConstant.r = ColorComponentHDR("Red", colorConstant.r, ref colorPickerHdrToggles[0]);
            colorConstant.g = ColorComponentHDR("Green", colorConstant.g, ref colorPickerHdrToggles[1]);
            colorConstant.b = ColorComponentHDR("Blue", colorConstant.b, ref colorPickerHdrToggles[2]);
            GUILayout.Space(8);
            if ((colorConstant.r > 1 || colorConstant.g > 1 || colorConstant.b > 1) &&
                colorPickerHdrToggles[3] == false)
            {
                colorPickerHdrToggles[3] = true;                        // If necessary we turn on HDR on V.
            }

            // Generating HSV values.
            hsv    = AmpsHelpers.RGBtoHSV(colorConstant);
            hsvLDR = AmpsHelpers.RGBtoHSV(NormalizeRGB(colorConstant));

            //Debug.Log("1 hsvldr: " + hsvLDR.x + " , " + hsvLDR.y + " , " + hsvLDR.z);

            // HSV sliders.
            if (hsvLDR.y == 0)                                                                                                                                  // If the Hue slider is locked due to 0 Saturation...
            {
                if (tempColorPickerUsed[0] == false)
                {
                    tempColorPickerUsed[0] = true;
                }
                tempColorPickerHsv.x = ColorComponentLDR("Hue", tempColorPickerHsv.x);                  // ...then we start using a dummy variable for Hue.
            }
            else
            {
                if (tempColorPickerUsed[0] == true)
                {
                    hsvLDR.x = tempColorPickerHsv.x;
                    tempColorPickerUsed[0] = false;
                }
                hsvLDR.x             = ColorComponentLDR("Hue", hsvLDR.x);
                tempColorPickerHsv.x = hsvLDR.x;
            }

            if (hsvLDR.z == 0)                                                                                                                                  // If the Saturation slider is locked due to 0 Value...
            {
                if (tempColorPickerUsed[1] == false)
                {
                    tempColorPickerUsed[1] = true;
                }
                tempColorPickerHsv.y = ColorComponentLDR("Sat", tempColorPickerHsv.y);                  // ...then we start using a dummy variable for Saturation.
            }
            else
            {
                if (tempColorPickerUsed[1] == true)
                {
                    hsvLDR.y = tempColorPickerHsv.y;
                    tempColorPickerUsed[1] = false;
                }
                hsvLDR.y             = ColorComponentLDR("Sat", hsvLDR.y);
                tempColorPickerHsv.y = hsvLDR.y;
            }

            hsv.z = ColorComponentHDR("Value", hsv.z, ref colorPickerHdrToggles[3]);
            GUILayout.Space(8);
            colorConstant.a = ColorComponentLDR("Alpha", colorConstant.a);

            // Hue, Saturation and LDR Value picking.
            if (Event.current.type == EventType.mouseDrag || Event.current.type == EventType.mouseDown)
            {
                if (colorPickerSVRect.Contains(Event.current.mousePosition))
                {
                    Vector2 mouseUV = (Event.current.mousePosition - new Vector2(colorPickerSVRect.x, colorPickerSVRect.y));
                    mouseUV.x = mouseUV.x / colorPickerSVRect.width;
                    mouseUV.y = mouseUV.y / colorPickerSVRect.height;
                    hsvLDR.y  = mouseUV.y;
                    tempColorPickerUsed[1] = false;
                    hsvLDR.z = mouseUV.x;
                    hsv.z    = hsvLDR.z;                        // Picking a Value discards existing HDR Value.

                    shouldRepaintPicker = true;
                }
                else if (colorPickerHRect.Contains(Event.current.mousePosition))
                {
                    Vector2 mouseUV = (Event.current.mousePosition - new Vector2(colorPickerHRect.x, colorPickerHRect.y));
                    mouseUV.x = mouseUV.x / colorPickerSVRect.width;
                    if (tempColorPickerUsed[0])
                    {
                        tempColorPickerHsv.x = mouseUV.x;
                    }
                    else
                    {
                        hsvLDR.x = mouseUV.x;
                    }

                    shouldRepaintPicker = true;
                }
            }

            hsv.x = hsvLDR.x;                   // Hue...
            hsv.y = hsvLDR.y;                   // ...and Saturation will always be LDR.

            if (tempColorPickerUsed[0])
            {
                matV.x = tempColorPickerHsv.x;
            }
            else
            {
                matV.x = hsvLDR.x;
            }
            if (tempColorPickerUsed[1])
            {
                matV.y = tempColorPickerHsv.y;
            }
            else
            {
                matV.y = hsvLDR.y;
            }
            matV.z = hsvLDR.z;

            colorPickerSVMaterial.SetVector("_HSV", matV);
            colorPickerHMaterial.SetFloat("_Hue", matV.x);

            // Converting HSV back to RGB.
            rgb             = AmpsHelpers.HSVtoRGB(hsv);
            colorConstant.r = rgb.r;
            colorConstant.g = rgb.g;
            colorConstant.b = rgb.b;

            //colorPickerAMaterial.SetFloat("_Alpha", matV.w);
            colorPreviewMaterial.SetVector("_Color", colorConstant);

            if (hsv.z > 1)
            {
                if (rgb.r > 1 && !colorPickerHdrToggles[0])
                {
                    colorPickerHdrToggles[0] = true;
                }
                if (rgb.g > 1 && !colorPickerHdrToggles[1])
                {
                    colorPickerHdrToggles[1] = true;
                }
                if (rgb.b > 1 && !colorPickerHdrToggles[2])
                {
                    colorPickerHdrToggles[2] = true;
                }
            }

            GUILayout.Space(8);
            GUILayout.EndVertical();

            inputColor = new Vector4(colorConstant.r, colorConstant.g, colorConstant.b, colorConstant.a);
        }