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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by pencilking2002 · Oct 27, 2013 at 05:51 PM · drawinglinesvectrosity

Don't delete discrete lines in Vectrosity

I'm looking to use Vectrosity plugin to draw discrete lines(on mousedown). There's a Demo that comes with the plugin that does this very well, however every time you draw a new line, the old one gets deleted, or 'redrawn'. How can I keep the the old lines that were drawn? and then create a brand new line on mousedown? Here's my current code:

 // The DrawLinesTouch script adapted to work with mouse input
 import Vectrosity;
 
     var lineMaterial : Material;
     var maxPoints = 1000;
     var maxLineWidth = 15.0; //maximum line width
     var minLineWidth = 4.0; //minimum line width
     var minPixelMove = 5;    // Must move at least this many pixels per sample for a new segment to be recorded
     var useEndCap = false;
     var capTex : Texture2D;
     var capMaterial : Material;
     
     private var linePoints : Vector2[];
     private var line : VectorLine;
     private var lineIndex = 0;
     private var previousPosition : Vector2;
     private var sqrMinPixelMove : int;
     private var canDraw = false;
     
     function Start () {
         if (useEndCap) {
             VectorLine.SetEndCap ("cap", EndCap.Mirror, capMaterial, capTex);
             lineMaterial = capMaterial;
         }
         
         linePoints = new Vector2[maxPoints];
         line = new VectorLine("DrawnLine", linePoints, lineMaterial, maxLineWidth, LineType.Continuous, Joins.Weld);
         if (useEndCap) {
             line.endCap = "cap";    
         }
         
         sqrMinPixelMove = minPixelMove*minPixelMove;
     }
     
     function Update () {
         var mousePos = Input.mousePosition;
         if (Input.GetMouseButtonDown(0)) {
             line.ZeroPoints();
             line.minDrawIndex = 0;
             line.Draw();
             previousPosition = linePoints[0] = mousePos;
             lineIndex = 0;
             canDraw = true;
             line.lineWidth = maxLineWidth; //reset line width on mouse press
             
         }
         else if (Input.GetMouseButton(0) && (mousePos - previousPosition).sqrMagnitude > sqrMinPixelMove && canDraw) {
             
             previousPosition = linePoints[++lineIndex] = mousePos;
             line.minDrawIndex = lineIndex-1;
             line.maxDrawIndex = lineIndex;
             if (useEndCap) line.drawEnd = lineIndex;
             if (lineIndex >= maxPoints-1) canDraw = false;
             line.Draw();
             if(line.lineWidth > minLineWidth) line.lineWidth--; //decrease line width unless we hit the minimum
         }
     }

The code is slightly modified to decrease line width as a stroke is made but its mostly the same as the DrawLinesTouch demo.

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
0
Best Answer

Answer by pencilking2002 · Oct 28, 2013 at 03:46 AM

Ok I got this to work. My code is attached to the main camera and augments the DrawLinesTouch example that comes with Vectrosity(I'm using the mouse). What it does is it kind of makes a drawing app(but the number of strokes you can do is limited). Basically what I am doing is caching VectorLine Objects into an array on Start(). In Update, I used mostly the same code as the demo but added line width changing, limiting the amount of strokes and of course now the lines that get drawn previously do not get deleted. Here's my final code:

 using UnityEngine;
 using System.Collections;
 using Vectrosity;
 
 public class Vectors : MonoBehaviour {
     
     //public Material lineMaterial;
     
     public int numOfLines = 5;
     public float minLineWidth = 2f;
     public float maxLineWidth = 15f;
     
     private int maxPoints = 1000; //maximum points a line can have
     private int lineCounter = 0; //incrementor that represents the current stroke the user is drawing
     private VectorLine[] linesArray;
     private Vector2[] pointsArray;
     private Vector2 previousPosition;
     
     private int minPixelMove = 5; // Must move at least this many pixels per sample for a new segment to be recorded
     private int sqrMinPixelMove;
     private bool canDraw = false; //var to toggle drawing
     private Vector2 mousePos;
     private int lineIndex = 0; //what is this?
     private VectorLine line; //shortcut for the VectorLine line inside update functions
     
     void Start () {
         
         linesArray = new VectorLine[numOfLines]; //set array to have numOfLines as a maximum amount of lines a player can draw
         pointsArray = new Vector2[maxPoints]; //set up an array of vectro2 points to use for the lines
         
         for(int i=0; i < numOfLines;i++){
             linesArray[i] = new VectorLine("Stroke", pointsArray, null, maxLineWidth, LineType.Continuous, Joins.Weld);
         }
         
         sqrMinPixelMove = minPixelMove*minPixelMove; //not sure what this does
     }
     
     
     // Update is called once per frame
     void Update () {
         mousePos = Input.mousePosition;
         if ( Input.GetMouseButtonDown(0) && lineCounter < numOfLines) {
             StartLine ();
             
         }  
         
         //Checks if the mouse is held down and mouse moved more than a certain minimum of pixels and if line counter 
         else if (Input.GetMouseButton(0) &&  (mousePos - previousPosition).sqrMagnitude > sqrMinPixelMove && (canDraw && lineCounter < numOfLines) ) {
             ContinueLine();
         }
         
         else if( Input.GetMouseButtonUp (0) )
             lineCounter++;
      
     }
     
     public void StartLine() {
         line = linesArray[lineCounter];
         line.ZeroPoints(); //sets all points of the current line being drawn to Vector.zero
         line.minDrawIndex = 0; //sets the lowest Vector2 point in the current line to be drawn(updated) 
         
         //draw the current line
         line.Draw();
         
         //set the previous position of the mouse to the first vector2 coords of the points array, but why?
         previousPosition = pointsArray[0] = mousePos;
         canDraw = true;
         lineIndex = 0;
         line.lineWidth = maxLineWidth; //reset line width on mouse press
         print (lineCounter);
     
     }
     
     public void ContinueLine() {
         line = linesArray[lineCounter];
         previousPosition = pointsArray[++lineIndex] = mousePos; //set the point in the line as its being drawn
              
         line.minDrawIndex = lineIndex - 1; //set the least most point to be redrawn to be the next one below
         line.maxDrawIndex = lineIndex; //only redraw the current point
         
         if (lineIndex >= maxPoints) canDraw = false; //if maximum points have been exceeded, end the line
         
         if(line.lineWidth > minLineWidth) line.lineWidth--; //decrease linewidth
         
         line.Draw ();
         
     }
         
     
     
 }
Comment
Add comment · Show 1 · 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 diggerjohn · May 13, 2014 at 07:02 PM 0
Share

Thank you very much. This is very helpful.

avatar image
0

Answer by Eric5h5 · Oct 28, 2013 at 02:54 AM

Basically you'd move the VectorLine initialization from Start to the GetMouseButtonDown code, so you'd get a new line with each click, and the old lines would remain.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D ellipse with Vectrosity 1 Answer

Need the user to be able to draw lines on the screen. 1 Answer

Vectrosity lines being distorted when rotated 0 Answers

Draw lines with Vectrosity in the direction transform.forward 1 Answer

Draw 2D polygon 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