UNPKG

@spearwolf/twopoint5d

Version:

Create 2.5D realtime graphics and pixelart with WebGL and three.js

102 lines 2.84 kB
import { LinearFilter, LinearSRGBColorSpace, NearestFilter, SRGBColorSpace, Texture, TextureLoader, } from 'three/webgpu'; 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.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