UNPKG

2.08 kBJavaScriptView Raw
1import { PRECISION } from '@pixi/constants';
2import { ProgramCache, isMobile } from '@pixi/utils';
3import defaultFragment from './defaultProgram.mjs';
4import defaultVertex from './defaultProgram2.mjs';
5import './utils/index.mjs';
6import { setPrecision } from './utils/setPrecision.mjs';
7import { getMaxFragmentPrecision } from './utils/getMaxFragmentPrecision.mjs';
8
9let UID = 0;
10const nameCache = {};
11const _Program = class {
12 constructor(vertexSrc, fragmentSrc, name = "pixi-shader", extra = {}) {
13 this.extra = {};
14 this.id = UID++;
15 this.vertexSrc = vertexSrc || _Program.defaultVertexSrc;
16 this.fragmentSrc = fragmentSrc || _Program.defaultFragmentSrc;
17 this.vertexSrc = this.vertexSrc.trim();
18 this.fragmentSrc = this.fragmentSrc.trim();
19 this.extra = extra;
20 if (this.vertexSrc.substring(0, 8) !== "#version") {
21 name = name.replace(/\s+/g, "-");
22 if (nameCache[name]) {
23 nameCache[name]++;
24 name += `-${nameCache[name]}`;
25 } else {
26 nameCache[name] = 1;
27 }
28 this.vertexSrc = `#define SHADER_NAME ${name}
29${this.vertexSrc}`;
30 this.fragmentSrc = `#define SHADER_NAME ${name}
31${this.fragmentSrc}`;
32 this.vertexSrc = setPrecision(this.vertexSrc, _Program.defaultVertexPrecision, PRECISION.HIGH);
33 this.fragmentSrc = setPrecision(this.fragmentSrc, _Program.defaultFragmentPrecision, getMaxFragmentPrecision());
34 }
35 this.glPrograms = {};
36 this.syncUniforms = null;
37 }
38 static get defaultVertexSrc() {
39 return defaultVertex;
40 }
41 static get defaultFragmentSrc() {
42 return defaultFragment;
43 }
44 static from(vertexSrc, fragmentSrc, name) {
45 const key = vertexSrc + fragmentSrc;
46 let program = ProgramCache[key];
47 if (!program) {
48 ProgramCache[key] = program = new _Program(vertexSrc, fragmentSrc, name);
49 }
50 return program;
51 }
52};
53let Program = _Program;
54Program.defaultVertexPrecision = PRECISION.HIGH;
55Program.defaultFragmentPrecision = isMobile.apple.device ? PRECISION.HIGH : PRECISION.MEDIUM;
56
57export { Program };
58//# sourceMappingURL=Program.mjs.map