-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine-v3.html
More file actions
889 lines (845 loc) · 31.9 KB
/
engine-v3.html
File metadata and controls
889 lines (845 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YoYoPixel Engine v3 · Sprite Templates + Smart Shading</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{min-height:100vh;display:flex;flex-direction:column;align-items:center;gap:20px;background:#0e0e1a;font-family:'Courier New',monospace;color:#888;padding:30px 20px}
canvas{image-rendering:pixelated;image-rendering:crisp-edges}
h1{font-size:14px;letter-spacing:3px;color:#aaa}
.row{display:flex;gap:16px;flex-wrap:wrap;justify-content:center;align-items:flex-end}
.card{text-align:center;background:#12122a;border:1px solid #222244;border-radius:8px;padding:12px 12px 8px;transition:border-color .3s}
.card:hover{border-color:#e94560}
.card h2{font-size:10px;color:#555;letter-spacing:2px;margin-top:6px}
.sep{width:100%;height:1px;background:#1a1a3e;margin:4px 0}
.note{font-size:11px;color:#444;max-width:700px;text-align:center;line-height:1.8}
.tag{display:inline-block;background:#1a1a3e;color:#5a5a8a;padding:1px 6px;border-radius:3px;font-size:9px;margin:1px}
.compare{display:flex;gap:8px;align-items:center}
.compare .label{font-size:9px;color:#555;writing-mode:vertical-rl}
</style>
</head>
<body>
<h1>ENGINE V3 · SPRITE TEMPLATES + HUE-SHIFT SHADING</h1>
<div class="row">
<div class="card"><canvas id="items_v3"></canvas><h2>SMALL ITEMS (V3)</h2></div>
<div class="card"><canvas id="items_v2"></canvas><h2>SAME ITEMS (V2 OLD)</h2></div>
</div>
<div class="row">
<div class="card"><canvas id="furniture_v3"></canvas><h2>FURNITURE (V3)</h2></div>
<div class="card"><canvas id="barrel_v3"></canvas><h2>BARREL + SACK (V3)</h2></div>
<div class="card"><canvas id="weapons_v3"></canvas><h2>WEAPONS (V3)</h2></div>
</div>
<div class="row">
<div class="card"><canvas id="shelf_v3"></canvas><h2>POTION SHELF (V3)</h2></div>
<div class="card"><canvas id="bed_v3"></canvas><h2>BED (V3)</h2></div>
<div class="card"><canvas id="table_v3"></canvas><h2>TABLE SET (V3)</h2></div>
</div>
<div class="note">
<span class="tag">Sprite Templates</span>
<span class="tag">Hue-Shift Shading</span>
<span class="tag">Per-Material Shader</span>
<span class="tag">Adaptive Outline</span>
<span class="tag">Warm Palette</span>
</div>
<script>
(() => {
'use strict';
// ================================================================
// ENGINE V3 — Sprite Templates + Hue-Shift Shading
// ================================================================
class PB {
constructor(w,h){this.w=w;this.h=h;this.data=new Uint8ClampedArray(w*h*4)}
set(x,y,r,g,b,a){x|=0;y|=0;if(x<0||x>=this.w||y<0||y>=this.h)return;const i=(y*this.w+x)<<2;this.data[i]=r;this.data[i+1]=g;this.data[i+2]=b;this.data[i+3]=a!==undefined?a:255}
setC(x,y,c,a){this.set(x,y,c[0],c[1],c[2],a)}
get(x,y){x|=0;y|=0;if(x<0||x>=this.w||y<0||y>=this.h)return null;const i=(y*this.w+x)<<2;if(this.data[i+3]===0)return null;return[this.data[i],this.data[i+1],this.data[i+2],this.data[i+3]]}
has(x,y){x|=0;y|=0;return x>=0&&x<this.w&&y>=0&&y<this.h&&this.data[((y*this.w+x)<<2)+3]>0}
blend(x,y,c,sA){x|=0;y|=0;if(x<0||x>=this.w||y<0||y>=this.h)return;const i=(y*this.w+x)<<2;const dA=this.data[i+3]/255,s=(sA!==undefined?sA:255)/255,oA=s+dA*(1-s);if(oA===0)return;this.data[i]=((c[0]*s+this.data[i]*dA*(1-s))/oA)|0;this.data[i+1]=((c[1]*s+this.data[i+1]*dA*(1-s))/oA)|0;this.data[i+2]=((c[2]*s+this.data[i+2]*dA*(1-s))/oA)|0;this.data[i+3]=(oA*255)|0}
}
function blit(s,d,dx,dy){for(let y=0;y<s.h;y++)for(let x=0;x<s.w;x++){const c=s.get(x,y);if(!c)continue;d.set(dx+x,dy+y,c[0],c[1],c[2],c[3])}}
function blitA(s,d,dx,dy,o){o=o||{};if(o.shadow)for(let y=0;y<s.h;y++)for(let x=0;x<s.w;x++){if(!s.has(x,y))continue;d.blend(dx+x+(o.so||[1,2])[0],dy+y+(o.so||[1,2])[1],[0,0,0],((o.sa||.18)*255)|0)};for(let y=0;y<s.h;y++)for(let x=0;x<s.w;x++){const c=s.get(x,y);if(!c)continue;d.set(dx+x,dy+y,c[0],c[1],c[2],c[3])}}
function H(x,y){let n=((x|0)*374761393+(y|0)*668265263+1013904223)|0;n=(((n>>13)^n)*1274126177)|0;return(((n>>16)^n)>>>0)/4294967296}
function hx(s){return[parseInt(s.slice(1,3),16),parseInt(s.slice(3,5),16),parseInt(s.slice(5,7),16)]}
function nz(c,x,y,r){const n=(H(x*7,y*13)-.5)*r;return c.map(v=>Math.max(0,Math.min(255,(v+n)|0)))}
function pk(a,x,y){return[...a[Math.floor(H(x,y)*a.length)%a.length]]}
function inE(x,y,cx,cy,rx,ry){const dx=(x-cx)/rx,dy=(y-cy)/ry;return dx*dx+dy*dy<=1}
function inRR(x,y,x1,y1,x2,y2,r){if(x<x1||x>=x2||y<y1||y>=y2)return false;for(const[cx,cy]of[[x1+r,y1+r],[x2-r,y1+r],[x1+r,y2-r],[x2-r,y2-r]]){if((x<x1+r||x>=x2-r)&&(y<y1+r||y>=y2-r)){const dx2=x-cx,dy2=y-cy;if(dx2*dx2+dy2*dy2>r*r)return false}}return true}
// ================================================================
// V3 CORE: Hue-Shift Shading
// ================================================================
// Replace dk/lt with hue-aware shade function
// amount: -1 (full shadow) to +1 (full highlight)
// warmth: how much to shift hue (>0 = warm highlights, <0 = cool highlights)
function shade(c, amount, warmth) {
warmth = warmth !== undefined ? warmth : 0.5;
const r = c[0], g = c[1], b = c[2];
if (amount > 0) {
// Highlight: shift toward warm (add yellow/white)
return [
Math.min(255, (r + (255-r) * amount + warmth * amount * 15) | 0),
Math.min(255, (g + (255-g) * amount + warmth * amount * 8) | 0),
Math.min(255, (b + (255-b) * amount - warmth * amount * 10) | 0),
];
} else {
// Shadow: shift toward cool (add blue/purple)
const a = -amount;
return [
Math.max(0, (r * (1-a) - warmth * a * 8) | 0),
Math.max(0, (g * (1-a) - warmth * a * 15) | 0),
Math.max(0, (b * (1-a) + warmth * a * 12) | 0),
];
}
}
// Simple dk/lt still available for backwards compat
function dk(c,a){return shade(c,-a,0.5)}
function lt(c,a){return shade(c,a,0.5)}
// ================================================================
// V3 CORE: Adaptive Outline
// ================================================================
function adaptiveOutlineColor(c) {
// Saturated dark version: increase saturation, decrease brightness, slight cool shift
const avg = (c[0]+c[1]+c[2])/3;
return [
Math.max(0, (c[0]*0.35 + (c[0]-avg)*0.15 - 5) | 0),
Math.max(0, (c[1]*0.30 + (c[1]-avg)*0.15 - 8) | 0),
Math.max(0, (c[2]*0.40 + (c[2]-avg)*0.15) | 0),
];
}
// ================================================================
// V3 CORE: Per-Material Shading Strategies
// ================================================================
const MAT_SHADERS = {
wood: {
shade: (c, nx, ny, zoneW, zoneH) => {
// Directional: top-left bright, bottom-right dark
const l = (0.5-nx)*0.22 + (0.5-ny)*0.15;
return shade(c, l, 0.6); // warm wood
}
},
wood_dark: {
shade: (c, nx, ny) => {
const l = (0.5-nx)*0.25 + (0.5-ny)*0.18;
return shade(c, l, 0.5);
}
},
glass: {
shade: (c, nx, ny, zw, zh, alpha) => {
// Round cylinder: bright center-left, dark edges
const center = Math.abs(nx-0.35)*1.5;
const l = 0.25 - center*0.4 - ny*0.15;
return shade(c, l, 0.2); // cool glass
},
alpha: 200,
specular: {nx: 0.25, ny: 0.2, r: 0.15, intensity: 0.5}
},
liquid: {
shade: (c, nx, ny) => {
// Horizontal gradient, surface line highlight
const surfLine = ny < 0.15 ? 0.3 : 0;
const l = 0.1 - ny*0.25 + surfLine;
return shade(c, l, 0.3);
}
},
metal: {
shade: (c, nx, ny) => {
// Sharp specular band
const spec = Math.exp(-((nx-0.3)**2)*30) * 0.4;
const l = (0.5-ny)*0.2 + spec;
return shade(c, l, 0.15); // cool metal
}
},
gold: {
shade: (c, nx, ny) => {
const spec = Math.exp(-((nx-0.35)**2)*25) * 0.35;
const l = (0.5-ny)*0.18 + spec;
return shade(c, l, 0.7); // warm gold
}
},
cloth: {
shade: (c, nx, ny) => {
// Soft directional with fold hint
const fold = Math.sin(nx*8+ny*4)*0.08;
const l = (0.5-nx)*0.15 + (0.5-ny)*0.12 + fold;
return shade(c, l, 0.4);
}
},
leather: {
shade: (c, nx, ny) => {
const l = (0.5-nx)*0.2 + (0.5-ny)*0.15;
return shade(c, l, 0.55);
}
},
ceramic: {
shade: (c, nx, ny) => {
const roundness = 1 - Math.sqrt((nx-0.4)**2 + (ny-0.4)**2)*1.2;
return shade(c, roundness*0.25, 0.3);
}
},
gem: {
shade: (c, nx, ny) => {
const facet = Math.sin(nx*6)*Math.cos(ny*6)*0.3;
const spec = (nx<0.3&&ny<0.3) ? 0.4 : 0;
return shade(c, facet+spec, 0.1);
}
},
paper: {
shade: (c, nx, ny) => shade(c, (0.5-ny)*0.08, 0.2)
},
flat: {
shade: (c) => c // no shading
},
cork: {
shade: (c, nx, ny) => shade(c, (0.4-ny)*0.15, 0.5)
},
plant: {
shade: (c, nx, ny) => {
const l = (0.5-nx)*0.18 + (0.5-ny)*0.2;
return shade(c, l, 0.4);
}
},
fire: {
shade: (c, nx, ny) => shade(c, (0.5-ny)*0.5, 0.8) // very warm, brighter on top
}
};
// ================================================================
// V3 CORE: Sprite Template Renderer
// ================================================================
function renderSprite(template, colors, scale) {
scale = scale || 1;
const w = template.grid[0].length * scale;
const h = template.grid.length * scale;
const pb = new PB(w, h);
// Pass 1: Fill base colors + per-material shading
const zones = {}; // track bounding box per material zone
// Pre-scan for zone bounds
for (let gy = 0; gy < template.grid.length; gy++) {
const row = template.grid[gy];
for (let gx = 0; gx < row.length; gx++) {
const ch = row[gx];
if (ch === '.' || !template.materials[ch]) continue;
if (!zones[ch]) zones[ch] = {x1:gx,y1:gy,x2:gx,y2:gy};
zones[ch].x1 = Math.min(zones[ch].x1, gx);
zones[ch].y1 = Math.min(zones[ch].y1, gy);
zones[ch].x2 = Math.max(zones[ch].x2, gx);
zones[ch].y2 = Math.max(zones[ch].y2, gy);
}
}
for (let gy = 0; gy < template.grid.length; gy++) {
const row = template.grid[gy];
for (let gx = 0; gx < row.length; gx++) {
const ch = row[gx];
if (ch === '.') continue;
const mat = template.materials[ch];
if (!mat) continue;
const baseColor = colors[ch] || [128,128,128];
const shader = MAT_SHADERS[mat.type || mat] || MAT_SHADERS.flat;
// Normalized position within zone
const z = zones[ch];
const zw = z.x2 - z.x1 + 1, zh = z.y2 - z.y1 + 1;
const nx = zw > 1 ? (gx - z.x1) / (zw - 1) : 0.5;
const ny = zh > 1 ? (gy - z.y1) / (zh - 1) : 0.5;
// Apply per-material shading
let c = shader.shade([...baseColor], nx, ny, zw, zh);
// Per-pixel noise (very subtle)
c = nz(c, gx*7, gy*13, 5);
const alpha = shader.alpha || (mat.alpha) || 255;
// Fill scaled pixels
for (let sy = 0; sy < scale; sy++)
for (let sx = 0; sx < scale; sx++)
pb.setC(gx*scale+sx, gy*scale+sy, c, alpha);
}
}
// Pass 2: Specular highlights
for (const ch in template.materials) {
const mat = template.materials[ch];
const shader = MAT_SHADERS[mat.type || mat] || MAT_SHADERS.flat;
if (!shader.specular) continue;
const z = zones[ch]; if (!z) continue;
const sp = shader.specular;
const spx = (z.x1 + (z.x2-z.x1) * sp.nx) * scale;
const spy = (z.y1 + (z.y2-z.y1) * sp.ny) * scale;
const spr = Math.max(1, sp.r * Math.max(z.x2-z.x1, z.y2-z.y1) * scale);
for (let y = Math.floor(spy-spr); y <= Math.ceil(spy+spr); y++)
for (let x = Math.floor(spx-spr); x <= Math.ceil(spx+spr); x++) {
const c = pb.get(x,y); if (!c) continue;
const d = Math.sqrt((x-spx)**2+(y-spy)**2);
if (d > spr) continue;
const intensity = (1-d/spr)**2 * sp.intensity;
pb.setC(x, y, shade(c, intensity, 0));
}
}
// Pass 3: Adaptive outline
const snap = new Uint8ClampedArray(pb.data);
for (let y = 0; y < pb.h; y++) for (let x = 0; x < pb.w; x++) {
if (!pb.has(x,y)) continue;
let isEdge = false;
for (const [ex,ey] of [[-1,0],[1,0],[0,-1],[0,1]])
if (!pb.has(x+ex,y+ey)) { isEdge = true; break; }
if (isEdge) {
const i = (y*pb.w+x)<<2;
const c = [snap[i],snap[i+1],snap[i+2]];
const oc = adaptiveOutlineColor(c);
pb.data[i]=oc[0]; pb.data[i+1]=oc[1]; pb.data[i+2]=oc[2];
}
}
return pb;
}
// ================================================================
// RENDER HELPER
// ================================================================
function render(pb,id,scale,bg){
const cv=document.getElementById(id);cv.width=pb.w*scale;cv.height=pb.h*scale;
const ctx=cv.getContext('2d');if(bg){ctx.fillStyle=bg;ctx.fillRect(0,0,cv.width,cv.height)}
for(let y=0;y<pb.h;y++)for(let x=0;x<pb.w;x++){const i=(y*pb.w+x)<<2;if(pb.data[i+3]===0)continue;ctx.fillStyle=`rgb(${pb.data[i]},${pb.data[i+1]},${pb.data[i+2]})`;ctx.fillRect(x*scale,y*scale,scale,scale)}}
// V2 old-style for comparison
function oldDk(c,a){return c.map(v=>Math.max(0,(v*(1-a))|0))}
function oldLt(c,a){return c.map(v=>Math.min(255,(v+(255-v)*a)|0))}
function oldPP(pb){
let sn=new Uint8ClampedArray(pb.data);
for(let y=0;y<pb.h;y++)for(let x=0;x<pb.w;x++){if(!pb.has(x,y))continue;const i=(y*pb.w+x)<<2;const c=[sn[i],sn[i+1],sn[i+2]];const l=(.5-x/pb.w)*.1+(.5-y/pb.h)*.06;const a=l>0?oldLt(c,l):oldDk(c,-l);pb.data[i]=a[0];pb.data[i+1]=a[1];pb.data[i+2]=a[2]}
sn=new Uint8ClampedArray(pb.data);for(let y=0;y<pb.h;y++)for(let x=0;x<pb.w;x++){if(!pb.has(x,y))continue;let e=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!pb.has(x+ex,y+ey)){e=true;break};if(e){const i=(y*pb.w+x)<<2;pb.data[i]=(sn[i]*.72)|0;pb.data[i+1]=(sn[i+1]*.72)|0;pb.data[i+2]=(sn[i+2]*.72)|0}}
return pb}
// ================================================================
// WARM PALETTES (v3 - more saturated)
// ================================================================
const C = {
// Wood tones (warm amber, not muddy brown)
wood_dk: hx('#4a2810'),
wood_md: hx('#7a4820'),
wood_lt: hx('#a86830'),
wood_hi: hx('#c88848'),
// Padded/leather
pad_dk: hx('#986830'),
pad_md: hx('#c89050'),
pad_lt: hx('#e0b068'),
pad_hi: hx('#f0c878'),
// Dark frame
frame_dk: hx('#2a1808'),
frame_md: hx('#3a2810'),
frame_lt: hx('#4a3818'),
// Iron
iron_dk: hx('#282830'),
iron_md: hx('#404050'),
iron_lt: hx('#606070'),
iron_hi: hx('#8888a0'),
// Steel (blue-ish)
steel_dk: hx('#303848'),
steel_md: hx('#485868'),
steel_lt: hx('#688898'),
steel_hi: hx('#90b8c8'),
// Gold
gold_dk: hx('#705010'),
gold_md: hx('#b08020'),
gold_lt: hx('#d8a838'),
gold_hi: hx('#f0d060'),
// Glass
glass_dk: hx('#405060'),
glass_md: hx('#6888a0'),
glass_lt: hx('#90b0c8'),
glass_hi: hx('#c0d8e8'),
// Potion blue
pot_blue: hx('#2848a0'),
pot_green:hx('#208848'),
pot_red: hx('#a02020'),
pot_purple:hx('#6030a0'),
// Cloth white
cloth_w: hx('#e8e0d0'),
cloth_r: hx('#a03828'),
// Cork
cork: hx('#907050'),
// Ceramic
ceramic: hx('#c8b898'),
// Paper
paper: hx('#d8c8a0'),
// Plant
plant_dk: hx('#1a5a10'),
plant_md: hx('#308818'),
plant_lt: hx('#48a828'),
// Food
food_y: hx('#d8a830'),
food_r: hx('#c04030'),
food_g: hx('#48a838'),
food_b: hx('#3858b8'),
// Fire
fire: hx('#f0c040'),
// Gem
gem_red: hx('#c83030'),
gem_blue: hx('#3060c0'),
};
// ================================================================
// SPRITE TEMPLATES (hand-crafted shapes + material zones)
// ================================================================
const SPRITES = {
potion: {
grid: [
'..cc..',
'..kk..',
'.GGGG.',
'GLLLLG',
'GLLLLG',
'GLLLLG',
'GLLLLG',
'.GGGG.',
],
materials: { c:'cork', k:'cork', G:{type:'glass',alpha:200}, L:'liquid' }
},
book: {
grid: [
'.SSSS.',
'SPPPP.',
'SPPPP.',
'SPPPP.',
'SPPPP.',
'SPPPP.',
'.SSSS.',
],
materials: { S:'leather', P:'paper' }
},
trophy: {
grid: [
'.GGGG.',
'GGGGGG',
'GGGGGG',
'.GGGG.',
'..GG..',
'..GG..',
'.MMMM.',
'.MMMM.',
],
materials: { G:'gold', M:'wood' }
},
sack: {
grid: [
'..rr..',
'..CC..',
'.CCCC.',
'CCCCCC',
'CCCCCC',
'CCCCCC',
'.CCCC.',
],
materials: { r:'cloth', C:'cloth' }
},
bowl: {
grid: [
'.FFFF.',
'.FFFF.',
'CCCCCC',
'CCCCCC',
'.CCCC.',
],
materials: { F:'flat', C:'ceramic' }
},
scroll: {
grid: [
'R.PPPP.R',
'RPPPPPPR',
'RPPPPPPR',
'RPPPPPPR',
'R.PPPP.R',
],
materials: { R:'cork', P:'paper' }
},
candle: {
grid: [
'..f.',
'.ff.',
'..W.',
'.WW.',
'.WW.',
'.WW.',
'.WW.',
'.MM.',
],
materials: { f:'fire', W:'cloth', M:'metal' }
},
dagger: {
grid: [
'.M.',
'.M.',
'.M.',
'.M.',
'.M.',
'.M.',
'GMG',
'.W.',
'.W.',
'.W.',
'.G.',
],
materials: { M:'metal', G:'gold', W:'wood' }
},
sword: {
grid: [
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'..M..',
'.MMM.',
'GGGGG',
'..W..',
'..W..',
'..W..',
'..W..',
'.GgG.',
],
materials: { M:'metal', G:'gold', g:'gem', W:{type:'wood_dark'} }
},
staff: {
grid: [
'.ggg.',
'.ggg.',
'GgggG',
'.GGG.',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'..W..',
'.GWG.',
'..W..',
],
materials: { g:'gem', G:'gold', W:'wood' }
},
bow: {
grid: [
'...W..',
'..W...',
'.W....',
'W.s...',
'W..s..',
'W..s..',
'W..s..',
'W..s..',
'W..s..',
'W.s...',
'.W....',
'..W...',
'...W..',
],
materials: { W:'wood', s:'cloth' }
},
vase: {
grid: [
'..CC..',
'.CCCC.',
'.CCCC.',
'..CC..',
'..CC..',
'.CCCC.',
'CCCCCC',
'CCCCCC',
'.CCCC.',
],
materials: { C:'ceramic' }
},
chest: {
grid: [
'.MMMMMMMMM.',
'MWWWWWWWWWM',
'MWWWWWWWWWM',
'MMMMMgMMMMM',
'MWWWWWWWWWM',
'MWWWWWWWWWM',
'MWWWWWWWWWM',
'MMMMMMMMMMM',
],
materials: { M:'metal', W:'wood', g:'gold' }
},
};
// ================================================================
// RENDER SPRITES AT SCALE
// ================================================================
function spriteWithColors(name, colorMap, scale) {
return renderSprite(SPRITES[name], colorMap, scale || 1);
}
// ================================================================
// DEMO 1: Small items comparison (v3 vs v2)
// ================================================================
const itemsV3 = new PB(120, 50);
for(let y=0;y<50;y++)for(let x=0;x<120;x++)itemsV3.set(x,y,192,192,184,255);
// Potion (blue)
blit(spriteWithColors('potion',{c:C.cork,k:C.cork,G:C.glass_md,L:C.pot_blue},2), itemsV3, 2, 10);
// Potion (green)
blit(spriteWithColors('potion',{c:C.cork,k:C.cork,G:C.glass_md,L:C.pot_green},2), itemsV3, 18, 10);
// Potion (red)
blit(spriteWithColors('potion',{c:C.cork,k:C.cork,G:C.glass_md,L:C.pot_red},2), itemsV3, 34, 10);
// Book (blue)
blit(spriteWithColors('book',{S:C.food_b,P:C.paper},2), itemsV3, 52, 12);
// Book (red)
blit(spriteWithColors('book',{S:C.cloth_r,P:C.paper},2), itemsV3, 66, 12);
// Trophy
blit(spriteWithColors('trophy',{G:C.gold_md,M:C.wood_dk},2), itemsV3, 82, 10);
// Sack
blit(spriteWithColors('sack',{r:C.cork,C:C.pad_dk},2), itemsV3, 98, 12);
// Second row
// Bowl with food
blit(spriteWithColors('bowl',{F:C.food_y,C:C.ceramic},2), itemsV3, 2, 34);
blit(spriteWithColors('bowl',{F:C.food_g,C:C.ceramic},2), itemsV3, 18, 34);
blit(spriteWithColors('bowl',{F:C.food_r,C:C.ceramic},2), itemsV3, 34, 34);
// Scroll
blit(spriteWithColors('scroll',{R:C.cork,P:C.paper},2), itemsV3, 52, 36);
// Candle
blit(spriteWithColors('candle',{f:C.fire,W:C.cloth_w,M:C.iron_md},2), itemsV3, 72, 30);
// Vase
blit(spriteWithColors('vase',{C:C.ceramic},2), itemsV3, 84, 30);
// Chest
blit(spriteWithColors('chest',{M:C.iron_md,W:C.wood_md,g:C.gold_md},2), itemsV3, 100, 32);
render(itemsV3, 'items_v3', 3, '#c0c0b8');
// V2 old style comparison
const itemsV2 = new PB(120, 50);
for(let y=0;y<50;y++)for(let x=0;x<120;x++)itemsV2.set(x,y,192,192,184,255);
// Recreate same items with old approach (flat color + old dk/lt + weak outline)
function oldSprite(template, colorMap, sc) {
sc=sc||1;const w=template.grid[0].length*sc,h=template.grid.length*sc;const pb=new PB(w,h);
for(let gy=0;gy<template.grid.length;gy++){const row=template.grid[gy];for(let gx=0;gx<row.length;gx++){const ch=row[gx];if(ch==='.'||!template.materials[ch])continue;const c=colorMap[ch]||[128,128,128];for(let sy=0;sy<sc;sy++)for(let sx=0;sx<sc;sx++)pb.setC(gx*sc+sx,gy*sc+sy,[...c])}}
return oldPP(pb)}
blit(oldSprite(SPRITES.potion,{c:C.cork,k:C.cork,G:hx('#6888a0'),L:hx('#2848a0')},2),itemsV2,2,10);
blit(oldSprite(SPRITES.potion,{c:C.cork,k:C.cork,G:hx('#6888a0'),L:hx('#208848')},2),itemsV2,18,10);
blit(oldSprite(SPRITES.potion,{c:C.cork,k:C.cork,G:hx('#6888a0'),L:hx('#a02020')},2),itemsV2,34,10);
blit(oldSprite(SPRITES.book,{S:hx('#3858b8'),P:hx('#d8c8a0')},2),itemsV2,52,12);
blit(oldSprite(SPRITES.book,{S:hx('#a03828'),P:hx('#d8c8a0')},2),itemsV2,66,12);
blit(oldSprite(SPRITES.trophy,{G:hx('#b08020'),M:hx('#4a2810')},2),itemsV2,82,10);
blit(oldSprite(SPRITES.sack,{r:hx('#907050'),C:hx('#8a7050')},2),itemsV2,98,12);
blit(oldSprite(SPRITES.bowl,{F:hx('#d8a830'),C:hx('#c8b898')},2),itemsV2,2,34);
blit(oldSprite(SPRITES.bowl,{F:hx('#48a838'),C:hx('#c8b898')},2),itemsV2,18,34);
blit(oldSprite(SPRITES.bowl,{F:hx('#c04030'),C:hx('#c8b898')},2),itemsV2,34,34);
blit(oldSprite(SPRITES.scroll,{R:hx('#907050'),P:hx('#d8c8a0')},2),itemsV2,52,36);
blit(oldSprite(SPRITES.candle,{f:hx('#f0c040'),W:hx('#e8e0d0'),M:hx('#404050')},2),itemsV2,72,30);
blit(oldSprite(SPRITES.vase,{C:hx('#c8b898')},2),itemsV2,84,30);
blit(oldSprite(SPRITES.chest,{M:hx('#404050'),W:hx('#7a4820'),g:hx('#b08020')},2),itemsV2,100,32);
render(itemsV2, 'items_v2', 3, '#c0c0b8');
// ================================================================
// DEMO 2: Furniture (v3 planks with strong contrast)
// ================================================================
function mkTableV3() {
const p = new PB(54, 44);
// Table top — v3 planks with strong gap contrast
for (let y = 8; y < 34; y++) for (let x = 8; x < 46; x++) {
const plankIdx = Math.floor((y-8) / 4);
const inGap = (y-8) % 4 === 0;
const plankSeed = H(plankIdx * 77, 1);
// Per-plank base color variation
const base = shade([...C.wood_md], (plankSeed-0.5)*0.15, 0.5);
if (inGap) {
p.setC(x, y, shade(C.frame_dk, -0.1, 0.3)); // deep dark gap
} else {
// Within plank: grain + directional shading
const nx = (x-8)/38, ny = ((y-8)%4)/3;
const grain = Math.sin(nx*14 + plankSeed*6)*0.08;
let c = shade(base, (0.5-nx)*0.18 + grain, 0.6);
c = nz(c, x, y, 4);
p.setC(x, y, c);
}
}
// Table frame edge
for (let x = 7; x < 47; x++) { p.setC(x, 7, shade(C.frame_md, 0.1, 0.3)); p.setC(x, 34, shade(C.frame_dk, -0.05, 0.3)); }
for (let y = 7; y < 35; y++) { p.setC(7, y, shade(C.frame_md, 0.05, 0.3)); p.setC(46, y, shade(C.frame_dk, -0.1, 0.3)); }
// Chairs (6)
for (const [cx,cy] of [[14,1],[27,1],[40,1],[14,36],[27,36],[40,36]]) {
for (let y=cy;y<cy+7;y++) for (let x=cx;x<cx+7;x++) {
if (!inRR(x,y,cx,cy,cx+7,cy+7,1)) continue;
const nx=(x-cx)/6, ny=(y-cy)/6;
p.setC(x, y, nz(shade(C.wood_dk, (0.5-nx)*0.15+(0.5-ny)*0.1, 0.5), x, y, 4));
}
}
// Adaptive outline
const snap = new Uint8ClampedArray(p.data);
for (let y=0;y<p.h;y++) for (let x=0;x<p.w;x++) {
if (!p.has(x,y)) continue;
let isEdge=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!p.has(x+ex,y+ey)){isEdge=true;break}
if(isEdge){const i=(y*p.w+x)<<2;const c=[snap[i],snap[i+1],snap[i+2]];const oc=adaptiveOutlineColor(c);p.data[i]=oc[0];p.data[i+1]=oc[1];p.data[i+2]=oc[2]}
}
return p;
}
render(mkTableV3(), 'furniture_v3', 5, '#c0c0b8');
// ================================================================
// DEMO 3: Barrel + Sack
// ================================================================
function mkBarrelV3() {
const p = new PB(48, 40);
// Barrel
for (let y = 4; y < 36; y++) for (let x = 22; x < 44; x++) {
const ny = (y-4)/32, bulge = 1+Math.sin(ny*Math.PI)*0.12;
if (!inE(x,y,33,20,11*bulge,16)) continue;
const nx = (x-22)/22;
const plank = Math.floor(nx*6); const inGap = Math.abs(nx*6-plank-0.5)>0.42;
if (inGap) { p.setC(x,y,shade(C.frame_dk,-0.05,0.3)); continue; }
const base = shade([...C.wood_md],(H(plank*77,1)-0.5)*0.12,0.5);
let c = shade(base,(0.5-nx)*0.2-ny*0.08,0.6);
p.setC(x,y,nz(c,x,y,4));
}
// Metal bands
for (const by of [7,18,30]) for (let x=23;x<43;x++) {
if (!p.has(x,by)) continue;
const nx=(x-23)/20;
let c = shade(C.iron_md, (0.5-nx)*0.25+Math.exp(-((nx-0.3)**2)*20)*0.3, 0.15);
p.setC(x,by,c); p.setC(x,by+1,shade(c,-0.1,0.2));
}
// Top rim
for (let x=25;x<41;x++) if(inE(x,3,33,4,8,2)){p.setC(x,3,shade(C.wood_lt,0.1,0.5));p.setC(x,2,shade(C.frame_md,0.05,0.3))}
// Sack (left, with template)
const sackPb = renderSprite({
grid:['...rr...','...CC...','..CCCC..','..CCCC..','.CCCCCC.','.CCCCCC.','CCCCCCCC','CCCCCCCC','CCCCCCCC','.CCCCCC.'],
materials:{r:'cloth',C:'cloth'}
},{r:C.cork,C:C.pad_dk},1);
blitA(sackPb,p,2,20,{shadow:true,so:[1,2],sa:.15});
// Outline whole scene
const snap=new Uint8ClampedArray(p.data);
for(let y=0;y<p.h;y++)for(let x=0;x<p.w;x++){if(!p.has(x,y))continue;let e=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!p.has(x+ex,y+ey)){e=true;break};if(e){const i=(y*p.w+x)<<2;const c=[snap[i],snap[i+1],snap[i+2]];const oc=adaptiveOutlineColor(c);p.data[i]=oc[0];p.data[i+1]=oc[1];p.data[i+2]=oc[2]}}
return p;
}
render(mkBarrelV3(), 'barrel_v3', 5, '#c0c0b8');
// ================================================================
// DEMO 4: Weapons
// ================================================================
const wpn = new PB(60, 42);
for(let y=0;y<42;y++)for(let x=0;x<60;x++)wpn.set(x,y,192,192,184,255);
blit(spriteWithColors('sword',{M:C.steel_md,G:C.gold_md,g:C.gem_red,W:C.wood_dk},2),wpn,2,2);
blit(spriteWithColors('staff',{g:C.gem_red,G:C.gold_md,W:C.wood_md},2),wpn,16,5);
blit(spriteWithColors('dagger',{M:C.steel_md,G:C.gold_md,W:C.wood_dk},2),wpn,30,14);
blit(spriteWithColors('bow',{W:C.wood_md,s:hx('#a09078')},2),wpn,40,8);
render(wpn,'weapons_v3',4,'#c0c0b8');
// ================================================================
// DEMO 5: Potion Shelf
// ================================================================
function mkShelfV3() {
const p = new PB(42, 44);
// Back panel (very dark)
for (let y=0;y<44;y++) for (let x=0;x<42;x++) {
if (!inRR(x,y,0,0,42,44,1)) continue;
p.setC(x,y,shade(C.frame_dk,-0.2,0.3));
}
// Shelves
for (const sy of [14,28]) for (let x=2;x<40;x++) {
p.setC(x,sy,shade(C.wood_md,0.08,0.5));
p.setC(x,sy+1,shade(C.wood_dk,-0.05,0.4));
}
// Frame edges
for (let y=0;y<44;y++){p.setC(0,y,shade(C.frame_md,(0.5-y/44)*0.15,0.4));p.setC(41,y,shade(C.frame_dk,-0.05,0.3));p.setC(1,y,shade(C.frame_md,(0.5-y/44)*0.1,0.4))}
for (let x=0;x<42;x++){p.setC(x,0,shade(C.frame_md,0.08,0.4));p.setC(x,43,shade(C.frame_dk,-0.08,0.3))}
// Potions on shelves
const potionTypes=[
{G:C.glass_md,L:C.pot_blue,c:C.cork,k:C.cork},
{G:C.glass_md,L:C.pot_green,c:C.cork,k:C.cork},
{G:C.glass_md,L:C.pot_purple,c:C.cork,k:C.cork},
{G:C.glass_md,L:C.pot_red,c:C.cork,k:C.cork},
{G:C.glass_md,L:C.pot_blue,c:C.cork,k:C.cork},
];
for (let shelf=0;shelf<2;shelf++) {
const baseY = shelf===0 ? 2 : 16;
for (let i=0;i<5;i++) {
const potion = spriteWithColors('potion', potionTypes[(i+shelf*2)%potionTypes.length], 1);
blit(potion, p, 4+i*7, baseY);
}
}
// Books on bottom shelf
const bookColors = [C.cloth_r, C.food_b, C.pot_green, C.gold_dk];
for (let i=0;i<4;i++) {
const book = spriteWithColors('book',{S:bookColors[i],P:C.paper},1);
blit(book,p,5+i*8,30);
}
// Outline
const snap=new Uint8ClampedArray(p.data);
for(let y=0;y<p.h;y++)for(let x=0;x<p.w;x++){if(!p.has(x,y))continue;let e=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!p.has(x+ex,y+ey)){e=true;break};if(e){const i=(y*p.w+x)<<2;const c=[snap[i],snap[i+1],snap[i+2]];const oc=adaptiveOutlineColor(c);p.data[i]=oc[0];p.data[i+1]=oc[1];p.data[i+2]=oc[2]}}
return p;
}
render(mkShelfV3(),'shelf_v3',5,'#c0c0b8');
// ================================================================
// DEMO 6: Bed
// ================================================================
function mkBedV3() {
const p = new PB(52, 30);
// Headboard
for (let y=0;y<8;y++) for (let x=0;x<52;x++) {
if (!inRR(x,y,0,0,52,8,2)) continue;
const nx=x/52;
p.setC(x,y,nz(shade(C.wood_dk,(0.5-nx)*0.2+(0.5-y/8)*0.1,0.5),x,y,4));
}
// Frame
for (let y=6;y<30;y++) for (let x=0;x<52;x++) {
const isFrame = x<3||x>=49||y>=27;
if (!isFrame && y<8) continue;
if (isFrame) p.setC(x,y,nz(shade(C.wood_dk,(0.5-x/52)*0.15,0.5),x,y,4));
}
// Blanket
for (let y=8;y<27;y++) for (let x=3;x<49;x++) {
const nx=(x-3)/46,ny=(y-8)/19;
const fold = Math.sin(nx*6+ny*3)*0.1;
p.setC(x,y,nz(shade(C.cloth_r,(0.5-nx)*0.15+fold,0.5),x,y,4));
}
// Pillows
for (let i=0;i<3;i++) {const px=6+i*15;
for(let y=6;y<14;y++)for(let x=px;x<px+12;x++){
if(!inRR(x,y,px,6,px+12,14,2))continue;
const nx2=(x-px)/12,ny2=(y-6)/8;
const puff=1-Math.sqrt((nx2-0.5)**2+(ny2-0.5)**2)*1.5;
p.setC(x,y,nz(shade(C.cloth_w,puff*0.2,0.3),x,y,3));
}
}
// Outline
const snap=new Uint8ClampedArray(p.data);
for(let y=0;y<p.h;y++)for(let x=0;x<p.w;x++){if(!p.has(x,y))continue;let e=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!p.has(x+ex,y+ey)){e=true;break};if(e){const i=(y*p.w+x)<<2;const c=[snap[i],snap[i+1],snap[i+2]];const oc=adaptiveOutlineColor(c);p.data[i]=oc[0];p.data[i+1]=oc[1];p.data[i+2]=oc[2]}}
return p;
}
render(mkBedV3(),'bed_v3',5,'#c0c0b8');
// ================================================================
// DEMO 7: Round Table Set
// ================================================================
function mkRoundTableV3(items) {
const p = new PB(38, 38);
// Chairs
for (const [cx,cy] of [[16,0],[16,30],[0,15],[30,15]]) {
for(let y=cy;y<cy+8;y++)for(let x=cx;x<cx+8;x++){
if(!inRR(x,y,cx,cy,cx+8,cy+8,2))continue;
const nx2=(x-cx)/7,ny2=(y-cy)/7;
p.setC(x,y,nz(shade(C.wood_dk,(0.5-nx2)*0.15+(0.5-ny2)*0.1,0.5),x,y,4));
}
}
// Table circle with cloth
for(let y=6;y<32;y++)for(let x=6;x<32;x++){
if(!inE(x,y,19,19,13,13))continue;
const nx2=(x-6)/26,ny2=(y-6)/26;
const fold=Math.sin(nx2*8+ny2*6)*0.06;
p.setC(x,y,nz(shade(C.cloth_w,(0.5-nx2)*0.12+(0.5-ny2)*0.08+fold,0.3),x,y,3));
}
// Items on table
if (items) {
for (const item of items) {
const sprite = spriteWithColors(item.type, item.colors, 1);
blit(sprite, p, item.x, item.y);
}
}
// Outline
const snap=new Uint8ClampedArray(p.data);
for(let y=0;y<p.h;y++)for(let x=0;x<p.w;x++){if(!p.has(x,y))continue;let e=false;for(const[ex,ey]of[[-1,0],[1,0],[0,-1],[0,1]])if(!p.has(x+ex,y+ey)){e=true;break};if(e){const i=(y*p.w+x)<<2;const c=[snap[i],snap[i+1],snap[i+2]];const oc=adaptiveOutlineColor(c);p.data[i]=oc[0];p.data[i+1]=oc[1];p.data[i+2]=oc[2]}}
return p;
}
const tableScene = new PB(82,42);
for(let y=0;y<42;y++)for(let x=0;x<82;x++)tableScene.set(x,y,192,192,184,255);
// Table with candle
blit(mkRoundTableV3([{type:'candle',colors:{f:C.fire,W:C.cloth_w,M:C.iron_md},x:16,y:14}]),tableScene,0,2);
// Table with food
blit(mkRoundTableV3([
{type:'bowl',colors:{F:C.food_y,C:C.ceramic},x:14,y:14},
{type:'bowl',colors:{F:C.food_r,C:C.ceramic},x:21,y:14},
]),tableScene,42,2);
render(tableScene,'table_v3',4,'#c0c0b8');
})();
</script>
</body>
</html>