# Pixel ratio

> The same scene at pixelRatio 1, 2 and the screen's own: finer 3D edges on dense screens, and a sprite showing why pixel art stays at 1.

Example `pixel-ratio`, running at https://nacatamalon.com/en/examples/pixel-ratio. The whole catalogue: https://nacatamalon.com/llms/examples.md.

The same 200×150 scene three times, all the same size on the page. `pixelRatio` in `createGame` is how many real pixels each game pixel is drawn with: at `2` the buffer is 400×300, and `'device'` follows the screen's `devicePixelRatio`. The game does not notice: cameras, positions, the pointer and `getWidth()` still measure 200×150.

Look at the thin ring and the far edge of the floor: that is what it is for, 3D and HD games on dense screens. Then look at the rotating sprite: above 1 it loses the stepped edges of the era. **Pixel art stays at 1**, and the cost grows with the square: four times the pixels at 2, nine at 3.

## Code

`index.ts`:

```ts
import { start } from './host/start';

const root = document.querySelector<HTMLElement>('#app')!;

root.style.cssText = 'display:flex;flex-wrap:wrap;gap:12px';

start(root, 1, 'pixelRatio: 1 (default)');
start(root, 2, 'pixelRatio: 2');
start(root, 'device', "pixelRatio: 'device' (" + window.devicePixelRatio + ')');
```

`components/exhibit.ts`:

```ts
import { createMesh, createSprite, useIcoSphereGeometry, useLoadTexture, usePlaneGeometry, useTorusGeometry, useUpdate } from 'nacatamalon';

export const Exhibit = () => {
    // Thin, far and diagonal: where a low resolution shows its steps.
    createMesh({
        geometry: usePlaneGeometry({
            width: 10,
            depth: 10
        }),
        tint: '#2d3344'
    });

    const ball = createMesh({
        geometry: useIcoSphereGeometry({
            subdivisions: 1
        }),
        tint: '#f5c518',
        transform: {
            y: 0.9
        }
    });

    const ring = createMesh({
        geometry: useTorusGeometry({
            radius: 1.2,
            tube: 0.03
        }),
        tint: '#22eeff',
        transform: {
            y: 0.9
        }
    });

    // Pixel art, rotating. At 1 its edges step like a SNES sprite; above 1 they come out smooth,
    // which is sharper and no longer of the era. This is why pixel art stays at 1.
    const sprite = createSprite({
        texture: useLoadTexture({
            src: '/example-assets/assets/gammix.png'
        }),
        width: 32,
        height: 32,
        zIndex: 5,
        transform: {
            x: 26,
            y: 26
        },
    });

    useUpdate((delta) => {
        ball.transform.rotationY += delta * 0.5;
        ring.transform.rotationX += delta * 0.4;
        ring.transform.rotationY += delta * 0.3;
        sprite.transform.rotation += delta * 0.6;
    });
};
```

`host/start.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from '../settings';
import { Game } from '../scenes/demo';

export const start = (root: HTMLElement, pixelRatio: number|'device', label: string) => {
    const wrap = document.createElement('div');

    wrap.style.cssText = 'display:flex;flex-direction:column;gap:4px';

    const tag = document.createElement('div');

    tag.textContent = label;
    tag.style.cssText = 'font:11px ui-monospace,monospace;color:#8ee6a0;letter-spacing:.08em';

    const host = document.createElement('div');

    wrap.append(tag, host);
    root.appendChild(wrap);

    const game = createGame(host, {
        width: Settings.WIDTH,
        height: Settings.HEIGHT,
        background: '#0a0a18',
        pixelRatio
    });

    // Declare Scenes
    game({
        Game
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera3d, useDirectionalLight, useSpawn } from 'nacatamalon';

import { Exhibit } from '../components/exhibit';

export const Game = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            y: 1.2,
            z: 4.2,
            rotationX: -0.25
        }
    });
    useDirectionalLight({
        intensity: 1.1,
        transform: {
            rotationX: -0.6,
            rotationY: 0.5
        }
    });

    useSpawn(Exhibit)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 200;
export const HEIGHT = 150;
```
