@spearwolf/twopoint5d
Version:
a library to create 2.5d realtime graphics and pixelart with three.js
103 lines • 2.94 kB
JavaScript
import { LinearFilter, LinearSRGBColorSpace, NearestFilter, SRGBColorSpace, Texture, TextureLoader, WebGLRenderer, } from 'three';
const TextureClasses = {
anisotrophy: {
anisotrophy: Infinity,
},
'anisotrophy-2': {
anisotrophy: 2,
},
'anisotrophy-4': {
anisotrophy: 4,
},
'no-anisotrophy': {
anisotrophy: 0,
},
nearest: {
magFilter: NearestFilter,
minFilter: NearestFilter,
},
'mag-nearest': {
magFilter: NearestFilter,
},
'min-nearest': {
minFilter: NearestFilter,
},
linear: {
magFilter: LinearFilter,
minFilter: LinearFilter,
},
'mag-linear': {
magFilter: LinearFilter,
},
'min-linear': {
minFilter: LinearFilter,
},
flipy: {
flipY: true,
},
'no-flipy': {
flipY: false,
},
srgb: {
colorSpace: SRGBColorSpace,
},
'linear-srgb': {
colorSpace: LinearSRGBColorSpace,
},
};
const TextureClassPriority = {
'no-anisotrophy': 1000,
'anisotrophy-2': 500,
'anisotrophy-4': 250,
anisotrophy: 0,
nearest: 1000,
'mag-nearest': 500,
'min-nearest': 500,
linear: 1000,
'mag-linear': 500,
'min-linear': 500,
flipy: 10,
'no-flipy': 0,
srgb: 0,
'linear-srgb': 10,
};
export class TextureFactory {
#maxAnisotrophy = 0;
#defaultOptions;
constructor(maxAnisotrophyOrRenderer = 0, defaultClassNames = ['nearest'], defaultOptions) {
this.#maxAnisotrophy =
typeof maxAnisotrophyOrRenderer === 'number'
? maxAnisotrophyOrRenderer
: (maxAnisotrophyOrRenderer.capabilities?.getMaxAnisotropy?.() ??
maxAnisotrophyOrRenderer.getMaxAnisotropy?.());
this.#defaultOptions = defaultOptions ?? {
anisotrophy: 0,
flipY: false,
};
this.#defaultOptions = this.getOptions(defaultClassNames);
this.textureLoader = new TextureLoader();
}
getOptions(classNames) {
const options = Object.assign({}, this.#defaultOptions, ...classNames
.map((className) => [TextureClassPriority[className], TextureClasses[className]])
.sort(([a], [b]) => b - a)
.map(([, opts]) => opts));
options.anisotrophy = Math.min(options.anisotrophy, this.#maxAnisotrophy);
return options;
}
create(source, ...classNames) {
const texture = new Texture(source);
return this.update(texture, ...classNames);
}
update(texture, ...classNames) {
Object.assign(texture, this.getOptions(classNames));
texture.needsUpdate = true;
return texture;
}
load(url, ...classNames) {
return this.textureLoader.load(url, (texture) => {
this.update(texture, ...classNames);
});
}
}
//# sourceMappingURL=TextureFactory.js.map