emeraldengine 2.2.1 → 3.0.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.
- package/README.md +2359 -968
- package/dist/types/index.d.ts +72 -32
- package/dist/types/src/Animator.d.ts +50 -0
- package/dist/types/{BitmapText.d.ts → src/BitmapText.d.ts} +19 -21
- package/dist/types/src/Camera.d.ts +122 -0
- package/dist/types/src/CameraController.d.ts +107 -0
- package/dist/types/src/CanvasText.d.ts +91 -0
- package/dist/types/src/CollisionLayers.d.ts +58 -0
- package/dist/types/{Color.d.ts → src/Color.d.ts} +5 -6
- package/dist/types/src/Coroutine.d.ts +111 -0
- package/dist/types/src/DebugOverlay.d.ts +86 -0
- package/dist/types/src/Drawable.d.ts +271 -0
- package/dist/types/src/Easing.d.ts +22 -0
- package/dist/types/src/Emerald.d.ts +420 -0
- package/dist/types/src/EmeraldDB.d.ts +159 -0
- package/dist/types/{FPSCounter.d.ts → src/FPSCounter.d.ts} +1 -3
- package/dist/types/src/GLUtils.d.ts +4 -0
- package/dist/types/{Instance.d.ts → src/Instance.d.ts} +37 -15
- package/dist/types/{InstancedTexture.d.ts → src/InstancedTexture.d.ts} +60 -23
- package/dist/types/src/Interpolator.d.ts +66 -0
- package/dist/types/src/Material.d.ts +87 -0
- package/dist/types/src/MathUtils.d.ts +80 -0
- package/dist/types/src/ParticleEmitter.d.ts +131 -0
- package/dist/types/{Physics.d.ts → src/Physics.d.ts} +88 -20
- package/dist/types/src/Pool.d.ts +53 -0
- package/dist/types/src/PostEffects.d.ts +68 -0
- package/dist/types/src/PostProcessor.d.ts +124 -0
- package/dist/types/src/RenderTarget.d.ts +56 -0
- package/dist/types/src/Scene.d.ts +62 -0
- package/dist/types/src/ScreenEffects.d.ts +111 -0
- package/dist/types/src/Serializer.d.ts +86 -0
- package/dist/types/src/Shaders.d.ts +2 -0
- package/dist/types/{Shapes.d.ts → src/Shapes.d.ts} +4 -6
- package/dist/types/src/SpatialGrid.d.ts +50 -0
- package/dist/types/src/SpriteBatch.d.ts +89 -0
- package/dist/types/src/StateMachine.d.ts +59 -0
- package/dist/types/src/Storage.d.ts +89 -0
- package/dist/types/{Texture.d.ts → src/Texture.d.ts} +3 -4
- package/dist/types/src/TextureAtlas.d.ts +55 -0
- package/dist/types/src/Tilemap.d.ts +91 -0
- package/dist/types/src/Time.d.ts +53 -0
- package/dist/types/src/Timer.d.ts +54 -0
- package/dist/types/src/Transform.d.ts +94 -0
- package/dist/types/src/Tween.d.ts +81 -0
- package/dist/types/src/UI.d.ts +121 -0
- package/dist/types/src/components/Behaviour.d.ts +71 -0
- package/dist/types/{components → src/components}/BoxCollider.d.ts +14 -14
- package/dist/types/src/components/BoxColliderDebug.d.ts +15 -0
- package/dist/types/{components → src/components}/CircleCollider.d.ts +14 -12
- package/dist/types/src/components/CircleColliderDebug.d.ts +15 -0
- package/dist/types/src/components/Collider.d.ts +96 -0
- package/dist/types/src/components/GameObject.d.ts +135 -0
- package/dist/types/src/components/RigidBody.d.ts +183 -0
- package/dist/types/src/importers/Aseprite.d.ts +79 -0
- package/dist/types/src/importers/TiledMap.d.ts +62 -0
- package/dist/types/{lights → src/lights}/DirectionalLight.d.ts +7 -9
- package/dist/types/{lights → src/lights}/PointLight.d.ts +6 -9
- package/dist/types/src/managers/AssetManager.d.ts +116 -0
- package/dist/types/src/managers/AudioManager.d.ts +259 -0
- package/dist/types/{managers → src/managers}/CameraManager.d.ts +8 -8
- package/dist/types/{managers → src/managers}/EventManager.d.ts +46 -32
- package/dist/types/src/managers/GLManager.d.ts +84 -0
- package/dist/types/src/managers/GLState.d.ts +37 -0
- package/dist/types/src/managers/IDManager.d.ts +31 -0
- package/dist/types/src/managers/InputManager.d.ts +290 -0
- package/dist/types/src/managers/NetworkManager.d.ts +93 -0
- package/dist/types/src/managers/RenderStats.d.ts +34 -0
- package/dist/types/src/managers/SceneManager.d.ts +44 -0
- package/dist/types/src/managers/ShaderManager.d.ts +55 -0
- package/dist/types/src/managers/TextureManager.d.ts +102 -0
- package/dist/types/src/particlesystem/Particle.d.ts +64 -0
- package/dist/types/{particlesystem → src/particlesystem}/ParticleSettings.d.ts +32 -24
- package/dist/types/{particlesystem → src/particlesystem}/Particles.d.ts +20 -12
- package/index.js +72 -0
- package/package.json +74 -60
- package/src/Animator.js +95 -0
- package/src/BitmapText.js +6 -5
- package/src/Camera.js +183 -0
- package/src/CameraController.js +192 -0
- package/src/CanvasText.js +281 -0
- package/src/CollisionLayers.js +86 -0
- package/src/Color.js +18 -18
- package/src/Coroutine.js +259 -0
- package/src/DebugOverlay.js +246 -0
- package/src/Drawable.js +842 -582
- package/src/Easing.js +57 -0
- package/src/Emerald.js +1150 -459
- package/src/EmeraldDB.js +328 -0
- package/src/FPSCounter.js +43 -43
- package/src/GLUtils.js +60 -67
- package/src/Instance.js +41 -5
- package/src/InstancedTexture.js +251 -118
- package/src/Interpolator.js +124 -0
- package/src/Material.js +202 -0
- package/src/MathUtils.js +133 -0
- package/src/ParticleEmitter.js +284 -0
- package/src/Physics.js +186 -5
- package/src/Pool.js +85 -0
- package/src/PostEffects.js +296 -0
- package/src/PostProcessor.js +304 -0
- package/src/RenderTarget.js +134 -0
- package/src/Scene.js +115 -83
- package/src/ScreenEffects.js +266 -0
- package/src/Serializer.js +131 -0
- package/src/Shaders.js +150 -165
- package/src/Shapes.js +118 -129
- package/src/SpatialGrid.js +111 -0
- package/src/SpriteBatch.js +299 -0
- package/src/StateMachine.js +82 -0
- package/src/Storage.js +175 -47
- package/src/Texture.js +58 -67
- package/src/TextureAtlas.js +96 -0
- package/src/Tilemap.js +274 -0
- package/src/Time.js +51 -6
- package/src/Timer.js +99 -0
- package/src/Transform.js +100 -7
- package/src/Tween.js +160 -0
- package/src/UI.js +394 -0
- package/src/components/Behaviour.js +90 -0
- package/src/components/BoxCollider.js +22 -3
- package/src/components/CircleCollider.js +19 -3
- package/src/components/CircleColliderDebug.js +24 -24
- package/src/components/Collider.js +140 -34
- package/src/components/GameObject.js +130 -21
- package/src/components/RigidBody.js +123 -2
- package/src/importers/Aseprite.js +142 -0
- package/src/importers/TiledMap.js +158 -0
- package/src/lights/DirectionalLight.js +6 -15
- package/src/lights/PointLight.js +4 -4
- package/src/managers/AssetManager.js +239 -0
- package/src/managers/AudioManager.js +565 -146
- package/src/managers/EventManager.js +488 -477
- package/src/managers/GLManager.js +57 -0
- package/src/managers/GLState.js +70 -0
- package/src/managers/IDManager.js +24 -2
- package/src/managers/InputManager.js +779 -0
- package/src/managers/NetworkManager.js +178 -0
- package/src/managers/RenderStats.js +34 -0
- package/src/managers/SceneManager.js +30 -0
- package/src/managers/ShaderManager.js +0 -2
- package/src/managers/TextureManager.js +218 -0
- package/src/particlesystem/Particle.js +82 -7
- package/src/particlesystem/ParticleSettings.js +21 -3
- package/src/particlesystem/Particles.js +80 -31
- package/dist/types/Drawable.d.ts +0 -157
- package/dist/types/Emerald.d.ts +0 -73
- package/dist/types/GLUtils.d.ts +0 -6
- package/dist/types/Scene.d.ts +0 -39
- package/dist/types/Shaders.d.ts +0 -4
- package/dist/types/Storage.d.ts +0 -46
- package/dist/types/Time.d.ts +0 -22
- package/dist/types/Transform.d.ts +0 -41
- package/dist/types/components/BoxColliderDebug.d.ts +0 -19
- package/dist/types/components/CircleColliderDebug.d.ts +0 -19
- package/dist/types/components/Collider.d.ts +0 -53
- package/dist/types/components/GameObject.d.ts +0 -72
- package/dist/types/components/RigidBody.d.ts +0 -104
- package/dist/types/managers/AudioManager.d.ts +0 -60
- package/dist/types/managers/GLManager.d.ts +0 -47
- package/dist/types/managers/IDManager.d.ts +0 -21
- package/dist/types/managers/SceneManager.d.ts +0 -22
- 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
|
-
|
|
6
|
-
attribute vec4
|
|
7
|
-
attribute vec4
|
|
8
|
-
attribute vec4
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
attribute vec2
|
|
13
|
-
attribute vec2
|
|
14
|
-
|
|
15
|
-
attribute
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
mat4 instanceMatrix = mat4(
|
|
28
|
-
aInstanceMatrix0,
|
|
29
|
-
aInstanceMatrix1,
|
|
30
|
-
aInstanceMatrix2,
|
|
31
|
-
aInstanceMatrix3
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if(vertexIndex ==
|
|
44
|
-
vTexCoord =
|
|
45
|
-
} else if(vertexIndex ==
|
|
46
|
-
vTexCoord =
|
|
47
|
-
} else
|
|
48
|
-
vTexCoord =
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
uniform
|
|
65
|
-
uniform
|
|
66
|
-
uniform
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
uniform
|
|
70
|
-
uniform
|
|
71
|
-
uniform float
|
|
72
|
-
uniform
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
uniform
|
|
77
|
-
uniform
|
|
78
|
-
uniform
|
|
79
|
-
uniform
|
|
80
|
-
|
|
81
|
-
uniform
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
varying
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
texColor
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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 };
|