Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by erwin · Apr 25, 2012 at 06:59 AM · texturetexture2dlinedrawing

create line on a texture

hi there, my code to draw a line on a Texture2D is poor :\ do you have some hints ?

     void drawLine(float x1, float y1, float x2, float y2){
     

     float x,y;
     
     float dy = y2-y1;
     float dx = x2-x1;
     float m = dy/dx;
     
     float dy_inc = -1;
     if(  dy < 0 ) dy = 1;
     float dx_inc = 1;
     if(  dx < 0 ) dx = -1;
     
     
     if( Mathf.Abs(dy) > Mathf.Abs(dx) ){
         
         for( y = y2; y < y1; y += dy_inc ){
             x = x1 + ( y - y1 ) * m;
             texture.SetPixel( (int)(x ) , (int)(y ), Color.clear );
         }     
         
     }else{
         for( x = x1; x < x2; x +=  dx_inc  ) {
                 y = y1 + ( x - x1 ) * m;
                 texture.SetPixel( (int)(x ) , (int)(y ), Color.clear );
             }
     }  
 }    

thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Syndias · Feb 06, 2016 at 10:35 AM

In case there is anyone still looking for a script that does this kind of thing:

 public static void DrawLine(this Texture2D tex, Vector2 p1, Vector2 p2, Color col)
 {
     Vector2 t = p1;
     float frac = 1/Mathf.Sqrt (Mathf.Pow (p2.x - p1.x, 2) + Mathf.Pow (p2.y - p1.y, 2));
     float ctr = 0;
     
     while ((int)t.x != (int)p2.x || (int)t.y != (int)p2.y) {
         t = Vector2.Lerp(p1, p2, ctr);
         ctr += frac;
         tex.SetPixel((int)t.x, (int)t.y, col);
     }
 }

use it like this: (don't forget to call .apply())

 public class LineDrawer : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         Texture2D txr = new Texture2D (512, 512);
         txr.DrawLine (new Vector2 (0, 0), new Vector2 (512, 256), Color.red);
         txr.Apply ();
         
         byte[] bytes = txr.EncodeToPNG();
         File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
     }
 }


Comment
Add comment · Share
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
avatar image
1

Answer by JamesT · Feb 20, 2013 at 09:14 PM

I tested your code and it works with a few changes...

  1. Make sure that the texture you are modifying was originally imported with Read/Write enabled (under advanced settings in the Inspector).

  2. Be sure to call texture.Apply().

Here's an example showing the modifications I made...

//----------------------------------------------------------- using UnityEngine;

public class DrawLine : MonoBehaviour { public Texture2D MyTexture;

 // Use this for initialization
 void Start () 
 {

     Draw(0, 0, 100, 100);
     renderer.material.mainTexture = MyTexture;
 }
 
 // Update is called once per frame
 void Update () 
 {
 
 }

 void Draw(float x1, float y1, float x2, float y2)
 {
     float x,y;
     float dy = y2-y1;    
     float dx = x2-x1;    
     float m = dy/dx; 
     float dy_inc = -1;    
     
     if(  dy < 0 ) 
         dy = 1;    
     
     float dx_inc = 1;    
     if(  dx < 0 ) 
         dx = -1; 
         
     if( Mathf.Abs(dy) > Mathf.Abs(dx) )
     {
         for( y = y2; y < y1; y += dy_inc )
         {                
             x = x1 + ( y - y1 ) * m;
             Debug.Log("Setting Pixel at: x=" + x + ", y=" + y);
             MyTexture.SetPixel((int)(x), (int)(y), Color.black);       
         }
     }
     else
     {
         for( x = x1; x < x2; x +=  dx_inc  ) 
         {
             y = y1 + ( x - x1 ) * m;
             Debug.Log("Setting Pixel at: x=" + x + ", y=" + y);
             MyTexture.SetPixel((int)(x), (int)(y), Color.black);           
         }
     }
     MyTexture.Apply();
 }

   

}

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing a solid circle onto texture 2 Answers

Drawing a Texture on top of another Texture 2 Answers

Positioning texture in a sphere 0 Answers

Select part of the texture in GUI.DrawTexture 2 Answers

Draw a line in opposite direction of mouse drag 1 Answer


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