private void FillMaskFromMinY(Bounds bounds, Terrain terrain, Serializable2DFloatArray heights, Vector2 minY) { Mask.Clear(); GridSize = WorldStampCreator.GetMinGridSize(bounds, terrain); for (var u = GridSize / 2f; u < bounds.size.x; u += GridSize) { for (var v = GridSize / 2f; v < bounds.size.z; v += GridSize) { var cell = GridManager.GetCell(new Vector3(u, 0, v)); var cellMax = GridManager.GetCellMax(cell).x0z() + bounds.min; var cellMin = GridManager.GetCellCenter(cell).x0z() + bounds.min; if (!bounds.Contains(cellMax) || !bounds.Contains(cellMin)) { continue; } var h = heights.BilinearSample(new Vector2(u / bounds.size.z, v / bounds.size.x)) * bounds.size.y; if (h < minY.x) { Mask.SetValue(cell, 0); } else if (h <= minY.y) { Mask.SetValue(cell, (h - minY.x) / (minY.y - minY.x)); } else { Mask.SetValue(cell, 1); } } } }
public override float SampleHeight(TerrainWrapper wrapper, Vector3 worldPos) { if (Heights == null || Heights.Width == 0 || Heights.Height == 0) { return(0); } var tSize = wrapper.Terrain.terrainData.size; var normalizedPos = worldPos - wrapper.transform.position; var step = wrapper.Terrain.terrainData.size.x / wrapper.Terrain.terrainData.heightmapResolution; normalizedPos = new Vector3(normalizedPos.x / (tSize.x + step), normalizedPos.y / tSize.y, normalizedPos.z / (tSize.z + step)); if (normalizedPos.x < 0 || normalizedPos.z < 0 || normalizedPos.x > 1 || normalizedPos.z > 1) { return(0); } var h = Heights.BilinearSample(new Vector2(normalizedPos.x, normalizedPos.z)); //var tHeight = wrapper.Terrain.terrainData.size.y; //return h * tHeight; return(h); }
public void SetMaskFromArray(WorldStampCreator parent, Serializable2DFloatArray mask) { GridSize = WorldStampCreator.GetMinGridSize(parent.Template.Bounds, parent.Template.Terrain); Mask.Clear(); for (var u = GridSize / 2f; u < parent.Template.Bounds.size.x; u += GridSize) { for (var v = GridSize / 2f; v < parent.Template.Bounds.size.z; v += GridSize) { var cell = GridManager.GetCell(new Vector3(u, 0, v)); var cellMax = GridManager.GetCellMax(cell).x0z() + parent.Template.Bounds.min; var cellMin = GridManager.GetCellCenter(cell).x0z() + parent.Template.Bounds.min; if (!parent.Template.Bounds.Contains(cellMax) || !parent.Template.Bounds.Contains(cellMin)) { continue; } var val = mask.BilinearSample(new Vector2(u / parent.Template.Bounds.size.x, v / parent.Template.Bounds.size.z)); Mask.SetValue(cell, val); } } }
public void Invalidate(Serializable2DFloatArray heights, Func <Vector3> displaySizeGetter, Func <Vector3> positionGetter, Func <Vector3> scaleGetter, Func <Quaternion> rotationGetter, Func <Vector3> dataSizeGetter, bool flipHeights, WorldStampMask mask, Common.Painter.GridManagerInt gridManager, Func <bool> existenceHook, int res) { _dataSize = dataSizeGetter; _displaySize = displaySizeGetter; _existenceHook = existenceHook; _position = positionGetter; _scale = scaleGetter; _rotation = rotationGetter; if (_mesh == null) { _mesh = new Mesh(); } if (_material == null) { _material = Resources.Load <Material>("MadMaps/WorldStamp/WorldStampPreviewMaterial"); } var verts = new Vector3[(res + 1) * (res + 1)]; var uv = new Vector2[verts.Length]; var colors = new Color[verts.Length]; int counter = 0; var dataSize = dataSizeGetter(); for (int u = 0; u <= res; u++) { var uF = u / (float)res; for (int v = 0; v <= res; v++) { var vF = v / (float)res; var samplePoint = flipHeights ? new Vector2(uF, vF) : new Vector2(vF, uF); var height = heights.BilinearSample(samplePoint); var pos = new Vector3(uF * dataSize.x, height * dataSize.y, vF * dataSize.z) - dataSize.xz().x0z() / 2; float val = 1; if (mask != null && gridManager != null) { val = mask.GetBilinear(gridManager, new Vector3(pos.x, 0, pos.z) + dataSize.xz().x0z() / 2); pos.y *= val; } verts[counter] = pos; uv[counter] = new Vector2(uF, vF); colors[counter] = Color.Lerp(Color.clear, Color.white, val); counter++; } } _mesh.vertices = verts; _mesh.uv = uv; _mesh.colors = colors; var tris = new int[((res + 1) * (res)) * 6]; for (var i = 0; i < tris.Length; i += 6) { var vIndex = i / 6; var t1 = vIndex + 0; var t2 = vIndex + 1; var t3 = vIndex + 2 + (res - 1); var t1r = t1 / (res + 1); var t2r = t2 / (res + 1); var t3r = (t3 / (res + 1)) - 1; if (t1r == t2r && t2r == t3r && t3r == t1r) { tris[i + 0] = t1; tris[i + 1] = t2; tris[i + 2] = t3; } var t4 = vIndex + 2 + (res - 1); var t5 = vIndex + 1 + (res - 1); var t6 = vIndex + 0; var t4r = (t4 / (res + 1)) - 1; var t5r = (t5 / (res + 1)) - 1; var t6r = t6 / (res + 1); if (t4r == t5r && t5r == t6r && t6r == t4r) { tris[i + 3] = t4; tris[i + 4] = t5; tris[i + 5] = t6; } } _mesh.triangles = tris; _mesh.RecalculateNormals(0); _mesh.RecalculateBounds(); #if UNITY_EDITOR UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUIDelegate; UnityEditor.SceneView.onSceneGUIDelegate += OnSceneGUIDelegate; #endif }