Created
February 10, 2020 04:58
-
-
Save Frooxius/e6ed7504ec41b36f6cac0f6c2b7bdfea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using BaseX; | |
using CodeX; | |
namespace FrooxEngine | |
{ | |
[Category("Assets/Procedural Textures")] | |
public class GridTexture : ProceduralTexture | |
{ | |
public readonly Sync<color> BackgroundColor; | |
public readonly SyncList<Grid> Grids; | |
public class Grid : SyncObject | |
{ | |
public readonly Sync<int2> Spacing; | |
public readonly Sync<int2> Offset; | |
public readonly Sync<int2> Width; | |
public readonly Sync<color> LineColor; | |
} | |
List<Grid> _grids = new List<Grid>(); | |
protected override void OnAwake() | |
{ | |
BackgroundColor.Value = color.White; | |
Mipmaps.Value = true; | |
} | |
protected override void OnAttach() | |
{ | |
var grid = Grids.Add(); | |
grid.Spacing.Value = int2.One * 32; | |
grid.Offset.Value = int2.One * 16; | |
grid.Width.Value = int2.One * 4; | |
grid.LineColor.Value = color.Black; | |
} | |
protected override void PrepareAssetUpdateData() | |
{ | |
// cache grids | |
_grids.AddRange(Grids); | |
} | |
protected override void ClearTextureData() | |
{ | |
_grids.Clear(); | |
} | |
protected override void UpdateTextureData(Bitmap2D tex2D) | |
{ | |
tex2D.Clear(BackgroundColor); | |
// Render the grids | |
foreach(var grid in _grids) | |
{ | |
var spacing = grid.Spacing.Value; | |
var offset = grid.Offset.Value; | |
var width = grid.Width.Value; | |
var color = grid.LineColor.Value; | |
// Vertical lines | |
int xPos = MathX.Repeat(offset.x, spacing.x) - spacing.x; | |
int yPos = MathX.Repeat(offset.y, spacing.y) - spacing.y; | |
while (xPos < (tex2D.Size.x + width.x)) | |
{ | |
for(int w = 0; w < width.x; w++) | |
{ | |
var off = ((w + 1) / 2) * (w%2 == 0 ? 1 : -1); | |
var x = xPos + off; | |
if (x < 0 || x >= tex2D.Size.x) | |
continue; | |
for (int y = 0; y < tex2D.Size.y; y++) | |
tex2D.SetPixel(x, y, color); | |
} | |
xPos += spacing.x; | |
} | |
while (yPos < (tex2D.Size.y + width.y)) | |
{ | |
for (int w = 0; w < width.y; w++) | |
{ | |
var off = ((w + 1) / 2) * (w % 2 == 0 ? 1 : -1); | |
var y = yPos + off; | |
if (y < 0 || y >= tex2D.Size.y) | |
continue; | |
for (int x = 0; x < tex2D.Size.x; x++) | |
tex2D.SetPixel(x, y, color); | |
} | |
yPos += spacing.y; | |
} | |
} | |
_grids.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment