void Setup(){ lodObjects = new LODObject[objectLODInfos.Length]; for (int i = 0; i < objectLODInfos.Length; i++) { //for each object type lodObjects[i] = new LODObject(); for (int x = 0; x < objectLODInfos[i].meshes.Length; x++) { lodObjects[i].lodElements.Add(new LODElement()); //The worker thread only needs this set up - It will then assign the splintered matrix list } } }
void ThreadedWork() { _threadRunning = true; bool workDone = false; // This pattern lets us interrupt the work at a safe point if neeeded. while (_threadRunning && !workDone) { Thread.Sleep(1); Vector3 pointToRemove = Vector3.zero; bool hasPointToRemove = false; if (removePoints.Count != 0) { pointToRemove = removePoints.Dequeue(); hasPointToRemove = true; } for (int objectIndex = 0; objectIndex < objectDatas.Length; objectIndex++) { //for each object ObjectData thisObject = objectDatas[objectIndex]; bool culled = thisObject.culled; bool hidden = thisObject.hidden; if (thisObject.rendering == false) continue; Vector3 objectPosition = thisObject.position; float xDif = objectPosition.x - camPos.x; float yDif = objectPosition.y - camPos.y; float zDif = objectPosition.z - camPos.z; thisObject.dirX = xDif; thisObject.dirY = yDif; thisObject.dirZ = zDif; float distance = (float)System.Math.Sqrt(xDif * xDif + yDif * yDif + zDif * zDif); float xNorm = xDif / distance; float yNorm = yDif / distance; float zNorm = zDif / distance; thisObject.dirNormX = xNorm; thisObject.dirNormY = yNorm; thisObject.dirNormZ = zNorm; thisObject.distanceFromCamera = distance; int objectLOD = thisObject.currentLOD; int objectID = thisObject.id; int lodAddingTo = -1; if (hasPointToRemove) { float dist = Vector3.Distance(thisObject.position, pointToRemove); if (dist < removerRadius) { thisObject.rendering = false; culled = true; } } if (!culled && !hidden) { LODObject lodObject = lodObjects[objectID]; for (int lodIndex = 0; lodIndex < objectLODInfos[objectID].renderDistances.Length; lodIndex++) { if (distance < objectLODInfos[objectID].renderDistances[lodIndex]) { if (objectLOD != lodIndex) { lodObject.lodElements[lodIndex].AddMatrix(thisObject.matrix, objectIndex); } lodAddingTo = lodIndex; break; } } } else { lodAddingTo = -1; } if (objectLOD != -1) { if (lodAddingTo != objectLOD) lodObjects[objectID].lodElements[objectLOD].RemoveMatrix(objectIndex); } thisObject.currentLOD = lodAddingTo; } // Do Work... } _threadRunning = false; } void ThreadedWorkCulling() { _threadRunning = true; bool workDone = false; // This pattern lets us interrupt the work at a safe point if neeeded. while (_threadRunning && !workDone) { Thread.Sleep(1); for (int objectIndex = 0; objectIndex < objectDatas.Length; objectIndex++) { //for each object ObjectData thisObject = objectDatas[objectIndex]; bool culled = false; if (thisObject.rendering == false) continue; if ((thisObject.dirX * camDir.x) + (thisObject.dirY * camDir.y) + (thisObject.dirZ * camDir.z) < 0) //dot product is less than 0 { culled = true; } thisObject.culled = culled; } // Do Work... } _threadRunning = false; } void ThreadedWorkCulling2() { _threadRunning = true; bool workDone = false; // This pattern lets us interrupt the work at a safe point if neeeded. while (_threadRunning && !workDone) { Thread.Sleep(1); List<ThreadedObstacle> obstacleIndexes = new List<ThreadedObstacle>();