playcanvas
Version:
PlayCanvas WebGL game engine
73 lines (70 loc) • 1.64 kB
JavaScript
import { Debug } from '../../core/debug.js';
import { PIXELFORMAT_RGBA8 } from './constants.js';
import { DeviceCache } from './device-cache.js';
import { Texture } from './texture.js';
var textureData = {
white: [
255,
255,
255,
255
],
gray: [
128,
128,
128,
255
],
black: [
0,
0,
0,
255
],
normal: [
128,
128,
255,
255
],
pink: [
255,
128,
255,
255
]
};
// class used to hold LUT textures in the device cache
class BuiltInTextures {
destroy() {
this.map.forEach((texture)=>{
texture.destroy();
});
}
constructor(){
/** @type Map<string, Texture> */ this.map = new Map();
}
}
// device cache storing built-in textures, taking care of their removal when the device is destroyed
var deviceCache = new DeviceCache();
var getBuiltInTexture = (device, name)=>{
var cache = deviceCache.get(device, ()=>{
return new BuiltInTextures();
});
if (!cache.map.has(name)) {
var texture = new Texture(device, {
name: "built-in-texture-" + name,
width: 1,
height: 1,
format: PIXELFORMAT_RGBA8
});
var pixels = texture.lock();
var data = textureData[name];
Debug.assert(data, "Data for built-in texture '" + name + "' not found");
pixels.set(data);
texture.unlock();
cache.map.set(name, texture);
}
return cache.map.get(name);
};
export { getBuiltInTexture };