import * as THREE from "three"; /** * Uniforms for `SpriteMaterial` */ export interface ISpriteUniforms { /** * Size of the tile as UVs: * `{ x: 0.1, y: 0.1 }` */ tileSize?: THREE.Uniform; /** * UV coordinates of the tile: * `{ x: 0.0, y: 0.9 }` */ tileCoord?: THREE.Uniform; /** * Amount of times to repeat the tile: * `{ x: 2, y: 2 }` */ tileFactor?: THREE.Uniform; /** * Rest of uniforms inherited from the super class */ [key: string]: THREE.IUniform | undefined; } /** * Options for `SpriteMaterial.tile()` */ export interface ISpriteTilingOptions { /** * Index or coordinates of a tile in the tileset. * For a tileset with a w/h of 128 and a tile size of * w/h 16, the coordinates/index for the second tile * would look like this: * `tile = { x: 16, y: 0 }` * `tile = 1` * * Default: `0` */ tile?: number | TVec; /** * Size of the tile in the tileset in px: * `{ x: 16, y: 16 }` */ tileSize?: TVec; /** * Size of the tileset in px: * `{ x: 128, y: 128 }` */ tilesetSize?: TVec; /** * Amount of times to repeat the tile on the sprite: * `{ x: 2, y: 2 }` * * Default: `{ x: 1, y: 1 }` */ repeat?: TVec; /** * Spacing between tiles in the tileset in px: * `spacing: 2` * * Default: `0` */ spacing?: number; } /** * Base material to render sprites: * * ``` * const sprite = new THREE.Mesh( * new THREE.PlaneGeometry(1, 1), * new SpriteMeshBasicMaterial({ map: myTileset }), * ); * sprite.material.tile({ * tile: 3, * tileSize: { x: 16, y: 32 }, * tilesetSize: { x: 96, y: 80 }, * }); * myScene.add(sprite); * ``` */ export declare abstract class SpriteMaterial extends THREE.Material { /** * The texture for the sprite. * https://threejs.org/docs/?q=basicmat#api/en/materials/MeshBasicMaterial.map */ map?: THREE.Texture | null; /** * Uniforms of the shader. May be set before shader * compilation. Can be used to manipulate tile/tileset * size/coordinates instead of calling `tile(options)`. * Useful to bypass px -> UV calculations of `tile()` in * case tiling data is already formatted as UVs: * * ``` * const mat = new SpriteMaterial({ map: myTexture }); * mat.uniforms = { * tileSize: { value: new THREE.Vector2(0.5, 0.5) }, * tileCoord: { value: new THREE.Vector2(0, 0.5) }, * tileFactor: { value: new THREE.Vector2(1, 1) }, * } * let n = 0; * while (await new Promise(res => setTimeout(res, 100))) { * mat.uniforms.tileCoord.value.set(0.5 * n, 0.5); * n = Number(!n); * } * ``` */ uniforms?: ISpriteUniforms; /** * The tiling options set via `.tile()`. Manipulating them * directly takes no effect until `.tile()` is called again. */ tiling?: Required>; /** * Sets the tiling options: * * ``` * const sprite = new THREE.Mesh( * new THREE.PlaneGeometry(1, 1), * new SpriteMeshBasicMaterial({ map: myTileset }), * ); * sprite.material.tile({ * tile: 3, * tileSize: { x: 16, y: 32 }, * tilesetSize: { x: 96, y: 80 }, * }); * myScene.add(sprite); * ``` * * @param {ISpriteTilingOptions} options Tiling options to set. * @returns {void} */ tile(options: ISpriteTilingOptions): void; /** * Overrides `THREE.Material.onBeforeCompile()`. * https://threejs.org/docs/?q=Material#api/en/materials/Material.customProgramCacheKey * * Returns a custom shader cache key identifying the base * material and tiling type. When inheriting from this class * and overriding this method, ensure to adopt the original * key to prevent stale shaders across different configurationsL * * ``` * customProgramCacheKey() { * const originalKey = super.customProgramCacheKey(); * return `${originalKey}-${myKey}`; * } * ``` * * @returns {string} */ customProgramCacheKey(): string; /** * Overrides `THREE.Material.onBeforeCompile()`. * https://threejs.org/docs/?q=Material#api/en/materials/Material.onBeforeCompile * * Injects tiling shader fragments into the material's original * shader program. When inheriting from this class and overriding * this method, ensure to call `super.onBeforeCompile()` * or `this.injectShaderFragments()` to ensure the tiling shader * artifacts are injected. * * ``` * onBeforeCompile(shader) { * // My shader manipulation logic here... * this.injectShaderFragments(shader); * } * ``` * * @param {THREE.WebGLProgramParametersWithUniforms} shader * The shader provided by the renderer. * @returns {void} */ onBeforeCompile(shader: THREE.WebGLProgramParametersWithUniforms): void; /** * Injects tiling shader fragments into the material's original * shader program. * * @param {THREE.WebGLProgramParametersWithUniforms} shader * The shader provided by the renderer. * @returns {void} */ protected injectShaderFragments(shader: THREE.WebGLProgramParametersWithUniforms): void; /** * Used internally to merge `ISpriteTilingOptions` from`.tile()` * with Required> on * `this.tiling`. * * @param {ISpriteTilingOptions} options Tiling options to set. * @returns {void} */ setTilingOptions(options: ISpriteTilingOptions): void; /** * Used internally to merge pre-existing uniforms after the * shader is (re-)compiled. * * @param {THREE.WebGLProgramParametersWithUniforms} shader * The shader provided by the renderer. * @returns {void} */ mergeUniforms(shader: THREE.WebGLProgramParametersWithUniforms): void; /** * Static method to create mixins of `THREE.Material`-based * classes and `SpriteMaterial`. Used internally to generate * mixin classes. Generates a new class instead of extending * the original class. Hence do not use to create mixins * during runtime or you'll risk leaking memory. * * @param {T} ctor The class to extend. * @returns {new () => T & TiledMaterial} */ static extendClass THREE.Material, TParam extends THREE.MaterialParameters>(ctor: TCtor): new (args: TParam) => TCtor & SpriteMaterial; /** * Static method to extend instanciated materials by * `SpriteMaterial`'s prototype. * * @param {T} material The material to extend. * @returns {SpriteMaterial & T} */ static extendMaterial(material: T): SpriteMaterial & T; }