feat: minecraft-style torch light - wall occlusion, warm flicker, offscreen lightmap
This commit is contained in:
parent
1f921f7620
commit
14f7408a86
109
game.js
109
game.js
|
|
@ -3467,41 +3467,104 @@
|
||||||
|
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
|
|
||||||
// lighting overlay at night
|
// lighting overlay at night (Minecraft-style: offscreen lightmap, wall occlusion, warm flicker)
|
||||||
if(night){
|
if(night){
|
||||||
// Рисуем полупрозрачный тёмный оверлей
|
// 1) Рисуем тёмный оверлей на offscreen canvas
|
||||||
ctx.save();
|
lightC.width = W*dpr;
|
||||||
ctx.fillStyle = 'rgba(0,0,0,0.50)';
|
lightC.height = H*dpr;
|
||||||
ctx.fillRect(0, 0, W, H);
|
lightCtx.setTransform(dpr,0,0,dpr,0,0);
|
||||||
ctx.restore();
|
lightCtx.fillStyle = 'rgba(0,0,12,0.82)';
|
||||||
|
lightCtx.fillRect(0,0,W,H);
|
||||||
|
|
||||||
// Освещение от факелов и костров
|
// 2) Вырезаем «окна света» через destination-out — свет не проходит сквозь стены
|
||||||
ctx.save();
|
lightCtx.globalCompositeOperation = 'destination-out';
|
||||||
ctx.globalCompositeOperation = 'lighter';
|
|
||||||
|
|
||||||
// Функция для рисования света
|
// Функция: рисуем мягкий луч света с затуханием за стенами
|
||||||
function drawLight(x, y, radius, intensity) {
|
function castLight(sx, sy, radius) {
|
||||||
const gradient = ctx.createRadialGradient(x, y, 0, x, y, radius);
|
const flick = 0.88 + Math.sin(now/80 + sx*0.01)*0.06 + Math.sin(now/130 + sy*0.02)*0.06;
|
||||||
gradient.addColorStop(0, `rgba(255, 255, 200, ${intensity})`);
|
const r = radius * flick;
|
||||||
gradient.addColorStop(1, `rgba(255, 255, 200, 0)`);
|
// 12 лучей — достаточно для мягкого круга
|
||||||
ctx.fillStyle = gradient;
|
const steps = 12;
|
||||||
ctx.beginPath();
|
// Собираем дистанции до стен по лучам
|
||||||
ctx.arc(x, y, radius, 0, Math.PI * 2);
|
const dists = new Float32Array(steps);
|
||||||
ctx.fill();
|
for(let i=0; i<steps; i++){
|
||||||
|
const angle = (i/steps) * Math.PI * 2;
|
||||||
|
const dx = Math.cos(angle);
|
||||||
|
const dy = Math.sin(angle);
|
||||||
|
let maxDist = r;
|
||||||
|
// Идём по лучу пока не упрёмся в стену
|
||||||
|
for(let step=TILE*0.5; step<r; step+=TILE*0.6){
|
||||||
|
const gx = Math.floor((sx + dx*step)/TILE);
|
||||||
|
const gy = Math.floor((sy + dy*step)/TILE);
|
||||||
|
const blk = getBlock(gx, gy);
|
||||||
|
if(blk && !blk.dead && BLOCKS[blk.t] && BLOCKS[blk.t].solid && step > TILE*0.3){
|
||||||
|
maxDist = step;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dists[i] = maxDist;
|
||||||
|
}
|
||||||
|
// Рисуем сглаженный полигон по dists
|
||||||
|
const cx = sx-camX, cy = sy-camY;
|
||||||
|
// Центр: яркая точка
|
||||||
|
const maxR = Math.max(...dists);
|
||||||
|
const grad = lightCtx.createRadialGradient(cx, cy, 0, cx, cy, maxR);
|
||||||
|
grad.addColorStop(0, 'rgba(255,255,255,1)');
|
||||||
|
grad.addColorStop(0.5, 'rgba(255,255,255,0.65)');
|
||||||
|
grad.addColorStop(1, 'rgba(255,255,255,0)');
|
||||||
|
lightCtx.fillStyle = grad;
|
||||||
|
// Рисуем shape по dists (звездоподобный полигон)
|
||||||
|
lightCtx.beginPath();
|
||||||
|
for(let i=0; i<=steps; i++){
|
||||||
|
const idx = i % steps;
|
||||||
|
const nextIdx = (i+1) % steps;
|
||||||
|
const avgD = (dists[idx] + dists[nextIdx]) / 2;
|
||||||
|
const angle = (idx/steps) * Math.PI * 2;
|
||||||
|
const px = cx + Math.cos(angle) * dists[idx];
|
||||||
|
const py = cy + Math.sin(angle) * dists[idx];
|
||||||
|
if(i===0) lightCtx.moveTo(px, py);
|
||||||
|
else lightCtx.lineTo(px, py);
|
||||||
|
}
|
||||||
|
lightCtx.closePath();
|
||||||
|
lightCtx.fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Освещение от факелов (1/3 яркости)
|
// Источники света
|
||||||
for(const b of blocks){
|
for(const b of blocks){
|
||||||
if(b.dead) continue;
|
if(b.dead) continue;
|
||||||
if(b.gx < minGX || b.gx > maxGX || b.gy < minGY || b.gy > maxGY) continue;
|
if(b.gx < minGX-5 || b.gx > maxGX+5 || b.gy < minGY-5 || b.gy > maxGY+5) continue;
|
||||||
const def = BLOCKS[b.t];
|
const def = BLOCKS[b.t];
|
||||||
if(def.lightRadius){
|
if(def.lightRadius){
|
||||||
const wx = b.gx*TILE + TILE/2 - camX;
|
castLight(b.gx*TILE + TILE/2, b.gy*TILE + TILE/2, def.lightRadius);
|
||||||
const wy = b.gy*TILE + TILE/2 - camY;
|
|
||||||
drawLight(wx, wy, def.lightRadius, 0.33);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3) Накладываем lightmap на основной canvas
|
||||||
|
lightCtx.globalCompositeOperation = 'source-over';
|
||||||
|
ctx.drawImage(lightC, 0, 0, W, H);
|
||||||
|
|
||||||
|
// 4) Тёплый оверлей от источников света (additive, мягкий)
|
||||||
|
ctx.save();
|
||||||
|
ctx.globalCompositeOperation = 'lighter';
|
||||||
|
for(const b of blocks){
|
||||||
|
if(b.dead) continue;
|
||||||
|
if(b.gx < minGX-3 || b.gx > maxGX+3 || b.gy < minGY-3 || b.gy > maxGY+3) continue;
|
||||||
|
const def = BLOCKS[b.t];
|
||||||
|
if(def.lightRadius){
|
||||||
|
const flick = 0.7 + Math.sin(now/90 + b.gx*3.7)*0.15 + Math.sin(now/140 + b.gy*2.3)*0.15;
|
||||||
|
const wx = b.gx*TILE + TILE/2 - camX;
|
||||||
|
const wy = b.gy*TILE + TILE/2 - camY;
|
||||||
|
const r = def.lightRadius * 0.6 * flick;
|
||||||
|
const grad = ctx.createRadialGradient(wx,wy, 0, wx,wy, r);
|
||||||
|
grad.addColorStop(0, `rgba(255,180,80,${0.12*flick})`);
|
||||||
|
grad.addColorStop(0.5, `rgba(255,140,40,${0.06*flick})`);
|
||||||
|
grad.addColorStop(1, 'rgba(255,100,20,0)');
|
||||||
|
ctx.fillStyle = grad;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(wx, wy, r, 0, Math.PI*2);
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
|
}
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="game.js?v=7"></script>
|
<script src="game.js?v=8"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue