cables_dev/cables/src/core/cg/cg_texture.js
const DEFAULT_TEXTURE_SIZE = 8;
export default class CgTexture
{
constructor(options = {})
{
this.id = CABLES.uuid();
this.width = 0;
this.height = 0;
this.name = "unknown";
options = options || {};
this.pixelFormat = options.pixelFormat || CgTexture.PFORMATSTR_RGBA8UB;
this.name = options.name || "unknown";
if (!options.width) options.width = DEFAULT_TEXTURE_SIZE;
if (!options.height) options.height = DEFAULT_TEXTURE_SIZE;
}
}
CgTexture.getDefaultTextureData = (name, size, options = {}) =>
{
if (name == "empty")
{
return new Uint8Array(size * size * 4).fill(0);
}
else
if (name == "color")
{
const data = new Uint8Array(size * size * 4);
let r = options.r || 1;
let g = options.g || 1;
let b = options.b || 1;
for (let x = 0; x < size * size; x++)
{
data[x * 4 + 0] = r;
data[x * 4 + 1] = g;
data[x * 4 + 2] = b;
data[x * 4 + 3] = 255;
}
return data;
}
else
if (name == "randomUInt")
{
const data = new Uint8Array(size * size * 4);
for (let x = 0; x < size * size; x++)
{
data[x * 4 + 0] = Math.random() * 255;
data[x * 4 + 1] = Math.random() * 255;
data[x * 4 + 2] = Math.random() * 255;
data[x * 4 + 3] = 255;
}
return data;
}
else
if (name == "random" || name == "randomFloat")
{
const data = new Float32Array(size * size * 4);
for (let x = 0; x < size * size; x++)
{
data[x * 4 + 0] = (Math.random() - 0.5) * 2.0;
data[x * 4 + 1] = (Math.random() - 0.5) * 2.0;
data[x * 4 + 2] = (Math.random() - 0.5) * 2.0;
data[x * 4 + 3] = 1;
}
return data;
}
else
if (name == "stripes")
{
const arr = [];
let r = options.r;
let g = options.g;
let b = options.b;
if (r === undefined)r = 1;
if (g === undefined)g = 1;
if (b === undefined)b = 1;
for (let y = 0; y < size; y++)
{
for (let x = 0; x < size; x++)
{
if ((x + y) % 64 < 32)
{
arr.push((200 + (y / size) * 25 + (x / size) * 25) * r);
arr.push((200 + (y / size) * 25 + (x / size) * 25) * g);
arr.push((200 + (y / size) * 25 + (x / size) * 25) * b);
}
else
{
arr.push((40 + (y / size) * 25 + (x / size) * 25) * r);
arr.push((40 + (y / size) * 25 + (x / size) * 25) * g);
arr.push((40 + (y / size) * 25 + (x / size) * 25) * b);
}
arr.push(255);
}
}
return new Uint8Array(arr);
}
else
{
console.warn("unknown default texture", name);
return CgTexture.getDefaultTextureData("stripes", size, { "r": 1, "g": 0, "b": 0 });
}
};
CgTexture.FILTER_NEAREST = 0;
CgTexture.FILTER_LINEAR = 1;
CgTexture.FILTER_MIPMAP = 2;
CgTexture.WRAP_REPEAT = 0;
CgTexture.WRAP_MIRRORED_REPEAT = 1;
CgTexture.WRAP_CLAMP_TO_EDGE = 2;
CgTexture.TYPE_DEFAULT = 0;
CgTexture.TYPE_DEPTH = 1;
CgTexture.TYPE_FLOAT = 2;
CgTexture.PFORMATSTR_RGB565 = "RGB 5/6/5bit ubyte";
CgTexture.PFORMATSTR_R8UB = "R 8bit ubyte";
CgTexture.PFORMATSTR_RG8UB = "RG 8bit ubyte";
CgTexture.PFORMATSTR_RGB8UB = "RGB 8bit ubyte";
CgTexture.PFORMATSTR_RGBA8UB = "RGBA 8bit ubyte";
CgTexture.PFORMATSTR_SRGBA8 = "SRGBA 8bit ubyte";
CgTexture.PFORMATSTR_R11FG11FB10F = "RGB 11/11/10bit float";
CgTexture.PFORMATSTR_R16F = "R 16bit float";
CgTexture.PFORMATSTR_RG16F = "RG 16bit float";
CgTexture.PFORMATSTR_RGB16F = "RGB 16bit float";
CgTexture.PFORMATSTR_RGBA16F = "RGBA 16bit float";
CgTexture.PFORMATSTR_R32F = "R 32bit float";
CgTexture.PFORMATSTR_RG32F = "RG 32bit float";
CgTexture.PFORMATSTR_RGB32F = "RGB 32bit float";
CgTexture.PFORMATSTR_RGBA32F = "RGBA 32bit float";
CgTexture.PFORMATSTR_DEPTH = "DEPTH";
CgTexture.PIXELFORMATS = [
CgTexture.PFORMATSTR_RGB565,
CgTexture.PFORMATSTR_R8UB,
CgTexture.PFORMATSTR_RG8UB,
CgTexture.PFORMATSTR_RGB8UB,
CgTexture.PFORMATSTR_RGBA8UB,
CgTexture.PFORMATSTR_SRGBA8,
CgTexture.PFORMATSTR_R11FG11FB10F,
CgTexture.PFORMATSTR_R16F,
CgTexture.PFORMATSTR_RG16F,
CgTexture.PFORMATSTR_RGBA16F,
CgTexture.PFORMATSTR_R32F,
CgTexture.PFORMATSTR_RGBA32F
];