@idosgames/gfx 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/chunk-CJTRRUEK.js +1 -0
- package/dist/chunk-PYGRABBR.js +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +1 -0
- package/dist/moods-CROfsmd7.d.cts +180 -0
- package/dist/moods-CROfsmd7.d.ts +180 -0
- package/dist/phaser/index.cjs +1 -0
- package/dist/phaser/index.d.cts +116 -0
- package/dist/phaser/index.d.ts +116 -0
- package/dist/phaser/index.js +1 -0
- package/dist/three/index.cjs +140 -0
- package/dist/three/index.d.cts +228 -0
- package/dist/three/index.d.ts +228 -0
- package/dist/three/index.js +140 -0
- package/package.json +78 -4
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { c as Mood, e as TierSettings } from '../moods-CROfsmd7.js';
|
|
3
|
+
|
|
4
|
+
interface RendererSetup {
|
|
5
|
+
/** Canvas pixels per CSS pixel the renderer was set to. */
|
|
6
|
+
readonly density: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Colour space, tone mapping, canvas density and the shadow map for a tier and a mood.
|
|
10
|
+
* `maxDensity` lowers the tier's cap (a game that knows it is heavy).
|
|
11
|
+
*/
|
|
12
|
+
declare function setupRenderer(renderer: THREE.WebGLRenderer, tier: TierSettings, mood: Mood, maxDensity?: number): RendererSetup;
|
|
13
|
+
/**
|
|
14
|
+
* Can the GPU draw the soft shadow: a variance map is drawn into a half-float target. Nearly every
|
|
15
|
+
* phone with WebGL2 can; one that cannot gets PCF, a tighter edge but the same lightness.
|
|
16
|
+
*/
|
|
17
|
+
declare function softShadowsSupported(renderer: THREE.WebGLRenderer): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* The shadow map of the standard: soft and light, like a casual game's. The edge is a blurred variance
|
|
20
|
+
* map (VSM), not PCF: PCF in three takes five samples, so a wide edge of it is grainy, and a narrow one
|
|
21
|
+
* is the hard dark shadow that makes a toy scene look like a render. With VSM every receiver also
|
|
22
|
+
* casts: a see-through thing must not receive (see the views).
|
|
23
|
+
*/
|
|
24
|
+
declare function setupShadowMap(renderer: THREE.WebGLRenderer, tier: TierSettings): void;
|
|
25
|
+
/**
|
|
26
|
+
* The sun's shadow for a tier: the map's size, the soft edge and how dark it is (`strength`, the mood's
|
|
27
|
+
* `shadow`). `mapSize` overrides the tier's map. The edge is about the same width in metres on every
|
|
28
|
+
* tier: a finer map blurs over more of its pixels.
|
|
29
|
+
*/
|
|
30
|
+
declare function setupSunShadow(renderer: THREE.WebGLRenderer, sun: THREE.DirectionalLight, tier: TierSettings, strength: number, mapSize?: number): void;
|
|
31
|
+
interface LightRig {
|
|
32
|
+
readonly hemi: THREE.HemisphereLight;
|
|
33
|
+
readonly sun: THREE.DirectionalLight;
|
|
34
|
+
/** Where the sun shines FROM (unit vector, world space). */
|
|
35
|
+
readonly sunDir: THREE.Vector3;
|
|
36
|
+
setMood(mood: Mood): void;
|
|
37
|
+
/** The shadow box: `size` metres around `target` (call when the camera's target moves). */
|
|
38
|
+
aim(target: THREE.Vector3, size: number): void;
|
|
39
|
+
/** The shadow map on or off (a slow device gives it up). */
|
|
40
|
+
setShadows(on: boolean): void;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
43
|
+
/** The sun and the sky's light. Added to `scene`; the sun casts the shadow by the tier. */
|
|
44
|
+
declare function createLightRig(scene: THREE.Scene, mood: Mood, tier: TierSettings, renderer: THREE.WebGLRenderer): LightRig;
|
|
45
|
+
|
|
46
|
+
interface Sky {
|
|
47
|
+
readonly object: THREE.Mesh;
|
|
48
|
+
setMood(mood: Mood): void;
|
|
49
|
+
/** Where the sun shines FROM (unit vector, world space). */
|
|
50
|
+
setSun(dir: THREE.Vector3): void;
|
|
51
|
+
/** Once a frame: the dome follows the camera, the clouds drift. */
|
|
52
|
+
update(camera: THREE.Camera, time: number): void;
|
|
53
|
+
dispose(): void;
|
|
54
|
+
}
|
|
55
|
+
declare function createSky(mood: Mood, detail: boolean): Sky;
|
|
56
|
+
|
|
57
|
+
type ToyFinish = "matte" | "plastic" | "glossy" | "glow";
|
|
58
|
+
interface ToyOptions {
|
|
59
|
+
readonly finish?: ToyFinish;
|
|
60
|
+
readonly alpha?: number;
|
|
61
|
+
readonly flat?: boolean;
|
|
62
|
+
/** The colour comes from a map or instance colours as well (the paint stays white). */
|
|
63
|
+
readonly map?: THREE.Texture | null;
|
|
64
|
+
readonly side?: THREE.Side;
|
|
65
|
+
readonly alphaTest?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The toy materials of one scene: cached by colour, finish and alpha, all sharing the mood's rim light.
|
|
69
|
+
* `get` returns a shared material: do not change it, ask for another one.
|
|
70
|
+
*/
|
|
71
|
+
declare class ToyLook {
|
|
72
|
+
private readonly cache;
|
|
73
|
+
private readonly shared;
|
|
74
|
+
private vibrance;
|
|
75
|
+
constructor(mood: Mood);
|
|
76
|
+
setMood(mood: Mood): void;
|
|
77
|
+
/** A cached material of a colour (#hex). */
|
|
78
|
+
get(hex: string, options?: ToyOptions): THREE.MeshLambertMaterial;
|
|
79
|
+
/** A material of its own (not cached): the caller owns and disposes it. */
|
|
80
|
+
make(hex: string, options?: ToyOptions): THREE.MeshLambertMaterial;
|
|
81
|
+
dispose(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Cells of the atlas, left to right. */
|
|
85
|
+
declare const SPRITES: readonly ["dot", "spark", "puff", "chip", "ring", "star"];
|
|
86
|
+
type SpriteName = (typeof SPRITES)[number];
|
|
87
|
+
/**
|
|
88
|
+
* A fixed pool of sprites. Fill slots with `put`, say how many are live with `commit`: the mesh draws
|
|
89
|
+
* the first `count` of them in one call.
|
|
90
|
+
*/
|
|
91
|
+
declare class Billboards {
|
|
92
|
+
readonly object: THREE.Mesh;
|
|
93
|
+
readonly capacity: number;
|
|
94
|
+
private readonly geometry;
|
|
95
|
+
private readonly material;
|
|
96
|
+
private readonly pos;
|
|
97
|
+
private readonly look;
|
|
98
|
+
private readonly color;
|
|
99
|
+
constructor(capacity: number, renderOrder?: number);
|
|
100
|
+
/** Writes slot `i`. `color` is linear RGB (a THREE.Color's r, g, b). */
|
|
101
|
+
put(i: number, x: number, y: number, z: number, size: number, turn: number, sprite: number, additive: boolean, r: number, g: number, b: number, alpha: number): void;
|
|
102
|
+
/** The first `count` slots are drawn; the buffers are uploaded only when something is live. */
|
|
103
|
+
commit(count: number): void;
|
|
104
|
+
dispose(): void;
|
|
105
|
+
}
|
|
106
|
+
declare const spriteIndex: (name: SpriteName) => number;
|
|
107
|
+
|
|
108
|
+
interface ParticleSpawn {
|
|
109
|
+
x: number;
|
|
110
|
+
y: number;
|
|
111
|
+
z: number;
|
|
112
|
+
vx?: number;
|
|
113
|
+
vy?: number;
|
|
114
|
+
vz?: number;
|
|
115
|
+
/** Seconds it lives. */
|
|
116
|
+
life: number;
|
|
117
|
+
/** Size in metres at birth and at death (default: shrinks to nothing). */
|
|
118
|
+
size: number;
|
|
119
|
+
sizeEnd?: number;
|
|
120
|
+
color: string;
|
|
121
|
+
sprite?: SpriteName;
|
|
122
|
+
/** Adds its light instead of covering (a spark, a glint). */
|
|
123
|
+
additive?: boolean;
|
|
124
|
+
/** Metres per second² down (default 0). */
|
|
125
|
+
gravity?: number;
|
|
126
|
+
/** Share of speed lost per second (0 = none). */
|
|
127
|
+
drag?: number;
|
|
128
|
+
/** Radians per second. */
|
|
129
|
+
spin?: number;
|
|
130
|
+
alpha?: number;
|
|
131
|
+
/** The floor it bounces off a little (none by default). */
|
|
132
|
+
floor?: number;
|
|
133
|
+
}
|
|
134
|
+
interface BurstOptions {
|
|
135
|
+
x: number;
|
|
136
|
+
y: number;
|
|
137
|
+
z: number;
|
|
138
|
+
color: string;
|
|
139
|
+
/** How many (default 14). */
|
|
140
|
+
n?: number;
|
|
141
|
+
/** How far they fly, metres (default 1.2). */
|
|
142
|
+
size?: number;
|
|
143
|
+
}
|
|
144
|
+
declare class Particles {
|
|
145
|
+
readonly object: THREE.Mesh;
|
|
146
|
+
private readonly sprites;
|
|
147
|
+
private readonly data;
|
|
148
|
+
private readonly flags;
|
|
149
|
+
/** Gravity, drag and the floor of every slot. */
|
|
150
|
+
private readonly side;
|
|
151
|
+
private count;
|
|
152
|
+
private readonly colors;
|
|
153
|
+
/** Share of a burst's particles really emitted (the tier's). */
|
|
154
|
+
amount: number;
|
|
155
|
+
constructor(capacity?: number, amount?: number);
|
|
156
|
+
get alive(): number;
|
|
157
|
+
private color;
|
|
158
|
+
spawn(p: ParticleSpawn): void;
|
|
159
|
+
private many;
|
|
160
|
+
/** Bright pieces flying out of a point and falling: a hit, a pickup, a thing breaking. */
|
|
161
|
+
burst(o: BurstOptions): void;
|
|
162
|
+
/** Paper pieces of many colours thrown up and fluttering down: a win. */
|
|
163
|
+
confetti(o: {
|
|
164
|
+
x: number;
|
|
165
|
+
y: number;
|
|
166
|
+
z: number;
|
|
167
|
+
colors: readonly string[];
|
|
168
|
+
n?: number;
|
|
169
|
+
size?: number;
|
|
170
|
+
}): void;
|
|
171
|
+
/** Glints twinkling around a point: a thing worth taking, a reward. */
|
|
172
|
+
sparkle(o: BurstOptions): void;
|
|
173
|
+
/** A low cloud kicked up from the ground: a landing, a skid, running feet. */
|
|
174
|
+
dust(o: BurstOptions): void;
|
|
175
|
+
/** A ring spreading over the ground: a shock wave, a landing, a thing being placed. */
|
|
176
|
+
ring(o: BurstOptions): void;
|
|
177
|
+
/** Once a frame. */
|
|
178
|
+
update(dt: number): void;
|
|
179
|
+
clear(): void;
|
|
180
|
+
dispose(): void;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Glow halos: a soft light around every thing that shines (a coin, a gem, a lamp), placed anew every
|
|
184
|
+
* frame (`begin`, `add`…, `end`). One draw call for all of them. It is the glow of the low tier, and
|
|
185
|
+
* under the post chain's bloom it makes the shining things bloom harder.
|
|
186
|
+
*/
|
|
187
|
+
declare class Halos {
|
|
188
|
+
readonly object: THREE.Mesh;
|
|
189
|
+
private readonly sprites;
|
|
190
|
+
private n;
|
|
191
|
+
private readonly colors;
|
|
192
|
+
constructor(capacity?: number);
|
|
193
|
+
begin(): void;
|
|
194
|
+
/** A soft light of a colour around a point (`size` metres across). */
|
|
195
|
+
add(x: number, y: number, z: number, size: number, hex: string, alpha?: number): void;
|
|
196
|
+
end(): void;
|
|
197
|
+
dispose(): void;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
declare class BlobShadows {
|
|
201
|
+
readonly object: THREE.InstancedMesh;
|
|
202
|
+
private readonly texture;
|
|
203
|
+
private readonly m;
|
|
204
|
+
private n;
|
|
205
|
+
constructor(capacity?: number, opacity?: number);
|
|
206
|
+
begin(): void;
|
|
207
|
+
/** A spot `w` × `d` metres at (x, y, z); `y` is the ground under the thing. */
|
|
208
|
+
add(x: number, y: number, z: number, w: number, d?: number): void;
|
|
209
|
+
end(): void;
|
|
210
|
+
dispose(): void;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
interface PostChain {
|
|
214
|
+
/** Draws the scene through the chain to the screen. */
|
|
215
|
+
render(dt: number): void;
|
|
216
|
+
setSize(width: number, height: number): void;
|
|
217
|
+
setMood(mood: Mood): void;
|
|
218
|
+
dispose(): void;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Builds the chain for a scene and a camera. Resolves to null when it cannot be built; the caller then
|
|
222
|
+
* keeps drawing with `renderer.render`. The renderer's own tone mapping is left as it is: three applies
|
|
223
|
+
* it only to a frame drawn straight to the screen, so the chain maps the tones itself (after the glow),
|
|
224
|
+
* and a caller that stops using the chain (a slow device) gets its tone mapping back with no switch.
|
|
225
|
+
*/
|
|
226
|
+
declare function createPostChain(renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.Camera, mood: Mood, tier: TierSettings): Promise<PostChain | null>;
|
|
227
|
+
|
|
228
|
+
export { Billboards, BlobShadows, type BurstOptions, Halos, type LightRig, type ParticleSpawn, Particles, type PostChain, type RendererSetup, SPRITES, type Sky, type SpriteName, type ToyFinish, ToyLook, type ToyOptions, createLightRig, createPostChain, createSky, setupRenderer, setupShadowMap, setupSunShadow, softShadowsSupported, spriteIndex };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {j as j$1}from'../chunk-PYGRABBR.js';import*as c from'three';function j(n,e,t,i=e.maxDensity){let r=typeof devicePixelRatio=="number"&&devicePixelRatio>0?devicePixelRatio:1,o=Math.max(1,Math.min(r,e.maxDensity,i));return n.setPixelRatio(o),n.outputColorSpace=c.SRGBColorSpace,n.toneMapping=c.NeutralToneMapping,n.toneMappingExposure=t.exposure,B(n,e),{density:o}}function F(n){let e=n.extensions;return e.has("EXT_color_buffer_float")||e.has("EXT_color_buffer_half_float")}function B(n,e){n.shadowMap.enabled=e.shadows!=="none",n.shadowMap.type=e.shadows!=="none"&&F(n)?c.VSMShadowMap:c.PCFShadowMap;}function I(n,e,t,i,r=t.shadowMap){e.castShadow=t.shadows!=="none",e.shadow.intensity=i,!(r<=0)&&(e.shadow.mapSize.set(r,r),n.shadowMap.type===c.VSMShadowMap?(e.shadow.radius=Math.max(3,Math.round(r/256)),e.shadow.blurSamples=8,e.shadow.bias=-8e-4,e.shadow.normalBias=.02):(e.shadow.radius=3,e.shadow.bias=-4e-4,e.shadow.normalBias=.03));}function U(n,e,t,i){let r=new c.HemisphereLight(e.ambientSky,e.ambientGround,e.ambientPower),o=new c.DirectionalLight(e.sun,e.sunPower),a=new c.Vector3(-0.5,1,-0.6).normalize();I(i,o,t,e.shadow),n.add(r,o,o.target);let s=0;return {hemi:r,sun:o,sunDir:a,setMood(l){r.color.set(l.ambientSky),r.groundColor.set(l.ambientGround),r.intensity=l.ambientPower,o.color.set(l.sun),o.intensity=l.sunPower,o.shadow.intensity=l.shadow;},aim(l,p){if(o.target.position.copy(l),o.position.copy(l).addScaledVector(a,p*1.2),p===s)return;s=p;let u=o.shadow.camera;u.left=u.bottom=-p/2,u.right=u.top=p/2,u.near=.5,u.far=p*3,u.updateProjectionMatrix();},setShadows(l){o.castShadow=l;},dispose(){n.remove(r,o,o.target),o.shadow.map?.dispose(),r.dispose(),o.dispose();}}}var _=`
|
|
2
|
+
varying vec3 vDir;
|
|
3
|
+
void main() {
|
|
4
|
+
vDir = position;
|
|
5
|
+
vec4 p = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
6
|
+
// on the far plane: everything of the world is drawn over it
|
|
7
|
+
gl_Position = p.xyww;
|
|
8
|
+
}
|
|
9
|
+
`,G=`
|
|
10
|
+
uniform vec3 uTop;
|
|
11
|
+
uniform vec3 uHorizon;
|
|
12
|
+
uniform vec3 uBottom;
|
|
13
|
+
uniform vec3 uSun;
|
|
14
|
+
uniform vec3 uSunDir;
|
|
15
|
+
uniform vec3 uCloud;
|
|
16
|
+
uniform float uClouds;
|
|
17
|
+
uniform float uStars;
|
|
18
|
+
uniform float uTime;
|
|
19
|
+
varying vec3 vDir;
|
|
20
|
+
|
|
21
|
+
#ifdef SKY_DETAIL
|
|
22
|
+
float hash(vec2 p) {
|
|
23
|
+
p = fract(p * vec2(123.34, 456.21));
|
|
24
|
+
p += dot(p, p + 45.32);
|
|
25
|
+
return fract(p.x * p.y);
|
|
26
|
+
}
|
|
27
|
+
float noise(vec2 p) {
|
|
28
|
+
vec2 i = floor(p);
|
|
29
|
+
vec2 f = fract(p);
|
|
30
|
+
vec2 u = f * f * (3.0 - 2.0 * f);
|
|
31
|
+
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x),
|
|
32
|
+
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);
|
|
33
|
+
}
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
void main() {
|
|
37
|
+
vec3 d = normalize(vDir);
|
|
38
|
+
float up = d.y;
|
|
39
|
+
// the horizon is a soft band: the zenith above it, the haze below
|
|
40
|
+
vec3 col = mix(uHorizon, uTop, pow(clamp(up, 0.0, 1.0), 0.55));
|
|
41
|
+
col = mix(col, uBottom, smoothstep(0.0, -0.35, up));
|
|
42
|
+
|
|
43
|
+
float s = max(dot(d, uSunDir), 0.0);
|
|
44
|
+
// a wide warm halo and a tight glow at the sun itself
|
|
45
|
+
col += uSun * (pow(s, 8.0) * 0.22 + pow(s, 96.0) * 0.9);
|
|
46
|
+
|
|
47
|
+
#ifdef SKY_DETAIL
|
|
48
|
+
if (uClouds > 0.0 && up > 0.02) {
|
|
49
|
+
vec2 p = d.xz / (up + 0.18) * 1.6 + vec2(uTime * 0.012, uTime * 0.004);
|
|
50
|
+
float n = noise(p) * 0.62 + noise(p * 2.3 + 7.1) * 0.38;
|
|
51
|
+
float c = smoothstep(1.0 - uClouds * 0.62, 1.0 - uClouds * 0.62 + 0.26, n);
|
|
52
|
+
// clouds thin out towards the horizon, lit by the sun's side
|
|
53
|
+
c *= smoothstep(0.02, 0.22, up);
|
|
54
|
+
col = mix(col, uCloud + uSun * pow(s, 6.0) * 0.25, c * 0.9);
|
|
55
|
+
}
|
|
56
|
+
if (uStars > 0.0 && up > 0.0) {
|
|
57
|
+
vec2 g = d.xz / (up + 0.4) * 46.0;
|
|
58
|
+
float h = hash(floor(g));
|
|
59
|
+
float tw = 0.6 + 0.4 * sin(uTime * 2.0 + h * 40.0);
|
|
60
|
+
float star = step(0.985, h) * smoothstep(0.5, 0.0, length(fract(g) - 0.5));
|
|
61
|
+
col += vec3(star * tw * uStars) * smoothstep(0.0, 0.3, up);
|
|
62
|
+
}
|
|
63
|
+
#endif
|
|
64
|
+
|
|
65
|
+
gl_FragColor = vec4(col, 1.0);
|
|
66
|
+
#include <tonemapping_fragment>
|
|
67
|
+
#include <colorspace_fragment>
|
|
68
|
+
}
|
|
69
|
+
`;function O(n,e){let t={uTop:{value:new c.Color},uHorizon:{value:new c.Color},uBottom:{value:new c.Color},uSun:{value:new c.Color},uSunDir:{value:new c.Vector3(-0.5,1,-0.6).normalize()},uCloud:{value:new c.Color},uClouds:{value:0},uStars:{value:0},uTime:{value:0}},i=new c.ShaderMaterial({uniforms:t,vertexShader:_,fragmentShader:G,defines:e?{SKY_DETAIL:1}:{},side:c.BackSide,depthWrite:false,depthTest:false,fog:false}),r=new c.SphereGeometry(1,24,12),o=new c.Mesh(r,i);o.frustumCulled=false,o.renderOrder=-1e3,o.matrixAutoUpdate=false;let a=s=>{t.uTop.value.set(s.skyTop),t.uHorizon.value.set(s.skyHorizon),t.uBottom.value.set(s.skyBottom),t.uSun.value.set(s.sun),t.uCloud.value.set(s.cloudColor),t.uClouds.value=s.clouds,t.uStars.value=s.stars;};return a(n),{object:o,setMood:a,setSun(s){t.uSunDir.value.copy(s).normalize();},update(s,l){t.uTime.value=l,o.matrix.makeTranslation(s.position.x,s.position.y,s.position.z),o.matrixWorldNeedsUpdate=true;},dispose(){r.dispose(),i.dispose();}}}var N={matte:{spec:0,gloss:8,rim:.15,emissive:0},plastic:{spec:.28,gloss:28,rim:1,emissive:0},glossy:{spec:.7,gloss:80,rim:1.15,emissive:0},glow:{spec:.2,gloss:28,rim:.6,emissive:.85}};function V(n,e,t){n.onBeforeCompile=i=>{i.uniforms.uToyRim=e.uToyRim,i.uniforms.uToyRimPower=e.uToyRimPower,i.uniforms.uToySpec={value:t.spec},i.uniforms.uToyGloss={value:t.gloss},i.uniforms.uToyRimK={value:t.rim},i.fragmentShader=i.fragmentShader.replace("#include <common>",`#include <common>
|
|
70
|
+
uniform vec3 uToyRim;
|
|
71
|
+
uniform float uToyRimPower;
|
|
72
|
+
uniform float uToySpec;
|
|
73
|
+
uniform float uToyGloss;
|
|
74
|
+
uniform float uToyRimK;`).replace("#include <opaque_fragment>",`{
|
|
75
|
+
vec3 toyV = normalize(vViewPosition);
|
|
76
|
+
vec3 toyN = normalize(normal);
|
|
77
|
+
float toyF = 1.0 - saturate(dot(toyN, toyV));
|
|
78
|
+
// the sky lights from above: the rim is strongest on edges that face up
|
|
79
|
+
vec3 toyUp = normalize((viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz);
|
|
80
|
+
float toySky = 0.35 + 0.65 * saturate(dot(toyN, toyUp) * 0.5 + 0.5);
|
|
81
|
+
outgoingLight += uToyRim * (toyF * toyF * toyF) * uToyRimPower * uToyRimK * toySky * diffuseColor.rgb;
|
|
82
|
+
#if NUM_DIR_LIGHTS > 0
|
|
83
|
+
vec3 toyH = normalize(directionalLights[0].direction + toyV);
|
|
84
|
+
// no highlight where the sun does not reach (a shadow, a back face)
|
|
85
|
+
float toyLit = saturate(dot(reflectedLight.directDiffuse, vec3(0.6)) * 4.0);
|
|
86
|
+
outgoingLight += directionalLights[0].color * pow(saturate(dot(toyN, toyH)), uToyGloss) * uToySpec * toyLit * 0.32;
|
|
87
|
+
#endif
|
|
88
|
+
}
|
|
89
|
+
#include <opaque_fragment>`);},n.customProgramCacheKey=()=>"idos-toy-1";}var C=class{cache=new Map;shared;vibrance;constructor(e){this.vibrance=e.vibrance,this.shared={uToyRim:{value:new c.Color(e.rim)},uToyRimPower:{value:e.rimPower}};}setMood(e){if(this.shared.uToyRim.value.set(e.rim),this.shared.uToyRimPower.value=e.rimPower,e.vibrance!==this.vibrance){this.vibrance=e.vibrance;for(let[t,i]of this.cache)i.color.set(j$1(t.slice(0,t.indexOf("|")),this.vibrance)),i.emissiveIntensity>0&&i.emissive.getHex()!==0&&i.emissive.copy(i.color);}}get(e,t={}){let i=t.finish??"plastic",r=t.alpha??1,o=`${e}|${i}|${r}|${t.flat?1:0}`,a=this.cache.get(o);return a||(a=this.make(e,t),this.cache.set(o,a)),a}make(e,t={}){let i=N[t.finish??"plastic"],r=t.alpha??1,o=new c.Color(t.map?e:j$1(e,this.vibrance)),a=new c.MeshLambertMaterial({color:o,transparent:r<1,opacity:r,depthWrite:r>=1,flatShading:!!t.flat,...t.map?{map:t.map}:{},...t.side===void 0?{}:{side:t.side},...t.alphaTest?{alphaTest:t.alphaTest}:{}});return i.emissive>0&&(a.emissive=o.clone(),a.emissiveIntensity=i.emissive),V(a,this.shared,i),a}dispose(){for(let e of this.cache.values())e.dispose();this.cache.clear();}};var x=["dot","spark","puff","chip","ring","star"],E=64,y=null;function W(){if(y)return y.users+=1,y.texture;let n=document.createElement("canvas");n.width=E*x.length,n.height=E;let e=n.getContext("2d"),t=E/2,i=(a,s)=>{e.save(),e.translate(a*E,0),e.beginPath(),e.rect(0,0,E,E),e.clip(),s(),e.restore();},r=(a,s=t)=>{let l=e.createRadialGradient(t,t,0,t,t,s);for(let[p,u]of a)l.addColorStop(p,`rgba(255,255,255,${u})`);e.fillStyle=l,e.fillRect(0,0,E,E);};i(0,()=>r([[0,1],[.25,.85],[.6,.22],[1,0]])),i(1,()=>{r([[0,1],[.5,.25],[1,0]],t*.55),e.fillStyle="rgba(255,255,255,0.95)";for(let a of [0,Math.PI/2])e.save(),e.translate(t,t),e.rotate(a),e.beginPath(),e.moveTo(-t*.96,0),e.quadraticCurveTo(0,-t*.1,t*.96,0),e.quadraticCurveTo(0,t*.1,-t*.96,0),e.fill(),e.restore();}),i(2,()=>r([[0,.7],[.55,.42],[1,0]])),i(3,()=>{e.fillStyle="#ffffff",e.beginPath(),e.roundRect(t*.3,t*.55,t*1.4,t*.9,t*.14),e.fill();}),i(4,()=>{e.strokeStyle="#ffffff",e.lineWidth=t*.16,e.beginPath(),e.arc(t,t,t*.8,0,Math.PI*2),e.stroke();}),i(5,()=>{e.fillStyle="#ffffff",e.beginPath();for(let a=0;a<10;a++){let s=a%2===0?t*.92:t*.4,l=-Math.PI/2+a*Math.PI/5;e.lineTo(t+Math.cos(l)*s,t+Math.sin(l)*s);}e.closePath(),e.fill();});let o=new c.CanvasTexture(n);return o.colorSpace=c.SRGBColorSpace,o.generateMipmaps=false,o.minFilter=c.LinearFilter,y={texture:o,users:1},o}function K(){y&&(y.users-=1,!(y.users>0)&&(y.texture.dispose(),y=null));}var $=`
|
|
90
|
+
attribute vec3 iPos;
|
|
91
|
+
attribute vec4 iLook; // size, turn, sprite cell, additive (0 / 1)
|
|
92
|
+
attribute vec4 iColor; // rgb, alpha
|
|
93
|
+
uniform float uCells;
|
|
94
|
+
varying vec2 vUv;
|
|
95
|
+
varying vec4 vColor;
|
|
96
|
+
varying float vAdd;
|
|
97
|
+
void main() {
|
|
98
|
+
float cs = cos(iLook.y);
|
|
99
|
+
float sn = sin(iLook.y);
|
|
100
|
+
vec2 corner = position.xy * iLook.x;
|
|
101
|
+
corner = vec2(corner.x * cs - corner.y * sn, corner.x * sn + corner.y * cs);
|
|
102
|
+
vec4 mv = modelViewMatrix * vec4(iPos, 1.0);
|
|
103
|
+
mv.xy += corner;
|
|
104
|
+
gl_Position = projectionMatrix * mv;
|
|
105
|
+
vUv = vec2((uv.x + iLook.z) / uCells, uv.y);
|
|
106
|
+
vColor = iColor;
|
|
107
|
+
vAdd = iLook.w;
|
|
108
|
+
}
|
|
109
|
+
`,q=`
|
|
110
|
+
uniform sampler2D uMap;
|
|
111
|
+
varying vec2 vUv;
|
|
112
|
+
varying vec4 vColor;
|
|
113
|
+
varying float vAdd;
|
|
114
|
+
void main() {
|
|
115
|
+
float a = texture2D(uMap, vUv).a * vColor.a;
|
|
116
|
+
if (a < 0.004) discard;
|
|
117
|
+
// premultiplied: an additive sprite adds its light and covers nothing
|
|
118
|
+
gl_FragColor = vec4(vColor.rgb * a, a * (1.0 - vAdd));
|
|
119
|
+
#include <tonemapping_fragment>
|
|
120
|
+
#include <colorspace_fragment>
|
|
121
|
+
}
|
|
122
|
+
`,R=class{object;capacity;geometry;material;pos;look;color;constructor(e,t=900){this.capacity=e;let i=new c.PlaneGeometry(1,1),r=new c.InstancedBufferGeometry;r.index=i.index,r.setAttribute("position",i.getAttribute("position")),r.setAttribute("uv",i.getAttribute("uv")),this.pos=new c.InstancedBufferAttribute(new Float32Array(e*3),3),this.look=new c.InstancedBufferAttribute(new Float32Array(e*4),4),this.color=new c.InstancedBufferAttribute(new Float32Array(e*4),4);for(let o of [this.pos,this.look,this.color])o.setUsage(c.DynamicDrawUsage);r.setAttribute("iPos",this.pos),r.setAttribute("iLook",this.look),r.setAttribute("iColor",this.color),r.instanceCount=0,this.geometry=r,this.material=new c.ShaderMaterial({uniforms:{uMap:{value:W()},uCells:{value:x.length}},vertexShader:$,fragmentShader:q,transparent:true,depthWrite:false,blending:c.CustomBlending,blendSrc:c.OneFactor,blendDst:c.OneMinusSrcAlphaFactor,blendSrcAlpha:c.OneFactor,blendDstAlpha:c.OneMinusSrcAlphaFactor,fog:false}),this.object=new c.Mesh(r,this.material),this.object.frustumCulled=false,this.object.renderOrder=t;}put(e,t,i,r,o,a,s,l,p,u,b,T){let d=this.pos.array,f=this.look.array,S=this.color.array;d[e*3]=t,d[e*3+1]=i,d[e*3+2]=r,f[e*4]=o,f[e*4+1]=a,f[e*4+2]=s,f[e*4+3]=l?1:0,S[e*4]=p,S[e*4+1]=u,S[e*4+2]=b,S[e*4+3]=T;}commit(e){this.geometry.instanceCount=e,this.object.visible=e>0,e!==0&&(this.pos.needsUpdate=true,this.look.needsUpdate=true,this.color.needsUpdate=true);}dispose(){this.geometry.dispose(),this.material.dispose(),K();}},z=n=>x.indexOf(n);var w=16,P=class{object;sprites;data;flags;side;count=0;colors=new Map;amount;constructor(e=512,t=1){this.sprites=new R(e),this.object=this.sprites.object,this.data=new Float32Array(e*w),this.flags=new Uint8Array(e),this.side=new Float32Array(e*3),this.amount=t,this.sprites.commit(0);}get alive(){return this.count}color(e){let t=this.colors.get(e);return t||(t=new c.Color(e),this.colors.set(e,t)),t}spawn(e){if(this.count>=this.sprites.capacity)return;let t=this.count++,i=this.data,r=t*w,o=this.color(e.color);i[r]=e.x,i[r+1]=e.y,i[r+2]=e.z,i[r+3]=e.vx??0,i[r+4]=e.vy??0,i[r+5]=e.vz??0,i[r+6]=0,i[r+7]=e.life,i[r+8]=e.size,i[r+9]=e.sizeEnd??0,i[r+10]=Math.random()*Math.PI*2,i[r+11]=e.spin??0,i[r+12]=o.r,i[r+13]=o.g,i[r+14]=o.b,i[r+15]=e.alpha??1,this.side[t*3]=e.gravity??0,this.side[t*3+1]=e.drag??0,this.side[t*3+2]=e.floor??Number.NEGATIVE_INFINITY,this.flags[t]=z(e.sprite??"dot")|(e.additive?16:0);}many(e){return Math.max(1,Math.round(e*this.amount))}burst(e){let t=this.many(e.n??14),i=e.size??1.2;for(let r=0;r<t;r++){let o=Math.random()*Math.PI*2,a=i*(1.4+Math.random()*2.2),s=r%3===0;this.spawn({x:e.x,y:e.y,z:e.z,vx:Math.cos(o)*a,vy:i*(2+Math.random()*3.2),vz:Math.sin(o)*a,life:.45+Math.random()*.35,size:i*(s?.34:.2)*(.7+Math.random()*.6),color:e.color,sprite:s?"spark":"dot",additive:s,gravity:16,drag:1.2,spin:s?0:4});}this.spawn({x:e.x,y:e.y,z:e.z,life:.22,size:i*.7,sizeEnd:i*2.2,color:e.color,sprite:"dot",additive:true,alpha:.7});}confetti(e){let t=this.many(e.n??40),i=e.size??2;for(let r=0;r<t;r++){let o=Math.random()*Math.PI*2,a=i*(.6+Math.random()*1.6);this.spawn({x:e.x,y:e.y,z:e.z,vx:Math.cos(o)*a,vy:i*(3+Math.random()*3),vz:Math.sin(o)*a,life:1.3+Math.random()*.9,size:i*(.08+Math.random()*.06),sizeEnd:i*.07,color:e.colors[r%e.colors.length]??"#ffffff",sprite:"chip",gravity:i*3.5,drag:1.6,spin:(Math.random()-.5)*16});}}sparkle(e){let t=this.many(e.n??6),i=e.size??.6;for(let r=0;r<t;r++)this.spawn({x:e.x+(Math.random()-.5)*i,y:e.y+(Math.random()-.2)*i,z:e.z+(Math.random()-.5)*i,vy:.3+Math.random()*.5,life:.5+Math.random()*.4,size:i*(.35+Math.random()*.35),color:e.color,sprite:"spark",additive:true});}dust(e){let t=this.many(e.n??5),i=e.size??.6;for(let r=0;r<t;r++){let o=Math.random()*Math.PI*2;this.spawn({x:e.x,y:e.y+.05,z:e.z,vx:Math.cos(o)*i*1.6,vy:.4+Math.random()*.6,vz:Math.sin(o)*i*1.6,life:.45+Math.random()*.3,size:i*.5,sizeEnd:i*1.3,color:e.color,sprite:"puff",drag:3,alpha:.55});}}ring(e){let t=e.size??1.5;this.spawn({x:e.x,y:e.y,z:e.z,life:.4,size:t*.4,sizeEnd:t*1.9,color:e.color,sprite:"ring",additive:true});}update(e){if(this.count===0){this.sprites.commit(0);return}let t=this.data,i=this.side,r=0;for(;r<this.count;){let o=r*w,a=t[o+6]+e,s=t[o+7];if(a>=s){let f=--this.count;f!==r&&(t.copyWithin(o,f*w,f*w+w),i.copyWithin(r*3,f*3,f*3+3),this.flags[r]=this.flags[f]);continue}t[o+6]=a;let l=Math.max(0,1-i[r*3+1]*e);t[o+3]=t[o+3]*l,t[o+4]=(t[o+4]-i[r*3]*e)*l,t[o+5]=t[o+5]*l,t[o]=t[o]+t[o+3]*e,t[o+1]=t[o+1]+t[o+4]*e,t[o+2]=t[o+2]+t[o+5]*e;let p=i[r*3+2];t[o+1]<p&&(t[o+1]=p,t[o+4]=-t[o+4]*.35),t[o+10]=t[o+10]+t[o+11]*e;let u=a/s,b=t[o+8]+(t[o+9]-t[o+8])*u,T=u<.65?1:1-(u-.65)/.35,d=this.flags[r];this.sprites.put(r,t[o],t[o+1],t[o+2],b,t[o+10],d&15,(d&16)!==0,t[o+12],t[o+13],t[o+14],t[o+15]*T),r++;}this.sprites.commit(this.count);}clear(){this.count=0,this.sprites.commit(0);}dispose(){this.sprites.dispose();}},A=class{object;sprites;n=0;colors=new Map;constructor(e=256){this.sprites=new R(e,850),this.object=this.sprites.object,this.sprites.commit(0);}begin(){this.n=0;}add(e,t,i,r,o,a=.5){if(this.n>=this.sprites.capacity)return;let s=this.colors.get(o);s||(s=new c.Color(o),this.colors.set(o,s)),this.sprites.put(this.n++,e,t,i,r,0,0,true,s.r,s.g,s.b,a);}end(){this.sprites.commit(this.n);}dispose(){this.sprites.dispose();}};var L=class{object;texture;m=new c.Matrix4;n=0;constructor(e=256,t=.28){let i=document.createElement("canvas");i.width=i.height=64;let r=i.getContext("2d"),o=r.createRadialGradient(32,32,0,32,32,32);o.addColorStop(0,"rgba(0,0,0,1)"),o.addColorStop(.35,"rgba(0,0,0,0.75)"),o.addColorStop(.7,"rgba(0,0,0,0.25)"),o.addColorStop(1,"rgba(0,0,0,0)"),r.fillStyle=o,r.fillRect(0,0,64,64),this.texture=new c.CanvasTexture(i);let a=new c.MeshBasicMaterial({map:this.texture,color:"#1d2a6b",transparent:true,opacity:t,depthWrite:false,toneMapped:false,polygonOffset:true,polygonOffsetFactor:-2,polygonOffsetUnits:-2}),s=new c.PlaneGeometry(1,1).rotateX(-Math.PI/2);this.object=new c.InstancedMesh(s,a,e),this.object.frustumCulled=false,this.object.count=0,this.object.renderOrder=10;}begin(){this.n=0;}add(e,t,i,r,o=r){this.n>=this.object.instanceMatrix.count||(this.m.makeScale(r,1,o).setPosition(e,t+.02,i),this.object.setMatrixAt(this.n++,this.m));}end(){this.object.count=this.n,this.object.visible=this.n>0,this.n>0&&(this.object.instanceMatrix.needsUpdate=true);}dispose(){this.object.geometry.dispose(),this.object.material.dispose(),this.texture.dispose(),this.object.dispose();}};var D=64;function Y(){return new c.ShaderMaterial({name:"SanitizeMaterial",uniforms:{inputBuffer:new c.Uniform(null)},vertexShader:`
|
|
123
|
+
varying vec2 vUv;
|
|
124
|
+
void main() {
|
|
125
|
+
vUv = position.xy * 0.5 + 0.5;
|
|
126
|
+
gl_Position = vec4(position.xy, 1.0, 1.0);
|
|
127
|
+
}
|
|
128
|
+
`,fragmentShader:`
|
|
129
|
+
uniform sampler2D inputBuffer;
|
|
130
|
+
varying vec2 vUv;
|
|
131
|
+
// NaN: nothing; +infinity (an overflowed highlight): as bright as kept; \u2212infinity: nothing
|
|
132
|
+
float finite(float v) {
|
|
133
|
+
return isnan(v) ? 0.0 : isinf(v) ? (v > 0.0 ? ${D.toFixed(1)} : 0.0) : v;
|
|
134
|
+
}
|
|
135
|
+
void main() {
|
|
136
|
+
vec4 c = texture2D(inputBuffer, vUv);
|
|
137
|
+
c = vec4(finite(c.r), finite(c.g), finite(c.b), finite(c.a));
|
|
138
|
+
gl_FragColor = vec4(clamp(c.rgb, 0.0, ${D.toFixed(1)}), clamp(c.a, 0.0, 1.0));
|
|
139
|
+
}
|
|
140
|
+
`,depthTest:false,depthWrite:false,toneMapped:false})}async function X(n,e,t,i,r){if(!r.post)return null;try{let o=await import('postprocessing'),a=r.tier==="high",s=new o.EffectComposer(n,{frameBufferType:a?c.HalfFloatType:c.UnsignedByteType,multisampling:Math.min(r.msaa,n.capabilities.maxSamples??0)});s.addPass(new o.RenderPass(e,t)),a&&s.addPass(new o.ShaderPass(Y(),"inputBuffer"));let l=new o.BloomEffect({mipmapBlur:!0,luminanceThreshold:i.bloomThreshold,luminanceSmoothing:.18,intensity:i.bloom,radius:.72,levels:a?6:4}),p=new o.ToneMappingEffect({mode:o.ToneMappingMode.NEUTRAL}),u=new o.HueSaturationEffect({saturation:i.saturation-1}),b=new o.BrightnessContrastEffect({contrast:i.contrast-1}),T=new o.VignetteEffect({offset:.32,darkness:i.vignette});return s.addPass(new o.EffectPass(t,l,p,u,b,T)),{render(d){s.render(d);},setSize(d,f){s.setSize(d,f,!1);},setMood(d){l.intensity=d.bloom,l.luminanceMaterial.threshold=d.bloomThreshold,u.saturation=d.saturation-1,b.contrast=d.contrast-1,T.darkness=d.vignette;},dispose(){s.dispose();}}}catch{return null}}export{R as Billboards,L as BlobShadows,A as Halos,P as Particles,x as SPRITES,C as ToyLook,U as createLightRig,X as createPostChain,O as createSky,j as setupRenderer,B as setupShadowMap,I as setupSunShadow,F as softShadowsSupported,z as spriteIndex};
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idosgames/gfx",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The one visual standard of iDosGames games, 2D and 3D: quality tiers for phones and desktops with a verified fallback, moods as data (sky, sun, ambient light, exposure, saturation, glow, vignette), hue-shifted shading, and the pieces a game draws with them — for three.js a renderer setup, a light rig, a gradient sky, a toy material, instanced particles, glow halos, blob shadows and a one-pass post chain; for Phaser a canvas density helper, particles, glow sprites and a frame grade. Engine-free core: engines live in subpaths.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "iDos Games",
|
|
8
|
+
"homepage": "https://github.com/iDos-Games/iDosGamesSDK_TS#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/iDos-Games/iDosGamesSDK_TS.git",
|
|
12
|
+
"directory": "packages/gfx"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/iDos-Games/iDosGamesSDK_TS/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"idosgames",
|
|
19
|
+
"gfx",
|
|
20
|
+
"three",
|
|
21
|
+
"phaser",
|
|
22
|
+
"postprocessing",
|
|
23
|
+
"particles",
|
|
24
|
+
"mobile"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js",
|
|
37
|
+
"require": "./dist/index.cjs"
|
|
38
|
+
},
|
|
39
|
+
"./three": {
|
|
40
|
+
"types": "./dist/three/index.d.ts",
|
|
41
|
+
"import": "./dist/three/index.js",
|
|
42
|
+
"require": "./dist/three/index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./phaser": {
|
|
45
|
+
"types": "./dist/phaser/index.d.ts",
|
|
46
|
+
"import": "./dist/phaser/index.js",
|
|
47
|
+
"require": "./dist/phaser/index.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"README.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
58
|
+
},
|
|
59
|
+
"//": "The core is engine-free (depcruise gfx-core-no-engine). Engines are optional peers, each needed only by its subpath: three — /three, phaser — /phaser. postprocessing is the frame post chain of /three: imported lazily, only on the mid and high tiers, so a low-tier phone never downloads or compiles it. ⚠ postprocessing caps three (< 0.188): raise three only together with it.",
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"postprocessing": "6.39.6"
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"phaser": "^4.2.1",
|
|
65
|
+
"three": ">=0.185.1 <0.188.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"phaser": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"three": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@types/three": "0.185.1",
|
|
77
|
+
"phaser": "4.2.1",
|
|
78
|
+
"three": "0.185.1"
|
|
79
|
+
}
|
|
80
|
+
}
|