[SOLVED]LineRenderer driving me batty

This script is supposed to draw lines that match up with the vertex positions of a separate primitive object. I’ve commented out the TextAsset that it reads the vertex positions from, and pasted the text from that into a variable to make testing easier for others.

This script is just attached to an empty GameObject for testing purposes right now.

Here’s the whole script:

//var LineList: TextAsset;

function Start(){

//this normally refers to the text asset commented out above, but I pasted the string here to make testing easier
var inString = "37|-0.5 0.0078125 -1 0.5 0.0078125 -1|-0.5 0.0078125 -1 -0.5 0.0078125 0|-0.5 0.0078125 -1 -0.5 0.0078125 -2|-0.5 0.0078125 -1 -0.75 0.0078125 -1.25|-0.5 0.0078125 -1 -0.75 0.0078125 -0.75|-0.5 0.0078125 -1 0.5 0.0078125 0|-0.5 0.0078125 -1 0.5 0.0078125 -2|-0.5 0.0078125 -1 0.75 0.0078125 -0.75|-0.5 0.0078125 -1 0.75 0.0078125 -1.25|-0.5 0.0078125 -1 -0.5 0.0078125 0|-0.5 0.0078125 -1 0.5 0.0078125 1|-0.5 0.0078125 -1 1.5 0.0078125 0|-0.5 0.0078125 -1 0.75 0.0078125 -0.25|-0.5 0.0078125 -1 -0.5 0.0078125 1|-0.5 0.0078125 -1 -1.5 0.0078125 0|-0.5 0.0078125 -1 -0.75 0.0078125 -0.25|-0.5 0.0078125 -1 -0.5 0.0078125 1|-0.5 0.0078125 -1 0.5 0.0078125 2|-0.5 0.0078125 -1 1.5 0.0078125 1|-0.5 0.0078125 -1 0.75 0.0078125 1.25|-0.5 0.0078125 -1 -0.5 0.0078125 2|-0.5 0.0078125 -1 -1.5 0.0078125 1|-0.5 0.0078125 -1 -0.75 0.0078125 1.25|-0.5 0.0078125 -1 -0.5 0.0078125 2|-0.5 0.0078125 -1 0.75 0.0078125 1.75|-0.5 0.0078125 -1 -0.75 0.0078125 1.75|-0.5 0.0078125 -1 0.5 0.0078125 -2|-0.5 0.0078125 -1 -0.75 0.0078125 -1.75|-0.5 0.0078125 -1 0.75 0.0078125 -1.75|-0.5 0.0078125 -1 1.5 0.0078125 1|-0.5 0.0078125 -1 -1.5 0.0078125 1|-0.5 0.0078125 -1 -0.75 0.0078125 -1.25|-0.5 0.0078125 -1 -0.75 0.0078125 -0.25|-0.5 0.0078125 -1 0.75 0.0078125 -0.25|-0.5 0.0078125 -1 0.75 0.0078125 -1.25|-0.5 0.0078125 -1 -0.75 0.0078125 1.75|-0.5 0.0078125 -1 0.75 0.0078125 1.75";

//split inString up
var txtLines = inString.Split("|"[0]);

//first token is the number of lines to read in data for (37 in this case)
var lineCount: int = parseInt(txtLines[0]);

var i: int = 0;

//loop through all 37 lines
for(i=1;i<=lineCount-1;i++){

	//create a new child object for each line vertex pair, then add a line renderer component
	var fl = new GameObject();
	fl.transform.parent = transform;
	var lr = fl.AddComponent(LineRenderer);
	
	//set up the line renderer
	lr.useWorldSpace = false;
	lr.SetWidth(0.01, 0.01);
	
	//each line has a start and end point only
	lr.SetVertexCount(2);

	//get xyz/xyz coordinate tokens for the line start/end points
	var strJunk = txtLines[i].Split(" "[0]);

	//convert to vector3
	var startPos: Vector3 = Vector3(parseFloat(strJunk[0]), parseFloat(strJunk[1]), parseFloat(strJunk[2]));
	var endPos: Vector3 = Vector3(parseFloat(strJunk[3]), parseFloat(strJunk[4]), parseFloat(strJunk[5]));

    //assign positional values to the line renderer
	lr.SetPosition(0, startPos);
	lr.SetPosition(1, endPos);

}

//done...

}

I’m pretty sure that I’m overlooking something stupid somewhere in the code, but I can’t put my finger on it. I’ve attached a screenshot of the expected output. What I’m actually seeing in Unity is an irregular radial sunburst pattern instead.

220520--7999--$expected_output_169.gif

The startPos for every line is exactly the same (-0.5 0.0078125 -1). So I’d say the problem is probably the data, rather than the code. (Although the code will fail in locales where the use of “,” and “.” is inverted.)

–Eric

::facepalm::

Looks like the problem is definitely somewhere between my chair and my keyboard, then. :sweat_smile:

I’ll take a look in my model parser and find out why it’s inserting the same value for startPos for every line. I know the original raw input file doesn’t have the same starting coordinates for every line, so I must’ve made a mistake somewhere in the model parser. (The script in the first post reads the output from that model parser.)

Thanks for pointing out the problem!

Also, thanks for pointing out the localization problem, as that hadn’t occurred to me. I’m not sure how I would fix that, though…(I’m assuming you mean parseFloat will fail in those locales when it encounters floats that are not correctly formatted for these locales.)

One way that should work is to use

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

as the first line.

–Eric

Success! The problem was pretty much exactly what you said it was. The file parser I wrote in Visual Studio was incorrectly tokenizing the original 3D model file–Val() apparently returns zero if a string character precedes a numeric character, and I’d forgotten to strip out the “V(” that preceded the first vertex index. So instead of reading the “2” in “V(2 3)”, it was reading it as “0 3”. Repeat that for every line, and you end up with that weird radial sunburst figure I was complaining about earlier. Moral of this story: drink lots of coffee. :lol:

Thanks for helping me fix the issue! I really appreciate it.

Now, the localization thing…inserting that line you mentioned looks to me like it would set up things correctly for the US. In order to make it work globally, would I need to explicitly set the current culture for each targeted destination, like in a switch statement? Or does that line basically just nudge the code, wink, and say “Pretend you’re in America before crunching all these numbers that are coming up.” regardless of the user’s locale?

220821--8011--$success_207.gif

Yeah, pretty much. Glad you got it working!

–Eric

Most excellent. I’ll make sure that line is present in every script that reads floats from a text file/text asset.

Thanks again!