forked from Blendman974/faceclaw
Depth effects
This commit is contained in:
1 parent
4320021222
commit
392554dd37
26 files changed
+623
-136
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { encodePresentation } from "./presentation-wire";
|
||||
/**
|
||||
* Marshals deferred-draw identity (text glyphs and icon images) to the Kotlin
|
||||
* side for the texture-cache pipeline (CFW modes 18/19/20; see
|
||||
@@ -84,6 +85,7 @@ const fwRegistered = new Set<string>();
|
||||
* unseen ink members' rasters with the Kotlin FwGlyphAtlas as a side effect.
|
||||
*/
|
||||
function prepareFwTextRun(placed: PlacedFwText): boolean {
|
||||
if (!textureAtlasAvailable()) return false;
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) return false;
|
||||
if (placed.glyphs.length === 0 || placed.glyphs.length > 255) return false;
|
||||
let registration: Array<{ cp: number; data: NonNullable<ReturnType<PlacedFwText["font"]["fwGlyphWireData"]>> }> | null = null;
|
||||
@@ -152,7 +154,7 @@ function inRange16(v: number): boolean {
|
||||
* in draw order. Returns null when nothing is expressible (or without a native atlas).
|
||||
*/
|
||||
export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null {
|
||||
if (!textureAtlasAvailable() || draws.length === 0) return null;
|
||||
if (draws.length === 0) return null;
|
||||
|
||||
// Pass 1: resolve ids, register unseen rasters, size the buffer.
|
||||
let registration: Map<FontWireState, RegistrationGroup> | null = null;
|
||||
@@ -175,7 +177,8 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
(placed.glyph.coverage ? group.aaGlyphs : group.glyphs).push(placed.glyph);
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
|
||||
if (placed.presentation) bytes += encodePresentation(placed).length;
|
||||
else if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
|
||||
} else {
|
||||
const ok = prepareFwTextRun(placed);
|
||||
fwRunOk.set(placed, ok);
|
||||
@@ -207,6 +210,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
offset += GLYPH_RECORD_BYTES;
|
||||
} else if (placed.kind === "image") {
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
if (placed.presentation) { const bytes = encodePresentation(placed); new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; }
|
||||
const id = imageId(placed);
|
||||
if (id === null) continue;
|
||||
out.setUint8(offset, 1);
|
||||
|
||||
+47
-6
@@ -73,6 +73,7 @@ export type PlacedGlyph = {
|
||||
* from the moment it is placed.
|
||||
*/
|
||||
export type PlacedImage = {
|
||||
presentation?: { mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
|
||||
kind: "image";
|
||||
source: GrayImage;
|
||||
x: number;
|
||||
@@ -391,6 +392,7 @@ export class GrayImage {
|
||||
hash = mixInt(hash, placed.source.width);
|
||||
hash = mixInt(hash, placed.source.height);
|
||||
hash = mixInt(hash, placed.source.sourceContentHash32());
|
||||
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value); }
|
||||
} else {
|
||||
hash = mixInt(hash, 0xf17e);
|
||||
hash = mixInt(hash, placed.font.atlasTag);
|
||||
@@ -501,6 +503,24 @@ export class GrayImage {
|
||||
this.drawList.push({ kind: "fwtext", font, x, y, value: clampByte(value), glyphs });
|
||||
}
|
||||
|
||||
/** Retain the selected row separately from the menu surface for glasses-side composition. */
|
||||
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2): void {
|
||||
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y,
|
||||
presentation: { radius, background, border, depth } });
|
||||
}
|
||||
|
||||
/** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */
|
||||
drawDepthImage(source: GrayImage, x: number, y: number, depth: number, masked = false): void {
|
||||
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y,
|
||||
presentation: { mode: masked ? "masked-image" : "image", radius: 0, background: 0, border: 0, depth } });
|
||||
}
|
||||
|
||||
copyPresentationsInto(target: GrayImage, dx: number, dy: number): void {
|
||||
for (const placed of this.drawList) if (placed.kind === "image" && placed.presentation) {
|
||||
target.drawList.push({ ...placed, x: placed.x + dx, y: placed.y + dy });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bake this image's deferred draws into its own pixels and clear the list.
|
||||
* Used where later raster must be able to cover earlier draws within the
|
||||
@@ -513,7 +533,8 @@ export class GrayImage {
|
||||
if (placed.kind === "glyph") {
|
||||
rasterizeGlyph(this, placed.font, placed.glyph, placed.x, placed.y, placed.value);
|
||||
} else if (placed.kind === "image") {
|
||||
this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
if (placed.presentation) { const one = new GrayImage(this.width, this.height); one.drawList.push(placed); one.bakeDrawsInto(this, 0, 0); }
|
||||
else this.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
} else {
|
||||
placed.font.bakeFwTextRun(this, placed, 0, 0);
|
||||
}
|
||||
@@ -534,7 +555,7 @@ export class GrayImage {
|
||||
const copy = new GrayImage(this.width, this.height, 0);
|
||||
copy.pixels.set(this.pixels);
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind === "image") {
|
||||
if (placed.kind === "image" && !placed.presentation) {
|
||||
copy.bitBlt(placed.source, placed.x, placed.y, { transparentZero: true });
|
||||
}
|
||||
}
|
||||
@@ -544,7 +565,10 @@ export class GrayImage {
|
||||
if (value !== 0) pixels[i] = dimValue(value, scale);
|
||||
}
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind !== "image") {
|
||||
if (placed.kind === "image" && placed.presentation) {
|
||||
const p = placed.presentation;
|
||||
copy.drawList.push({ ...placed, source: placed.source.dimmed(factor), presentation: { ...p, background: dimValue(p.background, scale), border: dimValue(p.border, scale) } });
|
||||
} else if (placed.kind !== "image") {
|
||||
copy.drawList.push({ ...placed, value: dimValue(placed.value, scale) });
|
||||
}
|
||||
}
|
||||
@@ -556,11 +580,11 @@ export class GrayImage {
|
||||
* Returns this image unchanged when there are none, otherwise a baked copy
|
||||
* with an empty draw list.
|
||||
*/
|
||||
withDrawsBaked(): GrayImage {
|
||||
withDrawsBaked(presentations = true): GrayImage {
|
||||
if (!this.drawList.length) return this;
|
||||
const baked = new GrayImage(this.width, this.height, 0);
|
||||
baked.pixels.set(this.pixels);
|
||||
this.bakeDrawsInto(baked, 0, 0);
|
||||
this.bakeDrawsInto(baked, 0, 0, presentations);
|
||||
return baked;
|
||||
}
|
||||
|
||||
@@ -579,11 +603,28 @@ export class GrayImage {
|
||||
}
|
||||
|
||||
/** Rasterize this image's deferred draws into target, translated by (dx, dy). */
|
||||
bakeDrawsInto(target: GrayImage, dx: number, dy: number): void {
|
||||
bakeDrawsInto(target: GrayImage, dx: number, dy: number, presentations = true): void {
|
||||
for (const placed of this.drawList) {
|
||||
if (placed.kind === "glyph") {
|
||||
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
|
||||
} else if (placed.kind === "image") {
|
||||
if (placed.presentation) {
|
||||
if (!presentations) continue;
|
||||
if (placed.presentation.mode) {
|
||||
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
|
||||
continue;
|
||||
}
|
||||
const { radius, background, border } = placed.presentation;
|
||||
const fill = new GrayImage(placed.source.width, placed.source.height);
|
||||
fill.fillRoundedRect(0, 0, fill.width, fill.height, background, radius);
|
||||
for (let y = 0; y < fill.height; y++) for (let x = 0; x < fill.width; x++) {
|
||||
const tx = placed.x + dx + x, ty = placed.y + dy + y;
|
||||
if (tx >= 0 && ty >= 0 && tx < target.width && ty < target.height) {
|
||||
const i = ty * target.width + tx; target.pixels[i] = Math.max(target.pixels[i], fill.pixels[y * fill.width + x]);
|
||||
}
|
||||
}
|
||||
target.drawRoundedRect(placed.x + dx, placed.y + dy, fill.width, fill.height, border, radius);
|
||||
}
|
||||
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
|
||||
} else {
|
||||
placed.font.bakeFwTextRun(target, placed, dx, dy);
|
||||
|
||||
+56
-19
@@ -1,4 +1,4 @@
|
||||
import { GrayImage, type DeferredDraw } from "./image";
|
||||
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
|
||||
|
||||
/**
|
||||
* The unit apps (and the shell) submit to the compositor: instead of one
|
||||
@@ -8,20 +8,17 @@ import { GrayImage, type DeferredDraw } from "./image";
|
||||
* transparent, 1 is black (the two quantize to the same 4bpp shade, so
|
||||
* reserving 0 costs nothing visible).
|
||||
*
|
||||
* This representation exists to carry structure the wire pipeline will later
|
||||
* exploit: a plane's glyph list can become on-glasses display-list references
|
||||
* (texture cache), and the offset can diverge per lens for stereo depth. For
|
||||
* now the compositor boundary just flattens (flattenPlanes) and feeds the
|
||||
* existing incremental-update and screen-preview pipeline.
|
||||
* Static planes form the incremental screen buffer. Planes with stereo depth
|
||||
* and retained presentation draws replay from resources in the root display list.
|
||||
*/
|
||||
export type Plane = {
|
||||
shellKey?: number;
|
||||
depth?: number;
|
||||
dimUnderneath?: number;
|
||||
image: GrayImage;
|
||||
/**
|
||||
* Offset of the plane's top-left within the submitted frame. Applied
|
||||
* identically to both lenses for now; a per-lens divergence (stereo depth
|
||||
* for floating overlays) is the planned extension.
|
||||
* before any per-lens horizontal offset from depth.
|
||||
*/
|
||||
x: number;
|
||||
y: number;
|
||||
@@ -72,8 +69,9 @@ export function flattenPlanes(
|
||||
/**
|
||||
* Flatten planes and also return the frame's deferred draws (glyphs and
|
||||
* images), translated into frame coordinates and in bake order (plane order,
|
||||
* then each image's draw order). The flattened image has every draw baked in,
|
||||
* exactly as flattenPlanes produces; the list preserves the draws' identity
|
||||
* then each image's draw order). Regular draws are baked into the image;
|
||||
* selected-menu presentation records remain unbaked for root-list replay.
|
||||
* The list preserves the draws' identity
|
||||
* so the texture-cache pipeline can replay them as on-glasses cached draws
|
||||
* (see graphics/glyph-wire.ts).
|
||||
*/
|
||||
@@ -81,15 +79,54 @@ export function flattenPlanesWithDraws(
|
||||
planes: readonly Plane[],
|
||||
size?: { width: number; height: number },
|
||||
): { image: GrayImage; draws: DeferredDraw[] } {
|
||||
const image = flattenPlanes(planes, size);
|
||||
const rasters = planes.map(p => p.image.withDrawsBaked(false));
|
||||
const image = flattenPlanes(planes.map((plane, i) => ({ ...plane, image: plane.depth ? new GrayImage(plane.image.width, plane.image.height) : rasters[i]! })), size);
|
||||
const draws: DeferredDraw[] = [];
|
||||
for (const plane of planes) {
|
||||
for (const placed of plane.image.draws) {
|
||||
draws.push(
|
||||
plane.x === 0 && plane.y === 0
|
||||
? placed
|
||||
: { ...placed, x: placed.x + plane.x, y: placed.y + plane.y },
|
||||
);
|
||||
for (const [index, plane] of planes.entries()) {
|
||||
const retained: DeferredDraw[] = [];
|
||||
if (plane.depth) {
|
||||
// Keep the whole menu off the screen buffer, including its black background.
|
||||
const raster = rasters[index]!;
|
||||
let left = raster.width, top = raster.height, right = -1, bottom = -1;
|
||||
for (let y = 0; y < raster.height; y++) for (let x = 0; x < raster.width; x++) if (raster.pixels[y*raster.width+x]) {
|
||||
left = Math.min(left,x); top = Math.min(top,y); right = Math.max(right,x); bottom = Math.max(bottom,y);
|
||||
}
|
||||
if (right >= left) {
|
||||
const crop = new GrayImage(right-left+1,bottom-top+1);
|
||||
for(let y=0;y<crop.height;y++) crop.pixels.set(raster.pixels.subarray((y+top)*raster.width+left,(y+top)*raster.width+right+1),y*crop.width);
|
||||
retained.push({ kind: "image", source: crop, x: left, y: top,
|
||||
presentation: { mode: "masked-image", radius: 0, background: 0, border: 0, depth: plane.depth } });
|
||||
}
|
||||
}
|
||||
const planeDraws = plane.depth ? [...retained, ...plane.image.draws.filter(d => d.kind === "image" && d.presentation).map(d => {
|
||||
const draw = d as PlacedImage;
|
||||
return { ...draw, presentation: { ...draw.presentation!, depth: draw.presentation!.depth + plane.depth! } };
|
||||
})] : plane.image.draws;
|
||||
for (const placed of planeDraws) {
|
||||
const translated = { ...placed, x: placed.x + plane.x, y: placed.y + plane.y };
|
||||
if (translated.kind === "image" && translated.presentation) {
|
||||
// Restore opaque pixels from later planes after replaying this row. The screen
|
||||
// already contains those pixels; rect copies preserve rounded/irregular occlusion.
|
||||
const occlusions: { x: number; y: number; width: number; height: number }[] = [];
|
||||
const depth = translated.presentation.depth;
|
||||
const leftShift = Math.floor(depth / 2), rightShift = -Math.floor((depth + 1) / 2);
|
||||
const left = Math.max(0, translated.x + Math.min(leftShift, rightShift));
|
||||
const right = Math.min(image.width, translated.x + translated.source.width + Math.max(leftShift, rightShift));
|
||||
for (let y = Math.max(0, translated.y); y < Math.min(image.height, translated.y + translated.source.height); y++) {
|
||||
let start = -1;
|
||||
for (let x = left; x <= right; x++) {
|
||||
const covered = x < right && planes.some((p, i) => i > index && !p.depth && x >= p.x && y >= p.y && x < p.x + p.image.width && y < p.y + p.image.height && rasters[i]!.pixels[(y-p.y)*p.image.width+x-p.x] !== 0);
|
||||
if (covered && start < 0) start = x;
|
||||
if (!covered && start >= 0) {
|
||||
const previous = occlusions.find(r => r.x === start && r.width === x - start && r.y + r.height === y);
|
||||
if (previous) previous.height++; else occlusions.push({ x: start, y, width: x-start, height: 1 });
|
||||
start = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
translated.presentation = { ...translated.presentation, occlusions };
|
||||
}
|
||||
draws.push(translated);
|
||||
}
|
||||
}
|
||||
return { image, draws };
|
||||
@@ -102,6 +139,6 @@ export function flattenPlanesWithDraws(
|
||||
*/
|
||||
export function planesFingerprint(planes: readonly Plane[]): string {
|
||||
return planes
|
||||
.map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
|
||||
.map((plane) => `${plane.depth ?? 0}:${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
|
||||
.join("|");
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { PlacedImage } from './image'
|
||||
|
||||
/** Bridge record: tag 3 (selection), 4 (image), 5 (gray8-masked image), signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
|
||||
export function encodePresentation(draw: PlacedImage): Uint8Array {
|
||||
const p = draw.presentation!
|
||||
const { width, height, pixels } = draw.source
|
||||
if (width < 1 || height < 1 || width > 640 || height > 480 || 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit')
|
||||
if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth')
|
||||
if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles')
|
||||
const bytes = new Uint8Array(16 + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
|
||||
bytes[0] = p.mode === "image" ? 4 : p.mode === "masked-image" ? 5 : 3
|
||||
view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true)
|
||||
view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true)
|
||||
bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth)
|
||||
view.setUint16(14, p.occlusions?.length ?? 0, true)
|
||||
bytes.set(pixels, 16)
|
||||
let offset = 16 + pixels.length
|
||||
for (const rect of p.occlusions ?? []) {
|
||||
view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true)
|
||||
view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
export type Selection = { mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
|
||||
export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } {
|
||||
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true)
|
||||
const selection: Selection = { mode: bytes[offset] === 4 ? "image" : bytes[offset] === 5 ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h,
|
||||
radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13),
|
||||
pixels:bytes.slice(offset+16,offset+16+w*h), occlusions:[] }
|
||||
let end=offset+16+w*h
|
||||
for(let i=0;i<v.getUint16(offset+14,true);i++) { selection.occlusions.push({x:v.getUint16(end,true),y:v.getUint16(end+2,true),width:v.getUint16(end+4,true),height:v.getUint16(end+6,true)});end+=8 }
|
||||
return {selection,end}
|
||||
}
|
||||
export function presentationRecords(buffer: ArrayBuffer | null): Selection[] {
|
||||
if (!buffer) return []
|
||||
const bytes=new Uint8Array(buffer), result:Selection[]=[]
|
||||
for(let p=0;p<bytes.length;) {
|
||||
if(bytes[p]===3 || bytes[p]===4 || bytes[p]===5) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
|
||||
else if(bytes[p]===0) p+=12
|
||||
else if(bytes[p]===1) p+=9
|
||||
else if(bytes[p]===2) p+=7+bytes[p+6]!*7
|
||||
else break
|
||||
}
|
||||
return result
|
||||
}
|
||||
export function paintPresentation(output: Uint8Array, screen: Uint8Array, width: number, height: number, row: Selection, right = false): void {
|
||||
const q=(n:number)=>Math.min(15,(n+8)>>4), shift=right?-Math.floor((row.depth+1)/2):Math.floor(row.depth/2)
|
||||
const inside=(x:number,y:number,w:number,h:number,r:number)=>{
|
||||
if(x<0||y<0||x>=w||y>=h) return false
|
||||
r=Math.min(r,Math.floor(w/2),Math.floor(h/2))
|
||||
const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1))
|
||||
return dx*dx+dy*dy<=4*r*r
|
||||
}
|
||||
for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
|
||||
const x=row.x+xx+shift,y=row.y+yy
|
||||
if(x<0||y<0||x>=width||y>=height) continue
|
||||
const i=y*width+x
|
||||
if(!row.mode && inside(xx,yy,row.width,row.height,row.radius)) output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
|
||||
const value=q(row.pixels[yy*row.width+xx]!)
|
||||
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[i]=value*16
|
||||
}
|
||||
for(const r of row.occlusions) for(let yy=r.y;yy<r.y+r.height;yy++) for(let xx=r.x;xx<r.x+r.width;xx++) if(xx>=0&&yy>=0&&xx<width&&yy<height) output[yy*width+xx]=q(screen[yy*width+xx]!)*16
|
||||
}
|
||||
@@ -1,18 +1,20 @@
|
||||
import { GrayImage } from './image'
|
||||
import { encodePresentation } from "./presentation-wire"
|
||||
import { GrayImage, type PlacedImage } from './image'
|
||||
import type { Plane } from './plane'
|
||||
|
||||
/** Crop one independently owned, opaque shell surface; never split it into tiles. */
|
||||
export function shellCrop(image: GrayImage, x: number, y: number, width: number, height: number, key: number): Plane {
|
||||
const baked = image.withDrawsBaked(), crop = new GrayImage(width, height, 0)
|
||||
const baked = image.withDrawsBaked(false), crop = new GrayImage(width, height, 0)
|
||||
for (let row = 0; row < height; row++) crop.pixels.set(baked.pixels.subarray((y + row) * image.width + x, (y + row) * image.width + x + width), row * width)
|
||||
image.copyPresentationsInto(crop, -x, -y)
|
||||
return { image: crop, x, y, shellKey: key }
|
||||
}
|
||||
|
||||
/** Shared Kotlin receives original 8-bit pixels and quantizes once, before composing. */
|
||||
export function encodeShellScene(planes: readonly Plane[]): Uint8Array {
|
||||
const layers: { image: GrayImage; x: number; y: number; key: number; dim: number }[] = []
|
||||
const layers: { image: GrayImage; x: number; y: number; key: number; depth: number; dim: number; selections: Uint8Array[] }[] = []
|
||||
for (const plane of planes) {
|
||||
const image = plane.image.withDrawsBaked()
|
||||
const image = plane.image.withDrawsBaked(false)
|
||||
let left = image.width, top = image.height, right = -1, bottom = -1
|
||||
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
|
||||
if (image.pixels[y * image.width + x] === 0) continue
|
||||
@@ -22,12 +24,16 @@ export function encodeShellScene(planes: readonly Plane[]): Uint8Array {
|
||||
const w = right - left + 1, h = bottom - top + 1
|
||||
if (5 + Math.ceil(w / 2) * h > 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`)
|
||||
const cropped = shellCrop(image, left, top, w, h, plane.shellKey!)
|
||||
layers.push({ image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) })
|
||||
const selections = plane.image.draws
|
||||
.filter((d): d is PlacedImage => d.kind === "image" && !!d.presentation)
|
||||
.map(d => encodePresentation({ ...d, x: d.x + plane.x, y: d.y + plane.y,
|
||||
presentation: { ...d.presentation!, depth: d.presentation!.depth + (plane.depth ?? 0) } }))
|
||||
layers.push({ depth: plane.depth ?? 0, selections, image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) })
|
||||
}
|
||||
const result = new Uint8Array(2 + layers.reduce((n, l) => n + 14 + l.image.pixels.length, 0))
|
||||
const result = new Uint8Array(2 + layers.reduce((n, l) => n + 16 + l.image.pixels.length + l.selections.reduce((n, b) => n + b.length, 0), 0))
|
||||
const view = new DataView(result.buffer); let p = 0
|
||||
const word = (n: number) => { view.setUint16(p, n, true); p += 2 }
|
||||
word(layers.length)
|
||||
for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(0); result.set(l.image.pixels, p); p += l.image.pixels.length }
|
||||
for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(l.selections.length); word(l.depth); result.set(l.image.pixels, p); p += l.image.pixels.length; for (const selection of l.selections) { result.set(selection, p); p += selection.length } }
|
||||
return result
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire"
|
||||
/** Platform-independent 8bpp surface composition for the local display. */
|
||||
import type { TextureFrame } from '../g2/texture-planner'
|
||||
export type SurfaceRect = { x: number; y: number; width: number; height: number }
|
||||
@@ -5,7 +6,7 @@ export type SurfaceConfiguration = SurfaceRect & {
|
||||
zOrder: number
|
||||
transparency: 'opaque' | 'color-key'
|
||||
}
|
||||
type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean }
|
||||
type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; selections: Selection[] }
|
||||
|
||||
export class SurfaceCompositor {
|
||||
private readonly surfaces = new Map<string, Surface>()
|
||||
@@ -25,7 +26,7 @@ export class SurfaceCompositor {
|
||||
const previous = this.surfaces.get(id)
|
||||
const pixels = previous?.width === options.width && previous.height === options.height
|
||||
? previous.pixels : new Uint8Array(options.width * options.height)
|
||||
this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true })
|
||||
this.surfaces.set(id, { ...options, pixels, visible: previous?.visible ?? true, selections: previous?.selections ?? [] })
|
||||
}
|
||||
removeSurface(id: string): void { this.surfaces.delete(id) }
|
||||
setSurfaceVisible(id: string, visible: boolean): void {
|
||||
@@ -45,6 +46,7 @@ export class SurfaceCompositor {
|
||||
|| rect.width <= 0 || rect.height <= 0 || pixels.length !== rect.width * rect.height) {
|
||||
throw new Error('Invalid frame buffer or rectangle')
|
||||
}
|
||||
surface.selections = presentationRecords(_draws)
|
||||
const left = Math.max(0, rect.x), right = Math.min(surface.width, rect.x + rect.width)
|
||||
const top = Math.max(0, rect.y), bottom = Math.min(surface.height, rect.y + rect.height)
|
||||
for (let y = top; y < bottom; y++) {
|
||||
@@ -74,19 +76,25 @@ export class SurfaceCompositor {
|
||||
}
|
||||
}
|
||||
}
|
||||
const screen = output.slice()
|
||||
if (!surfaces.some(s => s.zOrder > 1) && (this.shellScene || surfaces.some(s => s.selections.length))) {
|
||||
for(let i=0;i<output.length;i++) output[i]=Math.min(15,(output[i]!+8)>>4)*16
|
||||
for(const surface of surfaces) for(const row of surface.selections) paintPresentation(output,screen,this.width,this.height,{...row,x:row.x+surface.x,y:row.y+surface.y,occlusions:row.occlusions.map(r=>({...r,x:r.x+surface.x,y:r.y+surface.y}))})
|
||||
}
|
||||
if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) {
|
||||
const quantize = (v: number) => Math.min(15, (v + 8) >> 4)
|
||||
for (let i=0;i<output.length;i++) output[i]=quantize(output[i])*16
|
||||
const bytes=this.shellScene, view=new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
||||
let p=2
|
||||
for(let i=0;i<view.getUint16(0,true);i++) {
|
||||
const x=view.getInt16(p+2,true), y=view.getInt16(p+4,true), w=view.getUint16(p+6,true), h=view.getUint16(p+8,true), dim=view.getUint16(p+10,true)
|
||||
p+=14
|
||||
const x=view.getInt16(p+2,true), y=view.getInt16(p+4,true), w=view.getUint16(p+6,true), h=view.getUint16(p+8,true), dim=view.getUint16(p+10,true), selections=view.getUint16(p+12,true), shift=Math.floor(view.getInt16(p+14,true)/2)
|
||||
p+=16
|
||||
if(dim<256) for(let j=0;j<output.length;j++) output[j]=Math.floor((output[j]/16)*dim/256)*16
|
||||
for(let yy=0;yy<h;yy++) for(let xx=0;xx<w;xx++) {
|
||||
if(x+xx>=0 && y+yy>=0 && x+xx<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx]=quantize(bytes[p+yy*w+xx])*16
|
||||
if(x+xx+shift>=0 && y+yy>=0 && x+xx+shift<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx+shift]=quantize(bytes[p+yy*w+xx])*16
|
||||
}
|
||||
p+=w*h
|
||||
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;paintPresentation(output,screen,this.width,this.height,record.selection) }
|
||||
}
|
||||
}
|
||||
return output
|
||||
|
||||
Reference in new issue
Block a user