Here is a somewhat bastardised TronTrail.js code which allows me to draw on my surfaces when the mouse button is down and as long as they have a collider called “collider” on them.
Would anyone be able to point me in the right direction, so that when the mouse button is lifted and then pressed again, it results in a new drawing line. Currently it connects the last point to the new point.
Thanks
/*
Generates a trail that is always facing upwards using the scriptable mesh interface.
vertex colors and uv's are generated similar to the builtin Trail Renderer.
To use it
1. create an empty game object
2. attach this script and a MeshRenderer
3. Then assign a particle material to the mesh renderer
*/
var height = 2.0;//controls pen thickness
var alwaysUp = false;//flat line in which direction (for me it's left)
var minDistance = 1;//rough vs. smooth line
class TronTrailSection
{
var point : Vector3;
var upDir : Vector3;
}
private var sections = new Array();
function LateUpdate () {
if (Input.GetKey(KeyCode.LeftAlt))
{
}
else
{
if (Input.GetMouseButton (0)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
Physics.Raycast (ray, hit);
if(hit.collider.gameObject.tag.Contains("collider")) {
var position = hit.point + Vector3(0, 2, 0);
// Add a new trail section
if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
{
var section = TronTrailSection ();
section.point = position;
if (alwaysUp)
section.upDir = Vector3.left;
else
section.upDir = transform.TransformDirection(Vector3.up);
sections.Unshift(section);
}
// Rebuild the mesh
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();
// We need at least 2 sections to create the line
if (sections.length < 2)
return;
var vertices = new Vector3[sections.length * 2];
var colors = new Color[sections.length * 2];
var uv = new Vector2[sections.length * 2];
var previousSection : TronTrailSection = sections[0];
var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons
var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex
for (var i=0;i<sections.length;i++)
{
previousSection = currentSection;
currentSection = sections[i];
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + upDir * height);
}
// Generate triangles indices
var triangles = new int[(sections.length - 1) * 2 * 3];
for (i=0;i<triangles.length / 6;i++)
{
triangles[i * 6 + 0] = i * 2;
triangles[i * 6 + 1] = i * 2 + 1;
triangles[i * 6 + 2] = i * 2 + 2;
triangles[i * 6 + 3] = i * 2 + 2;
triangles[i * 6 + 4] = i * 2 + 1;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
}
}
}
}
@script RequireComponent (MeshFilter)
i havent closely studied your code, but it seems to me that you are putting all the sections into the same array, which obviously creates a single line. one way would be to make a new array for each line you want to draw, but you could also add “seperator” sections to the same array, which mark the end of the current line, so its not being connected to the next section.
Oh… I was thinking of doing it with instantiate. So while mouse button is down I draw a line, when mouse button is up stop the line, when mouse button down again instantiate new line etc…
Not sure how to split an array. Any pointer?
Thanks
The instantiation seems to be working. But the line continues to be drawn. So for example; I draw a line, then on second click draw a circle. The circle is drawn, plus the line continues the drawing and a circle is added to it. If I then draw a triangle on third click. I will get line+circle+triangle, circle+triangle, triangle.
So the script doesn’t end the drawing when the mouse button is no longer down. Pleas help.
I managed to stop the line from continuing to draw by setting a maxDistance variable.
// Add a new trail section
if (sections.length == 0 || (sections[0].point - position).sqrMagnitude < maxDistance)
But this doesn’t seem to be very reliable and clearly depends on the Update function. If I draw slowly, the line updates, but if I draw quickly, the line stops abruptly.
So back to the drawing board, if you excuse the pun…
Any ideas would be really appreciated. Thanks
I have decided to revisit this thread because drawing with Tron Trails is just not cutting it. The problem is the UpDirection. I need the trail to lie on the surface, no matter which way the surface is facing, up, down, left, right, back, forward. I have Vectrocity, so was wondering if instead of creating a procedural mesh like in Tron Trails, I can create vertice points and connect those with vector lines. Is this possible? Seems to me that this should be easier and faster. Thus drawing lines on a 3d object. Any Ideas? Suggestions?
Thanks
Current working code for drawing with tron trails.
var height = 2.0;//controls pen thickness
var minDistance = 2;//rough vs. smooth line
var maxDistance = 100;
class TronTrailSection
{
var point : Vector3;
var upDir : Vector3;
}
private var sections = new Array();
function Update () {
if (Input.GetKey(KeyCode.LeftAlt))
{
}
else
{
if (Input.GetMouseButton (0)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
Physics.Raycast (ray, hit);
if(hit.collider.gameObject.tag.Contains("collider")) {
var position = hit.point + (hit.normal * 0.1);
// Add a new trail section
if (sections.length == 0 || (sections[0].point - position).sqrMagnitude < minDistance)
{
var section = TronTrailSection ();
section.point = position;
section.upDir = hit.normal;
sections.Unshift(section);
}
// Rebuild the mesh
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();
// We need at least 2 sections to create the line
if (sections.length < 2)
return;
var vertices = new Vector3[sections.length * 2];
var colors = new Color[sections.length * 2];
var uv = new Vector2[sections.length * 2];
var previousSection : TronTrailSection = sections[0];
var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons
var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex
for (var i=0;i<sections.length;i++)
{
previousSection = currentSection;
currentSection = sections[i];
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 2 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 2 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point + Vector3.left * height);
}
// Generate triangles indices
var triangles = new int[(sections.length - 1) * 2 * 3];
for (i=0;i<triangles.length / 6;i++)
{
triangles[i * 6 + 0] = i * 2;
triangles[i * 6 + 1] = i * 2 + 1;
triangles[i * 6 + 2] = i * 2 + 2;
triangles[i * 6 + 3] = i * 2 + 2;
triangles[i * 6 + 4] = i * 2 + 1;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
}
}
}
}@script RequireComponent (MeshFilter)