public void SaveAndReturnToken(WorldDataToken token, Action onComplete) { /*SaveToken(token, (savedToken) => { * ReturnToken(savedToken); * onComplete(); * });*/ }
protected override void ThreadFunction() { _worldDataAccess.GetToken(new TokenRequest((int)_position.x - _radius, (int)_position.x + _radius, (int)_position.z + _radius, (int)_position.z - _radius), _persistentDataPath, token => { Debug.Log("LoadTokenJob.ThreadFunction token: " + token); Output = token; }); }
private void LowerEarthTokenRecievedComplete(WorldDataToken token) { for (int x = 0; x < token.Request.width; x++) { for (int y = 0; y < token.Request.height; y++) { token.SetUshort(x, y, (ushort)(token.GetUshort(x, y, UshortDataID.HeightLayerData) - 3), UshortDataID.HeightLayerData); } } // _worldDataAccessService.SaveToken(token, (WorldDataToken returnToken) => { }); }
private void AddWaterTokenRecievedComplete(WorldDataToken token) { for (int x = 0; x < token.Request.width; x++) { for (int y = 0; y < token.Request.height; y++) { token.SetByte(x, y, 200, ByteDataLyerID.WaterLayerData); } } // _worldDataAccessService.SaveToken(token, (WorldDataToken returnToken) => { }); }
private void MakeFlatTokenRecievedComplete(WorldDataToken token) { ushort height = token.GetUshort(token.Request.left, token.Request.top, UshortDataID.HeightLayerData); for (int x = 0; x < token.Request.width; x++) { for (int y = 0; y < token.Request.height; y++) { token.SetUshort(x, y, height, UshortDataID.HeightLayerData); } } // _worldDataAccessService.SaveToken(token, (WorldDataToken returnToken) => { }); }
private Vector3 GetVertice(WorldDataToken token, int x, int y) { return(new Vector3( (token.Request.left + x) * _scale, ((token.GetUshort(x, y, UshortDataID.HeightLayerData) + token.GetUshort(x - 1, y - 1, UshortDataID.HeightLayerData) + token.GetUshort(x, y - 1, UshortDataID.HeightLayerData) + token.GetUshort(x + 1, y - 1, UshortDataID.HeightLayerData) + token.GetUshort(x + 1, y, UshortDataID.HeightLayerData) + token.GetUshort(x + 1, y + 1, UshortDataID.HeightLayerData) + token.GetUshort(x, y + 1, UshortDataID.HeightLayerData) + token.GetUshort(x - 1, y + 1, UshortDataID.HeightLayerData) + token.GetUshort(x - 1, y, UshortDataID.HeightLayerData)) / 9f) * 0.5f, (token.Request.top + y) * _scale)); }
public void SaveToken(WorldDataToken token) { /* List<SaveAreaJob.SaveAreaRequest> saveRequests = new List<SaveAreaJob.SaveAreaRequest>(); * foreach (KeyValuePair<AreaIndex, string> tokenFile in token.Filepaths) * { * if(!savingAreas.Contains(tokenFile.Value)) * { * savingAreas.Add(tokenFile.Value); * saveRequests.Add(new SaveAreaJob.SaveAreaRequest(tokenFile.Key, tokenFile.Value)); * } * } * * SaveAreaJob job = new SaveAreaJob(saveRequests, token.WorldIndex); * job.Start();*/ }
private void SimulateAreaWithRadius(WorldDataToken token, int x, int y) { //test for frame rate byte[,] waterValues = new byte[3, 3]; ushort[,] depthValues = new ushort[3, 3]; for (int i = x - 1; i < x + 1; i++) { for (int j = y - 1; j < y + 1; j++) { int valuesX = i - (x - 1); int valuesY = j - (y - 1); waterValues[valuesX, valuesY] = token.GetByte(i, j, ByteDataLyerID.WaterLayerData); depthValues[valuesX, valuesY] = token.GetUshort(i, j, UshortDataID.HeightLayerData); } } byte baseWaterValue = waterValues[1, 1]; ushort baseDepthValue = depthValues[1, 1]; ApplyWaterValues(waterValues, depthValues, 0, 0); ApplyWaterValues(waterValues, depthValues, 1, 0); ApplyWaterValues(waterValues, depthValues, 2, 0); ApplyWaterValues(waterValues, depthValues, 2, 1); ApplyWaterValues(waterValues, depthValues, 2, 2); ApplyWaterValues(waterValues, depthValues, 1, 2); ApplyWaterValues(waterValues, depthValues, 0, 2); ApplyWaterValues(waterValues, depthValues, 0, 1); for (int i = x - 1; i < x + 1; i++) { for (int j = y - 1; j < y + 1; j++) { int valuesX = i - (x - 1); int valuesY = j - (y - 1); token.SetByte(i, j, waterValues[valuesX, valuesY], ByteDataLyerID.WaterLayerData); } } }
private void SetTriangles(WorldDataToken token, int x, int y, int[] triangles, List <Vector3> vectorTriangles) { //we are making 2 triangles per loop. so offset goes up by 6 each time int triangleOffset = (x * token.Request.height + y) * 6; int verticeX = token.Request.width + 1; int verticeY = token.Request.height + 1; //triangle 1 triangles[triangleOffset] = x * verticeY + y; triangles[1 + triangleOffset] = x * verticeY + y + 1; triangles[2 + triangleOffset] = x * verticeY + y + verticeY; vectorTriangles.Add(new Vector3(triangles[triangleOffset], triangles[1 + triangleOffset], triangles[2 + triangleOffset])); //triangle 2 triangles[3 + triangleOffset] = x * verticeY + y + verticeY; triangles[4 + triangleOffset] = x * verticeY + y + 1; triangles[5 + triangleOffset] = x * verticeY + y + verticeY + 1; vectorTriangles.Add(new Vector3(triangles[3 + triangleOffset], triangles[4 + triangleOffset], triangles[5 + triangleOffset])); }
/* * ((token.GetUshort(x, y, UshortDataID.HeightLayerData) + * token.GetUshort(x - 1, y -1, UshortDataID.HeightLayerData) + * token.GetUshort(x, y -1, UshortDataID.HeightLayerData) + * token.GetUshort(x + 1, y -1, UshortDataID.HeightLayerData) + * token.GetUshort(x + 1, y, UshortDataID.HeightLayerData) + * token.GetUshort(x + 1, y + 1, UshortDataID.HeightLayerData) + * token.GetUshort(x, y + 1, UshortDataID.HeightLayerData) + * token.GetUshort(x - 1, y + 1, UshortDataID.HeightLayerData) + * token.GetUshort(x - 1, y, UshortDataID.HeightLayerData)) / 9f) * 0.5f,*/ private void UpdateSurroundingHights(TerraVector vector, Vector3[] vertices, WorldDataToken token) { UpdateHights(vector, vertices, token); UpdateHights(new TerraVector() { x = vector.x - 1, y = vector.y - 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x, y = vector.y - 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x + 1, y = vector.y - 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x + 1, y = vector.y }, vertices, token); UpdateHights(new TerraVector() { x = vector.x + 1, y = vector.y + 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x, y = vector.y + 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x - 1, y = vector.y + 1 }, vertices, token); UpdateHights(new TerraVector() { x = vector.x - 1, y = vector.y }, vertices, token); }
public void ReturnToken(WorldDataToken token) { return; /* * if (_tokens.Contains(token)) * { * _tokens.Remove(token); * * HashSet<string> _removeCachedAreas = new HashSet<string>(); * * foreach (KeyValuePair<AreaIndex, string> keyValuePair in token.Filepaths) * { * _removeCachedAreas.Add(keyValuePair.Value); * } * * foreach (WorldDataToken cachedToken in _tokens) * { * foreach (KeyValuePair<AreaIndex, string> keyValuePair in cachedToken.Filepaths) * { * if (_removeCachedAreas.Contains(keyValuePair.Value)) * { * _removeCachedAreas.Remove(keyValuePair.Value); * } * } * } * * string[] removeResult = new string[_removeCachedAreas.Count]; * _removeCachedAreas.CopyTo(removeResult); * * foreach (string removeCache in removeResult) * { * _areaCache[removeCache].Destroy(); * _areaCache.Remove(removeCache); * } * }*/ }
private void UpdateHights(TerraVector vector, Vector3[] vertices, WorldDataToken token) { int vertPosition = (vector.x * (token.Request.width + 1)) + vector.y; vertices[vertPosition] = GetVertice(_currentToken, vector.x, vector.y); }
private IEnumerator RenderGround(WorldDataToken token) { _heightTexture = new Texture2D(token.Request.width, token.Request.height); _currentToken = token; MeshCollider collider = null; if (_renderingPlane == null) { _renderingPlane = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Plane)); collider = _renderingPlane.AddComponent <MeshCollider>(); Rigidbody rb = _renderingPlane.AddComponent <Rigidbody>(); rb.isKinematic = true; } else { collider = _renderingPlane.GetComponent <MeshCollider>(); } GameObject plane = _renderingPlane; plane.layer = _geometryLayer; plane.GetComponent <Renderer>().material = _material; MeshFilter meshFilter = plane.GetComponent <MeshFilter>(); _renderMeshFilter = meshFilter; Mesh mesh = meshFilter.sharedMesh; _renderMesh = mesh; collider.sharedMesh = mesh; int verticiesLength = (token.Request.width + 1) * (token.Request.height + 1); Vector3[] vertices = new Vector3[verticiesLength]; Color[] colors = new Color[vertices.Length]; Vector2[] uvs = new Vector2[vertices.Length]; Vector3[] normals = new Vector3[vertices.Length]; float[] grass = new float[vertices.Length]; // Vector2[] triangles = new Vector2[(int)(dimensions.Area * 2)]; //for every point, there is 2 triangles, equaling 6 total vertices int[] triangles = new int[(int)((token.Request.width * token.Request.height) * 6)]; float totalWidth = token.Request.width * _scale; float totalHeight = token.Request.height * _scale; //Create Vertices for (int x = 0; x < token.Request.width + 1; x++) { for (int y = 0; y < token.Request.height + 1; y++) { int position = (x * (token.Request.width + 1)) + y; if (x > 0 && y > 0 && x < token.Request.width && y < token.Request.height) { vertices[position] = GetVertice(token, x, y); } else { vertices[position] = new Vector3((token.Request.left + x) * _scale, token.GetUshort(x, y, UshortDataID.HeightLayerData) * 0.5f, (token.Request.top + y) * _scale); } colors[position] = new Color(0.5f, 0.5f, 0.5f); uvs[position] = new Vector2((float)x / (float)token.Request.width, (float)y / (float)token.Request.height); normals[position] = Vector3.up; grass[position] = 0.5f; string grassId = x + "_" + y; /* if (!_grassLocations.Contains(grassId)) * { * * float scaleFactor = 1-(25.5f - vertices[position].y) / 0.5f; * Vector3 scale = new Vector3(scaleFactor, scaleFactor, scaleFactor); * Vector3 grassPosition = new Vector3(vertices[position].x, (vertices[position].y - 1) + (scaleFactor * 2f), vertices[position].z); * GameObject grassInstance = Instantiate(_grassSprite, grassPosition, _grassSprite.transform.rotation, _grassContainer.transform); * grassInstance.transform.localScale = scale; * _grassLocations.Add(grassId); * }*/ } //yield return 0; } List <Vector3> vectorTriangles = new List <Vector3>(); //Create Triangles for (int x = 0; x < token.Request.width; x++) { for (int y = 0; y < token.Request.height; y++) { // Debug.Log("height "+token.GetUshort(x, y, UshortDataID.HeightLayerData)); //_heightTexture.SetPixel(x, y, new Color(0.5f, 1, 1)); _heightTexture.SetPixel(x, y, new Color(0.5f, (float)token.GetUshort(x, y, UshortDataID.HeightLayerData) / (float)100, 0.5f)); SetTriangles(token, x, y, triangles, vectorTriangles); } } mesh.Clear(); _heightTexture.Apply(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.uv = uvs; mesh.colors = colors; mesh.normals = normals; RedrawCountdown = 100; plane.SetActive(true); // _material.SetInt("_Width", token.Request.width + 1); // _material.SetInt("_Height", token.Request.height + 1); for (int i = 0; i < grass.Length / 1023; i++) { float[] grassInput = new float[1023]; for (int j = 0; j < 1023; j++) { grassInput[j] = grass[i * 1023 + j]; } // _material.SetFloatArray("_Grass" + i, grassInput); } mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.RecalculateTangents(); _dataPlaneRenderer.material.mainTexture = _heightTexture; yield return(0); }
private void OnGetTokenComplete(WorldDataToken token) { StartCoroutine(RenderGround(token)); }
public void GetToken(TokenRequest request, string persistentDataPath, Action <WorldDataToken> onComplete) { int leftArea = request.left / _worldIndex.AreaDimensions; int rightArea = (request.right - 1) / _worldIndex.AreaDimensions; int topArea = request.top / _worldIndex.AreaDimensions; int bottomArea = (request.bottom - 1) / _worldIndex.AreaDimensions; int horizontalAreaCount = (rightArea - leftArea) + 1; int verticalAreaCount = (bottomArea - topArea) + 1; LoadedArea[,] jobsResult = new LoadedArea[horizontalAreaCount, verticalAreaCount]; Dictionary <AreaIndex, string> filepaths = new Dictionary <AreaIndex, string>(); List <LoadedArea> loadedAreas = new List <LoadedArea>(); List <LoadedArea> newLoadedAreaRequests = new List <LoadedArea>(); for (int i = 0; i < horizontalAreaCount; i++) { for (int j = 0; j < verticalAreaCount; j++) { int areaX = i + leftArea; int areaY = j + topArea; string areaKey = String.Join("_", new string[] { areaX.ToString(), areaY.ToString() }); LoadedArea loadedArea = null; lock (_cache) { _cache.TryGetValue(areaKey, out loadedArea); if (loadedArea == null) { LoadAreaJob.AreaRequest loadRequest = new LoadAreaJob.AreaRequest(areaX, areaY); loadedArea = new LoadedArea(loadRequest); newLoadedAreaRequests.Add(loadedArea); _cache.Add(areaKey, loadedArea); } } loadedAreas.Add(loadedArea); } } LoadAreaJob loadAreaJob = null; loadAreaJob = new LoadAreaJob(_worldIndex, _dataConfig, newLoadedAreaRequests, persistentDataPath); loadAreaJob.Start(); loadAreaJob.OnComplete += (job) => { loadedAreas.ForEach((loadedArea) => { jobsResult[loadedArea.Request.areaX - leftArea, loadedArea.Request.areaY - topArea] = loadedArea; }); WorldDataToken token = new WorldDataToken(request, _worldIndex, jobsResult); onComplete(token); }; //wait until all areas loaded /*while (loadedAreas.Find((loadedArea) => loadedArea.Result == null) != null) * { * Thread.Sleep(10); * }*/ //_loadJobPool.Add(loadAreaJob); }