- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
import { Program } from './Program';
import { UniformGroup } from './UniformGroup';
import type { Dict } from '@pixi/utils';
/**
* A helper class for shaders.
* @memberof PIXI
*/
export class Shader
{
/** Program that the shader uses. */
public program: Program;
public uniformGroup: UniformGroup;
/**
* Used internally to bind uniform buffer objects.
* @ignore
*/
uniformBindCount = 0;
/**
* @param program - The program the shader will use.
* @param uniforms - Custom uniforms to use to augment the built-in ones.
*/
constructor(program: Program, uniforms?: Dict<any>)
{
this.program = program;
// lets see whats been passed in
// uniforms should be converted to a uniform group
if (uniforms)
{
if (uniforms instanceof UniformGroup)
{
this.uniformGroup = uniforms;
}
else
{
this.uniformGroup = new UniformGroup(uniforms);
}
}
else
{
this.uniformGroup = new UniformGroup({});
}
}
// TODO move to shader system..
checkUniformExists(name: string, group: UniformGroup): boolean
{
if (group.uniforms[name])
{
return true;
}
for (const i in group.uniforms)
{
const uniform = group.uniforms[i];
if (uniform.group)
{
if (this.checkUniformExists(name, uniform))
{
return true;
}
}
}
return false;
}
destroy(): void
{
// usage count on programs?
// remove if not used!
this.uniformGroup = null;
}
/**
* Shader uniform values, shortcut for `uniformGroup.uniforms`.
* @readonly
*/
get uniforms(): Dict<any>
{
return this.uniformGroup.uniforms;
}
/**
* A short hand function to create a shader based of a vertex and fragment shader.
* @param vertexSrc - The source of the vertex shader.
* @param fragmentSrc - The source of the fragment shader.
* @param uniforms - Custom uniforms to use to augment the built-in ones.
* @returns A shiny new PixiJS shader!
*/
static from(vertexSrc?: string, fragmentSrc?: string, uniforms?: Dict<any>): Shader
{
const program = Program.from(vertexSrc, fragmentSrc);
return new Shader(program, uniforms);
}
}