@idosgames/gfx 0.0.0-stage → 0.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.
@@ -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,122 @@
1
+ import {j as j$1}from'../chunk-PYGRABBR.js';import*as c from'three';function j(n,e,t,r=e.maxDensity){let i=typeof devicePixelRatio=="number"&&devicePixelRatio>0?devicePixelRatio:1,o=Math.max(1,Math.min(i,e.maxDensity,r));return n.setPixelRatio(o),n.outputColorSpace=c.SRGBColorSpace,n.toneMapping=c.NeutralToneMapping,n.toneMappingExposure=t.exposure,F(n,e),{density:o}}function I(n){let e=n.extensions;return e.has("EXT_color_buffer_float")||e.has("EXT_color_buffer_half_float")}function F(n,e){n.shadowMap.enabled=e.shadows!=="none",n.shadowMap.type=e.shadows!=="none"&&I(n)?c.VSMShadowMap:c.PCFShadowMap;}function B(n,e,t,r,i=t.shadowMap){e.castShadow=t.shadows!=="none",e.shadow.intensity=r,!(i<=0)&&(e.shadow.mapSize.set(i,i),n.shadowMap.type===c.VSMShadowMap?(e.shadow.radius=Math.max(3,Math.round(i/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 D(n,e,t,r){let i=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();B(r,o,t,e.shadow),n.add(i,o,o.target);let s=0;return {hemi:i,sun:o,sunDir:a,setMood(l){i.color.set(l.ambientSky),i.groundColor.set(l.ambientGround),i.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(i,o,o.target),o.shadow.map?.dispose(),i.dispose(),o.dispose();}}}var G=`
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
+ `,U=`
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 _(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}},r=new c.ShaderMaterial({uniforms:t,vertexShader:G,fragmentShader:U,defines:e?{SKY_DETAIL:1}:{},side:c.BackSide,depthWrite:false,depthTest:false,fog:false}),i=new c.SphereGeometry(1,24,12),o=new c.Mesh(i,r);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(){i.dispose(),r.dispose();}}}var O={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 N(n,e,t){n.onBeforeCompile=r=>{r.uniforms.uToyRim=e.uToyRim,r.uniforms.uToyRimPower=e.uToyRimPower,r.uniforms.uToySpec={value:t.spec},r.uniforms.uToyGloss={value:t.gloss},r.uniforms.uToyRimK={value:t.rim},r.fragmentShader=r.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,r]of this.cache)r.color.set(j$1(t.slice(0,t.indexOf("|")),this.vibrance)),r.emissiveIntensity>0&&r.emissive.getHex()!==0&&r.emissive.copy(r.color);}}get(e,t={}){let r=t.finish??"plastic",i=t.alpha??1,o=`${e}|${r}|${i}|${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 r=O[t.finish??"plastic"],i=t.alpha??1,o=new c.Color(t.map?e:j$1(e,this.vibrance)),a=new c.MeshLambertMaterial({color:o,transparent:i<1,opacity:i,depthWrite:i>=1,flatShading:!!t.flat,...t.map?{map:t.map}:{},...t.side===void 0?{}:{side:t.side},...t.alphaTest?{alphaTest:t.alphaTest}:{}});return r.emissive>0&&(a.emissive=o.clone(),a.emissiveIntensity=r.emissive),N(a,this.shared,r),a}dispose(){for(let e of this.cache.values())e.dispose();this.cache.clear();}};var S=["dot","spark","puff","chip","ring","star"],E=64,y=null;function V(){if(y)return y.users+=1,y.texture;let n=document.createElement("canvas");n.width=E*S.length,n.height=E;let e=n.getContext("2d"),t=E/2,r=(a,s)=>{e.save(),e.translate(a*E,0),e.beginPath(),e.rect(0,0,E,E),e.clip(),s(),e.restore();},i=(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);};r(0,()=>i([[0,1],[.25,.85],[.6,.22],[1,0]])),r(1,()=>{i([[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();}),r(2,()=>i([[0,.7],[.55,.42],[1,0]])),r(3,()=>{e.fillStyle="#ffffff",e.beginPath(),e.roundRect(t*.3,t*.55,t*1.4,t*.9,t*.14),e.fill();}),r(4,()=>{e.strokeStyle="#ffffff",e.lineWidth=t*.16,e.beginPath(),e.arc(t,t,t*.8,0,Math.PI*2),e.stroke();}),r(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 W(){y&&(y.users-=1,!(y.users>0)&&(y.texture.dispose(),y=null));}var K=`
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
+ `,T=class{object;capacity;geometry;material;pos;look;color;constructor(e,t=900){this.capacity=e;let r=new c.PlaneGeometry(1,1),i=new c.InstancedBufferGeometry;i.index=r.index,i.setAttribute("position",r.getAttribute("position")),i.setAttribute("uv",r.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);i.setAttribute("iPos",this.pos),i.setAttribute("iLook",this.look),i.setAttribute("iColor",this.color),i.instanceCount=0,this.geometry=i,this.material=new c.ShaderMaterial({uniforms:{uMap:{value:V()},uCells:{value:S.length}},vertexShader:K,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(i,this.material),this.object.frustumCulled=false,this.object.renderOrder=t;}put(e,t,r,i,o,a,s,l,p,u,b,g){let d=this.pos.array,f=this.look.array,M=this.color.array;d[e*3]=t,d[e*3+1]=r,d[e*3+2]=i,f[e*4]=o,f[e*4+1]=a,f[e*4+2]=s,f[e*4+3]=l?1:0,M[e*4]=p,M[e*4+1]=u,M[e*4+2]=b,M[e*4+3]=g;}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(),W();}},z=n=>S.indexOf(n);var R=16,P=class{object;sprites;data;flags;side;count=0;colors=new Map;amount;constructor(e=512,t=1){this.sprites=new T(e),this.object=this.sprites.object,this.data=new Float32Array(e*R),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++,r=this.data,i=t*R,o=this.color(e.color);r[i]=e.x,r[i+1]=e.y,r[i+2]=e.z,r[i+3]=e.vx??0,r[i+4]=e.vy??0,r[i+5]=e.vz??0,r[i+6]=0,r[i+7]=e.life,r[i+8]=e.size,r[i+9]=e.sizeEnd??0,r[i+10]=Math.random()*Math.PI*2,r[i+11]=e.spin??0,r[i+12]=o.r,r[i+13]=o.g,r[i+14]=o.b,r[i+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),r=e.size??1.2;for(let i=0;i<t;i++){let o=Math.random()*Math.PI*2,a=r*(1.4+Math.random()*2.2),s=i%3===0;this.spawn({x:e.x,y:e.y,z:e.z,vx:Math.cos(o)*a,vy:r*(2+Math.random()*3.2),vz:Math.sin(o)*a,life:.45+Math.random()*.35,size:r*(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:r*.7,sizeEnd:r*2.2,color:e.color,sprite:"dot",additive:true,alpha:.7});}confetti(e){let t=this.many(e.n??40),r=e.size??2;for(let i=0;i<t;i++){let o=Math.random()*Math.PI*2,a=r*(.6+Math.random()*1.6);this.spawn({x:e.x,y:e.y,z:e.z,vx:Math.cos(o)*a,vy:r*(3+Math.random()*3),vz:Math.sin(o)*a,life:1.3+Math.random()*.9,size:r*(.08+Math.random()*.06),sizeEnd:r*.07,color:e.colors[i%e.colors.length]??"#ffffff",sprite:"chip",gravity:r*3.5,drag:1.6,spin:(Math.random()-.5)*16});}}sparkle(e){let t=this.many(e.n??6),r=e.size??.6;for(let i=0;i<t;i++)this.spawn({x:e.x+(Math.random()-.5)*r,y:e.y+(Math.random()-.2)*r,z:e.z+(Math.random()-.5)*r,vy:.3+Math.random()*.5,life:.5+Math.random()*.4,size:r*(.35+Math.random()*.35),color:e.color,sprite:"spark",additive:true});}dust(e){let t=this.many(e.n??5),r=e.size??.6;for(let i=0;i<t;i++){let o=Math.random()*Math.PI*2;this.spawn({x:e.x,y:e.y+.05,z:e.z,vx:Math.cos(o)*r*1.6,vy:.4+Math.random()*.6,vz:Math.sin(o)*r*1.6,life:.45+Math.random()*.3,size:r*.5,sizeEnd:r*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,r=this.side,i=0;for(;i<this.count;){let o=i*R,a=t[o+6]+e,s=t[o+7];if(a>=s){let f=--this.count;f!==i&&(t.copyWithin(o,f*R,f*R+R),r.copyWithin(i*3,f*3,f*3+3),this.flags[i]=this.flags[f]);continue}t[o+6]=a;let l=Math.max(0,1-r[i*3+1]*e);t[o+3]=t[o+3]*l,t[o+4]=(t[o+4]-r[i*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=r[i*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,g=u<.65?1:1-(u-.65)/.35,d=this.flags[i];this.sprites.put(i,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]*g),i++;}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 T(e,850),this.object=this.sprites.object,this.sprites.commit(0);}begin(){this.n=0;}add(e,t,r,i,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,r,i,0,0,true,s.r,s.g,s.b,a);}end(){this.sprites.commit(this.n);}dispose(){this.sprites.dispose();}};var k=class{object;texture;m=new c.Matrix4;n=0;constructor(e=256,t=.28){let r=document.createElement("canvas");r.width=r.height=64;let i=r.getContext("2d"),o=i.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)"),i.fillStyle=o,i.fillRect(0,0,64,64),this.texture=new c.CanvasTexture(r);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,r,i,o=i){this.n>=this.object.instanceMatrix.count||(this.m.makeScale(i,1,o).setPosition(e,t+.02,r),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();}};async function $(n,e,t,r,i){if(!i.post)return null;try{let o=await import('postprocessing'),a=i.tier==="high",s=new o.EffectComposer(n,{frameBufferType:a?c.HalfFloatType:c.UnsignedByteType,multisampling:Math.min(i.msaa,n.capabilities.maxSamples??0)});s.addPass(new o.RenderPass(e,t));let l=new o.BloomEffect({mipmapBlur:!0,luminanceThreshold:r.bloomThreshold,luminanceSmoothing:.18,intensity:r.bloom,radius:.72,levels:a?6:4}),p=new o.ToneMappingEffect({mode:o.ToneMappingMode.NEUTRAL}),u=new o.HueSaturationEffect({saturation:r.saturation-1}),b=new o.BrightnessContrastEffect({contrast:r.contrast-1}),g=new o.VignetteEffect({offset:.32,darkness:r.vignette});return s.addPass(new o.EffectPass(t,l,p,u,b,g)),{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,g.darkness=d.vignette;},dispose(){s.dispose();}}}catch{return null}}export{T as Billboards,k as BlobShadows,A as Halos,P as Particles,S as SPRITES,C as ToyLook,D as createLightRig,$ as createPostChain,_ as createSky,j as setupRenderer,F as setupShadowMap,B as setupSunShadow,I as softShadowsSupported,z as spriteIndex};
package/package.json CHANGED
@@ -1,6 +1,80 @@
1
1
  {
2
2
  "name": "@idosgames/gfx",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
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
+ }