bg2e-js 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/dist/bg2e-js.js +11001 -0
  2. package/dist/bg2e-js.js.map +1 -0
  3. package/package.json +17 -6
  4. package/app/AppController.js +0 -32
  5. package/app/Canvas.js +0 -98
  6. package/app/EventBase.js +0 -8
  7. package/app/KeyboardEvent.js +0 -51
  8. package/app/MainLoop.js +0 -251
  9. package/app/MouseEvent.js +0 -77
  10. package/app/TouchEvent.js +0 -15
  11. package/base/Color.js +0 -131
  12. package/base/Environment.js +0 -78
  13. package/base/Light.js +0 -176
  14. package/base/Material.js +0 -619
  15. package/base/PolyList.js +0 -336
  16. package/base/Texture.js +0 -545
  17. package/db/Bg2LoaderPlugin.js +0 -113
  18. package/db/DBPluginApi.js +0 -40
  19. package/db/Loader.js +0 -91
  20. package/db/LoaderPlugin.js +0 -26
  21. package/db/MtlParser.js +0 -7
  22. package/db/ObjLoaderPlugin.js +0 -51
  23. package/db/ObjParser.js +0 -233
  24. package/db/ObjWriterPlugin.js +0 -18
  25. package/db/VitscnjLoaderPlugin.js +0 -87
  26. package/db/Writer.js +0 -49
  27. package/db/WriterPlugin.js +0 -20
  28. package/debug/DebugRenderer.js +0 -138
  29. package/debug/WebGLTextureViewer.js +0 -67
  30. package/manipulation/SelectionBuffer.js +0 -65
  31. package/manipulation/SelectionHighlight.js +0 -69
  32. package/manipulation/SelectionIdAssignVisitor.js +0 -86
  33. package/manipulation/SelectionManager.js +0 -134
  34. package/manipulation/SelectionMode.js +0 -6
  35. package/math/Mat3.js +0 -248
  36. package/math/Mat4.js +0 -694
  37. package/math/MatrixStrategy.js +0 -23
  38. package/math/Quat.js +0 -60
  39. package/math/Vec.js +0 -728
  40. package/math/constants.js +0 -44
  41. package/math/functions.js +0 -104
  42. package/math/index.js +0 -74
  43. package/phsics/joint.js +0 -124
  44. package/primitives/arrow.js +0 -58
  45. package/primitives/cone.js +0 -137
  46. package/primitives/cube.js +0 -59
  47. package/primitives/cylinder.js +0 -215
  48. package/primitives/index.js +0 -13
  49. package/primitives/plane.js +0 -31
  50. package/primitives/sphere.js +0 -808
  51. package/render/BRDFIntegrationMap.js +0 -4
  52. package/render/Environment.js +0 -105
  53. package/render/EnvironmentRenderer.js +0 -12
  54. package/render/FrameBuffer.js +0 -26
  55. package/render/MaterialRenderer.js +0 -28
  56. package/render/Pipeline.js +0 -83
  57. package/render/PolyListRenderer.js +0 -42
  58. package/render/RenderBuffer.js +0 -189
  59. package/render/RenderQueue.js +0 -162
  60. package/render/RenderState.js +0 -82
  61. package/render/Renderer.js +0 -202
  62. package/render/SceneAppController.js +0 -208
  63. package/render/SceneRenderer.js +0 -310
  64. package/render/Shader.js +0 -21
  65. package/render/ShadowRenderer.js +0 -159
  66. package/render/SkyCube.js +0 -77
  67. package/render/SkySphere.js +0 -94
  68. package/render/TextureMergerRenderer.js +0 -58
  69. package/render/TextureRenderer.js +0 -29
  70. package/render/webgl/FrameBuffer.js +0 -9
  71. package/render/webgl/MaterialRenderer.js +0 -99
  72. package/render/webgl/Pipeline.js +0 -82
  73. package/render/webgl/PolyListRenderer.js +0 -224
  74. package/render/webgl/RenderBuffer.js +0 -244
  75. package/render/webgl/Renderer.js +0 -239
  76. package/render/webgl/SceneRenderer.js +0 -43
  77. package/render/webgl/ShaderProgram.js +0 -348
  78. package/render/webgl/ShadowRenderer.js +0 -6
  79. package/render/webgl/SkyCube.js +0 -15
  80. package/render/webgl/SkySphere.js +0 -14
  81. package/render/webgl/State.js +0 -149
  82. package/render/webgl/TextureRenderer.js +0 -167
  83. package/render/webgl/VertexBuffer.js +0 -128
  84. package/scene/Camera.js +0 -378
  85. package/scene/Chain.js +0 -43
  86. package/scene/ChainJoint.js +0 -55
  87. package/scene/Component.js +0 -146
  88. package/scene/ComponentMap.js +0 -99
  89. package/scene/Drawable.js +0 -130
  90. package/scene/EnvironmentComponent.js +0 -123
  91. package/scene/FindNodeVisitor.js +0 -55
  92. package/scene/LightComponent.js +0 -146
  93. package/scene/MatrixState.js +0 -39
  94. package/scene/Node.js +0 -300
  95. package/scene/NodeVisitor.js +0 -12
  96. package/scene/OrbitCameraController.js +0 -407
  97. package/scene/SmoothOrbitCameraController.js +0 -92
  98. package/scene/Transform.js +0 -74
  99. package/scene/index.js +0 -24
  100. package/shaders/BasicDiffuseColorShader.js +0 -91
  101. package/shaders/BasicPBRLightShader.js +0 -238
  102. package/shaders/DebugRenderShader.js +0 -79
  103. package/shaders/DepthRenderShader.js +0 -69
  104. package/shaders/IrradianceMapCubeShader.js +0 -99
  105. package/shaders/PBRLightIBLShader.js +0 -380
  106. package/shaders/PickSelectionShader.js +0 -75
  107. package/shaders/PresentDebugFramebufferShader.js +0 -90
  108. package/shaders/PresentTextureShader.js +0 -73
  109. package/shaders/SelectionHighlightShader.js +0 -98
  110. package/shaders/ShaderFunction.js +0 -72
  111. package/shaders/SkyCubeShader.js +0 -78
  112. package/shaders/SkySphereShader.js +0 -77
  113. package/shaders/SpecularMapCubeShader.js +0 -145
  114. package/shaders/TextureMergerShader.js +0 -127
  115. package/shaders/webgl_shader_lib.js +0 -187
  116. package/tools/BinaryResourceProvider.js +0 -15
  117. package/tools/ImageResourceProvider.js +0 -65
  118. package/tools/MaterialModifier.js +0 -228
  119. package/tools/Resource.js +0 -177
  120. package/tools/ResourceProvider.js +0 -56
  121. package/tools/TextResourceProvider.js +0 -24
  122. package/tools/TextureCache.js +0 -44
  123. package/tools/TextureResourceDatabase.js +0 -87
  124. package/tools/UserAgent.js +0 -294
  125. package/tools/VideoResourceProvider.js +0 -51
  126. package/tools/WriteStrategy.js +0 -22
  127. package/tools/base64.js +0 -15
  128. package/tools/crypto.js +0 -15
  129. package/tools/endiantess.js +0 -15
  130. package/tools/image.js +0 -15
  131. package/tools/index.js +0 -33
  132. package/tools/processType.js +0 -56
