Esempio n. 1
0
        public override Sampler Clone(Sampler reuseObject)
        {
            Sampler2D newSampler;

            if (reuseObject != null)
            {
                newSampler = reuseObject as Sampler2D;
                newSampler.ResizeIfNeeded(size, step);
            }
            else
            {
                newSampler = new Sampler2D(size, step);
            }

            newSampler.min = min;
            newSampler.max = max;

            System.Buffer.BlockCopy(map, 0, newSampler.map, 0, map.Length);

            return(newSampler);
        }
Esempio n. 2
0
 public void Sampler2DPreview(string prefix, Sampler2D samp, bool update, bool settings = true, FilterMode fm = FilterMode.Bilinear)
 {
     Sampler2DPreview(new GUIContent(prefix), samp, update, settings, fm);
 }
Esempio n. 3
0
        public void Sampler2DPreview(GUIContent prefix, Sampler2D samp, bool update, bool settings = true, FilterMode fm = FilterMode.Bilinear)
        {
            int previewSize = (int)currentWindowRect.width - 20 - 20;             //padding + texture margin
            var e           = Event.current;

            if (samp == null)
            {
                return;
            }

            if (prefix != null && !String.IsNullOrEmpty(prefix.text))
            {
                EditorGUILayout.LabelField(prefix);
            }

            var fieldSettings = GetGUISettingData(() => {
                var state        = new PWGUISettings();
                state.filterMode = fm;
                state.debug      = false;
                state.gradient   = new SerializableGradient(
                    PWUtils.CreateGradient(
                        new KeyValuePair <float, Color>(0, Color.black),
                        new KeyValuePair <float, Color>(1, Color.white)
                        )
                    );
                return(state);
            });

            //recreated texture if it has been destoryed:
            if (fieldSettings.texture == null)
            {
                fieldSettings.texture            = new Texture2D(previewSize, previewSize, TextureFormat.RGBA32, false);
                fieldSettings.texture.filterMode = fieldSettings.filterMode;
            }

            //same for the gradient:
            if (fieldSettings.gradient == null || fieldSettings.gradient.alphaKeys == null)
            {
                fieldSettings.gradient = fieldSettings.serializableGradient;
            }

            Texture2D tex      = fieldSettings.texture;
            Gradient  gradient = fieldSettings.gradient;

            if (samp.size != tex.width)
            {
                tex.Resize(samp.size, samp.size, TextureFormat.RGBA32, false);
            }

            Rect previewRect = EditorGUILayout.GetControlRect(GUILayout.ExpandWidth(true), GUILayout.Height(0));

            if (previewRect.width > 2)
            {
                fieldSettings.savedRect = previewRect;
            }

            TexturePreview(tex, false, false, false);

            //draw the settings window
            if (settings && fieldSettings.active)
            {
                PWPopup.AddToRender(fieldSettings, "Sampler 2D settings", () => {
                    EditorGUILayout.BeginVertical();
                    {
                        EditorGUI.BeginChangeCheck();
                        fieldSettings.filterMode = (FilterMode)EditorGUILayout.EnumPopup(fieldSettings.filterMode);
                        if (EditorGUI.EndChangeCheck())
                        {
                            tex.filterMode = fieldSettings.filterMode;
                        }
                        gradient = (Gradient)gradientField.Invoke(null, new object[] { "", gradient, null });
                        if (!gradient.Compare(fieldSettings.serializableGradient))
                        {
                            fieldSettings.update = true;
                        }
                        fieldSettings.serializableGradient = (SerializableGradient)gradient;
                    }
                    EditorGUILayout.EndVertical();

                    if (e.type == EventType.KeyDown && fieldSettings.active)
                    {
                        if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter || e.keyCode == KeyCode.Escape)
                        {
                            fieldSettings.InActive();
                            e.Use();
                        }
                    }

                    EditorGUIUtility.labelWidth = 100;
                    fieldSettings.debug         = EditorGUILayout.Toggle("debug", fieldSettings.debug);

                    if (GUILayout.Button("force update"))
                    {
                        fieldSettings.update = true;
                    }
                });
            }

            if (settings)
            {
                //draw the setting icon and manage his events
                int  icSettingsSize    = 16;
                int  icSettingsPadding = 4;
                Rect icSettingsRect    = new Rect(previewRect.x + previewRect.width - icSettingsSize - icSettingsPadding, previewRect.y + icSettingsPadding, icSettingsSize, icSettingsSize);

                GUI.DrawTexture(icSettingsRect, ic_settings);
                if (e.type == EventType.MouseDown && e.button == 0)
                {
                    if (icSettingsRect.Contains(e.mousePosition))
                    {
                        fieldSettings.Invert(null);
                        e.Use();
                    }
                    else if (fieldSettings.active)
                    {
                        fieldSettings.InActive();
                        e.Use();
                    }
                }
            }

            if (!settings && fieldSettings.texture.filterMode != fm)
            {
                fieldSettings.texture.filterMode = fm;
            }

            //update the texture with the gradient
            if (update || fieldSettings.update)
            {
                samp.Foreach((x, y, val) => {
                    tex.SetPixel(x, y, gradient.Evaluate(Mathf.Clamp01(val)));
                }, true);
                tex.Apply();
                fieldSettings.update = false;
            }

            if (fieldSettings.debug)
            {
                Vector2 pixelPos = e.mousePosition - fieldSettings.savedRect.position;

                pixelPos *= samp.size / fieldSettings.savedRect.width;

                EditorGUILayout.LabelField("Sampler2D min: " + samp.min + ", max: " + samp.max);

                if (pixelPos.x >= 0 && pixelPos.y >= 0 && pixelPos.x < samp.size && pixelPos.y < samp.size)
                {
                    if (e.isMouse)
                    {
                        e.Use();
                    }
                    EditorGUILayout.LabelField("(" + (int)pixelPos.x + ", " + (int)pixelPos.y + "): " + samp[(int)pixelPos.x, (int)pixelPos.y]);
                }
                else
                {
                    EditorGUILayout.LabelField("(NA, NA): NA");
                }
            }
        }
Esempio n. 4
0
 public void Sampler2DPreview(Sampler2D samp, bool update, bool settings = true, FilterMode fm = FilterMode.Bilinear)
 {
     Sampler2DPreview((GUIContent)null, samp, update, settings, fm);
 }