/// <summary> /// Get the value of the Generator in world space. /// </summary> /// <param name="x">X coordinate in resolution</param> /// <param name="y">Y coordinate in resolution</param> /// <param name="position">Position in Terra grid units</param> /// <param name="resolution">Resolution of this Tile being sampled</param> /// <param name="spread">What to divide the x & y coordinates by before sampling</param> /// <param name="length">Length of a Tile</param> public float GetValue(int x, int y, GridPosition position, int resolution, float spread, int length) { Vector2 local = TileMesh.PositionToLocal(x, y, resolution); Vector2 world = TileMesh.LocalToWorld(position, local.x, local.y, length); return(_generator.GetValue(world.x / spread, world.y / spread, 0f)); }
/// <summary> /// Get the value of the Generator in world space. /// </summary> /// <param name="i">Index of X/Y location in </param> /// <param name="position">Position in Terra grid units</param> /// <param name="resolution">Resolution of this Tile being sampled</param> /// <param name="spread">What to divide the x & y coordinates by before sampling</param> /// <param name="length">Length of a Tile</param> public float GetValue(int i, GridPosition position, int resolution, float spread, int length) { int x = i % resolution; int y = i / resolution; Vector2 local = TileMesh.PositionToLocal(x, y, resolution); Vector2 world = TileMesh.LocalToWorld(position, local.x, local.y, length); float value = _generator.GetValue(world.x / spread, world.y / spread, 0f); return(GetRemappedValue(value)); }
/// <summary> /// Averages the normals of the passed TileMesh with this TileMesh's ActiveMesh. /// </summary> /// <param name="tm">Mesh to average normals with</param> /// <param name="orientation">Orientation of the passed mesh in relation to this one.</param> private void AverageNormalsWith(TileMesh tm, Orientation orientation) { int res = HeightmapResolution; int tmRes = tm.HeightmapResolution; if (tmRes == 0 || res == 0) { //One or more of the referenced tiles hasn't been //generated yet. Skip. return; } bool incrX = false; int x1Start, x2Start, z1Start, z2Start; x1Start = x2Start = z1Start = z2Start = 0; switch (orientation) { case Orientation.Up: z1Start = res - 1; incrX = true; break; case Orientation.Right: x1Start = res - 1; break; case Orientation.Down: z2Start = tmRes - 1; incrX = true; break; case Orientation.Left: x2Start = tmRes - 1; break; } Vector3[] norms1 = ActiveMesh.normals; Vector3[] norms2 = tm.ActiveMesh.normals; //Since meshes can be different resolutions, x an z //vector components across both meshes are //incremented independently int incrAmt1 = 1; int incrAmt2 = 1; if (res > tm.HeightmapResolution) { incrAmt1 = res / tm.HeightmapResolution; } else { incrAmt2 = tm.HeightmapResolution / res; } for (int i = 0; i < Math.Min(res, tm.HeightmapResolution); i++) { Vector3 average = (norms1[x1Start + z1Start * HeightmapResolution] + norms2[x2Start + z2Start * tm.HeightmapResolution]) / 2; average = average.normalized; norms1[x1Start + z1Start * HeightmapResolution] = average; norms2[x2Start + z2Start * tm.HeightmapResolution] = average; if (incrX) { x1Start += incrAmt1; x2Start += incrAmt2; } else { z1Start += incrAmt1; z2Start += incrAmt2; } } ActiveMesh.normals = norms1; tm.ActiveMesh.normals = norms2; }