emeraldengine 2.2.1 → 3.1.0

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 (208) hide show
  1. package/README.md +2198 -968
  2. package/dist/types/index.d.ts +75 -32
  3. package/dist/types/src/Animator.d.ts +50 -0
  4. package/dist/types/{BitmapText.d.ts → src/BitmapText.d.ts} +19 -21
  5. package/dist/types/src/Camera.d.ts +122 -0
  6. package/dist/types/src/CameraController.d.ts +107 -0
  7. package/dist/types/src/CanvasText.d.ts +91 -0
  8. package/dist/types/src/CollisionLayers.d.ts +58 -0
  9. package/dist/types/{Color.d.ts → src/Color.d.ts} +6 -6
  10. package/dist/types/src/Coroutine.d.ts +111 -0
  11. package/dist/types/src/DebugOverlay.d.ts +86 -0
  12. package/dist/types/src/Drawable.d.ts +271 -0
  13. package/dist/types/src/Easing.d.ts +22 -0
  14. package/dist/types/src/Emerald.d.ts +420 -0
  15. package/dist/types/src/EmeraldDB.d.ts +159 -0
  16. package/dist/types/{FPSCounter.d.ts → src/FPSCounter.d.ts} +1 -3
  17. package/dist/types/src/GLUtils.d.ts +4 -0
  18. package/dist/types/{Instance.d.ts → src/Instance.d.ts} +37 -15
  19. package/dist/types/{InstancedTexture.d.ts → src/InstancedTexture.d.ts} +75 -23
  20. package/dist/types/src/Interpolator.d.ts +66 -0
  21. package/dist/types/src/Material.d.ts +87 -0
  22. package/dist/types/src/MathUtils.d.ts +81 -0
  23. package/dist/types/src/ParticleEmitter.d.ts +131 -0
  24. package/dist/types/src/Physics.d.ts +337 -0
  25. package/dist/types/src/Pool.d.ts +53 -0
  26. package/dist/types/src/PostEffects.d.ts +68 -0
  27. package/dist/types/src/PostProcessor.d.ts +124 -0
  28. package/dist/types/src/RenderTarget.d.ts +56 -0
  29. package/dist/types/src/Scene.d.ts +62 -0
  30. package/dist/types/src/ScreenEffects.d.ts +111 -0
  31. package/dist/types/src/Serializer.d.ts +86 -0
  32. package/dist/types/src/Shaders.d.ts +2 -0
  33. package/dist/types/{Shapes.d.ts → src/Shapes.d.ts} +4 -6
  34. package/dist/types/src/SpatialGrid.d.ts +50 -0
  35. package/dist/types/src/SpriteBatch.d.ts +89 -0
  36. package/dist/types/src/StateMachine.d.ts +59 -0
  37. package/dist/types/src/Storage.d.ts +89 -0
  38. package/dist/types/{Texture.d.ts → src/Texture.d.ts} +3 -4
  39. package/dist/types/src/TextureAtlas.d.ts +55 -0
  40. package/dist/types/src/Tilemap.d.ts +91 -0
  41. package/dist/types/src/Time.d.ts +53 -0
  42. package/dist/types/src/Timer.d.ts +54 -0
  43. package/dist/types/src/Transform.d.ts +94 -0
  44. package/dist/types/src/Tween.d.ts +81 -0
  45. package/dist/types/src/UI.d.ts +121 -0
  46. package/dist/types/src/components/Behaviour.d.ts +71 -0
  47. package/dist/types/{components → src/components}/BoxCollider.d.ts +14 -14
  48. package/dist/types/src/components/BoxColliderDebug.d.ts +15 -0
  49. package/dist/types/{components → src/components}/CircleCollider.d.ts +14 -12
  50. package/dist/types/src/components/CircleColliderDebug.d.ts +15 -0
  51. package/dist/types/src/components/Collider.d.ts +102 -0
  52. package/dist/types/src/components/GameObject.d.ts +135 -0
  53. package/dist/types/src/components/PolygonCollider.d.ts +33 -0
  54. package/dist/types/src/components/RigidBody.d.ts +456 -0
  55. package/dist/types/src/importers/Aseprite.d.ts +79 -0
  56. package/dist/types/src/importers/ForgeLevel.d.ts +97 -0
  57. package/dist/types/src/importers/TiledMap.d.ts +62 -0
  58. package/dist/types/{lights → src/lights}/DirectionalLight.d.ts +7 -9
  59. package/dist/types/{lights → src/lights}/PointLight.d.ts +6 -9
  60. package/dist/types/src/managers/AssetManager.d.ts +116 -0
  61. package/dist/types/src/managers/AudioManager.d.ts +259 -0
  62. package/dist/types/{managers → src/managers}/CameraManager.d.ts +8 -8
  63. package/dist/types/{managers → src/managers}/EventManager.d.ts +46 -32
  64. package/dist/types/src/managers/GLManager.d.ts +84 -0
  65. package/dist/types/src/managers/GLState.d.ts +37 -0
  66. package/dist/types/src/managers/Gamepad.d.ts +102 -0
  67. package/dist/types/src/managers/IDManager.d.ts +31 -0
  68. package/dist/types/src/managers/InputManager.d.ts +381 -0
  69. package/dist/types/src/managers/NetworkManager.d.ts +93 -0
  70. package/dist/types/src/managers/RenderStats.d.ts +34 -0
  71. package/dist/types/src/managers/SceneManager.d.ts +44 -0
  72. package/dist/types/src/managers/ShaderManager.d.ts +55 -0
  73. package/dist/types/src/managers/TextureManager.d.ts +102 -0
  74. package/dist/types/src/particlesystem/Particle.d.ts +64 -0
  75. package/dist/types/{particlesystem → src/particlesystem}/ParticleSettings.d.ts +32 -24
  76. package/dist/types/{particlesystem → src/particlesystem}/Particles.d.ts +20 -12
  77. package/dist/types/src/physics/AABB.d.ts +92 -0
  78. package/dist/types/src/physics/Body.d.ts +435 -0
  79. package/dist/types/src/physics/BodyType.d.ts +6 -0
  80. package/dist/types/src/physics/BroadPhase.d.ts +210 -0
  81. package/dist/types/src/physics/Collision.d.ts +102 -0
  82. package/dist/types/src/physics/Contact.d.ts +206 -0
  83. package/dist/types/src/physics/ContactSolver.d.ts +108 -0
  84. package/dist/types/src/physics/Distance.d.ts +54 -0
  85. package/dist/types/src/physics/DistanceJoint.d.ts +90 -0
  86. package/dist/types/src/physics/Fixture.d.ts +221 -0
  87. package/dist/types/src/physics/Island.d.ts +52 -0
  88. package/dist/types/src/physics/Joint.d.ts +59 -0
  89. package/dist/types/src/physics/Math2D.d.ts +371 -0
  90. package/dist/types/src/physics/RevoluteJoint.d.ts +119 -0
  91. package/dist/types/src/physics/Settings.d.ts +22 -0
  92. package/dist/types/src/physics/Shapes.d.ts +207 -0
  93. package/dist/types/src/physics/TimeOfImpact.d.ts +22 -0
  94. package/dist/types/src/physics/World.d.ts +274 -0
  95. package/dist/types/src/physics/index.d.ts +34 -0
  96. package/index.js +78 -0
  97. package/package.json +73 -60
  98. package/src/Animator.js +95 -0
  99. package/src/BitmapText.js +6 -5
  100. package/src/Camera.js +183 -0
  101. package/src/CameraController.js +192 -0
  102. package/src/CanvasText.js +281 -0
  103. package/src/CollisionLayers.js +86 -0
  104. package/src/Color.js +26 -18
  105. package/src/Coroutine.js +259 -0
  106. package/src/DebugOverlay.js +246 -0
  107. package/src/Drawable.js +842 -582
  108. package/src/Easing.js +57 -0
  109. package/src/Emerald.js +1150 -459
  110. package/src/EmeraldDB.js +328 -0
  111. package/src/FPSCounter.js +43 -43
  112. package/src/GLUtils.js +60 -67
  113. package/src/Instance.js +41 -5
  114. package/src/InstancedTexture.js +308 -127
  115. package/src/Interpolator.js +124 -0
  116. package/src/Material.js +202 -0
  117. package/src/MathUtils.js +134 -0
  118. package/src/ParticleEmitter.js +284 -0
  119. package/src/Physics.js +431 -40
  120. package/src/Pool.js +85 -0
  121. package/src/PostEffects.js +296 -0
  122. package/src/PostProcessor.js +304 -0
  123. package/src/RenderTarget.js +134 -0
  124. package/src/Scene.js +115 -83
  125. package/src/ScreenEffects.js +266 -0
  126. package/src/Serializer.js +131 -0
  127. package/src/Shaders.js +150 -165
  128. package/src/Shapes.js +118 -129
  129. package/src/SpatialGrid.js +111 -0
  130. package/src/SpriteBatch.js +299 -0
  131. package/src/StateMachine.js +82 -0
  132. package/src/Storage.js +175 -47
  133. package/src/Texture.js +58 -67
  134. package/src/TextureAtlas.js +96 -0
  135. package/src/Tilemap.js +274 -0
  136. package/src/Time.js +51 -6
  137. package/src/Timer.js +99 -0
  138. package/src/Transform.js +100 -7
  139. package/src/Tween.js +160 -0
  140. package/src/UI.js +394 -0
  141. package/src/components/Behaviour.js +90 -0
  142. package/src/components/BoxCollider.js +29 -12
  143. package/src/components/BoxColliderDebug.js +3 -4
  144. package/src/components/CircleCollider.js +26 -12
  145. package/src/components/CircleColliderDebug.js +25 -24
  146. package/src/components/Collider.js +150 -34
  147. package/src/components/GameObject.js +130 -21
  148. package/src/components/PolygonCollider.js +55 -0
  149. package/src/components/RigidBody.js +556 -8
  150. package/src/importers/Aseprite.js +142 -0
  151. package/src/importers/ForgeLevel.js +581 -0
  152. package/src/importers/TiledMap.js +158 -0
  153. package/src/lights/DirectionalLight.js +6 -15
  154. package/src/lights/PointLight.js +4 -4
  155. package/src/managers/AssetManager.js +239 -0
  156. package/src/managers/AudioManager.js +565 -146
  157. package/src/managers/EventManager.js +488 -477
  158. package/src/managers/GLManager.js +57 -0
  159. package/src/managers/GLState.js +70 -0
  160. package/src/managers/Gamepad.js +126 -0
  161. package/src/managers/IDManager.js +24 -2
  162. package/src/managers/InputManager.js +905 -0
  163. package/src/managers/NetworkManager.js +178 -0
  164. package/src/managers/RenderStats.js +34 -0
  165. package/src/managers/SceneManager.js +30 -0
  166. package/src/managers/ShaderManager.js +0 -2
  167. package/src/managers/TextureManager.js +218 -0
  168. package/src/particlesystem/Particle.js +82 -7
  169. package/src/particlesystem/ParticleSettings.js +21 -3
  170. package/src/particlesystem/Particles.js +80 -31
  171. package/src/physics/AABB.js +207 -0
  172. package/src/physics/Body.js +862 -0
  173. package/src/physics/BodyType.js +16 -0
  174. package/src/physics/BroadPhase.js +641 -0
  175. package/src/physics/Collision.js +534 -0
  176. package/src/physics/Contact.js +500 -0
  177. package/src/physics/ContactSolver.js +526 -0
  178. package/src/physics/Distance.js +403 -0
  179. package/src/physics/DistanceJoint.js +227 -0
  180. package/src/physics/Fixture.js +346 -0
  181. package/src/physics/Island.js +203 -0
  182. package/src/physics/Joint.js +78 -0
  183. package/src/physics/Math2D.js +573 -0
  184. package/src/physics/RevoluteJoint.js +278 -0
  185. package/src/physics/Settings.js +78 -0
  186. package/src/physics/Shapes.js +549 -0
  187. package/src/physics/TimeOfImpact.js +87 -0
  188. package/src/physics/World.js +731 -0
  189. package/src/physics/index.js +79 -0
  190. package/dist/types/Drawable.d.ts +0 -157
  191. package/dist/types/Emerald.d.ts +0 -73
  192. package/dist/types/GLUtils.d.ts +0 -6
  193. package/dist/types/Physics.d.ts +0 -139
  194. package/dist/types/Scene.d.ts +0 -39
  195. package/dist/types/Shaders.d.ts +0 -4
  196. package/dist/types/Storage.d.ts +0 -46
  197. package/dist/types/Time.d.ts +0 -22
  198. package/dist/types/Transform.d.ts +0 -41
  199. package/dist/types/components/BoxColliderDebug.d.ts +0 -19
  200. package/dist/types/components/CircleColliderDebug.d.ts +0 -19
  201. package/dist/types/components/Collider.d.ts +0 -53
  202. package/dist/types/components/GameObject.d.ts +0 -72
  203. package/dist/types/components/RigidBody.d.ts +0 -104
  204. package/dist/types/managers/AudioManager.d.ts +0 -60
  205. package/dist/types/managers/GLManager.d.ts +0 -47
  206. package/dist/types/managers/IDManager.d.ts +0 -21
  207. package/dist/types/managers/SceneManager.d.ts +0 -22
  208. package/dist/types/particlesystem/Particle.d.ts +0 -42