@@ -1,98 +0,0 @@
1
-
2
- import Texture, { TextureTargetName } from '../base/Texture';
3
- import Shader from '../render/Shader';
4
- import ShaderProgram from '../render/webgl/ShaderProgram';
5
- import ShaderFunction from './ShaderFunction';
6
- import { applyConvolution } from './webgl_shader_lib';
7
- import Vec from '../math/Vec';
8
-
9
- const g_code = {
10
- webgl: {
11
- vertex: `precision mediump float;
12
-
13
- attribute vec3 position;
14
- attribute vec2 texCoord;
15
-
16
- varying vec2 fragTexCoord;
17
-
18
- void main() {
19
- fragTexCoord = texCoord;
20
- gl_Position = vec4(position, 1.0);
21
- }`,
22
-
23
- fragment: ShaderFunction.GetShaderCode(`precision mediump float;
24
- varying vec2 fragTexCoord;
25
-
26
- uniform sampler2D uTexture;
27
- uniform float uConvMatrix[9];
28
- uniform vec4 uBorderColor;
29
- uniform float uBorderWidth;
30
- uniform vec2 uTexSize;
31
- `,
32
- [
33
- new ShaderFunction('void','main','',`{
34
- vec4 selectionColor = applyConvolution(uTexture, fragTexCoord, uTexSize, uConvMatrix, uBorderWidth);
35
- if (selectionColor.r!=0.0 && selectionColor.g!=0.0 && selectionColor.b!=0.0) {
36
- gl_FragColor = uBorderColor;
37
- }
38
- else {
39
- discard;
40
- }
41
- }`, [applyConvolution])
42
- ])
43
- }
44
- }
45
-
46
- export default class SelectionHighlightShader extends Shader {
47
- constructor(renderer) {
48
- super(renderer);
49
-
50
- if (renderer.typeId !== "WebGL") {
51
- throw Error("SelectionHighlightShader is only compatible with WebGL renderer");
52
- }
53
- }
54
-
55
- async load() {
56
- const { gl } = this.renderer;
57
-
58
- this._program = new ShaderProgram(gl, "SelectionHighlightShader");
59
- this._program.attachVertexSource(g_code.webgl.vertex);
60
- this._program.attachFragmentSource(g_code.webgl.fragment);
61
- this._program.link();
62
-
63
- this._borderWidth = 3;
64
- this._borderColor = new Vec([0.0, 0.7, 1, 1.0]);
65
- this._convMatrix = [
66
- 0, 1, 0,
67
- 1,-4, 1,
68
- 0, 1, 0
69
- ];
70
- }
71
-
72
- setup(plistRenderer, materialRenderer, modelMatrix, viewMatrix, projectionMatrix) {
73
- const { gl, viewport } = this.renderer;
74
-
75
- this.renderer.state.shaderProgram = this._program;
76
-
77
- gl.activeTexture(gl.TEXTURE0);
78
- this._program.uniform1i('uTexture', 0);
79
- this._program.uniform2f('uTexSize', viewport[2], viewport[3]);
80
- this._program.uniform1f('uBorderWidth', this._borderWidth);
81
- this._program.uniform4fv('uBorderColor', this._borderColor);
82
- this._program.uniform1fv('uConvMatrix', this._convMatrix, 4);
83
-
84
- const material = materialRenderer.material;
85
- if (material.diffuse instanceof Texture) {
86
- const webglTexture = materialRenderer.getTextureRenderer('diffuse').getApiObject();
87
- const target = TextureTargetName[material.diffuse.target];
88
- gl.bindTexture(gl[target], webglTexture);
89
-
90
- }
91
- else {
92
- throw new Error("PresentTextureShader: invalid material setup. The diffuse material attribute must to be a texture");
93
- }
94
-
95
- this._program.positionAttribPointer(plistRenderer.positionAttribParams("position"));
96
- this._program.texCoordAttribPointer(plistRenderer.texCoord0AttribParams("texCoord"));
97
- }
98
- }
@@ -1,72 +0,0 @@
1
-
2
-
3
- const isRequired = (candidateFunction,includedFunctions) => {
4
- return !includedFunctions.find(includedFunc => includedFunc.name === candidateFunction.name);
5
- }
6
-
7
- const getAllDependencies = (fn, result = []) => {
8
- fn.dependencies.forEach(depFn => {
9
- getAllDependencies(depFn, result);
10
- result.push(depFn);
11
- });
12
- result.push(fn);
13
- }
14
-
15
- const getDependencies = (fn) => {
16
- const allFunctions = [];
17
- getAllDependencies(fn, allFunctions);
18
- const includedFunctions = [];
19
- return allFunctions.filter(candidateFn => {
20
- if (includedFunctions.indexOf(candidateFn.name) === -1) {
21
- includedFunctions.push(candidateFn.name);
22
- return true;
23
- }
24
- });
25
- }
26
-
27
- export default class ShaderFunction {
28
- constructor(returnType, name, params, body, deps = []) {
29
- this._returnType = returnType;
30
- this._name = name;
31
- this._params = params;
32
- this._body = body;
33
- this._deps = deps;
34
- }
35
-
36
- get returnType() {
37
- return this._returnType;
38
- }
39
-
40
- get name() {
41
- return this._name;
42
- }
43
-
44
- get params() {
45
- return this._params;
46
- }
47
-
48
- get body() {
49
- return this._body;
50
- }
51
-
52
- get dependencies() {
53
- return this._deps;
54
- }
55
-
56
- getFunctionText() {
57
- return `${this.returnType} ${this.name}(${this.params}) ${this.body}`;
58
- }
59
-
60
- static GetShaderCode(header, requiredFunctions) {
61
- let allFunctions = [];
62
- requiredFunctions.forEach(req => {
63
- allFunctions = [...allFunctions, ...getDependencies(req, allFunctions)];
64
- });
65
- let code = header;
66
- allFunctions.forEach(fn => {
67
- code += fn.getFunctionText() + "\n\n";
68
- });
69
-
70
- return code;
71
- }
72
- }
@@ -1,78 +0,0 @@
1
- import Mat4 from "../math/Mat4";
2
- import Vec from "../math/Vec";
3
- import Shader from "../render/Shader";
4
- import ShaderProgram from "../render/webgl/ShaderProgram";
5
-
6
- const g_code = {
7
- webgl: {
8
- vertex: `precision mediump float;
9
-
10
- attribute vec3 vertPosition;
11
-
12
- uniform mat4 uMVP;
13
-
14
- varying vec3 fragNormal;
15
-
16
- void main() {
17
- gl_Position = uMVP * vec4(vertPosition,1.0);
18
-
19
- // The normal can be extracted from the position
20
- // since this shader is designed to be used with
21
- // a cube centered in 0,0,0
22
- fragNormal = normalize(vertPosition);
23
- }
24
- `,
25
-
26
- fragment: `precision mediump float;
27
- varying vec3 fragNormal;
28
-
29
- uniform samplerCube uCubemap;
30
-
31
- void main() {
32
- gl_FragColor = textureCube(uCubemap, normalize(fragNormal));
33
- }`
34
- }
35
- };
36
-
37
- export default class SkyCubeShader extends Shader {
38
- constructor(renderer) {
39
- super(renderer);
40
-
41
- if (renderer.typeId !== "WebGL") {
42
- throw Error("PresentTextureShader is only compatible with WebGL renderer");
43
- }
44
- }
45
-
46
- async load() {
47
- const { gl } = this.renderer;
48
-
49
- this._program = new ShaderProgram(gl, "SkyCubeShader");
50
- this._program.attachVertexSource(g_code.webgl.vertex);
51
- this._program.attachFragmentSource(g_code.webgl.fragment);
52
- this._program.link();
53
- }
54
-
55
- setup(plistRenderer,materialRenderer,modelMatrix,viewMatrix,projectionMatrix) {
56
- const { material } = materialRenderer;
57
- const { gl } = this.renderer;
58
- this.renderer.state.shaderProgram = this._program;
59
-
60
- const mvp = Mat4.Mult(projectionMatrix, viewMatrix);
61
- this._program.uniformMatrix4fv('uMVP', false, mvp);
62
-
63
- gl.activeTexture(gl.TEXTURE0);
64
- this._program.uniform1i('uCubemap', 0);
65
- if (material.diffuse instanceof Vec) {
66
- throw new Error("Invalid material configuration in SkySphereShader: diffuse component must be a cube map texture");
67
- }
68
- const webglTexture = materialRenderer.getTextureRenderer('diffuse').getApiObject();
69
- gl.bindTexture(gl.TEXTURE_CUBE_MAP, webglTexture);
70
-
71
- this._program.positionAttribPointer(plistRenderer.positionAttribParams("vertPosition"));
72
- }
73
-
74
- destroy() {
75
- ShaderProgram.Delete(this._program);
76
- this._program = null;
77
- }
78
- }
@@ -1,77 +0,0 @@
1
- import Shader from "../render/Shader";
2
- import ShaderProgram from "../render/webgl/ShaderProgram";
3
- import { TextureTargetName } from "../base/Texture";
4
- import Vec from "../math/Vec";
5
-
6
- const g_code = {
7
- webgl: {
8
- vertex: `precision mediump float;
9
-
10
- attribute vec3 vertPosition;
11
- attribute vec2 t0Position;
12
-
13
- varying vec2 fragT0Pos;
14
-
15
- uniform mat4 mView;
16
- uniform mat4 mProj;
17
-
18
- void main() {
19
- fragT0Pos = t0Position;
20
- gl_Position = mProj * mView * vec4(vertPosition, 1.0);
21
- }`,
22
-
23
- fragment: `precision mediump float;
24
-
25
- varying vec2 fragT0Pos;
26
-
27
- uniform sampler2D uTexture;
28
-
29
- void main() {
30
- vec4 texColor = texture2D(uTexture, fragT0Pos);
31
- gl_FragColor = vec4(texColor.rgb, 1.0);
32
- }`
33
- }
34
- };
35
-
36
- export default class SkySphereShader extends Shader {
37
- constructor(renderer) {
38
- super(renderer);
39
-
40
- if (renderer.typeId !== "WebGL") {
41
- throw Error("PresentTextureShader is only compatible with WebGL renderer");
42
- }
43
- }
44
-
45
- async load() {
46
- const { gl } = this.renderer;
47
- this._program = new ShaderProgram(gl, "SkySphereShader");
48
- this._program.attachVertexSource(g_code.webgl.vertex);
49
- this._program.attachFragmentSource(g_code.webgl.fragment);
50
- this._program.link();
51
- }
52
-
53
- setup(plistRenderer, materialRenderer, modelMatrix, viewMatrix, projectionMatrix) {
54
- const { material } = materialRenderer;
55
- const { gl } = this.renderer;
56
- this.renderer.state.shaderProgram = this._program;
57
-
58
- this._program.uniformMatrix4fv('mView', false, viewMatrix);
59
- this._program.uniformMatrix4fv('mProj', false, projectionMatrix);
60
-
61
- gl.activeTexture(gl.TEXTURE0);
62
- this._program.uniform1i('uTexture', 0);
63
- if (material.diffuse instanceof Vec) {
64
- throw new Error("Invalid material configuration in SkySphereShader: diffuse component must be a texture");
65
- }
66
- const webglTexture = materialRenderer.getTextureRenderer('diffuse').getApiObject();
67
- const target = TextureTargetName[material.diffuse.target];
68
- gl.bindTexture(gl[target], webglTexture);
69
-
70
- this._program.positionAttribPointer(plistRenderer.positionAttribParams("vertPosition"));
71
- this._program.texCoordAttribPointer(plistRenderer.texCoord0AttribParams("t0Position"));
72
- }
73
-
74
- destroy() {
75
- ShaderProgram.Delete(this._program);
76
- }
77
- }
@@ -1,145 +0,0 @@
1
- import Shader from "../render/Shader";
2
- import ShaderProgram from "../render/webgl/ShaderProgram";
3
- import Mat4 from "../math/Mat4";
4
- import Vec from "../math/Vec";
5
-
6
- const g_code = {
7
- webgl: {
8
- vertex: `precision mediump float;
9
-
10
- attribute vec3 vertPosition;
11
-
12
- uniform mat4 uMVP;
13
-
14
- varying vec3 fragNormal;
15
-
16
- void main() {
17
- gl_Position = uMVP * vec4(vertPosition, 1.0);
18
- fragNormal = normalize(vertPosition);
19
- }`,
20
-
21
- fragment: (sampleCount,roughness) => `precision mediump float;
22
-
23
- varying vec3 fragNormal;
24
-
25
- uniform samplerCube uCubemap;
26
-
27
- float vanDerCorpus(int n, int base) {
28
- float invBase = 1.0 / float(base);
29
- float denom = 1.0;
30
- float result = 0.0;
31
-
32
- for(int i = 0; i < 16; ++i)
33
- {
34
- if(n > 0)
35
- {
36
- denom = mod(float(n), 2.0);
37
- result += denom * invBase;
38
- invBase = invBase / 2.0;
39
- n = int(float(n) / 2.0);
40
- }
41
- }
42
-
43
- return result;
44
- }
45
-
46
- vec2 hammersleyNoBitOps(int i, int N) {
47
- return vec2(float(i)/float(N), vanDerCorpus(i, 2));
48
- }
49
-
50
- vec3 importanceSampleGGX(vec2 Xi, vec3 N) {
51
- // compute roughness^4 outside the gpu
52
- float a = ${ roughness*roughness*roughness*roughness };
53
-
54
- float phi = ${ 2.0 * Math.PI } * Xi.x;
55
- float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a - 1.0) * Xi.y));
56
- float sinTheta = sqrt(1.0 - cosTheta*cosTheta);
57
-
58
- // from spherical coordinates to cartesian coordinates
59
- vec3 H;
60
- H.x = sin(phi) * sinTheta;
61
- H.y = cos(phi) * sinTheta;
62
- H.z = cosTheta;
63
-
64
- // from tangent-space vector to world-space sample vector
65
- vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
66
- vec3 tangent = normalize(cross(up, N));
67
- vec3 bitangent = cross(N, tangent);
68
-
69
- vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
70
- return normalize(sampleVec);
71
- }
72
-
73
- void main() {
74
- vec3 N = normalize(fragNormal);
75
- vec3 R = N;
76
- vec3 V = R;
77
-
78
- float totalWeight = 0.0;
79
- vec3 prefilteredColor = vec3(0.0);
80
- for (int i = 0; i < ${ sampleCount }; ++i)
81
- {
82
- vec2 Xi = hammersleyNoBitOps(i, ${ sampleCount });
83
- vec3 H = importanceSampleGGX(Xi, N);
84
- vec3 L = normalize(2.0 * dot(V, H) * H - V);
85
-
86
- float NdotL = max(dot(N,L), 0.0);
87
- if (NdotL > 0.0)
88
- {
89
- prefilteredColor += textureCube(uCubemap, L).rgb * NdotL;
90
- totalWeight += NdotL;
91
- }
92
- }
93
- prefilteredColor = prefilteredColor / totalWeight;
94
-
95
- gl_FragColor = vec4(prefilteredColor, 1.0);
96
- }`
97
- }
98
- };
99
-
100
- export default class SpecularMapCubeShader extends Shader {
101
- constructor(renderer) {
102
- super(renderer);
103
-
104
- if (renderer.typeId !== "WebGL") {
105
- throw Error("SpecularMapCubeShader is only compatible with WebGL renderer");
106
- }
107
- }
108
-
109
- get roughness() { return this._roughness; }
110
-
111
- async load(roughness = 0.5) {
112
- const { gl } = this.renderer;
113
-
114
- this._roughness = roughness;
115
-
116
- this._program = new ShaderProgram(gl, "SpecularMapCubeShader");
117
- this._program.attachVertexSource(g_code.webgl.vertex);
118
- this._program.attachFragmentSource(g_code.webgl.fragment(128, this._roughness));
119
- this._program.link();
120
- }
121
-
122
- setup(plistRenderer, materialRenderer, modelMatrix, viewMatrix, projectionMatrix) {
123
- const { material } = materialRenderer;
124
- const { gl } = this.renderer;
125
- this.renderer.state.shaderProgram = this._program;
126
-
127
- const mvp = Mat4.Mult(projectionMatrix, viewMatrix);
128
- this._program.uniformMatrix4fv('uMVP', false, mvp);
129
-
130
- gl.activeTexture(gl.TEXTURE0);
131
- this._program.uniform1i('uCubemap', 0);
132
- if (material.diffuse instanceof Vec) {
133
- throw new Error('Invalid material configuration in SpecularMapCubeShader: diffuse component must be a cube map')
134
- }
135
- const webglTexture = materialRenderer.getTextureRenderer('diffuse').getApiObject();
136
- gl.bindTexture(gl.TEXTURE_CUBE_MAP, webglTexture);
137
-
138
- this._program.positionAttribPointer(plistRenderer.positionAttribParams('vertPosition'));
139
- }
140
-
141
- destroy() {
142
- ShaderProgram.Delete(this._program);
143
- this._program = null;
144
- }
145
- }
@@ -1,127 +0,0 @@
1
-
2
- import { TextureChannel } from '../base/Texture';
3
- import Shader from '../render/Shader';
4
- import ShaderProgram from '../render/webgl/ShaderProgram';
5
-
6
- const g_code = {
7
- webgl: {
8
- vertex: `precision mediump float;
9
-
10
- attribute vec3 position;
11
- attribute vec2 texCoord;
12
-
13
- varying vec2 fragTexCoord;
14
-
15
- void main() {
16
- fragTexCoord = texCoord;
17
- gl_Position = vec4(position, 1.0);
18
- }`,
19
-
20
- fragment: `precision mediump float;
21
-
22
- varying vec2 fragTexCoord;
23
-
24
- uniform sampler2D uTextureR;
25
- uniform sampler2D uTextureG;
26
- uniform sampler2D uTextureB;
27
- uniform sampler2D uTextureA;
28
-
29
- uniform int uRChannel;
30
- uniform int uGChannel;
31
- uniform int uBChannel;
32
- uniform int uAChannel;
33
-
34
- float getChannel(vec4 color, int channel) {
35
- if (channel == 1) {
36
- return color.r;
37
- }
38
- else if (channel == 2) {
39
- return color.g;
40
- }
41
- else if (channel == 3) {
42
- return color.b;
43
- }
44
- else {
45
- return color.a;
46
- }
47
- }
48
-
49
- void main() {
50
- vec4 result = vec4(
51
- getChannel(texture2D(uTextureR, fragTexCoord), uRChannel),
52
- getChannel(texture2D(uTextureG, fragTexCoord), uGChannel),
53
- getChannel(texture2D(uTextureB, fragTexCoord), uBChannel),
54
- getChannel(texture2D(uTextureA, fragTexCoord), uAChannel)
55
- );
56
-
57
- gl_FragColor = result;
58
- }`
59
- }
60
- }
61
-
62
- const g_renderers = {};
63
- export default class TextureMergerShader extends Shader {
64
- static GetUnique(renderer) {
65
- if (!g_renderers[renderer.uniqueId]) {
66
- g_renderers[renderer.uniqueId] = new TextureMergerShader(renderer);
67
- g_renderers[renderer.uniqueId].load();
68
- }
69
- return g_renderers[renderer.uniqueId];
70
- }
71
-
72
- constructor(renderer) {
73
- super(renderer);
74
-
75
- if (renderer.typeId !== "WebGL") {
76
- throw Error("TextureMergerShader is only compatible with WebGL renderer");
77
- }
78
-
79
- this._textures = {};
80
- this._textureChannels = {};
81
- }
82
-
83
- load() {
84
- const { gl } = this.renderer;
85
-
86
- this._program = new ShaderProgram(gl, "DefaultTextureMergerShader");
87
- this._program.attachVertexSource(g_code.webgl.vertex);
88
- this._program.attachFragmentSource(g_code.webgl.fragment);
89
- this._program.link();
90
- }
91
-
92
- setTexture(tex,channel,dstChannel = TextureChannel.R) {
93
- if (channel<TextureChannel.R || channel>TextureChannel.A) {
94
- throw new Error(`TextureMergerShader: invalid texture channel set ${ channel }`);
95
- }
96
- this._textures[channel] = tex;
97
- this._textureChannels[channel] = dstChannel;
98
- }
99
-
100
- get isComplete() {
101
- return this._textures[TextureChannel.R] &&
102
- this._textures[TextureChannel.G] &&
103
- this._textures[TextureChannel.B] &&
104
- this._textures[TextureChannel.A] && true;
105
- }
106
-
107
- setup(plistRenderer, materialRenderer, modelMatrix, viewMatrix, projectionMatrix) {
108
- if (!this.isComplete) {
109
- throw new Error("TextureMergerShader: the texture shader is not complete. Check that the textures are set for all channels");
110
- }
111
-
112
- this.renderer.state.shaderProgram = this._program;
113
-
114
- this._program.bindTexture("uTextureR", this.renderer.factory.texture(this._textures[TextureChannel.R]), 0);
115
- this._program.bindTexture("uTextureG", this.renderer.factory.texture(this._textures[TextureChannel.G]), 1);
116
- this._program.bindTexture("uTextureB", this.renderer.factory.texture(this._textures[TextureChannel.B]), 2);
117
- this._program.bindTexture("uTextureA", this.renderer.factory.texture(this._textures[TextureChannel.A]), 3);
118
-
119
- this._program.uniform1i("uRChannel", this._textureChannels[TextureChannel.R]);
120
- this._program.uniform1i("uGChannel", this._textureChannels[TextureChannel.G]);
121
- this._program.uniform1i("uBChannel", this._textureChannels[TextureChannel.B]);
122
- this._program.uniform1i("uAChannel", this._textureChannels[TextureChannel.A]);
123
-
124
- this._program.positionAttribPointer(plistRenderer.positionAttribParams("position"));
125
- this._program.texCoordAttribPointer(plistRenderer.texCoord0AttribParams("texCoord"));
126
- }
127
- }