private void Init(List<Vector2f> points, Rectf plotBounds) { sites = new SiteList(); sitesIndexedByLocation = new Dictionary<Vector2f, Site>(); AddSites(points); this.plotBounds = plotBounds; triangles = new List<Triangle>(); edges = new List<Edge>(); FortunesAlgorithm(); }
/* * * @param point * @param bounds * @return an int with the appropriate bits set if the Point lies on the corresponding bounds lines */ public static int Check(Vector2f point, Rectf bounds) { int value = 0; if (point.X == bounds.left) { value |= LEFT; } if (point.X == bounds.right) { value |= RIGHT; } if (point.Y == bounds.top) { value |= TOP; } if (point.Y == bounds.bottom) { value |= BOTTOM; } return value; }
public void Dispose() { sites.Dispose(); sites = null; foreach (Triangle t in triangles) { t.Dispose(); } triangles.Clear(); foreach (Edge e in edges) { e.Dispose(); } edges.Clear(); plotBounds = Rectf.zero; sitesIndexedByLocation.Clear(); sitesIndexedByLocation = null; }
/* * Set clippedVertices to contain the two ends of the portion of the Voronoi edge that is visible * within the bounds. If no part of the Edge falls within the bounds, leave clippedVertices null * @param bounds */ public void ClipVertices(Rectf bounds) { float xmin = bounds.x; float ymin = bounds.y; float xmax = bounds.right; float ymax = bounds.bottom; Vertex vertex0, vertex1; float x0, x1, y0, y1; if (a == 1 && b >= 0) { vertex0 = rightVertex; vertex1 = leftVertex; } else { vertex0 = leftVertex; vertex1 = rightVertex; } if (a == 1) { y0 = ymin; if (vertex0 != null && vertex0.y > ymin) { y0 = vertex0.y; } if (y0 > ymax) { return; } x0 = c - b * y0; y1 = ymax; if (vertex1 != null && vertex1.y < ymax) { y1 = vertex1.y; } if (y1 < ymin) { return; } x1 = c - b * y1; if ((x0 > xmax && x1 > xmax) || (x0 < xmin && x1 < xmin)) { return; } if (x0 > xmax) { x0 = xmax; y0 = (c - x0)/b; } else if (x0 < xmin) { x0 = xmin; y0 = (c - x0)/b; } if (x1 > xmax) { x1 = xmax; y1 = (c - x1)/b; } else if (x1 < xmin) { x1 = xmin; y1 = (c - x1)/b; } } else { x0 = xmin; if (vertex0 != null && vertex0.x > xmin) { x0 = vertex0.x; } if (x0 > xmax) { return; } y0 = c - a * x0; x1 = xmax; if (vertex1 != null && vertex1.x < xmax) { x1 = vertex1.x; } if (x1 < xmin) { return; } y1 = c - a * x1; if ((y0 > ymax && y1 > ymax) || (y0 < ymin && y1 < ymin)) { return; } if (y0 > ymax) { y0 = ymax; x0 = (c - y0)/a; } else if (y0 < ymin) { y0 = ymin; x0 = (c - y0)/a; } if (y1 > ymax) { y1 = ymax; x1 = (c - y1)/a; } else if (y1 < ymin) { y1 = ymin; x1 = (c - y1)/a; } } clippedVertices = new Dictionary<LR, Vector2f>(); if (vertex0 == leftVertex) { clippedVertices[LR.LEFT] = new Vector2f(x0, y0); clippedVertices[LR.RIGHT] = new Vector2f(x1, y1); } else { clippedVertices[LR.RIGHT] = new Vector2f(x0, y0); clippedVertices[LR.LEFT] = new Vector2f(x1, y1); } }
public Voronoi(List<Vector2f> points, Rectf plotBounds, int lloydIterations) { weigthDistributor = new Random(); Init(points,plotBounds); LloydRelaxation(lloydIterations); }
public void LloydRelaxation(int nbIterations) { // Reapeat the whole process for the number of iterations asked for (int i = 0; i < nbIterations; i++) { List <Vector2f> newPoints = new List <Vector2f>(); // Go thourgh all sites sites.ResetListIndex(); Site site = sites.Next(); while (site != null) { // Loop all corners of the site to calculate the centroid List <Vector2f> region = site.Region(plotBounds); if (region.Count < 1) { site = sites.Next(); continue; } Vector2f centroid = Vector2f.zero; float signedArea = 0; float x0 = 0; float y0 = 0; float x1 = 0; float y1 = 0; float a = 0; // For all vertices except last for (int j = 0; j < region.Count - 1; j++) { x0 = region[j].x; y0 = region[j].y; x1 = region[j + 1].x; y1 = region[j + 1].y; a = x0 * y1 - x1 * y0; signedArea += a; centroid.x += (x0 + x1) * a; centroid.y += (y0 + y1) * a; } // Do last vertex x0 = region[region.Count - 1].x; y0 = region[region.Count - 1].y; x1 = region[0].x; y1 = region[0].y; a = x0 * y1 - x1 * y0; signedArea += a; centroid.x += (x0 + x1) * a; centroid.y += (y0 + y1) * a; signedArea *= 0.5f; centroid.x /= (6 * signedArea); centroid.y /= (6 * signedArea); // Move site to the centroid of its Voronoi cell newPoints.Add(centroid); site = sites.Next(); } // Between each replacement of the cendroid of the cell, // we need to recompute Voronoi diagram: Rectf origPlotBounds = this.plotBounds; Dispose(); Init(newPoints, origPlotBounds); } }
public Voronoi(List <Vector2f> points, Rectf plotBounds, int lloydIterations) { weigthDistributor = new Random(); Init(points, plotBounds); LloydRelaxation(lloydIterations); }
private void Connect(ref List <Vector2f> points, int j, Rectf bounds, bool closingUp = false) { Vector2f rightPoint = points[points.Count - 1]; Edge newEdge = edges[j]; LR newOrientation = edgeOrientations[j]; // The point that must be conected to rightPoint: Vector2f newPoint = newEdge.ClippedEnds[newOrientation]; if (!CloseEnough(rightPoint, newPoint)) { // The points do not coincide, so they must have been clipped at the bounds; // see if they are on the same border of the bounds: if (rightPoint.x != newPoint.x && rightPoint.y != newPoint.y) { // They are on different borders of the bounds; // insert one or two corners of bounds as needed to hook them up: // (NOTE this will not be correct if the region should take up more than // half of the bounds rect, for then we will have gone the wrong way // around the bounds and included the smaller part rather than the larger) int rightCheck = BoundsCheck.Check(rightPoint, bounds); int newCheck = BoundsCheck.Check(newPoint, bounds); float px, py; if ((rightCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; if ((newCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { py = bounds.top; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height) { py = bounds.top; } else { py = bounds.bottom; } points.Add(new Vector2f(px, py)); points.Add(new Vector2f(bounds.left, py)); } } else if ((rightCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; if ((newCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { py = bounds.top; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.RIGHT) != 0) { if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height) { py = bounds.top; } else { py = bounds.bottom; } points.Add(new Vector2f(px, py)); points.Add(new Vector2f(bounds.right, py)); } } else if ((rightCheck & BoundsCheck.TOP) != 0) { py = bounds.top; if ((newCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.BOTTOM) != 0) { if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width) { px = bounds.left; } else { px = bounds.right; } points.Add(new Vector2f(px, py)); points.Add(new Vector2f(px, bounds.bottom)); } } else if ((rightCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; if ((newCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; points.Add(new Vector2f(px, py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width) { px = bounds.left; } else { px = bounds.right; } points.Add(new Vector2f(px, py)); points.Add(new Vector2f(px, bounds.top)); } } } if (closingUp) { // newEdge's ends have already been added return; } points.Add(newPoint); } Vector2f newRightPoint = newEdge.ClippedEnds[LR.Other(newOrientation)]; if (!CloseEnough(points[0], newRightPoint)) { points.Add(newRightPoint); } }
private extern void SetGlobalBounds(Rectf value);
public List<Vector2f> Region(Rectf clippingBounds) { if (edges == null || edges.Count == 0) { return new List<Vector2f>(); } if (edgeOrientations == null) { ReorderEdges(); region = ClipToBounds(clippingBounds); if ((new Polygon(region)).PolyWinding() == Winding.CLOCKWISE) { region.Reverse(); } } return region; }
private void Connect(ref List<Vector2f> points, int j, Rectf bounds, bool closingUp = false) { Vector2f rightPoint = points[points.Count-1]; Edge newEdge = edges[j]; LR newOrientation = edgeOrientations[j]; // The point that must be conected to rightPoint: Vector2f newPoint = newEdge.ClippedEnds[newOrientation]; if (!CloseEnough(rightPoint, newPoint)) { // The points do not coincide, so they must have been clipped at the bounds; // see if they are on the same border of the bounds: if (rightPoint.x != newPoint.x && rightPoint.y != newPoint.y) { // They are on different borders of the bounds; // insert one or two corners of bounds as needed to hook them up: // (NOTE this will not be correct if the region should take up more than // half of the bounds rect, for then we will have gone the wrong way // around the bounds and included the smaller part rather than the larger) int rightCheck = BoundsCheck.Check(rightPoint, bounds); int newCheck = BoundsCheck.Check(newPoint, bounds); float px, py; if ((rightCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; if ((newCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { py = bounds.top; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height) { py = bounds.top; } else { py = bounds.bottom; } points.Add(new Vector2f(px,py)); points.Add(new Vector2f(bounds.left, py)); } } else if ((rightCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; if ((newCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { py = bounds.top; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.RIGHT) != 0) { if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height) { py = bounds.top; } else { py = bounds.bottom; } points.Add(new Vector2f(px,py)); points.Add(new Vector2f(bounds.right, py)); } } else if ((rightCheck & BoundsCheck.TOP) != 0) { py = bounds.top; if ((newCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.BOTTOM) != 0) { if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width) { px = bounds.left; } else { px = bounds.right; } points.Add(new Vector2f(px,py)); points.Add(new Vector2f(px, bounds.bottom)); } } else if ((rightCheck & BoundsCheck.BOTTOM) != 0) { py = bounds.bottom; if ((newCheck & BoundsCheck.RIGHT) != 0) { px = bounds.right; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.LEFT) != 0) { px = bounds.left; points.Add(new Vector2f(px,py)); } else if ((newCheck & BoundsCheck.TOP) != 0) { if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width) { px = bounds.left; } else { px = bounds.right; } points.Add(new Vector2f(px,py)); points.Add(new Vector2f(px, bounds.top)); } } } if (closingUp) { // newEdge's ends have already been added return; } points.Add(newPoint); } Vector2f newRightPoint = newEdge.ClippedEnds[LR.Other(newOrientation)]; if (!CloseEnough(points[0], newRightPoint)) { points.Add(newRightPoint); } }
/// <summary> /// Construct a OpenGLViewportTarget that uses the specified Rect as it's initial area. /// </summary> /// <param name="owner"></param> /// <param name="area"> /// Rect object describing the initial viewport area that should be used for the RenderTarget. /// </param> internal OpenGLViewportTarget(OpenGLRendererBase owner, Rectf area) : base(owner) { SetArea(area); }
void Start() { var scale = transform.localScale; var left = -scale.x / 2.0f; var right = scale.x / 2.0f; var down = -scale.y / 2.0f; var up = scale.y / 2.0f; var bounds = new Rectf(left, down, right - left, up - down); //var bounds = new Rectf(-0.5f, -0.5f, 1.0f, 1.0f); var sites = new List <Vector2f>(); //sites.Add(new Vector2f(0.0f, 0.25f)); //sites.Add(new Vector2f(-0.25f, -0.25f)); //sites.Add(new Vector2f(0.25f, -0.25f)); for (int i = 0; i < NumberOfPoints; i++) { var x = Random.Range(left, right); var y = Random.Range(down, up); var point = new Vector2f(x, y); sites.Add(point); } var voronoi = new Voronoi(sites, bounds, 1); var regions = voronoi.Regions(); foreach (var region in regions) { var len = region.Count; var mesh = new Mesh(); var trisCount = 12 * (len - 1); //var trisCount = 6 * (len - 1); var tris = new int[trisCount]; var verts = new Vector3[3 * len]; var uvs = new Vector2[3 * len]; for (int v = 0; v < len; v++) { var coord = region[v]; verts[v] = new Vector3(coord.x, coord.y, scale.z / 2.0f); verts[len + v] = new Vector3(coord.x, coord.y, -scale.z / 2.0f); verts[2 * len + v] = new Vector3(coord.x, coord.y, -scale.z / 2.0f); uvs[v] = new Vector2(-1, -1); uvs[2 * len + v] = new Vector2(-1, -1); uvs[len + v] = new Vector2(coord.x, coord.y); } var t = 0; for (int v = 1; v < len - 1; v++) { //tris[t++] = 0; //tris[t++] = v; //tris[t++] = v + 1; tris[t++] = len + v + 1; tris[t++] = len + v; tris[t++] = len; tris[t++] = 2 * len; tris[t++] = 2 * len + v; tris[t++] = 2 * len + v + 1; } for (int v = 0; v < len; v++) { var n = v == (len - 1) ? 0 : v + 1; tris[t++] = v; tris[t++] = len + v; tris[t++] = len + n; tris[t++] = v; tris[t++] = len + n; tris[t++] = n; } mesh.vertices = verts; mesh.triangles = tris; mesh.uv = uvs; //mesh.RecalculateNormals(); var shard = Instantiate(ShardPrefab); shard.transform.SetParent(transform, false); shard.transform.localPosition = Vector3.zero; shard.transform.localRotation = Quaternion.identity; shard.transform.localScale = Vector3.one; shard.GetComponent <MeshCollider>().sharedMesh = mesh; shard.GetComponentInChildren <MeshFilter>().sharedMesh = mesh; //shard.GetComponentInChildren<MeshRenderer>().sharedMaterial = GetComponent<MeshRenderer>().sharedMaterial; transform.localScale = Vector3.one; GetComponent <MeshRenderer>().enabled = false; } }
/*! * \brief * Tell the treeItem where its button is located. * Calculated and set in Tree.cpp. * * \param buttonOffset * Location of the button in screenspace. */ public void SetButtonLocation(Rectf buttonOffset) { d_buttonLocation = buttonOffset; }
public override void AddToRenderGeometry(GeometryBuffer geomBuffer, ref Rectf renderArea, ref Rectf?clipArea, ColourRect colours) { throw new NotImplementedException(); }
public override void OnDraw() { base.OnDraw(); Rectf zone = Backpack.percentage(bsc.ViewportRect, 0, 0.15f, 1, 0.1f); bsc.game1.drawSquare(zone, bsc.monochrome(0.2f), 0); Rectf bar = Backpack.percentagef(zone, 0.3f, 0.3f, 0.4f, 0.4f); Rectf text = Backpack.percentagef(zone, 0.7f, 0, 0.1f, 1); Color[] colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Blue }; Color[] textColors = new Color[colors.Length]; for (int i = 0; i < textColors.Length; ++i) { textColors[i] = colors[i]; } textColors[colors.Length - 1] = Color.White; bsc.game1.drawSquare(text, Color.Black, 0); bsc.game1.drawString(ComboMultiplier + "x", (Rectangle)text, textColors[combo], new Vector2(0.5f), true); Rectf meter = Backpack.percentagef(bar, 0.03f, 0.1f, 0.94f, 0.8f); bsc.game1.drawSquare(meter, Color.Black, 0); for (int i = 0; i < comboMeterTarget.Length; ++i) { if (combo < i) { break; } float valueA = 0; float valueB = 0; valueA = comboMeter; if (combo > i) { valueA = comboMeterTarget[i]; } valueB = comboMeterTarget[i]; float pct = 1; if (valueB != 0) { pct = valueA / valueB; } Rectf p3 = Backpack.percentagef(meter, 0, 0, pct, 1); bsc.game1.drawSquare(p3, colors[i], 0); //p = DrawProgressBar(valueA, valueB, 0.15f, 0.1f, colors[i]); } bsc.game1.drawTexture(barTx, bar.GetLocation(), Color.White, 0, bar.Width, bar.Height); bsc.playRectY = zone.GetBottom() / bsc.ViewportRect.Height; bsc.playRectHeight = 1 - bsc.playRectY; if (bsc.collectionTiles.Count > 0) { Vector2 start = bsc.collectionTiles.First().position; Vector2 end = new Vector2(bsc.ViewportRect.Width, 0); float pct = bsc.collectionElapsed / bsc.collectionDuration; Vector2 lineA = Vector2.Lerp(start, end, pct); Vector2 lineB = Vector2.Lerp(start, end, pct - .1f); Color color = Color.Green; if (!collectionWordFound) { color = Color.Red; } bsc.game1.drawLine(lineA, lineB, color, 3); } //bsc.game1.drawSquare(bsc.multiplierRect, Color.Black, 0); //bsc.game1.drawString(ComboMultiplier + "x", (Rectangle)bsc.multiplierRect, colors[combo], default(Vector2), true); }
public List<List<Vector2f>> Regions(Rectf plotBounds) { List<List<Vector2f>> regions = new List<List<Vector2f>>(); foreach (Site site in sites) { regions.Add(site.Region(plotBounds)); } return regions; }
/// <summary> /// Return a value that represents this dimension as absolute pixels. /// </summary> /// <param name="wnd"> /// Window object that may be used by the specialised class to aid in /// calculating the final value (typically would be used to obtain /// window/widget dimensions). /// </param> /// <param name="container"> /// Rect object which describes an area to be considered as the base area /// when calculating the final value. Basically this means that relative /// values are calculated from the dimensions of this Rect. /// </param> /// <returns> /// float value which represents, in pixels, the same value as this BaseDim. /// </returns> public abstract float GetValue(Window wnd, Rectf container);
private void GenerateGraphs(List <Vector2> points) { //Generate the Voronoi Rectf bounds = new Rectf(0, 0, size.x, size.y); Voronoi voronoi = new Voronoi(points, bounds, relaxation); //Cell centers foreach (var site in voronoi.SitesIndexedByLocation) { CellCenter c = new CellCenter(); c.index = cells.Count; c.position = site.Key; cells.Add(c); } //Cell Corners foreach (var edge in voronoi.Edges) { //If the edge doesn't have clipped ends, it was not withing bounds if (edge.ClippedEnds == null) { continue; } if (!corners.Any(x => x.position == edge.ClippedEnds[LR.LEFT])) { CellCorner c = new CellCorner(); c.index = corners.Count; c.position = edge.ClippedEnds[LR.LEFT]; c.isBorder = c.position.x == 0 || c.position.x == size.x || c.position.y == 0 || c.position.y == size.y; corners.Add(c); } if (!corners.Any(x => x.position == edge.ClippedEnds[LR.RIGHT])) { CellCorner c = new CellCorner(); c.index = corners.Count; c.position = edge.ClippedEnds[LR.RIGHT]; c.isBorder = c.position.x == 0 || c.position.x == size.x || c.position.y == 0 || c.position.y == size.y; corners.Add(c); } } //Define some local helper functions to help with the loop below void AddPointToPointList <T>(List <T> list, T point) where T : MapPoint { if (!list.Contains(point)) { list.Add(point); } } //Voronoi and Delaunay edges. Each edge point to two cells and two corners, so we can store both the sites and corners into a single edge object (thus making two edges into one object) foreach (var voronoiEdge in voronoi.Edges) { if (voronoiEdge.ClippedEnds == null) { continue; } CellEdge edge = new CellEdge(); edge.index = edges.Count; //Set the voronoi edge edge.v0 = corners.First(x => x.position == voronoiEdge.ClippedEnds[LR.LEFT]); edge.v1 = corners.First(x => x.position == voronoiEdge.ClippedEnds[LR.RIGHT]); //Set the Delaunay edge edge.d0 = cells.First(x => x.position == voronoiEdge.LeftSite.Coord); edge.d1 = cells.First(x => x.position == voronoiEdge.RightSite.Coord); edges.Add(edge); /*Set the relationships*/ //Set the relationship between this edge and the connected cells centers/corners edge.d0.borderEdges.Add(edge); edge.d1.borderEdges.Add(edge); edge.v0.connectedEdges.Add(edge); edge.v1.connectedEdges.Add(edge); //Set the relationship between the CELL CENTERS connected to this edge AddPointToPointList(edge.d0.neighborCells, edge.d1); AddPointToPointList(edge.d1.neighborCells, edge.d0); //Set the relationship between the CORNERS connected to this edge AddPointToPointList(edge.v0.neighborCorners, edge.v1); AddPointToPointList(edge.v1.neighborCorners, edge.v0); //Set the relationship of the CORNERS connected to this edge and the CELL CENTERS connected to this edge AddPointToPointList(edge.d0.cellCorners, edge.v0); AddPointToPointList(edge.d0.cellCorners, edge.v1); AddPointToPointList(edge.d1.cellCorners, edge.v0); AddPointToPointList(edge.d1.cellCorners, edge.v1); //Same as above, but the other way around AddPointToPointList(edge.v0.touchingCells, edge.d0); AddPointToPointList(edge.v0.touchingCells, edge.d1); AddPointToPointList(edge.v1.touchingCells, edge.d0); AddPointToPointList(edge.v1.touchingCells, edge.d1); } }
private List<Vector2f> ClipToBounds(Rectf bounds) { List<Vector2f> points = new List<Vector2f>(); int n = edges.Count; int i = 0; Edge edge; while (i < n && !edges[i].Visible()) { i++; } if (i == n) { // No edges visible return new List<Vector2f>(); } edge = edges[i]; LR orientation = edgeOrientations[i]; points.Add(edge.ClippedEnds[orientation]); points.Add(edge.ClippedEnds[LR.Other(orientation)]); for (int j = i + 1; j < n; j++) { edge = edges[j]; if (!edge.Visible()) { continue; } Connect(ref points, j, bounds); } // Close up the polygon by adding another corner point of the bounds if needed: Connect(ref points, i, bounds, true); return points; }
public override float GetValue(Window wnd, Rectf container) { // This dimension type does not alter when whithin a container Rect. return(GetValue(wnd)); }
private static Vector2f[][] GetPhysicsShape(IExportContainer container, SpriteAtlas originAtlas, Sprite origin, Rectf rect, Vector2f pivot) { if (Sprite.HasPhysicsShape(container.Version)) { return(origin.GeneratePhysicsShape(originAtlas, rect, pivot)); } return(Array.Empty <Vector2f[]>()); }
protected override void ExportYAMLInner(IExportContainer container, YAMLMappingNode node) { base.ExportYAMLInner(container, node); SpriteMetaData[] sprites = new SpriteMetaData[m_sprites.Count]; for (int i = 0; i < m_sprites.Count; i++) { sprites[i] = new SpriteMetaData(m_sprites[i]); } node.AddSerializedVersion(GetSerializedVersion(container.Version)); YAMLMappingNode mipmap = new YAMLMappingNode(); mipmap.Add("mipMapMode", (int)TextureImporterMipFilter.BoxFilter); mipmap.Add("enableMipMap", m_texture.MipCount > 1 ? true : false); mipmap.Add("sRGBTexture", m_texture.ColorSpace == ColorSpace.Gamma ? true : false); mipmap.Add("linearTexture", false); mipmap.Add("fadeOut", false); mipmap.Add("borderMipMap", false); mipmap.Add("mipMapsPreserveCoverage", false); mipmap.Add("alphaTestReferenceValue", 0.5f); mipmap.Add("mipMapFadeDistanceStart", 1); mipmap.Add("mipMapFadeDistanceEnd", 3); node.Add("mipmaps", mipmap); YAMLMappingNode bumpmap = new YAMLMappingNode(); bumpmap.Add("convertToNormalMap", false); bumpmap.Add("externalNormalMap", false); bumpmap.Add("heightScale", 0.25f); bumpmap.Add("normalMapFilter", (int)TextureImporterNormalFilter.Standard); node.Add("bumpmap", bumpmap); node.Add("isReadable", m_texture.IsReadable); node.Add("grayScaleToAlpha", false); node.Add("generateCubemap", (int)TextureImporterGenerateCubemap.AutoCubemap); node.Add("cubemapConvolution", 0); node.Add("seamlessCubemap", false); node.Add("textureFormat", (int)m_texture.TextureFormat); int maxSize = m_texture.Width > m_texture.Height ? m_texture.Width : m_texture.Height; maxSize = maxSize > 2048 ? maxSize : 2048; node.Add("maxTextureSize", maxSize); TextureImportSettings importSettings = new TextureImportSettings(m_texture.TextureSettings); node.Add("textureSettings", importSettings.ExportYAML(container)); node.Add("nPOTScale", false); node.Add("lightmap", false); node.Add("compressionQuality", 50); SpriteImportMode spriteMode; switch (m_sprites.Count) { case 0: spriteMode = SpriteImportMode.Single; break; case 1: Sprite sprite = m_sprites[0]; Rectf textureRect = new Rectf(0.0f, 0.0f, m_texture.Width, m_texture.Height); if (m_sprites[0].Rect == textureRect) { spriteMode = sprite.Name == m_texture.Name ? SpriteImportMode.Single : SpriteImportMode.Multiple; } else { spriteMode = SpriteImportMode.Multiple; } break; default: spriteMode = SpriteImportMode.Multiple; break; } node.Add("spriteMode", (int)spriteMode); node.Add("spriteExtrude", 1); node.Add("spriteMeshType", (int)SpriteMeshType.Tight); node.Add("alignment", 0); Vector2f pivot = new Vector2f(0.5f, 0.5f); node.Add("spritePivot", pivot.ExportYAML(container)); node.Add("spriteBorder", default(Rectf).ExportYAML(container)); node.Add("spritePixelsToUnits", 100); node.Add("alphaUsage", true); node.Add("alphaIsTransparency", true); node.Add("spriteTessellationDetail", -1); TextureImporterType type; if (m_texture.LightmapFormat.IsNormalmap()) { type = TextureImporterType.NormalMap; } else { type = m_sprites.Count == 0 ? TextureImporterType.Default : TextureImporterType.Sprite; } node.Add("textureType", (int)type); TextureImporterShape shape = (m_texture is Cubemap) ? TextureImporterShape.TextureCube : TextureImporterShape.Texture2D; node.Add("textureShape", (int)shape); node.Add("maxTextureSizeSet", false); node.Add("compressionQualitySet", false); node.Add("textureFormatSet", false); TextureImporterPlatformSettings platform = new TextureImporterPlatformSettings(); TextureImporterPlatformSettings[] platforms = new TextureImporterPlatformSettings[] { platform }; node.Add("platformSettings", platforms.ExportYAML(container)); SpriteSheetMetaData spriteSheet; if (spriteMode == SpriteImportMode.Single) { if (sprites.Length == 0) { spriteSheet = new SpriteSheetMetaData(sprites); } else { spriteSheet = new SpriteSheetMetaData(sprites[0]); } } else { spriteSheet = new SpriteSheetMetaData(sprites); } node.Add("spriteSheet", spriteSheet.ExportYAML(container)); node.Add("spritePackingTag", string.Empty); }
public static float percentageW(Rectf basis, float percentage) { return(percentage * basis.Width); }
private extern void GetGlobalBounds(ref Rectf value);
public static float percentageH(Rectf basis, float percentage) { return(percentage * basis.Height); }
// Use this for initialization void Start() { // Create the bounds of the voronoi diagram based on the screen resolution bounds = new Rectf(0, 0, Screen.width, Screen.height); }
private extern void GetLocalBounds(ref Rectf value);
public Voronoi(List <Vector2f> points, Rectf plotBounds) { weigthDistributor = new Random(); Init(points, plotBounds); }
private extern void SetLocalBounds(Rectf value);
private void FortunesAlgorithm() { Site newSite, bottomSite, topSite, tempSite; Vertex v, vertex; Vector2f newIntStar = Vector2f.zero; LR leftRight; Halfedge lbnd, rbnd, llbnd, rrbnd, bisector; Edge edge; Rectf dataBounds = sites.GetSitesBounds(); int sqrtSitesNb = (int)Math.Sqrt(sites.Count() + 4); HalfedgePriorityQueue heap = new HalfedgePriorityQueue(dataBounds.y, dataBounds.height, sqrtSitesNb); EdgeList edgeList = new EdgeList(dataBounds.x, dataBounds.width, sqrtSitesNb); List <Halfedge> halfEdges = new List <Halfedge>(); List <Vertex> vertices = new List <Vertex>(); Site bottomMostSite = sites.Next(); newSite = sites.Next(); while (true) { if (!heap.Empty()) { newIntStar = heap.Min(); } if (newSite != null && (heap.Empty() || CompareByYThenX(newSite, newIntStar) < 0)) { // New site is smallest //Debug.Log("smallest: new site " + newSite); // Step 8: lbnd = edgeList.EdgeListLeftNeighbor(newSite.Coord); // The halfedge just to the left of newSite //UnityEngine.Debug.Log("lbnd: " + lbnd); rbnd = lbnd.edgeListRightNeighbor; // The halfedge just to the right //UnityEngine.Debug.Log("rbnd: " + rbnd); bottomSite = RightRegion(lbnd, bottomMostSite); // This is the same as leftRegion(rbnd) // This Site determines the region containing the new site //UnityEngine.Debug.Log("new Site is in region of existing site: " + bottomSite); // Step 9 edge = Edge.CreateBisectingEdge(bottomSite, newSite); //UnityEngine.Debug.Log("new edge: " + edge); edges.Add(edge); bisector = Halfedge.Create(edge, LR.LEFT); halfEdges.Add(bisector); // Inserting two halfedges into edgelist constitutes Step 10: // Insert bisector to the right of lbnd: edgeList.Insert(lbnd, bisector); // First half of Step 11: if ((vertex = Vertex.Intersect(lbnd, bisector)) != null) { vertices.Add(vertex); heap.Remove(lbnd); lbnd.vertex = vertex; lbnd.ystar = vertex.y + newSite.Dist(vertex); heap.Insert(lbnd); } lbnd = bisector; bisector = Halfedge.Create(edge, LR.RIGHT); halfEdges.Add(bisector); // Second halfedge for Step 10:: // Insert bisector to the right of lbnd: edgeList.Insert(lbnd, bisector); // Second half of Step 11: if ((vertex = Vertex.Intersect(bisector, rbnd)) != null) { vertices.Add(vertex); bisector.vertex = vertex; bisector.ystar = vertex.y + newSite.Dist(vertex); heap.Insert(bisector); } newSite = sites.Next(); } else if (!heap.Empty()) { // Intersection is smallest lbnd = heap.ExtractMin(); llbnd = lbnd.edgeListLeftNeighbor; rbnd = lbnd.edgeListRightNeighbor; rrbnd = rbnd.edgeListRightNeighbor; bottomSite = LeftRegion(lbnd, bottomMostSite); topSite = RightRegion(rbnd, bottomMostSite); // These three sites define a Delaunay triangle // (not actually using these for anything...) // triangles.Add(new Triangle(bottomSite, topSite, RightRegion(lbnd, bottomMostSite))); v = lbnd.vertex; v.SetIndex(); lbnd.edge.SetVertex(lbnd.leftRight, v); rbnd.edge.SetVertex(rbnd.leftRight, v); edgeList.Remove(lbnd); heap.Remove(rbnd); edgeList.Remove(rbnd); leftRight = LR.LEFT; if (bottomSite.y > topSite.y) { tempSite = bottomSite; bottomSite = topSite; topSite = tempSite; leftRight = LR.RIGHT; } edge = Edge.CreateBisectingEdge(bottomSite, topSite); edges.Add(edge); bisector = Halfedge.Create(edge, leftRight); halfEdges.Add(bisector); edgeList.Insert(llbnd, bisector); edge.SetVertex(LR.Other(leftRight), v); if ((vertex = Vertex.Intersect(llbnd, bisector)) != null) { vertices.Add(vertex); heap.Remove(llbnd); llbnd.vertex = vertex; llbnd.ystar = vertex.y + bottomSite.Dist(vertex); heap.Insert(llbnd); } if ((vertex = Vertex.Intersect(bisector, rrbnd)) != null) { vertices.Add(vertex); bisector.vertex = vertex; bisector.ystar = vertex.y + bottomSite.Dist(vertex); heap.Insert(bisector); } } else { break; } } // Heap should be empty now heap.Dispose(); edgeList.Dispose(); foreach (Halfedge halfedge in halfEdges) { halfedge.ReallyDispose(); } halfEdges.Clear(); // we need the vertices to clip the edges foreach (Edge e in edges) { e.ClipVertices(plotBounds); } // But we don't actually ever use them again! foreach (Vertex ve in vertices) { ve.Dispose(); } vertices.Clear(); }
private extern void GetTextureSource(ref Rectf value);
public Voronoi(List<Vector2f> points, Rectf plotBounds) { weigthDistributor = new Random(); Init(points,plotBounds); }
private extern void SetTextureSource(Rectf value);
// Use this for initialization void Start() { // set seed Random.seed = seed; // pick number of polygons polygonNumber = Random.Range (minPolygons, maxPolygons); // get terrain terrain = (Terrain)gameObject.GetComponent ("Terrain"); // get heightmap heightmap = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight]; tw = terrain.terrainData.heightmapWidth; th = terrain.terrainData.heightmapHeight; // initialize heightmap array and set values to 0 for(int i=0;i<heightmap.GetLength(0);i++){ for(int j=0;j<heightmap.GetLength(1);j++){ heightmap[i,j] = 1f; // for easy identification } } // Create your sites (lets call that the center of your polygons) List<Vector2f> points = CreateRandomPoint(); islands = new List<List<Vector2f>> (); // Create the bounds of the voronoi diagram // Use Rectf instead of Rect; it's a struct just like Rect and does pretty much the same, // but like that it allows you to run the delaunay library outside of unity (which mean also in another tread) Rectf bounds = new Rectf(0,0,512,512); // There is a two ways you can create the voronoi diagram: with or without the lloyd relaxation // Here I used it with 2 iterations of the lloyd relaxation Voronoi voronoi = new Voronoi(points,bounds,5); // Now retreive the edges from it, and the new sites position if you used lloyd relaxtion sites = voronoi.SitesIndexedByLocation; edges = voronoi.Edges; // apply polygons DisplayVoronoiDiagram(); // discover islands //int k = 0; foreach (KeyValuePair<Vector2f,Site> kv in sites) { // create list List<Vector2f> newIsland = new List<Vector2f>(); // choose modifier float mod = (float)Random.Range(0,islandRange); float targh = ((float)minIslandHeight+mod)/terrain.terrainData.size.y; // floodfill FloodFill(kv.Key,1f,targh,newIsland); // add islands.Add(newIsland); // inc //k++; } // generate maze for (int i=0; i<connectionIts; i++) { RandomConnections (); } // add noise heightmap = PerlinNoise (heightmap); // reattatch array to terrain terrain.terrainData.SetHeights(0,0,heightmap); // move player PlacePlayer (); }
public override float GetValue(Window wnd, Rectf container) { return(_value); }
void Start() { // Create your sites (lets call that the center of your polygons) List<Vector2f> points = CreateRandomPoint(); // Create the bounds of the voronoi diagram // Use Rectf instead of Rect; it's a struct just like Rect and does pretty much the same, // but like that it allows you to run the delaunay library outside of unity (which mean also in another tread) Rectf bounds = new Rectf(0,0,512,512); // There is a two ways you can create the voronoi diagram: with or without the lloyd relaxation // Here I used it with 2 iterations of the lloyd relaxation Voronoi voronoi = new Voronoi(points,bounds,5); // But you could also create it without lloyd relaxtion and call that function later if you want //Voronoi voronoi = new Voronoi(points,bounds); //voronoi.LloydRelaxation(5); // Now retreive the edges from it, and the new sites position if you used lloyd relaxtion sites = voronoi.SitesIndexedByLocation; edges = voronoi.Edges; MakeVoronoiCells(); }
private bool OnStart() { var backbufferWidth = (float)GraphicsDevice.BackbufferWidth(); var backbufferHeight = (float)GraphicsDevice.BackbufferHeight(); // Init area. var borderThickness = 32.0f; var area = new Rectf(borderThickness, borderThickness, backbufferWidth - borderThickness * 2.0f, backbufferHeight - borderThickness); // Create player. var playerSize = new Vector2(128.0f, 32.0f); var playerPosition = new Vector2(area.Right - area.width / 2.0f, area.Bottom - borderThickness * 4.0f); player = new Player(playerPosition, playerSize, area); player.Rect.Fill = Color.Green; // Create borders. var borderElements = new Rectangle[4]; // Right. borderElements[0] = new Rectangle(new Vector2(backbufferWidth - borderThickness / 2.0f, backbufferHeight / 2.0f), new Vector2(borderThickness, backbufferHeight)); // Left. borderElements[1] = new Rectangle(new Vector2(borderThickness / 2.0f, backbufferHeight / 2.0f), new Vector2(borderThickness, backbufferHeight)); // Top. borderElements[2] = new Rectangle(new Vector2(backbufferWidth / 2.0f, borderThickness / 2.0f), new Vector2(backbufferWidth - borderThickness, borderThickness)); // Bottom. borderElements[3] = new Rectangle(new Vector2(backbufferWidth / 2.0f, backbufferHeight - borderThickness / 2.0f), new Vector2(backbufferWidth - borderThickness, borderThickness)); // Create ball. const float radius = 32.0f; var playerTransform = player.Rect.Transform; ball = new Ball(new Vector2(playerTransform.position.x + player.Rect.LocalBounds.width / 2.0f, playerTransform.position.y - player.Rect.LocalBounds.height * 2.0f), radius, new Rectf[] { borderElements[0].GlobalBounds, borderElements[1].GlobalBounds, borderElements[2].GlobalBounds, borderElements[3].GlobalBounds }); ball.Circle.Fill = Color.Green; ball.Circle.BorderFill = new Color(125, 125, 0, 255); // Create 8x8 grid of bricks. bricks = new List <Rectangle>(); // Offset. const float rows = 8.0f; const float columns = 8.0f; const float boff = 64.0f; // Start x and y. var bsx = area.Left + boff; var bsy = area.Top + boff; // Width and height. var bw = (area.width - boff * 2.0f) / columns; var bh = (area.height / 2.0f) / rows; for (var h = 0; h < 8; h++) { for (var w = 0; w < 8; w++) { var x = bsx + w * bw; var y = bsy + h * bh; var brick = new Rectangle(x, y, bw, bh); var color = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble()); var transform = brick.Transform; transform.origin = Vector3.Zero; brick.Transform = transform; brick.Fill = color; bricks.Add(brick); } } // Create layer and add elements. var layer = Layers.CreateDynamic("bottom"); layer.Add(player.Rect); layer.Add(ball.Circle); foreach (var element in borderElements) { layer.Add(element); } foreach (var element in bricks) { layer.Add(element); } return(true); }