• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ben.aten · May 01, 2015 at 04:10 AM · painting

Unity paint - Arongranberg

Hi, I have wen through unity project done by Arongranberg, its works well with static sized images and not working properly with textures and image rect which drawn based on screen.height and screen.width, whats the value i need to change, please help me out on this.

Please check the lines 47, 48, 91 & 97 in which i made change

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Painter : MonoBehaviour {

 public Texture2D sourceBaseTex;
 private Texture2D baseTex;

 void Start()
 {
     baseTex = (Texture2D)Instantiate(sourceBaseTex);
 }


 private Vector2 dragStart;
 private Vector2 dragEnd;
 public enum Tool
 {
     None,
     Line,
     Brush,
     Eraser,
     Vector
 }
 private int tool2 = 1;
 public Drawing.Samples AntiAlias = Drawing.Samples.Samples4;
 public Tool tool = Tool.Brush;
 public Texture[] toolimgs;
 public Texture2D colorCircle;
 public float lineWidth = 1;
 public float strokeWidth = 1;
 public Color col = Color.white;
 public Color col2 = Color.white;
 public GUISkin gskin;
 public LineTool lineTool = new LineTool();
 public BrushTool brush = new BrushTool();
 public EraserTool eraser = new EraserTool();
 public Stroke stroke = new Stroke();
 public int zoom = 1;
 Drawing.BezierPoint[] BezierPoints;
 void OnGUI()
 {
     GUI.skin = gskin;

     GUILayout.BeginArea(new Rect(5, 5, 100 + Screen.width * zoom, Screen.height * zoom), "", "Box");
     GUILayout.BeginArea(new Rect(0, 0, 100, Screen.height * zoom));
     tool2 = GUILayout.Toolbar(tool2, toolimgs, "Tool");

     //      tool = System.Enum.Parse (Tool,tool2.ToString ());

     // FIXME: Defaults to brush tool, fix enum parse above.
     tool = Tool.Brush;


     GUILayout.Label("Drawing Options");
     GUILayout.Space(10);
     switch (tool)
     {
         case Tool.Line:
             GUILayout.Label("Size " + Mathf.Round(lineTool.width * 10) / 10);
             lineTool.width = GUILayout.HorizontalSlider(lineTool.width, 0, 40);
             col = GUIControls.RGBCircle(col, "", colorCircle);
             break;
         case Tool.Brush:
             GUILayout.Label("Size " + Mathf.Round(brush.width * 10) / 10);
             brush.width = GUILayout.HorizontalSlider(brush.width, 0, 40);
             GUILayout.Label("Hardness " + Mathf.Round(brush.hardness * 10) / 10);
             brush.hardness = GUILayout.HorizontalSlider(brush.hardness, 0.1f, 50);
             col = GUIControls.RGBCircle(col, "", colorCircle);
             break;
         case Tool.Eraser:
             GUILayout.Label("Size " + Mathf.Round(eraser.width * 10) / 10);
             eraser.width = GUILayout.HorizontalSlider(eraser.width, 0, 50);
             GUILayout.Label("Hardness " + Mathf.Round(eraser.hardness * 10) / 10);
             eraser.hardness = GUILayout.HorizontalSlider(eraser.hardness, 1, 50);
             break;
     }

     if (tool == Tool.Line)
     {
         stroke.enabled = GUILayout.Toggle(stroke.enabled, "Stroke");
         GUILayout.Label("Stroke Width " + Mathf.Round(stroke.width * 10) / 10);
         stroke.width = GUILayout.HorizontalSlider(stroke.width, 0, lineWidth);
         GUILayout.Label("Secondary Color");
         col2 = GUIControls.RGBCircle(col2, "", colorCircle);
     }

     GUILayout.EndArea();
     GUI.DrawTexture (new Rect(Screen.width *(1.04f/6.55f), Screen.height *(1.02f/6.3f), Screen.width *(5.165f/6.55f) *zoom, Screen.height *(4.1f/6.3f)*zoom), baseTex);
     GUILayout.EndArea();
 }
 private Vector2 preDrag;
 void Update()
 {
     Rect imgRect = new Rect(Screen.width *(1.04f/6.55f), Screen.height *(1.02f/6.3f), Screen.width *(5.165f/6.55f) *zoom, Screen.height *(4.1f/6.3f)*zoom);
     Vector2 mouse = Input.mousePosition;
     mouse.y = Screen.height - mouse.y;

     if (Input.GetKeyDown("t"))
     {
         test();
     }
     if (Input.GetKeyDown("mouse 0"))
     {

         if (imgRect.Contains(mouse))
         {
             if (tool == Tool.Vector)
             {
                 var m2 = mouse - new Vector2(imgRect.x, imgRect.y);
                 m2.y = imgRect.height - m2.y;
                 var bz = new ArrayList(BezierPoints);
                 bz.Add(new Drawing.BezierPoint(m2, m2 - new Vector2(50, 10), m2 + new Vector2(50, 10)));
                 BezierPoints = (Drawing.BezierPoint[])bz.ToArray();
                 Drawing.DrawBezier(BezierPoints, lineTool.width, col, baseTex);
             }

             dragStart = mouse - new Vector2(imgRect.x, imgRect.y);
             dragStart.y = imgRect.height - dragStart.y;
             dragStart.x = Mathf.Round(dragStart.x / zoom);
             dragStart.y = Mathf.Round(dragStart.y / zoom);
             //LineStart (mouse - Vector2 (imgRect.x,imgRect.y));

             dragEnd = mouse - new Vector2(imgRect.x, imgRect.y);
             dragEnd.x = Mathf.Clamp(dragEnd.x, 0, imgRect.width);
             dragEnd.y = imgRect.height - Mathf.Clamp(dragEnd.y, 0, imgRect.height);
             dragEnd.x = Mathf.Round(dragEnd.x / zoom);
             dragEnd.y = Mathf.Round(dragEnd.y / zoom);
         }
         else
         {
             dragStart = Vector3.zero;
         }

     }
     if (Input.GetKey("mouse 0"))
     {
         if (dragStart == Vector2.zero)
         {
             return;
         }
         dragEnd = mouse - new Vector2(imgRect.x, imgRect.y);
         dragEnd.x = Mathf.Clamp(dragEnd.x, 0, imgRect.width);
         dragEnd.y = imgRect.height - Mathf.Clamp(dragEnd.y, 0, imgRect.height);
         dragEnd.x = Mathf.Round(dragEnd.x / zoom);
         dragEnd.y = Mathf.Round(dragEnd.y / zoom);

         if (tool == Tool.Brush)
         {
             Brush(dragEnd, preDrag);
         }
         if (tool == Tool.Eraser)
         {
             Eraser(dragEnd, preDrag);
         }

     }
     if (Input.GetKeyUp("mouse 0") && dragStart != Vector2.zero)
     {
         if (tool == Tool.Line)
         {
             dragEnd = mouse - new Vector2(imgRect.x, imgRect.y);
             dragEnd.x = Mathf.Clamp(dragEnd.x, 0, imgRect.width);
             dragEnd.y = imgRect.height - Mathf.Clamp(dragEnd.y, 0, imgRect.height);
             dragEnd.x = Mathf.Round(dragEnd.x / zoom);
             dragEnd.y = Mathf.Round(dragEnd.y / zoom);
             Debug.Log("Draw Line");
             Drawing.NumSamples = AntiAlias;
             if (stroke.enabled)
             {
                 baseTex = Drawing.DrawLine(dragStart, dragEnd, lineTool.width, col, baseTex, true, col2, stroke.width);
             }
             else
             {
                 baseTex = Drawing.DrawLine(dragStart, dragEnd, lineTool.width, col, baseTex);
             }
         }
         dragStart = Vector2.zero;
         dragEnd = Vector2.zero;
     }
     preDrag = dragEnd;
 }

 void Brush(Vector2 p1, Vector2 p2)
 {
     Drawing.NumSamples = AntiAlias;
     if (p2 == Vector2.zero)
     {
         p2 = p1;
     }
     Drawing.PaintLine(p1, p2, brush.width, col, brush.hardness, baseTex);
     baseTex.Apply();
 }

 void Eraser(Vector2 p1, Vector2 p2)
 {
     Drawing.NumSamples = AntiAlias;
     if (p2 == Vector2.zero)
     {
         p2 = p1;
     }
     Drawing.PaintLine(p1, p2, eraser.width, Color.white, eraser.hardness, baseTex);
     baseTex.Apply();
 }

 void test()
 {
     float startTime = Time.realtimeSinceStartup;
     var w = 100;
     var h = 100;
     var p1 = new Drawing.BezierPoint(new Vector2(10, 0), new Vector2(5, 20), new Vector2(20, 0));
     var p2 = new Drawing.BezierPoint(new Vector2(50, 10), new Vector2(40, 20), new Vector2(60, -10));
     var c = new Drawing.BezierCurve(p1.main, p1.control2, p2.control1, p2.main);
     p1.curve2 = c;
     p2.curve1 = c;
     Vector2 elapsedTime = new Vector2((Time.realtimeSinceStartup - startTime) * 10, 0);
     float startTime2 = Time.realtimeSinceStartup;
     for (var i = 0; i < w * h; i++)
     {
         Mathfx.IsNearBezier(new Vector2(Random.value * 80, Random.value * 30), p1, p2, 10);
     }

     Vector2 elapsedTime2 = new Vector2((Time.realtimeSinceStartup - startTime2) * 10, 0);
     Debug.Log("Drawing took " + elapsedTime.ToString() + "  " + elapsedTime2.ToString());

 }

 public class LineTool
 {
     public float width = 1;
 }
 public class EraserTool
 {
     public float width = 1;
     public float hardness = 1;
 }
 public class BrushTool
 {
     public float width = 1;
     public float hardness = 0;
     public float spacing = 10;
 }
 public class Stroke
 {
     public bool enabled = false;
     public float width = 1;
 }

}

Thanks, ben

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How are trees and detail meshes placed on the terrain? 0 Answers

Is there a way to paint a texture onto a mesh like in UE4? 1 Answer

Multiplayer drawing game 0 Answers

terrain editing help 0 Answers

painting program not drawing at cursor point on texture 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges