/** * Looks at the space around the player, and if a required Terrain Tile does * not exist, starts generation. **/ bool fillGaps(Vector2Int c) { bool someScheduled = false; for (int x = c.x - renderDistance; x <= (c.x + renderDistance); x++) { for (int y = c.y - renderDistance; y <= (c.y + renderDistance); y++) { Vector2Int testV = new Vector2Int(x, y); if (!meshes.ContainsKey(testV)) { // Don't do anything if we're too busy if (terrainGenerationsInProgress >= maxTerrainsToSchedule) { return(true); } // Register that we're doing some work terrainControllerChannel.annotate("Schedule new", Color.green); someScheduled = true; // Creating a new TerrainData object will create a new Job // for the Unity Job Scheduler. Vector3 centerPoint = new Vector3((float)testV.x * worldDimensions.x, 0, (float)testV.y * worldDimensions.z); TerrainData data = new TerrainData(centerPoint, armTracks[terrainGenerationsInProgress], this); terrainGenerationsInProgress++; meshes[testV] = data; // Register that we finished our work. terrainControllerChannel.end(); } } } return(someScheduled); }
/** * We periodically call this on every existing TerrainData. * It checks to see if the job completed, and if so, does cleanup and * makes a new Mesh from all the stuff that the job generated for us. * * **/ public bool completeIfNeeded() { bool terrainGenerationCompleted = false; if (!completed && jobHandle.IsCompleted) { completed = true; jobHandle.Complete(); // Apparently this is required. // It is possible that we got deleted before we finished - if so, // don't actually do anything with the GameObject. if (!deleted) { channel.annotate("Completing", Color.blue); Mesh mesh = obj.GetComponent <MeshFilter>().mesh; mesh.vertices = job.vertices.ToArray(); mesh.uv = job.uv.ToArray(); mesh.uv2 = job.uv2.ToArray(); mesh.SetTriangles(job.grassTriangles.ToArray(), 0); mesh.SetTriangles(job.sandTriangles.ToArray(), 1); mesh.SetTriangles(job.waterTriangles.ToArray(), 2); mesh.RecalculateNormals(); channel.end(); terrainGenerationCompleted = true; } // Register the start and stop times of the job track.registerJob(obj.name, Color.red, job.timings[0], job.timings[1]); job.cleanUp(); } return(terrainGenerationCompleted); }