/// <summary> /// Perfoms handeling of the terrain. Must be running asyncronously. /// </summary> /// <param name="inpData">Terrain to handle data, packed from MeshDataContainer instance.</param> void AsyncTerrainMethod(object inpData) { if (agressiveUseMultithreading) { Interlocked.Increment(ref aliveHandelingTasksCount); } TerrainDataContainer tDCInst = (TerrainDataContainer)inpData; int curTaskId = uniqeHandlingTaskID; Interlocked.Increment(ref uniqeHandlingTaskID); AsyncTaskDataContainer aTDCInstance = new AsyncTaskDataContainer { gameObjectName = tDCInst.gameObjectName, threadName = Thread.CurrentThread.Name, uniqeTaskID = curTaskId, trisCount = (tDCInst.xEnd - tDCInst.xStart) * (tDCInst.zEnd - tDCInst.zStart) * 2, verticesCount = tDCInst.hMArray.Length }; aliveTasksDict.TryAdd(curTaskId, aTDCInstance); try { CancellationToken localCancToken = (CancellationToken)tDCInst.CTInstance; Vector3 p0, p1, p2, p3; int step = 1; int hMWidth = tDCInst.hMWidth; Vector3 terrPos = tDCInst.terrainPos; float hZ = tDCInst.hZ; float hX = tDCInst.hX; float[,] hMArray = tDCInst.hMArray; float maxH = tDCInst.maxH; for (var x = tDCInst.xStart; x < tDCInst.xEnd; x += step) { if (broadcastCancToken.IsCancellationRequested || localCancToken.IsCancellationRequested) { return; } for (var z = tDCInst.zStart; z < tDCInst.zEnd; z += step) { p0 = new Vector3(((float)(z - step) / hMWidth) * hZ + terrPos.x, (hMArray[x - step, z - step] * maxH) + terrPos.y, ((float)(x - step) / hMWidth) * hX + terrPos.z); p1 = new Vector3(((float)(z - step) / hMWidth) * hZ + terrPos.x, (hMArray[x, z - step] * maxH) + terrPos.y, ((float)x / hMWidth) * hX + terrPos.z); p2 = new Vector3(((float)z / hMWidth) * hZ + terrPos.x, (hMArray[x - step, z] * maxH) + terrPos.y, ((float)(x - step) / hMWidth) * hX + terrPos.z); p3 = new Vector3(((float)z / hMWidth) * hZ + terrPos.x, (hMArray[x, z] * maxH) + terrPos.y, ((float)x / hMWidth) * hX + terrPos.z); OccupyCellsForATriangle(p0, p1, p2, tDCInst.instanceID); OccupyCellsForATriangle(p1, p2, p3, tDCInst.instanceID); } } aliveTasksDict.TryRemove(curTaskId, out aTDCInstance); Interlocked.Decrement(ref aliveHandelingTasksCount); if (!isPrimaryProcessingCompleted && processedTrisCount >= totalTrisCount && aliveHandelingTasksCount == 0) { NotifyRediness(); } } catch (Exception ex) { UnityEngine.Debug.Log(ex.Message + " " + ex.StackTrace); } }
/// <summary> /// Perfoms handeling of a mesh. Must be running asyncronously. /// </summary> /// <param name="inpData">Mesh to handle data, packed from MeshDataContainer instance. </param> void AsyncMeshMethod(object inpData) { try { if (agressiveUseMultithreading) { Interlocked.Increment(ref aliveHandelingTasksCount); } MeshDataContainer mDCInstance = (MeshDataContainer)inpData; int curTaskId = uniqeHandlingTaskID; Interlocked.Increment(ref uniqeHandlingTaskID); AsyncTaskDataContainer aTDCInstance = new AsyncTaskDataContainer { gameObjectName = mDCInstance.gameObjectName, threadName = Thread.CurrentThread.Name, uniqeTaskID = curTaskId, trisCount = mDCInstance.triangles.Length / 3, verticesCount = mDCInstance.vertices.Length }; aliveTasksDict.TryAdd(curTaskId, aTDCInstance); int[] triangles = null; int p0i = 0; int p1i = 0; int p2i = 0; Vector3[] vertices = mDCInstance.vertices; triangles = mDCInstance.triangles; CustomTransform meshCTransf = mDCInstance.transformData; int instanceID = mDCInstance.instanceID; for (var i = 1; i <= mDCInstance.triangles.Length / 3; ++i) { try { if ((broadcastCancToken.IsCancellationRequested) || ((CancellationToken)mDCInstance.CTInstance).IsCancellationRequested) { return; } p0i = triangles[(i - 1) * 3]; p1i = triangles[(i - 1) * 3 + 1]; p2i = triangles[(i - 1) * 3 + 2]; Vector3 p0 = vertices[triangles[(i - 1) * 3]]; Vector3 p1 = vertices[triangles[(i - 1) * 3 + 1]]; Vector3 p2 = vertices[triangles[(i - 1) * 3 + 2]]; OccupyCellsForATriangle( meshCTransf.position + meshCTransf.rotation * (Vector3.Scale(meshCTransf.localScale, p0)), meshCTransf.position + meshCTransf.rotation * (Vector3.Scale(meshCTransf.localScale, p1)), meshCTransf.position + meshCTransf.rotation * (Vector3.Scale(meshCTransf.localScale, p2)), instanceID ); } catch (Exception ex) { if (!isPrimaryProcessingCompleted) { Interlocked.Increment(ref processedTrisCount); } throw (ex); } } aliveTasksDict.TryRemove(curTaskId, out aTDCInstance); Interlocked.Decrement(ref aliveHandelingTasksCount); if (!isPrimaryProcessingCompleted && processedTrisCount >= totalTrisCount && aliveHandelingTasksCount == 0) { NotifyRediness(); } } catch (Exception ex) { UnityEngine.Debug.Log(ex.Message); } }