|
| 1 | +import { Filter, GlProgram, GpuProgram } from 'pixi.js'; |
| 2 | +import { vertex, wgslVertex } from '../defaults'; |
| 3 | +import fragment from './simplex.frag'; |
| 4 | +import source from './simplex.wgsl'; |
| 5 | + |
| 6 | +/** Options for the SimplexNoiseFilter constructor. */ |
| 7 | +export interface SimplexNoiseFilterOptions |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Noise map strength. |
| 11 | + * @default 0.5 |
| 12 | + */ |
| 13 | + strength?: number; |
| 14 | + /** |
| 15 | + * Noise map scale. |
| 16 | + * @default 10.0 |
| 17 | + */ |
| 18 | + noiseScale?: number; |
| 19 | + /** |
| 20 | + * Horizontal offset for the noise map. |
| 21 | + * @default 0 |
| 22 | + */ |
| 23 | + offsetX?: number; |
| 24 | + /** |
| 25 | + * Vertical offset for the noise map. |
| 26 | + * @default 0 |
| 27 | + */ |
| 28 | + offsetY?: number; |
| 29 | + /** |
| 30 | + * Depth offset for the noise map. |
| 31 | + * @default 0 |
| 32 | + */ |
| 33 | + offsetZ?: number; |
| 34 | + /** |
| 35 | + * The threshold used with the step function to create a blocky effect in the noise pattern. |
| 36 | + * When this is greater than 0, the step function is used to compare the noise value to this threshold. |
| 37 | + * @default -1 |
| 38 | + */ |
| 39 | + step?: number; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * The SimplexNoiseFilter multiplies simplex noise with the current texture data. <br> |
| 44 | + *  |
| 45 | + * @class |
| 46 | + * @extends Filter |
| 47 | + * @see {@link https://www.npmjs.com/package/pixi-filters|pixi-filters} |
| 48 | + */ |
| 49 | +export class SimplexNoiseFilter extends Filter |
| 50 | +{ |
| 51 | + /** Default constructor options. */ |
| 52 | + public static readonly defaults: SimplexNoiseFilterOptions = { |
| 53 | + strength: 0.5, |
| 54 | + noiseScale: 10.0, |
| 55 | + offsetX: 0, |
| 56 | + offsetY: 0, |
| 57 | + offsetZ: 0, |
| 58 | + step: -1, |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * @param options - Options for the SimplexNoise constructor. |
| 63 | + */ |
| 64 | + constructor(options?: SimplexNoiseFilterOptions) |
| 65 | + { |
| 66 | + options = { ...SimplexNoiseFilter.defaults, ...options }; |
| 67 | + |
| 68 | + const gpuProgram = GpuProgram.from({ |
| 69 | + vertex: { |
| 70 | + source: wgslVertex, |
| 71 | + entryPoint: 'mainVertex', |
| 72 | + }, |
| 73 | + fragment: { |
| 74 | + source, |
| 75 | + entryPoint: 'mainFragment', |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const glProgram = GlProgram.from({ |
| 80 | + vertex, |
| 81 | + fragment, |
| 82 | + name: 'simplex-filter', |
| 83 | + }); |
| 84 | + |
| 85 | + super({ |
| 86 | + gpuProgram, |
| 87 | + glProgram, |
| 88 | + resources: { |
| 89 | + simplexUniforms: { |
| 90 | + uStrength: { value: options?.strength ?? 0, type: 'f32' }, |
| 91 | + uNoiseScale: { value: options?.noiseScale ?? 0, type: 'f32' }, |
| 92 | + uOffsetX: { value: options?.offsetX ?? 0, type: 'f32' }, |
| 93 | + uOffsetY: { value: options?.offsetY ?? 0, type: 'f32' }, |
| 94 | + uOffsetZ: { value: options?.offsetZ ?? 0, type: 'f32' }, |
| 95 | + uStep: { value: options?.step ?? 0, type: 'f32' }, |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Strength of the noise (color = (noiseMap + strength) * texture) |
| 103 | + * @default 0.5 |
| 104 | + */ |
| 105 | + get strength(): number { return this.resources.simplexUniforms.uniforms.uStrength; } |
| 106 | + set strength(value: number) { this.resources.simplexUniforms.uniforms.uStrength = value; } |
| 107 | + |
| 108 | + /** |
| 109 | + * Noise map scale. |
| 110 | + * @default 10 |
| 111 | + */ |
| 112 | + get noiseScale(): number { return this.resources.simplexUniforms.uniforms.uNoiseScale; } |
| 113 | + set noiseScale(value: number) { this.resources.simplexUniforms.uniforms.uNoiseScale = value; } |
| 114 | + |
| 115 | + /** |
| 116 | + * Horizontal offset for the noise map. |
| 117 | + * @default 0 |
| 118 | + */ |
| 119 | + get offsetX(): number { return this.resources.simplexUniforms.uniforms.uOffsetX; } |
| 120 | + set offsetX(value: number) { this.resources.simplexUniforms.uniforms.uOffsetX = value; } |
| 121 | + |
| 122 | + /** |
| 123 | + * Vertical offset for the noise map. |
| 124 | + * @default 0 |
| 125 | + */ |
| 126 | + get offsetY(): number { return this.resources.simplexUniforms.uniforms.uOffsetY; } |
| 127 | + set offsetY(value: number) { this.resources.simplexUniforms.uniforms.uOffsetY = value; } |
| 128 | + |
| 129 | + /** |
| 130 | + * Depth offset for the noise map. |
| 131 | + * @default 0 |
| 132 | + */ |
| 133 | + get offsetZ(): number { return this.resources.simplexUniforms.uniforms.uOffsetZ; } |
| 134 | + set offsetZ(value: number) { this.resources.simplexUniforms.uniforms.uOffsetZ = value; } |
| 135 | + |
| 136 | + /** |
| 137 | + * The threshold used with the step function to create a blocky effect in the noise pattern. |
| 138 | + * When this is greater than 0, the step function is used to compare the noise value to this threshold. |
| 139 | + * @default -1 |
| 140 | + */ |
| 141 | + get step(): number { return this.resources.simplexUniforms.uniforms.uStep; } |
| 142 | + set step(value: number) { this.resources.simplexUniforms.uniforms.uStep = value; } |
| 143 | +} |
0 commit comments