public void Start() { KZTexture texture = new KZTexture(32, 32); int edge = 4; Color transparent = new Color(0, 0, 0, 0); for(int y=0; y<texture.height; y++) { for(int x=0; x<texture.width; x++) { texture.SetPixel(x, y, transparent); } } for(int y=edge; y<texture.height-edge; y++) { for(int x=edge; x<texture.width-edge; x++) { texture.SetPixel(x, y, Color.white); } } //for(int y=0; y<texture.height / 2; y++) { // for(int x=0; x<texture.width / 2; x++) { // texture.SetPixel(x, y, Color.black); // } //} for(int i=0;i<1;i++) { texture = KZTexture.BoxBlur(texture); } Material material = new Material(Shader.Find(DEFAULT_SHADER)); material.mainTexture = texture.ToTexture2D(); GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Quad); obj.GetComponent<MeshRenderer>().material = material; }
public static KZTexture BoxBlur(KZTexture texture, float[,] kernel) { KZTexture buffer = new KZTexture( texture.width, texture.height); for(int x=0; x<texture.width; x++) { for(int y=0; y<texture.height; y++) { BlurPixel(texture, buffer, x, y, kernel); } } return buffer; }
public static KZTexture GetChequeredTexture( int w, int h, Color colorA, Color colorB) { KZTexture texture = new KZTexture(w, h); for(int i=0; i<texture.width; i++) { for(int j=0; j<texture.height; j++) { Color color = colorA; if(Even(i) && Odd(j) || Odd(i) && Even(j)) { color = colorB; } texture.SetPixel(i, j, color); } } return texture; }
public static KZTexture BoxBlur(KZTexture texture) { return BoxBlur(texture, box); }
private static void BlurPixel( KZTexture src, KZTexture dest, int x, int y, float[,] kernel) { Color color = new Color(0, 0, 0, 0); Color defaultColor = KZColor.GetColor(src.GetPixel(x, y), 0); int index = 0; int row = kernel.GetLength(0); int col = kernel.GetLength(1); int halfRow = row / 2; int halfCol = col / 2; for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { Color c = KZColor.Mul( src.GetPixel(x - halfCol + j, y - halfRow + i, defaultColor), kernel[i, j]); color.r += c.r; color.g += c.g; color.b += c.b; color.a += c.a; } } //Debug.Log(color); dest.SetPixel(x, y, color); }
public static KZTexture GetCircle(int radius, Color color) { int length = 2 * radius; KZTexture texture = new KZTexture(length, length); float middle = (length - 1) * .5f; Vector2 center = new Vector2(middle, middle); float sqrRadius = radius * radius; Color transparent = new Color(color.r, color.g, color.b, 0); for(int i=0; i<texture.width; i++) { for(int j=0; j<texture.height; j++) { if((new Vector2(i, j) - center).sqrMagnitude < sqrRadius) { texture.SetPixel(i, j, color); } else { texture.SetPixel(i, j, transparent); //texture.SetPixel(i, j, Color.black); } } } return texture; }
private static void ApplyPerlin( KZTexture texture, float perlinStart, float perlinScale) { for(int x=0; x<texture.width; x++) { float perlin = Mathf.PerlinNoise( perlinStart + (float)x / texture.width * perlinScale, 0); for(int y=0; y<texture.height; y++) { //Debug.Log(perlin); Color c = texture.GetPixel(x, y); texture.SetPixel(x, y, new Color( c.r, c.g, c.b, Mathf.Min(1, perlin * c.a))); } } }
private static void ApplyNoise(KZTexture texture, float intensity) { if(intensity == 0) return; for(int y=0; y<texture.height; y++) { for(int x=0; x<texture.width; x++) { Color c = texture.GetPixel(x, y); texture.SetPixel(x, y, new Color( c.r, c.g, c.b, c.a + Random.Range(-intensity, intensity))); } } }
private static void ApplyGradient(KZTexture texture, float maxAlpha) { for(int y=0; y<texture.height; y++) { float a = maxAlpha - ((float)y/(texture.height-1) * maxAlpha); for(int x=0; x<texture.width; x++) { Color c = texture.GetPixel(x, y); texture.SetPixel(x, y, new Color(c.r, c.g, c.b, c.a * a)); } } }
private static void ApplyColorWithTint( KZTexture texture, Color c, Color tint, float alphaScale) { for(int y=0; y<texture.height; y++) { Color t = KZColor.GetTint( c, tint, (float)y / (texture.height-1)); for(int x=0; x<texture.width; x++) { texture.SetPixel(x, y, new Color(t.r, t.g, t.b, t.a * alphaScale)); } } }
private static void ApplyColor( KZTexture texture, Color c, float alphaScale) { texture.Clear(new Color(c.r, c.g, c.b, c.a * alphaScale)); }
protected void Reinitialize() { if(lightMaterial == null) { throw new System.Exception("Please assign a material!"); } //lightMaterial = new Material(lightMaterial); // light.GetComponent<MeshRenderer>().material = lightMaterial; KZTexture texture = new KZTexture(textureWidth, textureHeight); Texture2D texture2d = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false); //TextureFormat.RGB24, false); texture2d.wrapMode = TextureWrapMode.Clamp; lightMaterial.mainTexture = CreateTexture(texture, texture2d); UpdateProperties(); }
protected Texture2D CreateTexture(KZTexture t, Texture2D t2d) { return Filter(t).ToTexture2D(t2d); }
/* private void PlaceLightsInCircle() { float angle = 0; for(int i=0; i<numberOfDuplicates; i++) { Vector3 diff = new Vector3( Mathf.Cos(angle), Mathf.Sin(angle), 0) * duplicateDiff; diff += new Vector3(0, 0, transform.position.z + duplicateZDiff * i); lights[i].transform.localPosition = diff; angle += TWO_PI / numberOfDuplicates; } } */ public virtual KZTexture Filter(KZTexture texture) { if(enableTint) ApplyColorWithTint(texture, color, tint, alpha); else ApplyColor(texture, color, alpha); if(enableFallOff) ApplyGradient(texture, alpha); /* //[ vertical blur float k = 1.0f/5; texture = KZTexture.BoxBlur(texture, //new float[,] {{k}, {k}, {k}, {k}, {k}}); new float[,] {{k, k, k, k, k}}); */ if(enablePerlin) ApplyPerlin(texture, perlinStart, perlinScale); ApplyNoise(texture, noiseIntensity); return texture; }
public void Start() { KZTexture kzt = new KZTexture(1, 1); kzt.SetPixel(0, 0, color); GetComponent<Renderer>().material.mainTexture = kzt.ToTexture2D(); }