public static Texture CHECKERBOARD(int w, int h, int cellbits, uint oddColor, uint evenColor) { Texture t = new Texture(w, h); int pos = 0; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) t.pixel[pos++] = (((x >> cellbits) + (y >> cellbits)) & 1) == 0 ? evenColor : oddColor; return t; }
// TIP: For wooden textures public static Texture GRAIN(int w, int h, float persistency, float density, int samples, int levels, int scale) { initNoiseBuffer(); Texture t = new Texture(w, h); int pos = 0; float wavelength = (float) ((w > h) ? w : h) / density; float perlin; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { perlin = (float) levels * perlin2d(x, y, wavelength, persistency, samples); t.pixel[pos++] = (uint) ((float) scale * (perlin - (float) (int) perlin)); } return t; }
// Material loader public void loadMaterial(Material material) { color = material.color; transparency = material.transparency; reflectivity = material.reflectivity; texture = material.texture; if (material.envmap != null) envmap = material.envmap.pixel; else envmap = null; if (texture != null) { tw = texture.width - 1; th = texture.height - 1; tbitW = texture.bitWidth; tbitH = texture.bitHeight; } mode = 0; if (!material.flat) mode |= P; if (envmap != null) mode |= E; if (texture != null) mode |= T; if (material.wireframe) mode |= W; materialLoaded = true; ready = lightmapLoaded && materialLoaded; }
public Material(Texture t) { setTexture(t); reflectivity = 255; }
private void readTexture(BinaryReader inStream, bool textureId) { Texture t = null; switch (inStream.ReadSByte()) { case 1: t = new Texture(readString(inStream)); if ((t != null) && textureId) { this.texturePath = t.path; this.textureSettings = null; this.setTexture(t); } if (!((t == null) || textureId)) { this.envmapPath = t.path; this.envmapSettings = null; this.setEnvmap(t); } break; case 2: { int w = this.readInt(inStream); int h = this.readInt(inStream); int num4 = inStream.ReadSByte(); float persistency = this.readInt(inStream); float density = this.readInt(inStream); persistency = 0.5f; density = 0.5f; int samples = inStream.ReadByte(); int num8 = inStream.ReadByte(); uint[] colors = new uint[num8]; for (int i = 0; i < num8; i++) { colors[i] = (uint) this.readInt(inStream); } switch (num4) { case 1: t = TextureFactory.PERLIN(w, h, persistency, density, samples, 0x400).colorize(ColorUtility.makeGradient(colors, 0x400)); break; case 2: t = TextureFactory.WAVE(w, h, persistency, density, samples, 0x400).colorize(ColorUtility.makeGradient(colors, 0x400)); break; case 3: t = TextureFactory.GRAIN(w, h, persistency, density, samples, 20, 0x400).colorize(ColorUtility.makeGradient(colors, 0x400)); break; } if (textureId) { this.texturePath = null; this.textureSettings = new TextureSettings(t, w, h, num4, persistency, density, samples, colors); this.setTexture(t); } else { this.envmapPath = null; this.envmapSettings = new TextureSettings(t, w, h, num4, persistency, density, samples, colors); this.setEnvmap(t); } return; } } }
// Setters public void setTexture(Texture t) { texture = t; if (texture != null) texture.Resize(); }
public void setEnvmap(Texture env) { envmap = env; env.resize(256, 256); }
// Private part of image overlaying private void draw(uint[] buffer, int width, int height, Texture texture, int posx, int posy, int xsize, int ysize) { if (texture == null) return; int w = xsize; int h = ysize; int xBase = posx; int yBase = posy; int tx = texture.width * 255; int ty = texture.height * 255; int tw = texture.width; int dtx = tx / w; int dty = ty / h; int txBase = MathUtility.Crop(-xBase * dtx, 0, 255 * tx); int tyBase = MathUtility.Crop(-yBase * dty, 0, 255 * ty); int xend = MathUtility.Crop(xBase + w, 0, width); int yend = MathUtility.Crop(yBase + h, 0, height); int pos, offset1, offset2; xBase = MathUtility.Crop(xBase, 0, width); yBase = MathUtility.Crop(yBase, 0, height); ty = tyBase; for (int j = yBase; j < yend; j++) { tx = txBase; offset1 = j * width; offset2 = (ty >> 8) * tw; for (int i = xBase; i < xend; i++) { buffer[i + offset1] = texture.pixel[(tx >> 8) + offset2]; tx += dtx; } ty += dty; } }
// inverts the texture public Texture multiply(Texture multiplicative) { for (int i = width * height - 1; i >= 0; i--) pixel[i] = ColorUtility.multiply(pixel[i], multiplicative.pixel[i]); return this; }
// mixes the texture with another one public Texture mix(Texture newData) { for (int i = width * height - 1; i >= 0; i--) pixel[i] = ColorUtility.mix(pixel[i], newData.pixel[i]); return this; }
public Texture Clone() { Texture t = new Texture(width, height); MathUtility.copyBuffer(pixel, t.pixel); return t; }
// additive blends another texture with this public Texture add(Texture additive) { for (int i = width * height - 1; i >= 0; i--) pixel[i] = ColorUtility.add(pixel[i], additive.pixel[i]); return this; }
public static Texture blendTopDown(Texture top, Texture down) { down.resize(top.width, top.height); Texture t = new Texture(top.width, top.height); int pos = 0; uint alpha; for (int y = 0; y < top.height; y++) { alpha = (uint) (255 * y / (top.height - 1)); for (int x = 0; x < top.width; x++) { t.pixel[pos] = ColorUtility.transparency(down.pixel[pos], top.pixel[pos], alpha); pos++; } } return t; }
public void add(Texture texture, int posx, int posy, int xsize, int ysize) { add(pixel, width, height, texture, posx, posy, xsize, ysize); }
public static Texture WAVE(int w, int h, float persistency, float density, int samples, int scale) { initNoiseBuffer(); Texture t = new Texture(w, h); int pos = 0; float wavelength = (float) ((w > h) ? w : h) / density; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) t.pixel[pos++] = (uint) ((double) scale * (Math.Sin(32 * perlin2d(x, y, wavelength, persistency, samples)) * 0.5 + 0.5)); return t; }
/// <summary> /// Assigns new data for the texture. /// </summary> /// <param name="newData"></param> /// <returns></returns> public Texture put(Texture newData) { Array.Copy(newData.pixel, 0, pixel, 0, width * height); return this; }
// subtractive blends another texture with this public Texture sub(Texture subtractive) { for (int i = width * height - 1; i >= 0; i--) pixel[i] = ColorUtility.sub(pixel[i], subtractive.pixel[i]); return this; }
public void setBackground(Texture t) { environment.setBackground(t); }
public void drawBackground(Texture texture, int posx, int posy, int xsize, int ysize) { draw(p, w, h, texture, posx, posy, xsize, ysize); }