public static void Conify(TerraMesh _tMesh, float _strength, Progress.OnUpdate _onUpdate = null) { if (_onUpdate == null) { _onUpdate = (_prog, _str) => { } } ; var prog = new Progress(); prog.SetOnUpdate(_onUpdate); var cent = _tMesh.m_Bounds.center.ToVec2(); var min = _tMesh.m_Bounds.min.ToVec2(); var maxMag = (min - cent).magnitude; prog.Update(0, "Conifying"); for (var sIdx = 0; sIdx < _tMesh.Vertices.Length; ++sIdx) { var sitePos = _tMesh.Vertices[sIdx]; var magScal = (new Vector2(sitePos.x, sitePos.y) - cent).magnitude / maxMag - 0.5f; var zShift = magScal * _strength / 2f; var newPos = new Vector3(sitePos.x, sitePos.y, zShift + sitePos.z); _tMesh.Vertices[sIdx] = newPos; prog.Update((float)sIdx / _tMesh.Vertices.Length); } _tMesh.RecalculateBounds(); }
public static void Erode(TerraMesh _tMesh, float _maxErosionRate, float[] _waterFlux) { var sitePositions = _tMesh.GetAllSitePositions(); //TODO SLOW //Get all site slope vectors and slope values var slopeVecs = new Vector3[sitePositions.Length]; var slopeVals = new float[sitePositions.Length]; for (var pIdx = 0; pIdx < sitePositions.Length; ++pIdx) { var p = sitePositions[pIdx]; var aveSlp = Vector3.zero; var nbrCnt = 0; foreach (var nIdx in _tMesh.SiteNeighbors[pIdx]) { if (nIdx == SiteIdxNull) { continue; } nbrCnt++; var n = sitePositions[nIdx]; var slpVec = n - p; if (slpVec.z > 0) { slpVec = -slpVec; } aveSlp += slpVec; } var slpVecCell = aveSlp / nbrCnt; slopeVecs[pIdx] = slpVecCell; slopeVals[pIdx] = slpVecCell.z / new Vector2(slpVecCell.x, slpVecCell.y).magnitude; } //Apply erosion to terra mesh surface for (var pIdx = 0; pIdx < _waterFlux.Length; ++pIdx) { var sitePos = sitePositions[pIdx]; var fx = (float)Math.Sqrt(_waterFlux[pIdx]); var slp = slopeVals[pIdx]; var erosionShift = Math.Min(-slp * fx, _maxErosionRate); //var newPos = new Vector3(sitePos.x, sitePos.y, sitePos.z - erosionShift); SetSiteHeight(_tMesh, pIdx, sitePos.z - erosionShift); } _tMesh.RecalculateBounds(); }
public static void Blob(TerraMesh _tMesh, float _strength, float _radius, Vector2 _loc) { for (var sIdx = 0; sIdx < _tMesh.Vertices.Length; ++sIdx) { var sPos = _tMesh.Vertices[sIdx]; var vert2d = sPos.ToVec2(); var dist = (vert2d - _loc).magnitude; if (dist > _radius) { continue; } var cosVal = dist / _radius * (float)Math.PI / 2f; var zShift = _strength * (float)Math.Cos(cosVal); var newZ = sPos.z + zShift; var newPos = new Vector3(vert2d.x, vert2d.y, sPos.z + zShift); _tMesh.Vertices[sIdx] = newPos; } _tMesh.RecalculateBounds(); }
public static void SlopeGlobal(TerraMesh _tMesh, Vector2 _dir, float _strength, Progress.OnUpdate _onUpdate = null) { if (_onUpdate == null) { _onUpdate = (_prog, _str) => { } } ; var prog = new Progress(); prog.SetOnUpdate(_onUpdate); var dir = _dir.normalized; var bnds = _tMesh.bounds; Func <Vector3, float> strf = _pos => { var xPct = (_pos.x - bnds.min.x) / bnds.size.x - 0.5f; var yPct = (_pos.y - bnds.min.y) / bnds.size.y - 0.5f; var xStr = xPct * dir.x; var yStr = yPct * dir.y; return((xStr + yStr) * _strength / 4f); }; prog.Update(0, "Global Slope"); for (var sIdx = 0; sIdx < _tMesh.Vertices.Length; ++sIdx) { //if (_tMesh.HullSites.Contains(sIdx)) // Trace.WriteLine("Debug Hullsites"); //TODO DEbug var sitePos = _tMesh.Vertices[sIdx]; var zShift = strf(sitePos); var newZ = sitePos.z + zShift; var newPos = new Vector3(sitePos.x, sitePos.y, newZ); _tMesh.Vertices[sIdx] = newPos; prog.Update((float)sIdx / _tMesh.Vertices.Length); } _tMesh.RecalculateBounds(); }