Example #1
0
        public Brush(IntVector2 size, Color color)
        {
            m_Size = size;
            m_Colors = new Color[m_Size.x * m_Size.y];

            for (int i = 0; i < m_Colors.Length; i++)
                m_Colors[i] = color;
        }
Example #2
0
        public static void DrawPixelLine(Texture2D texture, Brush brush, IntVector2 p1, IntVector2 p2)
        {
            List<IntVector2> points = GetPointsOnLine(p1.x, p1.y, p2.x, p2.y).ToList();
            if (p1 != points[0])
                points.Reverse();

            foreach (IntVector2 pixel in points)
                SetPixelsClamped (texture, pixel - brush.m_BrushPivot, brush.m_Size, brush.m_Colors);

            texture.Apply ();
        }
Example #3
0
 public static Vector3 PixelToWorld(IntVector2 pixelPosition, Transform transform, Sprite sprite, bool clamp)
 {
     Vector2 uvPosition = Utility.PixelToUV (pixelPosition, sprite, clamp);
     Vector3 localPosition = Utility.UVToLocal (uvPosition, sprite, clamp);
     Vector2 worldPosition = Utility.LocalToWorld (localPosition, transform);
     return worldPosition;
 }
Example #4
0
        public static void SetPixelsClamped(Texture2D texture, IntVector2 position, IntVector2 size, Color[] colors)
        {
            IntVector2 min = new IntVector2(
                Math.Max (Math.Min (position.x, texture.width - 1), 0),
                Math.Max (Math.Min (position.y, texture.height - 1), 0)
                );
            IntVector2 max = new IntVector2(
                Math.Max(Math.Min(position.x + size.x - 1, texture.width - 1), 0),
                Math.Max(Math.Min(position.y + size.y - 1, texture.height - 1), 0)
                );

            IntVector2 texturePos = min;

            int colorIndex = 0;
            for (texturePos.y = position.y; texturePos.y < position.y + size.y; texturePos.y++)
            {
                for (texturePos.x = position.x; texturePos.x < position.x + size.x; texturePos.x++)
                {
                    if (texturePos.x >= min.x && texturePos.x <= max.x && texturePos.y >= min.y && texturePos.y <= max.y)
                        texture.SetPixel(texturePos.x, texturePos.y, colors[colorIndex]);
                    colorIndex++;
                }
            }
        }
Example #5
0
 public static Vector2 PixelToWorld(IntVector2 pixelPosition)
 {
     return new Vector2(
         pixelPosition.x / k_DefaultPixelsToUnits,
         pixelPosition.y / k_DefaultPixelsToUnits
         );
 }
Example #6
0
 public static Vector3 PixelToWorld(IntVector2 pixelPosition, Transform transform, Sprite sprite)
 {
     return PixelToWorld (pixelPosition, transform, sprite, true);
 }
Example #7
0
 public static Vector2 PixelToUV(IntVector2 pixel, Sprite sprite)
 {
     return PixelToUV (pixel, sprite, true);
 }
Example #8
0
        public static Vector2 PixelToUV(IntVector2 pixel, Sprite sprite, bool clamp)
        {
            if (sprite == null)
                return Vector2.zero;

            Vector2 result = new Vector2((float)pixel.x / (float)sprite.texture.width, (float)pixel.y / (float)sprite.texture.height);

            if (clamp)
                result = new Vector2 (
                    Mathf.Max (Mathf.Min (result.x, 1.0f - k_UVEpsilon), 0f),
                    Mathf.Max (Mathf.Min (result.y, 1.0f - k_UVEpsilon), 0f)
                    );

            return result;
        }
Example #9
0
 public Brush(IntVector2 size, Color[] colors)
 {
     m_Size = size;
     m_Colors = colors;
 }
Example #10
0
 private void Initialize(RagePixelState state)
 {
     if (!m_Initialized)
     {
         m_Initialized = true;
         m_Size = new IntVector2(state.sprite.textureRect.size);
     }
 }
Example #11
0
        public static Color[] GetYReversed(Color[] colors, IntVector2 size)
        {
            Color[] result = new Color[colors.Length];

            int i = 0;
            for (int y = size.y - 1; y >= 0; y--)
                for (int x = 0; x < size.x; x++)
                    result[i++] = colors[y*size.x + x];

            return result;
        }
Example #12
0
 public static bool PixelInBounds(IntVector2 pixelPosition, Sprite sprite)
 {
     return (pixelPosition.x < sprite.rect.width && pixelPosition.y < sprite.rect.height && pixelPosition.x >= 0f && pixelPosition.y >= 0f);
 }