@@ -0,0 +1,131 @@
1
+ /**
2
+ * @class Serializer
3
+ * @description Saves/loads a scene to/from plain JSON. Because components hold
4
+ * live GL/physics handles that can't be serialized generically, reconstruction
5
+ * goes through registered factories keyed by a `prefabType`. Each object's
6
+ * transform, layer, opacity and an optional `serialize()` payload are captured.
7
+ *
8
+ * @example
9
+ * Serializer.register("coin", (data) => makeCoin(data.value));
10
+ * coin.prefabType = "coin";
11
+ * coin.serialize = () => ({ value: 5 });
12
+ *
13
+ * const json = Serializer.toJSON(scene); // save
14
+ * Serializer.fromJSON(json, new Scene()); // load
15
+ */
16
+ class Serializer {
17
+ /**
18
+ * @method register
19
+ * @description Registers a factory that rebuilds an object from its data.
20
+ * @param {string} type - The prefabType
21
+ * @param {(data:any, entry:any) => Object} factory - Returns a GameObject
22
+ */
23
+ static register(type, factory) {
24
+ Serializer.factories.set(type, factory);
25
+ }
26
+
27
+ /**
28
+ * @method serializeObject
29
+ * @description Captures a single object's serializable state.
30
+ */
31
+ static serializeObject(obj) {
32
+ const t = obj.transform;
33
+ return {
34
+ type: obj.prefabType || null,
35
+ name: obj.name,
36
+ layer: obj.layer || 0,
37
+ opacity: typeof obj.opacity === "number" ? obj.opacity : 1,
38
+ screenSpace: !!obj.screenSpace,
39
+ transform: t
40
+ ? {
41
+ position: { x: t.position.x, y: t.position.y, z: t.position.z },
42
+ rotation: t.rotation,
43
+ scale: { x: t.scale.x, y: t.scale.y },
44
+ }
45
+ : null,
46
+ data:
47
+ typeof obj.serialize === "function"
48
+ ? obj.serialize()
49
+ : obj.data || null,
50
+ };
51
+ }
52
+
53
+ /**
54
+ * @method serializeScene
55
+ * @description Returns a plain object describing the scene.
56
+ */
57
+ static serializeScene(scene) {
58
+ return { objects: scene.objects.map((o) => Serializer.serializeObject(o)) };
59
+ }
60
+
61
+ /**
62
+ * @method applyState
63
+ * @description Applies a serialized entry's transform/layer/opacity to an object.
64
+ */
65
+ static applyState(obj, entry) {
66
+ if (entry.transform && obj.transform) {
67
+ const t = entry.transform;
68
+ obj.transform.position.x = t.position.x;
69
+ obj.transform.position.y = t.position.y;
70
+ obj.transform.position.z = t.position.z;
71
+ obj.transform.rotation = t.rotation;
72
+ obj.transform.scale.x = t.scale.x;
73
+ obj.transform.scale.y = t.scale.y;
74
+ }
75
+ if (typeof entry.layer === "number") obj.layer = entry.layer;
76
+ if (typeof entry.opacity === "number") obj.opacity = entry.opacity;
77
+ if (entry.screenSpace && typeof obj.setScreenSpace === "function") {
78
+ obj.setScreenSpace(true);
79
+ }
80
+ if (entry.name) obj.name = entry.name;
81
+ }
82
+
83
+ /**
84
+ * @method deserializeObject
85
+ * @description Rebuilds an object from an entry via its registered factory.
86
+ * @returns {Object|null}
87
+ */
88
+ static deserializeObject(entry) {
89
+ const factory = Serializer.factories.get(entry.type);
90
+ if (!factory) {
91
+ console.warn(`[Serializer] > No factory for type "${entry.type}"`);
92
+ return null;
93
+ }
94
+ const obj = factory(entry.data, entry);
95
+ if (obj) Serializer.applyState(obj, entry);
96
+ return obj;
97
+ }
98
+
99
+ /**
100
+ * @method deserializeScene
101
+ * @description Rebuilds objects into the given scene.
102
+ * @returns {Scene} - The populated scene
103
+ */
104
+ static deserializeScene(json, scene) {
105
+ for (const entry of json.objects || []) {
106
+ const obj = Serializer.deserializeObject(entry);
107
+ if (obj) scene.add(obj);
108
+ }
109
+ return scene;
110
+ }
111
+
112
+ /**
113
+ * @method toJSON
114
+ * @description Serializes a scene to a JSON string.
115
+ */
116
+ static toJSON(scene) {
117
+ return JSON.stringify(Serializer.serializeScene(scene));
118
+ }
119
+
120
+ /**
121
+ * @method fromJSON
122
+ * @description Loads a scene from a JSON string into the given scene.
123
+ */
124
+ static fromJSON(str, scene) {
125
+ return Serializer.deserializeScene(JSON.parse(str), scene);
126
+ }
127
+ }
128
+
129
+ Serializer.factories = new Map();
130
+
131
+ export default Serializer;
package/src/Shaders.js CHANGED
@@ -1,165 +1,150 @@
1
- const STANDARD_VERTEX_SHADER = `
2
- attribute vec4 aVertexPosition;
3
- attribute vec2 aTextureCoord;
4
-
5
- // Instance matrix (each row as a separate attribute)
6
- attribute vec4 aInstanceMatrix0;
7
- attribute vec4 aInstanceMatrix1;
8
- attribute vec4 aInstanceMatrix2;
9
- attribute vec4 aInstanceMatrix3;
10
-
11
- // Per-instance texture coordinates (one for each vertex of the quad)
12
- attribute vec2 aInstanceTexCoord0;
13
- attribute vec2 aInstanceTexCoord1;
14
- attribute vec2 aInstanceTexCoord2;
15
- attribute vec2 aInstanceTexCoord3;
16
-
17
- uniform mat4 uModelViewMatrix;
18
- uniform mat4 uInstancedModelViewMatrix;
19
- uniform mat4 uProjectionMatrix;
20
- uniform bool useInstances;
21
-
22
- varying highp vec2 vTexCoord;
23
- varying vec2 vFragPos;
24
-
25
- void main() {
26
- // Construct the instance matrix
27
- mat4 instanceMatrix = mat4(
28
- aInstanceMatrix0,
29
- aInstanceMatrix1,
30
- aInstanceMatrix2,
31
- aInstanceMatrix3
32
- );
33
-
34
- // Apply the instance-specific transform, then the global transforms
35
- if(useInstances) {
36
- gl_Position = uProjectionMatrix * uInstancedModelViewMatrix * instanceMatrix * aVertexPosition;
37
- vFragPos = (uInstancedModelViewMatrix * instanceMatrix * aVertexPosition).xy;
38
-
39
- // Use per-instance texture coordinates based on vertex index
40
- // Vertex order: top-right, top-left, bottom-right, bottom-left
41
- int vertexIndex = int(aVertexPosition.x > 0.0 ? (aVertexPosition.y > 0.0 ? 0 : 2) : (aVertexPosition.y > 0.0 ? 1 : 3));
42
-
43
- if(vertexIndex == 0) {
44
- vTexCoord = aInstanceTexCoord0;
45
- } else if(vertexIndex == 1) {
46
- vTexCoord = aInstanceTexCoord1;
47
- } else if(vertexIndex == 2) {
48
- vTexCoord = aInstanceTexCoord2;
49
- } else {
50
- vTexCoord = aInstanceTexCoord3;
51
- }
52
- } else {
53
- gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
54
- vFragPos = (uModelViewMatrix * aVertexPosition).xy;
55
- vTexCoord = aTextureCoord;
56
- }
57
- }
58
- `;
59
-
60
- const STANDARD_FRAGMENT_SHADER = `
61
- #ifdef GL_ES
62
- precision highp float;
63
- #endif
64
- uniform bool useTexture;
65
- uniform vec4 uColor;
66
- uniform sampler2D uSampler;
67
- uniform float uOpacity; // Global opacity multiplier per GameObject
68
-
69
- uniform vec2 uLightPosition[4]; // Support up to 4 lights
70
- uniform vec3 uLightColor[4]; // RGB colors for each light
71
- uniform float uLightIntensity[4]; // Intensity of each light
72
- uniform float uLightRadius[4]; // Radius of each light
73
- uniform int uActiveLights; // Number of active lights
74
-
75
- // Directional lights
76
- uniform vec2 uDirLightPosition[4]; // Position of directional lights
77
- uniform vec2 uDirLightDirection[4]; // Direction of directional lights
78
- uniform vec3 uDirLightColor[4]; // RGB colors for each directional light
79
- uniform float uDirLightIntensity[4]; // Intensity of each directional light
80
- uniform float uDirLightWidth[4]; // Width of each directional light beam
81
- uniform int uActiveDirLights; // Number of active directional lights
82
-
83
- uniform vec3 uAmbientLightValues; // Ambient light values
84
- uniform bool uUseLighting; // Whether to apply lighting to this object
85
-
86
- varying vec2 vTexCoord;
87
- varying vec2 vFragPos;
88
-
89
- void main() {
90
- vec4 ambientLight = vec4(uAmbientLightValues.xyz, 1.0); // Move ambient light here
91
-
92
- vec4 texColor;
93
- if (useTexture) {
94
- texColor = texture2D(uSampler, vTexCoord);
95
- // Discard only fully or nearly fully transparent texels to keep soft edges
96
- if (texColor.a < 0.01) discard;
97
- // Apply color tint to texture
98
- texColor *= uColor;
99
- } else {
100
- texColor = uColor;
101
- }
102
-
103
- // Start with ambient light
104
- vec3 lighting = ambientLight.rgb;
105
-
106
- // Calculate contribution from each point light
107
- for(int i = 0; i < 4; i++) {
108
- if(i >= uActiveLights) break;
109
-
110
- float distance = length(uLightPosition[i] - vFragPos);
111
-
112
- // Check if fragment is within light radius
113
- if(distance < uLightRadius[i]) {
114
- // Calculate attenuation (falloff)
115
- float attenuation = 1.0 - distance / uLightRadius[i];
116
-
117
- // Add this light's contribution
118
- lighting += uLightColor[i] * attenuation * uLightIntensity[i];
119
- }
120
- }
121
-
122
- // Calculate contribution from each directional light
123
- for(int i = 0; i < 4; i++) {
124
- if(i >= uActiveDirLights) break;
125
-
126
- vec2 lightPos = uDirLightPosition[i];
127
- vec2 lightDir = normalize(uDirLightDirection[i]);
128
- vec2 toFrag = vFragPos - lightPos;
129
-
130
- // Check if fragment is in front of the light
131
- float projection = dot(toFrag, lightDir);
132
- if(projection < 0.0) continue;
133
-
134
- // Calculate perpendicular distance from light beam center
135
- float perpDistance = abs(dot(toFrag, vec2(-lightDir.y, lightDir.x)));
136
-
137
- // Check if fragment is within light beam width
138
- if(perpDistance > uDirLightWidth[i] * 0.5) continue;
139
-
140
- // Simplified lighting without shadows for now
141
- float widthFactor = 1.0 - (perpDistance / (uDirLightWidth[i] * 0.5));
142
-
143
- // Optional: Add distance attenuation
144
- float distance = length(toFrag);
145
- float maxDistance = 1000.0; // Maximum effective distance
146
- float distanceFactor = max(0.0, 1.0 - (distance / maxDistance));
147
-
148
- // Add this directional light's contribution
149
- lighting += uDirLightColor[i] * uDirLightIntensity[i] * widthFactor * distanceFactor;
150
- }
151
-
152
- // Apply lighting to texture color only if lighting is enabled
153
- vec4 outColor;
154
- if (uUseLighting) {
155
- outColor = vec4(texColor.rgb * lighting, texColor.a);
156
- } else {
157
- outColor = texColor;
158
- }
159
- // Apply global opacity multiplier
160
- outColor.a *= clamp(uOpacity, 0.0, 1.0);
161
- gl_FragColor = outColor;
162
- }
163
- `;
164
-
165
- export { STANDARD_VERTEX_SHADER, STANDARD_FRAGMENT_SHADER };
1
+ const STANDARD_VERTEX_SHADER = `
2
+ attribute vec4 aVertexPosition;
3
+ attribute vec2 aTextureCoord;
4
+
5
+ attribute vec4 aInstanceMatrix0;
6
+ attribute vec4 aInstanceMatrix1;
7
+ attribute vec4 aInstanceMatrix2;
8
+ attribute vec4 aInstanceMatrix3;
9
+
10
+ attribute vec2 aInstanceTexCoord0;
11
+ attribute vec2 aInstanceTexCoord1;
12
+ attribute vec2 aInstanceTexCoord2;
13
+ attribute vec2 aInstanceTexCoord3;
14
+
15
+ attribute vec4 aInstanceColor;
16
+
17
+ uniform mat4 uModelViewMatrix;
18
+ uniform mat4 uInstancedModelViewMatrix;
19
+ uniform mat4 uProjectionMatrix;
20
+ uniform bool useInstances;
21
+
22
+ varying highp vec2 vTexCoord;
23
+ varying vec2 vFragPos;
24
+ varying vec4 vInstanceColor;
25
+
26
+ void main() {
27
+ mat4 instanceMatrix = mat4(
28
+ aInstanceMatrix0,
29
+ aInstanceMatrix1,
30
+ aInstanceMatrix2,
31
+ aInstanceMatrix3
32
+ );
33
+
34
+ if(useInstances) {
35
+ gl_Position = uProjectionMatrix * uInstancedModelViewMatrix * instanceMatrix * aVertexPosition;
36
+ vFragPos = (uInstancedModelViewMatrix * instanceMatrix * aVertexPosition).xy;
37
+ vInstanceColor = aInstanceColor;
38
+
39
+ int vertexIndex = int(aVertexPosition.x > 0.0 ? (aVertexPosition.y > 0.0 ? 0 : 2) : (aVertexPosition.y > 0.0 ? 1 : 3));
40
+
41
+ if(vertexIndex == 0) {
42
+ vTexCoord = aInstanceTexCoord0;
43
+ } else if(vertexIndex == 1) {
44
+ vTexCoord = aInstanceTexCoord1;
45
+ } else if(vertexIndex == 2) {
46
+ vTexCoord = aInstanceTexCoord2;
47
+ } else {
48
+ vTexCoord = aInstanceTexCoord3;
49
+ }
50
+ } else {
51
+ gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
52
+ vFragPos = (uModelViewMatrix * aVertexPosition).xy;
53
+ vTexCoord = aTextureCoord;
54
+ vInstanceColor = vec4(1.0);
55
+ }
56
+ }
57
+ `;
58
+
59
+ const STANDARD_FRAGMENT_SHADER = `
60
+ #ifdef GL_ES
61
+ precision highp float;
62
+ #endif
63
+ uniform bool useTexture;
64
+ uniform vec4 uColor;
65
+ uniform sampler2D uSampler;
66
+ uniform float uOpacity;
67
+
68
+ uniform vec2 uLightPosition[4];
69
+ uniform vec3 uLightColor[4];
70
+ uniform float uLightIntensity[4];
71
+ uniform float uLightRadius[4];
72
+ uniform int uActiveLights;
73
+
74
+ uniform vec2 uDirLightPosition[4];
75
+ uniform vec2 uDirLightDirection[4];
76
+ uniform vec3 uDirLightColor[4];
77
+ uniform float uDirLightIntensity[4];
78
+ uniform float uDirLightWidth[4];
79
+ uniform int uActiveDirLights;
80
+
81
+ uniform vec3 uAmbientLightValues;
82
+ uniform bool uUseLighting;
83
+
84
+ varying vec2 vTexCoord;
85
+ varying vec2 vFragPos;
86
+ varying vec4 vInstanceColor;
87
+
88
+ void main() {
89
+ vec4 ambientLight = vec4(uAmbientLightValues.xyz, 1.0);
90
+
91
+ vec4 texColor;
92
+ if (useTexture) {
93
+ texColor = texture2D(uSampler, vTexCoord);
94
+ if (texColor.a < 0.01) discard;
95
+ texColor *= uColor;
96
+ } else {
97
+ texColor = uColor;
98
+ }
99
+
100
+ texColor *= vInstanceColor;
101
+
102
+ vec3 lighting = ambientLight.rgb;
103
+
104
+ for(int i = 0; i < 4; i++) {
105
+ if(i >= uActiveLights) break;
106
+
107
+ float distance = length(uLightPosition[i] - vFragPos);
108
+
109
+ if(distance < uLightRadius[i]) {
110
+ float attenuation = 1.0 - distance / uLightRadius[i];
111
+
112
+ lighting += uLightColor[i] * attenuation * uLightIntensity[i];
113
+ }
114
+ }
115
+
116
+ for(int i = 0; i < 4; i++) {
117
+ if(i >= uActiveDirLights) break;
118
+
119
+ vec2 lightPos = uDirLightPosition[i];
120
+ vec2 lightDir = normalize(uDirLightDirection[i]);
121
+ vec2 toFrag = vFragPos - lightPos;
122
+
123
+ float projection = dot(toFrag, lightDir);
124
+ if(projection < 0.0) continue;
125
+
126
+ float perpDistance = abs(dot(toFrag, vec2(-lightDir.y, lightDir.x)));
127
+
128
+ if(perpDistance > uDirLightWidth[i] * 0.5) continue;
129
+
130
+ float widthFactor = 1.0 - (perpDistance / (uDirLightWidth[i] * 0.5));
131
+
132
+ float distance = length(toFrag);
133
+ float maxDistance = 1000.0;
134
+ float distanceFactor = max(0.0, 1.0 - (distance / maxDistance));
135
+
136
+ lighting += uDirLightColor[i] * uDirLightIntensity[i] * widthFactor * distanceFactor;
137
+ }
138
+
139
+ vec4 outColor;
140
+ if (uUseLighting) {
141
+ outColor = vec4(texColor.rgb * lighting, texColor.a);
142
+ } else {
143
+ outColor = texColor;
144
+ }
145
+ outColor.a *= clamp(uOpacity, 0.0, 1.0);
146
+ gl_FragColor = outColor;
147
+ }
148
+ `;
149
+
150
+ export { STANDARD_VERTEX_SHADER, STANDARD_FRAGMENT_SHADER };