- Home /
MultiThreading Logic Error?
I have a function that isn't making sense with mutithreading, because it is writing results from a different gameobject than called by the function:
function multicore_normals (go: GameObject ){
yield WaitForSeconds(0.2);
//yield WaitForFixedUpdate;
var mesh : Mesh = go.GetComponent(MeshFilter).mesh;
var indicies: int[] = mesh.triangles;
var myvertices : Vector3[] = mesh.vertices;
var mynormals: Vector3[] = mesh.normals;
mynormals = new Vector3[mesh.vertices.length];
Loom.RunAsync(function() {//----=-=-=-=-multicore from unitygems
for(var i :int= 0; i < indicies.Length; i += 3)
{
var v0: Vector3= myvertices[indicies[i]];
var v1 :Vector3= myvertices[indicies[i+1]];
var v2 :Vector3= myvertices[indicies[i+2]];
var normal : Vector3= Vector3.Normalize(Vector3.Cross(v1 - v0, v2 - v0));
mynormals[indicies[i]] += normal;
mynormals[indicies[i+1]] += normal;
mynormals[indicies[i+2]] += normal;
}
for(i = 0; i < myvertices.Length; i++)
{
mynormals[i] = Vector3.Normalize(mynormals[i]);
}
Loom.QueueOnMainThread(function() {//----=-=-=-=-
//mesh.normals = mynormals ;
});});//----=-=-=-=-
yield WaitForFixedUpdate;
mesh.normals = mynormals ;
}
Result, every 1000 or so objects, i find a GameObject that has normals applied to it, that belong to a different GameObject, or that haven't been calculated at all for that object. how can it rewrite the vertices of the object, and then fail to write normals on it, or write normals from a different GameObject of unity?
To call the newnormals function, the code does:
get meshObject, rewrite vertices, newnormals(meshObject)
You have some strange pseude code there... it's not clear on which data which thread is working and when you reassign the arrays to the mesh. How do you synchronise your worker thread with the main thread?
A little bit actual code would help to understand what you're actually doing since your pseudo code doesn't make any sense...
Answer by Bunny83 · Sep 16, 2014 at 04:18 AM
You might want to take a look at the Loom class when it comes to threading.
hey bunny, it's using the loom class from unitygems. i've put the function that makes errors in the original post. the other day it ran for about 2k instances with no errors, i restarted my pc again and saw 5 errors in 400 instances of mesh.
Your answer
Follow this Question
Related Questions
MonoBehaviour.Invoke and Threading 2 Answers
multithreaded rendering on android breaks nativetexture update in plugin 0 Answers
EventHandler from external DLL not firing in MonoBehaviour C# script 0 Answers
> 0.9f progress of SceneManager.LoadSceneAsync isn't 0 Answers
How to stop waiting on Serial Output 2 Answers