Example #13
0
        public static Rect GetPixelMarqueeRect(IntVector2 pixelA, IntVector2 pixelB)
        {
            int minX = Mathf.Min (pixelA.x, pixelB.x);
            int minY = Mathf.Min (pixelA.y, pixelB.y);
            int maxX = Mathf.Max (pixelA.x, pixelB.x);
            int maxY = Mathf.Max (pixelA.y, pixelB.y);

            return new Rect(minX, minY, maxX - minX, maxY - minY);
        }
Example #14
0
        public static void DrawSpriteBounds(Color color, Color shadowColor, Transform transform, Sprite sprite)
        {
            IntVector2 minPixel = new IntVector2(0, 0);
            IntVector2 maxPixel = new IntVector2(sprite.rect.width, sprite.rect.height);

            Vector3[] screenPolyLine = new Vector3[5];
            screenPolyLine[0] = PixelToScreen(new IntVector2(minPixel.x, minPixel.y), transform, sprite) + new Vector2(1f, -3f);
            screenPolyLine[1] = PixelToScreen(new IntVector2(maxPixel.x, minPixel.y), transform, sprite) + new Vector2(2f, -3f);
            screenPolyLine[2] = PixelToScreen(new IntVector2(maxPixel.x, maxPixel.y), transform, sprite) + new Vector2(2f, -2f);
            screenPolyLine[3] = PixelToScreen(new IntVector2(minPixel.x, maxPixel.y), transform, sprite) + new Vector2(1f, -2f);
            screenPolyLine[4] = screenPolyLine[0];

            Vector3[] shadowPolyLine = new Vector3[5];
            for (int i = 0; i < screenPolyLine.Length; i++)
                shadowPolyLine[i] = screenPolyLine[i] + new Vector3 (1f, 1f, 0f);

            Handles.BeginGUI ();
            Handles.color = shadowColor;
            Handles.DrawPolyLine (shadowPolyLine);
            Handles.color = color;
            Handles.DrawPolyLine (screenPolyLine);

            Handles.EndGUI ();
        }
Example #15
0
        public void OnSceneGUI(RagePixelState state)
        {
            Initialize (state);

            Vector2 uvPos = Utility.PixelToUV (m_Size, state.sprite, false);
            Vector3 localPos = Utility.UVToLocal (uvPos, state.sprite, false);
            Vector3 worldPos = Utility.LocalToWorld (localPos, state.transform);

            EditorGUI.BeginChangeCheck();
            worldPos = Handles.FreeMoveHandle (worldPos, Quaternion.identity, HandleUtility.GetHandleSize (worldPos) * 0.1f, Vector3.zero, Handles.RectangleCap);
            if (EditorGUI.EndChangeCheck ())
            {
                localPos = Utility.WorldToLocal (worldPos, state.transform);
                uvPos = Utility.LocalToUV (localPos, state.sprite, false);
                m_Size = Utility.UVToPixel (uvPos, state.sprite, false);
                m_Size = new IntVector2((int)Mathf.Clamp(m_Size.x, k_MinSize, k_MaxSize), (int)Mathf.Clamp(m_Size.y, k_MinSize, k_MaxSize));
                state.Repaint ();
            }
        }
Example #16
0
        public static IntVector2 UVToPixel(Vector2 uv, Sprite sprite, bool clamp)
        {
            if (sprite == null)
                return default(IntVector2);

            Vector2 result = new Vector2(uv.x, uv.y);

            if (clamp)
                result = new Vector2(
                    Mathf.Max (Mathf.Min (result.x, 1.0f - k_UVEpsilon), 0f),
                    Mathf.Max (Mathf.Min (result.y, 1.0f - k_UVEpsilon), 0f)
                    );

            IntVector2 resultInt = new IntVector2(result.x * sprite.texture.width, result.y * sprite.texture.height);

            return resultInt;
        }
Example #17
0
 public static Vector2 PixelToScreen(IntVector2 pixelPosition, Transform transform, Sprite sprite, bool clamp)
 {
     Vector2 uvPosition = Utility.PixelToUV (pixelPosition, sprite, clamp);
     Vector3 localPosition = Utility.UVToLocal (uvPosition, sprite, clamp);
     Vector2 screenPosition = Utility.LocalToScreen (localPosition, transform);
     return screenPosition;
 }
Example #18
0
 public static Vector2 PixelToScreen(IntVector2 pixelPosition, Transform transform, Sprite sprite)
 {
     return PixelToScreen (pixelPosition, transform, sprite, true);
 }
Example #19
0
 public Brush(Texture2D texture, Rect area)
 {
     m_Size = new IntVector2(area.width, area.height);
     m_Colors = texture.GetPixels ((int)area.xMin, (int)area.yMin, m_Size.x, m_Size.y);
 }