- Home /
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.
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 ();
}
}
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.
Your answer
Follow this Question
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