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)); }
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) => { }); }
public void ChangeHeight(TerraVector position, int delta) { if (_currentToken == null) { throw new InvalidOperationException(string.Format("There is no current {0}", typeof(WorldDataToken))); } else { Mesh mesh = _renderMeshFilter.sharedMesh; List <Vector3> vectorTriangles = new List <Vector3>(); var triangles = mesh.triangles; Vector3[] vertices = mesh.vertices; Color[] colors = mesh.colors; Vector2[] uvs = mesh.uv; Vector3[] normals = mesh.normals; ushort currentValue = _currentToken.GetUshort(position.x - _currentToken.Request.left, position.y - _currentToken.Request.top, UshortDataID.HeightLayerData); TerraVector localVector = _currentToken.ToLocal(position); int vertPosition = (localVector.x * (_currentToken.Request.width + 1)) + localVector.y; _currentToken.SetUshort(localVector.x, localVector.y, (ushort)((int)currentValue + delta), UshortDataID.HeightLayerData); //vertices[vertPosition] = GetVertice(_currentToken, localVector.x, localVector.y); UpdateSurroundingHights(localVector, vertices, _currentToken); mesh.vertices = vertices; mesh.RecalculateNormals(); mesh.RecalculateBounds(); mesh.RecalculateTangents(); /*vertices[localVector.x * localVector.y] = * GetVertice(_currentToken, localVector.x, localVector.y); * _renderMeshFilter.sharedMesh.vertices = _renderMesh.vertices; * * * * //SetTriangles(_currentToken, localVector.x, localVector.y, triangles, vectorTriangles); * * mesh.Clear(); * * mesh.vertices = vertices; * mesh.triangles = triangles; * mesh.uv = uvs; * mesh.colors = colors; * mesh.normals = normals; * * _renderMeshFilter.sharedMesh.RecalculateNormals(); * _renderMeshFilter.sharedMesh.RecalculateBounds(); * _renderMeshFilter.sharedMesh.RecalculateTangents();*/ //RedrawCountdown = 1; // RenderGround(_currentToken); } }
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 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 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); }