private void _ComputeFlow() { var W = Terrain.GetWidth(); var H = Terrain.GetHeight(); var t = Terrain.GetData(); var f = Flow.GetData(); t.Lock(); f.Lock(); for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { var Flow = f.GetPixel(x, y); var curr = t.GetPixel(x, y); var left = t.GetPixel(x - 1, y); var right = t.GetPixel(x + 1, y); var up = t.GetPixel(x, y - 1); var down = t.GetPixel(x, y + 1); float height = curr.r + curr.g; float fl = 0f; float fr = 0f; float fu = 0f; float fd = 0f; if (x - 1 >= 0) { fl = Flow.r + Mathf.Max(0, height - left.r - left.g); } if (x + 1 < W) { fr = Flow.g + Mathf.Max(0, height - right.r - right.g); } if (y - 1 >= 0) { fu = Flow.b + Mathf.Max(0, height - up.r - up.g); } if (y + 1 < H) { fd = Flow.a + Mathf.Max(0, height - down.r - down.g); } float fout = fl + fr + fu + fd; float K = 0f; if (fout != 0) { K = Mathf.Min(1, curr.r / fout); } f.SetPixel(x, y, new Color(K * fl, K * fr, K * fu, K * fd)); } } t.Unlock(); f.Unlock(); }
public override void _Ready() { if (Terrain == null || Terrain.GetWidth() != TileSize || Terrain.GetHeight() != TileSize) { Terrain = new ImageTexture(); Terrain.Create(TileSize, TileSize, Image.Format.Rgbaf); terrainImg = new Image(); terrainImg.Create(TileSize, TileSize, false, Image.Format.Rgbaf); } if (Mesh == null) { var mesh = new PlaneMesh(); mesh.Size = new Vector2(TileSize, TileSize); mesh.SubdivideWidth = TileSize; mesh.SubdivideDepth = TileSize; Mesh = mesh; var mat = new ShaderMaterial(); mat.SetShader(TerrainShader); SetSurfaceMaterial(0, mat); } if (WorldPos != Vector3.Zero) { _LoadTerrainData(); } hydraulics = new HydraulicSimulation(Terrain); terrainImg = Terrain.GetData(); }
public void ExportTo(ImageTexture texture) { var image = texture.GetData() ?? new Image(); ExportTo(image); texture.CreateFromImage(image, 0); }
protected List <Texture> ChopSprites(string fileName, Texture texture, int frames, int position) { List <Texture> sprites = new List <Texture>(); Image sheet = texture.GetData(); int width = sheet.GetWidth(); int height = sheet.GetHeight(); const int size = GlobalConstants.SPRITE_TEXTURE_SIZE; int index = 0; if (this.CachedTiles.ContainsKey(fileName)) { if (this.CachedTiles[fileName].ContainsKey(position)) { for (index = position; index < position + frames; index++) { if (this.CachedTiles[fileName].ContainsKey(index)) { sprites.Add(this.CachedTiles[fileName][index]); } else { break; } } if (sprites.Count == frames) { return(sprites); } } } int p = 0; for (int y = 0; y < height; y += size) { for (int x = 0; x < width; x += size) { ImageTexture imageTexture = new ImageTexture(); imageTexture.CreateFromImage(sheet.GetRect(new Rect2(x, y, size, size)), 2); if (imageTexture.GetData().IsInvisible()) { continue; } imageTexture.ResourceLocalToScene = true; if (this.CachedTiles.ContainsKey(fileName)) { if (this.CachedTiles[fileName].ContainsKey(p) == false) { this.CachedTiles[fileName].Add(p, imageTexture); } } else { this.CachedTiles.Add( fileName, new System.Collections.Generic.Dictionary <int, Texture> { { p, imageTexture } }); } if (p >= position && p < position + frames) { sprites.Add(imageTexture); } else if (p > position + frames) { return(sprites); } p++; } } return(sprites); }